-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add options "create-str" and "update-str"
- Loading branch information
Showing
15 changed files
with
351 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod todos; |
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,92 @@ | ||
/* @generated and managed by dsync */ | ||
|
||
use crate::diesel::*; | ||
use crate::schema::*; | ||
use diesel::QueryResult; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
|
||
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Selectable)] | ||
#[diesel(table_name=todos, primary_key(text))] | ||
pub struct Todo { | ||
pub text: String, | ||
pub text_nullable: Option<String>, | ||
pub varchar: String, | ||
pub varchar_nullable: Option<String>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset)] | ||
#[diesel(table_name=todos)] | ||
pub struct CreateTodo<'a> { | ||
pub text: Cow<'a, str>, | ||
pub text_nullable: Option<Cow<'a, str>>, | ||
pub varchar: Cow<'a, str>, | ||
pub varchar_nullable: Option<Cow<'a, str>>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Default)] | ||
#[diesel(table_name=todos)] | ||
pub struct UpdateTodo<'a> { | ||
pub text_nullable: Option<Cow<'a, str>>, | ||
pub varchar: Option<Cow<'a, str>>, | ||
pub varchar_nullable: Option<Cow<'a, str>>, | ||
} | ||
|
||
|
||
#[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 Todo { | ||
|
||
pub fn create(db: &mut ConnectionType, item: &CreateTodo) -> QueryResult<Self> { | ||
use crate::schema::todos::dsl::*; | ||
|
||
insert_into(todos).values(item).get_result::<Self>(db) | ||
} | ||
|
||
pub fn read(db: &mut ConnectionType, param_text: String) -> QueryResult<Self> { | ||
use crate::schema::todos::dsl::*; | ||
|
||
todos.filter(text.eq(param_text)).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::todos::dsl::*; | ||
|
||
let page_size = if page_size < 1 { 1 } else { page_size }; | ||
let total_items = todos.count().get_result(db)?; | ||
let items = todos.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 update(db: &mut ConnectionType, param_text: String, item: &UpdateTodo) -> QueryResult<Self> { | ||
use crate::schema::todos::dsl::*; | ||
|
||
diesel::update(todos.filter(text.eq(param_text))).set(item).get_result(db) | ||
} | ||
|
||
pub fn delete(db: &mut ConnectionType, param_text: String) -> QueryResult<usize> { | ||
use crate::schema::todos::dsl::*; | ||
|
||
diesel::delete(todos.filter(text.eq(param_text))).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 use generated::*; | ||
pub mod 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,9 @@ | ||
diesel::table! { | ||
todos (text) { | ||
text -> Text, | ||
text_nullable -> Nullable<Text>, | ||
#[max_length = 255] | ||
varchar -> Varchar, | ||
varchar_nullable -> Nullable<Varchar>, | ||
} | ||
} |
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>>" --create-str=cow --update-str=cow |
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 @@ | ||
pub mod todos; |
Oops, something went wrong.