Skip to content

Commit 007ace8

Browse files
committed
test: add test for "once-common-connection-type" + "once-common-structs" + "single-model-file"
1 parent ac206e5 commit 007ace8

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* @generated and managed by dsync */
2+
#[derive(Debug, Serialize)]
3+
pub struct PaginationResult<T> {
4+
pub items: Vec<T>,
5+
pub total_items: i64,
6+
/// 0-based index
7+
pub page: i64,
8+
pub page_size: i64,
9+
pub num_pages: i64,
10+
}
11+
12+
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod common;
2+
pub mod table1;
3+
pub mod table2;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* @generated and managed by dsync */
2+
3+
use crate::diesel::*;
4+
use crate::schema::*;
5+
use diesel::QueryResult;
6+
use crate::models::common::*;
7+
use serde::{Deserialize, Serialize};
8+
9+
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, Selectable)]
10+
#[diesel(table_name=table1, primary_key(id))]
11+
pub struct Table1 {
12+
pub id: crate::schema::sql_types::Int,
13+
}
14+
15+
16+
17+
18+
impl Table1 {
19+
20+
pub fn create(db: &mut ConnectionType) -> QueryResult<Self> {
21+
use crate::schema::table1::dsl::*;
22+
23+
insert_into(table1).default_values().get_result::<Self>(db)
24+
}
25+
26+
pub fn read(db: &mut ConnectionType, param_id: crate::schema::sql_types::Int) -> QueryResult<Self> {
27+
use crate::schema::table1::dsl::*;
28+
29+
table1.filter(id.eq(param_id)).first::<Self>(db)
30+
}
31+
32+
/// Paginates through the table where page is a 0-based index (i.e. page 0 is the first page)
33+
pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> {
34+
use crate::schema::table1::dsl::*;
35+
36+
let page_size = if page_size < 1 { 1 } else { page_size };
37+
let total_items = table1.count().get_result(db)?;
38+
let items = table1.limit(page_size).offset(page * page_size).load::<Self>(db)?;
39+
40+
Ok(PaginationResult {
41+
items,
42+
total_items,
43+
page,
44+
page_size,
45+
/* ceiling division of integers */
46+
num_pages: total_items / page_size + i64::from(total_items % page_size != 0)
47+
})
48+
}
49+
50+
pub fn delete(db: &mut ConnectionType, param_id: crate::schema::sql_types::Int) -> QueryResult<usize> {
51+
use crate::schema::table1::dsl::*;
52+
53+
diesel::delete(table1.filter(id.eq(param_id))).execute(db)
54+
}
55+
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* @generated and managed by dsync */
2+
3+
use crate::diesel::*;
4+
use crate::schema::*;
5+
use diesel::QueryResult;
6+
use crate::models::common::*;
7+
use serde::{Deserialize, Serialize};
8+
9+
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, Selectable)]
10+
#[diesel(table_name=table2, primary_key(id))]
11+
pub struct Table2 {
12+
pub id: crate::schema::sql_types::Int,
13+
}
14+
15+
16+
17+
18+
impl Table2 {
19+
20+
pub fn create(db: &mut ConnectionType) -> QueryResult<Self> {
21+
use crate::schema::table2::dsl::*;
22+
23+
insert_into(table2).default_values().get_result::<Self>(db)
24+
}
25+
26+
pub fn read(db: &mut ConnectionType, param_id: crate::schema::sql_types::Int) -> QueryResult<Self> {
27+
use crate::schema::table2::dsl::*;
28+
29+
table2.filter(id.eq(param_id)).first::<Self>(db)
30+
}
31+
32+
/// Paginates through the table where page is a 0-based index (i.e. page 0 is the first page)
33+
pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> {
34+
use crate::schema::table2::dsl::*;
35+
36+
let page_size = if page_size < 1 { 1 } else { page_size };
37+
let total_items = table2.count().get_result(db)?;
38+
let items = table2.limit(page_size).offset(page * page_size).load::<Self>(db)?;
39+
40+
Ok(PaginationResult {
41+
items,
42+
total_items,
43+
page,
44+
page_size,
45+
/* ceiling division of integers */
46+
num_pages: total_items / page_size + i64::from(total_items % page_size != 0)
47+
})
48+
}
49+
50+
pub fn delete(db: &mut ConnectionType, param_id: crate::schema::sql_types::Int) -> QueryResult<usize> {
51+
use crate::schema::table2::dsl::*;
52+
53+
diesel::delete(table2.filter(id.eq(param_id))).execute(db)
54+
}
55+
56+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
diesel::table! {
2+
table1 (id) {
3+
id -> Int,
4+
}
5+
}
6+
7+
diesel::table! {
8+
table2 (id) {
9+
id -> Int,
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
4+
5+
cd $SCRIPT_DIR
6+
7+
cargo run -- -i schema.rs -o models -g id -g created_at -g updated_at -c "diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>" --once-common-structs --once-connection-type --single-model-file

0 commit comments

Comments
 (0)