-
Notifications
You must be signed in to change notification settings - Fork 15
feat(code): add options "create-bytes" and "update-bytes" #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,98 @@ | ||
| /* @generated and managed by dsync */ | ||
|
|
||
| use crate::diesel::*; | ||
| use crate::schema::*; | ||
| use diesel::QueryResult; | ||
|
|
||
| pub type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::pg::PgConnection>>; | ||
|
|
||
| /// Struct representing a row in table `todos` | ||
| #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Queryable, Selectable, QueryableByName)] | ||
| #[diesel(table_name=todos, primary_key(data))] | ||
| pub struct Todos { | ||
| /// Field representing column `data` | ||
| pub data: Vec<u8>, | ||
| /// Field representing column `data_nullable` | ||
| pub data_nullable: Option<Vec<u8>>, | ||
| } | ||
|
|
||
| /// Create Struct for a row in table `todos` for [`Todos`] | ||
| #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Insertable)] | ||
| #[diesel(table_name=todos)] | ||
| pub struct CreateTodos<'a> { | ||
| /// Field representing column `data` | ||
| pub data: Cow<'a, [u8]>, | ||
| /// Field representing column `data_nullable` | ||
| pub data_nullable: Option<Cow<'a, [u8]>>, | ||
| } | ||
|
|
||
| /// Update Struct for a row in table `todos` for [`Todos`] | ||
| #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, AsChangeset, PartialEq, Default)] | ||
| #[diesel(table_name=todos)] | ||
| pub struct UpdateTodos<'a> { | ||
| /// Field representing column `data_nullable` | ||
| pub data_nullable: Option<Option<Cow<'a, [u8]>>>, | ||
| } | ||
|
|
||
| /// Result of a `.paginate` function | ||
| #[derive(Debug, serde::Serialize)] | ||
| pub struct PaginationResult<T> { | ||
| /// Resulting items that are from the current page | ||
| pub items: Vec<T>, | ||
| /// The count of total items there are | ||
| pub total_items: i64, | ||
| /// Current page, 0-based index | ||
| pub page: i64, | ||
| /// Size of a page | ||
| pub page_size: i64, | ||
| /// Number of total possible pages, given the `page_size` and `total_items` | ||
| pub num_pages: i64, | ||
| } | ||
|
|
||
| impl Todos { | ||
| /// Insert a new row into `todos` with a given [`CreateTodos`] | ||
| pub fn create(db: &mut ConnectionType, item: &CreateTodos) -> QueryResult<Self> { | ||
| use crate::schema::todos::dsl::*; | ||
|
|
||
| insert_into(todos).values(item).get_result::<Self>(db) | ||
| } | ||
|
|
||
| /// Get a row from `todos`, identified by the primary key | ||
| pub fn read(db: &mut ConnectionType, param_data: Vec<u8>) -> QueryResult<Self> { | ||
| use crate::schema::todos::dsl::*; | ||
|
|
||
| todos.filter(data.eq(param_data)).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) | ||
| }) | ||
| } | ||
|
|
||
| /// Update a row in `todos`, identified by the primary key with [`UpdateTodos`] | ||
| pub fn update(db: &mut ConnectionType, param_data: Vec<u8>, item: &UpdateTodos) -> QueryResult<Self> { | ||
| use crate::schema::todos::dsl::*; | ||
|
|
||
| diesel::update(todos.filter(data.eq(param_data))).set(item).get_result(db) | ||
| } | ||
|
|
||
| /// Delete a row in `todos`, identified by the primary key | ||
| pub fn delete(db: &mut ConnectionType, param_data: Vec<u8>) -> QueryResult<usize> { | ||
| use crate::schema::todos::dsl::*; | ||
|
|
||
| diesel::delete(todos.filter(data.eq(param_data))).execute(db) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,6 @@ | ||
| diesel::table! { | ||
| todos (data) { | ||
| data -> Binary, | ||
| data_nullable -> Nullable<Binary>, | ||
| } | ||
| } |
This file contains hidden or 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::pg::PgConnection>>" --create-bytes=cow --update-bytes=cow |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.