|
| 1 | +/* @generated and managed by dsync */ |
| 2 | + |
| 3 | +use crate::diesel::*; |
| 4 | +use crate::schema::*; |
| 5 | +use diesel::QueryResult; |
| 6 | + |
| 7 | +pub type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::pg::PgConnection>>; |
| 8 | + |
| 9 | +/// Struct representing a row in table `todos` |
| 10 | +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Queryable, Selectable, QueryableByName)] |
| 11 | +#[diesel(table_name=todos, primary_key(data))] |
| 12 | +pub struct Todos { |
| 13 | + /// Field representing column `data` |
| 14 | + pub data: Vec<u8>, |
| 15 | + /// Field representing column `data_nullable` |
| 16 | + pub data_nullable: Option<Vec<u8>>, |
| 17 | +} |
| 18 | + |
| 19 | +/// Create Struct for a row in table `todos` for [`Todos`] |
| 20 | +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Insertable)] |
| 21 | +#[diesel(table_name=todos)] |
| 22 | +pub struct CreateTodos { |
| 23 | + /// Field representing column `data` |
| 24 | + pub data: Cow<'a, [u8]>, |
| 25 | + /// Field representing column `data_nullable` |
| 26 | + pub data_nullable: Option<Cow<'a, [u8]>>, |
| 27 | +} |
| 28 | + |
| 29 | +/// Update Struct for a row in table `todos` for [`Todos`] |
| 30 | +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, AsChangeset, PartialEq, Default)] |
| 31 | +#[diesel(table_name=todos)] |
| 32 | +pub struct UpdateTodos { |
| 33 | + /// Field representing column `data_nullable` |
| 34 | + pub data_nullable: Option<Option<Cow<'a, [u8]>>>, |
| 35 | +} |
| 36 | + |
| 37 | +/// Result of a `.paginate` function |
| 38 | +#[derive(Debug, serde::Serialize)] |
| 39 | +pub struct PaginationResult<T> { |
| 40 | + /// Resulting items that are from the current page |
| 41 | + pub items: Vec<T>, |
| 42 | + /// The count of total items there are |
| 43 | + pub total_items: i64, |
| 44 | + /// Current page, 0-based index |
| 45 | + pub page: i64, |
| 46 | + /// Size of a page |
| 47 | + pub page_size: i64, |
| 48 | + /// Number of total possible pages, given the `page_size` and `total_items` |
| 49 | + pub num_pages: i64, |
| 50 | +} |
| 51 | + |
| 52 | +impl Todos { |
| 53 | + /// Insert a new row into `todos` with a given [`CreateTodos`] |
| 54 | + pub fn create(db: &mut ConnectionType, item: &CreateTodos) -> QueryResult<Self> { |
| 55 | + use crate::schema::todos::dsl::*; |
| 56 | + |
| 57 | + insert_into(todos).values(item).get_result::<Self>(db) |
| 58 | + } |
| 59 | + |
| 60 | + /// Get a row from `todos`, identified by the primary key |
| 61 | + pub fn read(db: &mut ConnectionType, param_data: Vec<u8>) -> QueryResult<Self> { |
| 62 | + use crate::schema::todos::dsl::*; |
| 63 | + |
| 64 | + todos.filter(data.eq(param_data)).first::<Self>(db) |
| 65 | + } |
| 66 | + |
| 67 | + /// Paginates through the table where page is a 0-based index (i.e. page 0 is the first page) |
| 68 | + pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> { |
| 69 | + use crate::schema::todos::dsl::*; |
| 70 | + |
| 71 | + let page_size = if page_size < 1 { 1 } else { page_size }; |
| 72 | + let total_items = todos.count().get_result(db)?; |
| 73 | + let items = todos.limit(page_size).offset(page * page_size).load::<Self>(db)?; |
| 74 | + |
| 75 | + Ok(PaginationResult { |
| 76 | + items, |
| 77 | + total_items, |
| 78 | + page, |
| 79 | + page_size, |
| 80 | + /* ceiling division of integers */ |
| 81 | + num_pages: total_items / page_size + i64::from(total_items % page_size != 0) |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + /// Update a row in `todos`, identified by the primary key with [`UpdateTodos`] |
| 86 | + pub fn update(db: &mut ConnectionType, param_data: Vec<u8>, item: &UpdateTodos) -> QueryResult<Self> { |
| 87 | + use crate::schema::todos::dsl::*; |
| 88 | + |
| 89 | + diesel::update(todos.filter(data.eq(param_data))).set(item).get_result(db) |
| 90 | + } |
| 91 | + |
| 92 | + /// Delete a row in `todos`, identified by the primary key |
| 93 | + pub fn delete(db: &mut ConnectionType, param_data: Vec<u8>) -> QueryResult<usize> { |
| 94 | + use crate::schema::todos::dsl::*; |
| 95 | + |
| 96 | + diesel::delete(todos.filter(data.eq(param_data))).execute(db) |
| 97 | + } |
| 98 | +} |
0 commit comments