forked from Wulf/dsync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
335 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
test/once_common_structs_once_connection_type/models/common.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* @generated and managed by dsync */ | ||
#[derive(Debug, Serialize)] | ||
pub struct PaginationResult<T> { | ||
pub items: Vec<T>, | ||
pub total_items: i64, | ||
/// 0-based index | ||
pub page: i64, | ||
pub page_size: i64, | ||
pub num_pages: i64, | ||
} | ||
|
||
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod common; | ||
pub mod table1; | ||
pub mod table2; |
56 changes: 56 additions & 0 deletions
56
test/once_common_structs_once_connection_type/models/table1/generated.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* @generated and managed by dsync */ | ||
|
||
use crate::diesel::*; | ||
use crate::schema::*; | ||
use diesel::QueryResult; | ||
use crate::models::common::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, Selectable)] | ||
#[diesel(table_name=table1, primary_key(id))] | ||
pub struct Table1 { | ||
pub id: crate::schema::sql_types::Int, | ||
} | ||
|
||
|
||
|
||
|
||
impl Table1 { | ||
|
||
pub fn create(db: &mut ConnectionType) -> QueryResult<Self> { | ||
use crate::schema::table1::dsl::*; | ||
|
||
insert_into(table1).default_values().get_result::<Self>(db) | ||
} | ||
|
||
pub fn read(db: &mut ConnectionType, param_id: crate::schema::sql_types::Int) -> QueryResult<Self> { | ||
use crate::schema::table1::dsl::*; | ||
|
||
table1.filter(id.eq(param_id)).first::<Self>(db) | ||
} | ||
|
||
/// Paginates through the table where page is a 0-based index (i.e. page 0 is the first page) | ||
pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> { | ||
use crate::schema::table1::dsl::*; | ||
|
||
let page_size = if page_size < 1 { 1 } else { page_size }; | ||
let total_items = table1.count().get_result(db)?; | ||
let items = table1.limit(page_size).offset(page * page_size).load::<Self>(db)?; | ||
|
||
Ok(PaginationResult { | ||
items, | ||
total_items, | ||
page, | ||
page_size, | ||
/* ceiling division of integers */ | ||
num_pages: total_items / page_size + i64::from(total_items % page_size != 0) | ||
}) | ||
} | ||
|
||
pub fn delete(db: &mut ConnectionType, param_id: crate::schema::sql_types::Int) -> QueryResult<usize> { | ||
use crate::schema::table1::dsl::*; | ||
|
||
diesel::delete(table1.filter(id.eq(param_id))).execute(db) | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
test/once_common_structs_once_connection_type/models/table1/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod generated; | ||
pub use generated::*; |
56 changes: 56 additions & 0 deletions
56
test/once_common_structs_once_connection_type/models/table2/generated.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* @generated and managed by dsync */ | ||
|
||
use crate::diesel::*; | ||
use crate::schema::*; | ||
use diesel::QueryResult; | ||
use crate::models::common::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, Selectable)] | ||
#[diesel(table_name=table2, primary_key(id))] | ||
pub struct Table2 { | ||
pub id: crate::schema::sql_types::Int, | ||
} | ||
|
||
|
||
|
||
|
||
impl Table2 { | ||
|
||
pub fn create(db: &mut ConnectionType) -> QueryResult<Self> { | ||
use crate::schema::table2::dsl::*; | ||
|
||
insert_into(table2).default_values().get_result::<Self>(db) | ||
} | ||
|
||
pub fn read(db: &mut ConnectionType, param_id: crate::schema::sql_types::Int) -> QueryResult<Self> { | ||
use crate::schema::table2::dsl::*; | ||
|
||
table2.filter(id.eq(param_id)).first::<Self>(db) | ||
} | ||
|
||
/// Paginates through the table where page is a 0-based index (i.e. page 0 is the first page) | ||
pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> { | ||
use crate::schema::table2::dsl::*; | ||
|
||
let page_size = if page_size < 1 { 1 } else { page_size }; | ||
let total_items = table2.count().get_result(db)?; | ||
let items = table2.limit(page_size).offset(page * page_size).load::<Self>(db)?; | ||
|
||
Ok(PaginationResult { | ||
items, | ||
total_items, | ||
page, | ||
page_size, | ||
/* ceiling division of integers */ | ||
num_pages: total_items / page_size + i64::from(total_items % page_size != 0) | ||
}) | ||
} | ||
|
||
pub fn delete(db: &mut ConnectionType, param_id: crate::schema::sql_types::Int) -> QueryResult<usize> { | ||
use crate::schema::table2::dsl::*; | ||
|
||
diesel::delete(table2.filter(id.eq(param_id))).execute(db) | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
test/once_common_structs_once_connection_type/models/table2/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod generated; | ||
pub use generated::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
diesel::table! { | ||
table1 (id) { | ||
id -> Int, | ||
} | ||
} | ||
|
||
diesel::table! { | ||
table2 (id) { | ||
id -> Int, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" | ||
|
||
cd $SCRIPT_DIR | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @generated and managed by dsync */ | ||
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod common; | ||
pub mod table1; | ||
pub mod table2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* @generated and managed by dsync */ | ||
|
||
use crate::diesel::*; | ||
use crate::schema::*; | ||
use diesel::QueryResult; | ||
use crate::models::common::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, Selectable)] | ||
#[diesel(table_name=table1, primary_key(id))] | ||
pub struct Table1 { | ||
pub id: crate::schema::sql_types::Int, | ||
} | ||
|
||
|
||
|
||
|
||
#[derive(Debug, Serialize)] | ||
pub struct PaginationResult<T> { | ||
pub items: Vec<T>, | ||
pub total_items: i64, | ||
/// 0-based index | ||
pub page: i64, | ||
pub page_size: i64, | ||
pub num_pages: i64, | ||
} | ||
|
||
impl Table1 { | ||
|
||
pub fn create(db: &mut ConnectionType) -> QueryResult<Self> { | ||
use crate::schema::table1::dsl::*; | ||
|
||
insert_into(table1).default_values().get_result::<Self>(db) | ||
} | ||
|
||
pub fn read(db: &mut ConnectionType, param_id: crate::schema::sql_types::Int) -> QueryResult<Self> { | ||
use crate::schema::table1::dsl::*; | ||
|
||
table1.filter(id.eq(param_id)).first::<Self>(db) | ||
} | ||
|
||
/// Paginates through the table where page is a 0-based index (i.e. page 0 is the first page) | ||
pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> { | ||
use crate::schema::table1::dsl::*; | ||
|
||
let page_size = if page_size < 1 { 1 } else { page_size }; | ||
let total_items = table1.count().get_result(db)?; | ||
let items = table1.limit(page_size).offset(page * page_size).load::<Self>(db)?; | ||
|
||
Ok(PaginationResult { | ||
items, | ||
total_items, | ||
page, | ||
page_size, | ||
/* ceiling division of integers */ | ||
num_pages: total_items / page_size + i64::from(total_items % page_size != 0) | ||
}) | ||
} | ||
|
||
pub fn delete(db: &mut ConnectionType, param_id: crate::schema::sql_types::Int) -> QueryResult<usize> { | ||
use crate::schema::table1::dsl::*; | ||
|
||
diesel::delete(table1.filter(id.eq(param_id))).execute(db) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod generated; | ||
pub use generated::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* @generated and managed by dsync */ | ||
|
||
use crate::diesel::*; | ||
use crate::schema::*; | ||
use diesel::QueryResult; | ||
use crate::models::common::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, Selectable)] | ||
#[diesel(table_name=table2, primary_key(id))] | ||
pub struct Table2 { | ||
pub id: crate::schema::sql_types::Int, | ||
} | ||
|
||
|
||
|
||
|
||
#[derive(Debug, Serialize)] | ||
pub struct PaginationResult<T> { | ||
pub items: Vec<T>, | ||
pub total_items: i64, | ||
/// 0-based index | ||
pub page: i64, | ||
pub page_size: i64, | ||
pub num_pages: i64, | ||
} | ||
|
||
impl Table2 { | ||
|
||
pub fn create(db: &mut ConnectionType) -> QueryResult<Self> { | ||
use crate::schema::table2::dsl::*; | ||
|
||
insert_into(table2).default_values().get_result::<Self>(db) | ||
} | ||
|
||
pub fn read(db: &mut ConnectionType, param_id: crate::schema::sql_types::Int) -> QueryResult<Self> { | ||
use crate::schema::table2::dsl::*; | ||
|
||
table2.filter(id.eq(param_id)).first::<Self>(db) | ||
} | ||
|
||
/// Paginates through the table where page is a 0-based index (i.e. page 0 is the first page) | ||
pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> { | ||
use crate::schema::table2::dsl::*; | ||
|
||
let page_size = if page_size < 1 { 1 } else { page_size }; | ||
let total_items = table2.count().get_result(db)?; | ||
let items = table2.limit(page_size).offset(page * page_size).load::<Self>(db)?; | ||
|
||
Ok(PaginationResult { | ||
items, | ||
total_items, | ||
page, | ||
page_size, | ||
/* ceiling division of integers */ | ||
num_pages: total_items / page_size + i64::from(total_items % page_size != 0) | ||
}) | ||
} | ||
|
||
pub fn delete(db: &mut ConnectionType, param_id: crate::schema::sql_types::Int) -> QueryResult<usize> { | ||
use crate::schema::table2::dsl::*; | ||
|
||
diesel::delete(table2.filter(id.eq(param_id))).execute(db) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod generated; | ||
pub use generated::*; |
Oops, something went wrong.