From fd5862dcac7ab47a764134378cf1d56b1a73700b Mon Sep 17 00:00:00 2001 From: Haris <4259838+Wulf@users.noreply.github.com> Date: Mon, 15 Jan 2024 18:09:16 -0500 Subject: [PATCH] feat: Advanced queries flag (BREAKING CHANGE) (#126) * feat: Advanced queries flag BREAKING CHANGE * fix(advanced-queries): Fix filters for nullable columns * Update src/code.rs Co-authored-by: hasezoey * Update src/code.rs Co-authored-by: hasezoey * update tests --------- Co-authored-by: hasezoey --- Cargo.toml | 2 + README.md | 13 +- src/bin/main.rs | 13 ++ src/code.rs | 98 ++++++++- src/error.rs | 4 + src/lib.rs | 38 +++- test/advanced_queries/models/mod.rs | 1 + .../models/todos/generated.rs | 190 ++++++++++++++++++ test/advanced_queries/models/todos/mod.rs | 2 + test/advanced_queries/schema.rs | 14 ++ test/advanced_queries/test.sh | 7 + .../models/todos/generated.rs | 18 -- .../models/todos/generated.rs | 18 -- .../models/todos/generated.rs | 18 -- .../models/todos/generated.rs | 18 -- .../models/todos/generated.rs | 18 -- .../models/todos/generated.rs | 18 -- .../models/todos/generated.rs | 18 -- .../models/todos/generated.rs | 18 -- .../data/models/table_a/generated.rs | 18 -- .../data/models/table_b/generated.rs | 18 -- .../models/table_a/generated.rs | 18 -- .../models/table_b/generated.rs | 18 -- .../models/todos/generated.rs | 18 -- .../models/users/generated.rs | 18 -- .../models/todos/generated.rs | 18 -- .../models/table1/generated.rs | 18 -- .../models/table2/generated.rs | 18 -- .../models/table1/generated.rs | 18 -- .../models/table2/generated.rs | 18 -- .../models/table1.rs | 18 -- .../models/table2.rs | 18 -- .../models/table1/generated.rs | 18 -- .../models/table2/generated.rs | 18 -- .../models/user/generated.rs | 18 -- test/readonly/models/normal/generated.rs | 18 -- .../readonly/models/prefix_table/generated.rs | 18 -- .../models/prefix_table_suffix/generated.rs | 18 -- .../readonly/models/table_suffix/generated.rs | 18 -- test/simple_table/models/todos/generated.rs | 18 -- .../models/todos/generated.rs | 18 -- .../models/todos/generated.rs | 18 -- .../models/todos/generated.rs | 18 -- test/single_model_file/models/table1.rs | 18 -- test/single_model_file/models/table2.rs | 18 -- .../models/fang_tasks/generated.rs | 18 -- 46 files changed, 373 insertions(+), 639 deletions(-) create mode 100644 test/advanced_queries/models/mod.rs create mode 100644 test/advanced_queries/models/todos/generated.rs create mode 100644 test/advanced_queries/models/todos/mod.rs create mode 100644 test/advanced_queries/schema.rs create mode 100755 test/advanced_queries/test.sh diff --git a/Cargo.toml b/Cargo.toml index 158c6b89..d7719beb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,8 @@ async = [] backtrace = [] # enable derive "QueryableByName" derive-queryablebyname = [] +# enable *experimental* queries +advanced-queries = [] [dependencies] clap = { version = "4.4", features = ["derive", "wrap_help"] } diff --git a/README.md b/README.md index 8e95b0ba..e9aa97f3 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,7 @@ async fn demo(db: Connection) { text: "Create a demo", completed: false, })?; - - let todos_list = todos::paginate(&mut db, 1, 10)?; - + let updated_todo = todos::update(&mut db, created_todo.id, UpdateTodo { text: created_todo.text, completed: true, @@ -120,6 +118,7 @@ cargo install dsync * `--single-model-file`: (optional) Generate only a single model file, instead of a directory with `mod.rs` and `generated.rs` * `--readonly-prefix`: (optional, repeatable) A prefix to treat a table matching this as readonly *2 * `--readonly-suffix`: (optional, repeatable) A suffix to treat a table matching this as readonly *2 +* `--diesel-backend`: (when the "advanced-queries" feature is enabled) The diesel backend in use (possible values include `diesel::pg::Pg`, `diesel::sqlite::Sqlite`, `diesel::mysql::Mysql`, or your custom backend type) * note: the CLI has fail-safes to prevent accidental file overwriting ```sh @@ -131,6 +130,14 @@ Notes: - *2: "readonly" tables dont have `Update*` & `Create*` structs, only `*`(no suffix / prefix) structs. For example this is useful for Sqlite views, which are read-only (cannot be written to, but can be read) +## Experimental API + +We're currently experimenting with advanced query generation. This includes pagination, filtering/searching, and the like. Enable the `advanced-queries` feature flag to see some of it in action. + +Alternatively, you can see what gets generated in the advanced queries test here: [`test/advanced_queries/models`](test/advanced_queries/models) + +Feel free to open an issue to discuss these API and provide your feeedback. + ## Docs See `dsync --help` for more information. diff --git a/src/bin/main.rs b/src/bin/main.rs index ea4adf11..6923d685 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -125,6 +125,17 @@ pub struct MainOptions { /// A Suffix to treat a table matching this as readonly (only generate the Read struct) #[arg(long = "readonly-suffix")] pub readonly_suffixes: Vec, + + #[cfg(feature = "advanced-queries")] + /// Set which diesel backend to use (something which implements `diesel::backend::Backend`) + /// Diesel provides the following backends: + /// - `diesel::pg::Pg` + /// - `diesel::sqlite::Sqlite` + /// - `diesel::mysql::Mysql` + /// + /// See `crate::GenerationConfig::diesel_backend` for more details. + #[arg(short = 'b', long = "diesel-backend")] + pub diesel_backend: String, } #[derive(Debug, ValueEnum, Clone, PartialEq, Default)] @@ -251,6 +262,8 @@ fn actual_main() -> dsync::Result<()> { once_connection_type: args.once_connection_type, readonly_prefixes: args.readonly_prefixes, readonly_suffixes: args.readonly_suffixes, + #[cfg(feature = "advanced-queries")] + diesel_backend: args.diesel_backend, }, )?; diff --git a/src/code.rs b/src/code.rs index 88a25569..57608ff0 100644 --- a/src/code.rs +++ b/src/code.rs @@ -535,14 +535,16 @@ fn build_table_fns( "## )); + #[cfg(feature = "advanced-queries")] buffer.push_str(&format!(r##" /// Paginates through the table where page is a 0-based index (i.e. page 0 is the first page) - pub{async_keyword} fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> diesel::QueryResult> {{ + pub{async_keyword} fn paginate(db: &mut ConnectionType, page: i64, page_size: i64, filter: {struct_name}Filter) -> diesel::QueryResult> {{ use {schema_path}{table_name}::dsl::*; - let page_size = if page_size < 1 {{ 1 }} else {{ page_size }}; - let total_items = {table_name}.count().get_result(db){await_keyword}?; - let items = {table_name}.limit(page_size).offset(page * page_size).load::(db){await_keyword}?; + let page = page.max(0); + let page_size = page_size.max(1); + let total_items = Self::filter(filter.clone()).count().get_result(db)?; + let items = Self::filter(filter).limit(page_size).offset(page * page_size).load::(db){await_keyword}?; Ok(PaginationResult {{ items, @@ -555,6 +557,67 @@ fn build_table_fns( }} "##)); + #[cfg(feature = "advanced-queries")] + // Table::filter() helper fn + { + let diesel_backend = &config.diesel_backend; + let filters = table + .columns + .iter() + .map(|column| { + let column_name = column.name.to_string(); + + if column.is_nullable { + // "Option::None" will never match anything, and "is_null" is required to be used, see https://docs.diesel.rs/master/diesel/expression_methods/trait.ExpressionMethods.html#method.eq + format!( + r##" + if let Some(filter_{column_name}) = filter.{column_name}.clone() {{ + query = if filter_{column_name}.is_some() {{ + query.filter({schema_path}{table_name}::{column_name}.eq(filter_{column_name})) + }} else {{ + query.filter({schema_path}{table_name}::{column_name}.is_null()) + }}; + }}"## + ) + } else { + format!( + r##" + if let Some(filter_{column_name}) = filter.{column_name}.clone() {{ + query = query.filter({schema_path}{table_name}::{column_name}.eq(filter_{column_name})); + }}"## + ) + } + }) + .collect::>() + .join(""); + buffer.push_str(&format!( + r##" + /// A utility function to help build custom search queries + /// + /// Example: + /// + /// ``` + /// // create a filter for completed todos + /// let query = Todo::filter(TodoFilter {{ + /// completed: Some(true), + /// ..Default::default() + /// }}); + /// + /// // delete completed todos + /// diesel::delete(query).execute(db)?; + /// ``` + pub fn filter<'a>( + filter: {struct_name}Filter, + ) -> {schema_path}{table_name}::BoxedQuery<'a, {diesel_backend}> {{ + let mut query = {schema_path}{table_name}::table.into_boxed(); + {filters} + + query + }} +"## + )); + } + // TODO: If primary key columns are attached to the form struct (not optionally) // then don't require item_id_params (otherwise it'll be duplicated) @@ -589,6 +652,33 @@ fn build_table_fns( buffer.push_str("}\n"); + #[cfg(feature = "advanced-queries")] + // generate filter struct for filter() helper function + { + let filter_fields = table + .columns + .iter() + .map(|column| { + let struct_field = StructField::from(column); + format!( + "pub {column_name}: Option<{column_type}>,", + column_name = struct_field.name, + column_type = struct_field.to_rust_type() + ) + }) + .collect::>() + .join("\n "); + + buffer.push_str(&formatdoc!( + r##" + #[derive(Debug, Default, Clone)] + pub struct {struct_name}Filter {{ + {filter_fields} + }} + "## + )); + } + buffer } diff --git a/src/error.rs b/src/error.rs index 4e1f972f..70d5641a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -122,6 +122,10 @@ pub enum ErrorEnum { #[error("NoFileSignature: {0}")] NoFileSignature(String), + /// Invalid generation config + #[error("InvalidGenerationConfig: {0}")] + InvalidGenerationConfig(String), + /// Variant for Other messages #[error("Other: {0}")] Other(String), diff --git a/src/lib.rs b/src/lib.rs index d269d1fb..56deeff1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,8 @@ use std::collections::HashMap; use std::fmt::Display; use std::path::{Path, PathBuf}; +use crate::error::ErrorEnum; + /// Available options for string types #[derive(Debug, Clone, Copy, PartialEq, Default)] pub enum StringType { @@ -306,9 +308,9 @@ impl<'a> Default for TableOptions<'a> { /// Global config, not table specific #[derive(Debug, Clone)] pub struct GenerationConfig<'a> { - /// Specific Table options for a given table + /// Specific code generation options for a particular table pub table_options: HashMap<&'a str, TableOptions<'a>>, - /// Default table options, used when not in `table_options` + /// Default table options, can be overriden by `table_options` pub default_table_options: TableOptions<'a>, /// Connection type to insert /// @@ -335,6 +337,16 @@ pub struct GenerationConfig<'a> { pub readonly_prefixes: Vec, /// Suffixes to treat tables as readonly pub readonly_suffixes: Vec, + + #[cfg(feature = "advanced-queries")] + /// Diesel backend + /// + /// For example: + /// - `diesel::pg::Pg` (default) + /// - `diesel::sqlite::Sqlite` + /// - `diesel::mysql::Mysql` + /// - or, your custom diesel backend type (struct which implements `diesel::backend::Backend`) + pub diesel_backend: String, } impl GenerationConfig<'_> { @@ -356,6 +368,25 @@ impl GenerationConfig<'_> { } } +#[cfg(feature = "advanced-queries")] +pub fn validate_config(config: &GenerationConfig) -> Result<()> { + const VALID_BACKENDS: [&str; 3] = [ + "diesel::pg::Pg", + "diesel::sqlite::Sqlite", + "diesel::mysql::Mysql", + ]; + + if config.diesel_backend.is_empty() { + return Err(Error::new(ErrorEnum::InvalidGenerationConfig(format!( + "Invalid diesel_backend '{}', please use one of the following: {:?}; or, a custom diesel backend type (a struct which implements `diesel::backend::Backend`).", + &config.diesel_backend, + VALID_BACKENDS.join(", ") + )))); + } + + Ok(()) +} + /// Generate a model for the given schema contents /// /// Model is returned and not saved to disk yet @@ -436,6 +467,9 @@ pub fn generate_files( output_models_dir: &Path, config: GenerationConfig, ) -> Result> { + #[cfg(feature = "advanced-queries")] + validate_config(&config)?; + let generated = generate_code( &std::fs::read_to_string(input_diesel_schema_file) .attach_path_err(input_diesel_schema_file)?, diff --git a/test/advanced_queries/models/mod.rs b/test/advanced_queries/models/mod.rs new file mode 100644 index 00000000..015a6a2b --- /dev/null +++ b/test/advanced_queries/models/mod.rs @@ -0,0 +1 @@ +pub mod todos; diff --git a/test/advanced_queries/models/todos/generated.rs b/test/advanced_queries/models/todos/generated.rs new file mode 100644 index 00000000..2e995c7f --- /dev/null +++ b/test/advanced_queries/models/todos/generated.rs @@ -0,0 +1,190 @@ +/* @generated and managed by dsync */ + +use crate::diesel::*; +use crate::schema::*; + +pub type ConnectionType = diesel::r2d2::PooledConnection>; + +/// Struct representing a row in table `todos` +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, diesel::Queryable, diesel::Selectable, diesel::QueryableByName)] +#[diesel(table_name=todos, primary_key(id))] +pub struct Todos { + /// Field representing column `id` + pub id: i32, + /// Field representing column `unsigned` + pub unsigned: u32, + /// Field representing column `unsigned_nullable` + pub unsigned_nullable: Option, + /// Field representing column `text` + pub text: String, + /// Field representing column `completed` + pub completed: bool, + /// Field representing column `type` + pub type_: String, + /// Field representing column `created_at` + pub created_at: chrono::DateTime, + /// Field representing column `updated_at` + pub updated_at: chrono::DateTime, +} + +/// Create Struct for a row in table `todos` for [`Todos`] +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, diesel::Insertable)] +#[diesel(table_name=todos)] +pub struct CreateTodos { + /// Field representing column `unsigned` + pub unsigned: u32, + /// Field representing column `unsigned_nullable` + pub unsigned_nullable: Option, + /// Field representing column `text` + pub text: String, + /// Field representing column `completed` + pub completed: bool, + /// Field representing column `type` + pub type_: String, +} + +/// Update Struct for a row in table `todos` for [`Todos`] +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, diesel::AsChangeset, PartialEq, Default)] +#[diesel(table_name=todos)] +pub struct UpdateTodos { + /// Field representing column `unsigned` + pub unsigned: Option, + /// Field representing column `unsigned_nullable` + pub unsigned_nullable: Option>, + /// Field representing column `text` + pub text: Option, + /// Field representing column `completed` + pub completed: Option, + /// Field representing column `type` + pub type_: Option, + /// Field representing column `created_at` + pub created_at: Option>, + /// Field representing column `updated_at` + pub updated_at: Option>, +} + +/// Result of a `.paginate` function +#[derive(Debug, serde::Serialize)] +pub struct PaginationResult { + /// Resulting items that are from the current page + pub items: Vec, + /// 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) -> diesel::QueryResult { + use crate::schema::todos::dsl::*; + + diesel::insert_into(todos).values(item).get_result::(db) + } + + /// Get a row from `todos`, identified by the primary key + pub fn read(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { + use crate::schema::todos::dsl::*; + + todos.filter(id.eq(param_id)).first::(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, filter: TodosFilter) -> diesel::QueryResult> { + use crate::schema::todos::dsl::*; + + let page = page.max(0); + let page_size = page_size.max(1); + let total_items = Self::filter(filter.clone()).count().get_result(db)?; + let items = Self::filter(filter).limit(page_size).offset(page * page_size).load::(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) + }) + } + + /// A utility function to help build custom search queries + /// + /// Example: + /// + /// ``` + /// // create a filter for completed todos + /// let query = Todo::filter(TodoFilter { + /// completed: Some(true), + /// ..Default::default() + /// }); + /// + /// // delete completed todos + /// diesel::delete(query).execute(db)?; + /// ``` + pub fn filter<'a>( + filter: TodosFilter, + ) -> crate::schema::todos::BoxedQuery<'a, diesel::pg::Pg> { + let mut query = crate::schema::todos::table.into_boxed(); + + if let Some(filter_id) = filter.id.clone() { + query = query.filter(crate::schema::todos::id.eq(filter_id)); + } + if let Some(filter_unsigned) = filter.unsigned.clone() { + query = query.filter(crate::schema::todos::unsigned.eq(filter_unsigned)); + } + if let Some(filter_unsigned_nullable) = filter.unsigned_nullable.clone() { + query = if filter_unsigned_nullable.is_some() { + query.filter(crate::schema::todos::unsigned_nullable.eq(filter_unsigned_nullable)) + } else { + query.filter(crate::schema::todos::unsigned_nullable.is_null()) + }; + } + if let Some(filter_text) = filter.text.clone() { + query = query.filter(crate::schema::todos::text.eq(filter_text)); + } + if let Some(filter_completed) = filter.completed.clone() { + query = query.filter(crate::schema::todos::completed.eq(filter_completed)); + } + if let Some(filter_type_) = filter.type_.clone() { + query = query.filter(crate::schema::todos::type_.eq(filter_type_)); + } + if let Some(filter_created_at) = filter.created_at.clone() { + query = query.filter(crate::schema::todos::created_at.eq(filter_created_at)); + } + if let Some(filter_updated_at) = filter.updated_at.clone() { + query = query.filter(crate::schema::todos::updated_at.eq(filter_updated_at)); + } + + query + } + + /// Update a row in `todos`, identified by the primary key with [`UpdateTodos`] + pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodos) -> diesel::QueryResult { + use crate::schema::todos::dsl::*; + + diesel::update(todos.filter(id.eq(param_id))).set(item).get_result(db) + } + + /// Delete a row in `todos`, identified by the primary key + pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { + use crate::schema::todos::dsl::*; + + diesel::delete(todos.filter(id.eq(param_id))).execute(db) + } +} +#[derive(Debug, Default, Clone)] +pub struct TodosFilter { + pub id: Option, + pub unsigned: Option, + pub unsigned_nullable: Option>, + pub text: Option, + pub completed: Option, + pub type_: Option, + pub created_at: Option>, + pub updated_at: Option>, +} diff --git a/test/advanced_queries/models/todos/mod.rs b/test/advanced_queries/models/todos/mod.rs new file mode 100644 index 00000000..a5bb9b90 --- /dev/null +++ b/test/advanced_queries/models/todos/mod.rs @@ -0,0 +1,2 @@ +pub use generated::*; +pub mod generated; diff --git a/test/advanced_queries/schema.rs b/test/advanced_queries/schema.rs new file mode 100644 index 00000000..dc033baf --- /dev/null +++ b/test/advanced_queries/schema.rs @@ -0,0 +1,14 @@ +diesel::table! { + todos (id) { + id -> Int4, + unsigned -> Unsigned, + unsigned_nullable -> Nullable>, + text -> Text, + completed -> Bool, + #[sql_name = "type"] + #[max_length = 255] + type_ -> Varchar, + created_at -> Timestamptz, + updated_at -> Timestamptz, + } +} diff --git a/test/advanced_queries/test.sh b/test/advanced_queries/test.sh new file mode 100755 index 00000000..b843c869 --- /dev/null +++ b/test/advanced_queries/test.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +cd $SCRIPT_DIR + +cargo run -F advanced-queries -- --diesel-backend diesel::pg::Pg -i schema.rs -o models -g id -g created_at -g updated_at -c "diesel::r2d2::PooledConnection>" \ No newline at end of file diff --git a/test/autogenerated_all/models/todos/generated.rs b/test/autogenerated_all/models/todos/generated.rs index c95c56dd..96a9e952 100644 --- a/test/autogenerated_all/models/todos/generated.rs +++ b/test/autogenerated_all/models/todos/generated.rs @@ -53,24 +53,6 @@ impl Todos { todos.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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_id: i32, item: &UpdateTodos) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/autogenerated_attributes/models/todos/generated.rs b/test/autogenerated_attributes/models/todos/generated.rs index d9e3d9dc..dbe37fe9 100644 --- a/test/autogenerated_attributes/models/todos/generated.rs +++ b/test/autogenerated_attributes/models/todos/generated.rs @@ -61,24 +61,6 @@ impl Todos { todos.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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_id: i32, item: &UpdateTodos) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/autogenerated_primary_keys/models/todos/generated.rs b/test/autogenerated_primary_keys/models/todos/generated.rs index 67a76628..4b9c2dd7 100644 --- a/test/autogenerated_primary_keys/models/todos/generated.rs +++ b/test/autogenerated_primary_keys/models/todos/generated.rs @@ -61,24 +61,6 @@ impl Todos { todos.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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_id: i32, item: &UpdateTodos) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/cleanup_generated_content/models/todos/generated.rs b/test/cleanup_generated_content/models/todos/generated.rs index aceaeb41..9ebf1942 100644 --- a/test/cleanup_generated_content/models/todos/generated.rs +++ b/test/cleanup_generated_content/models/todos/generated.rs @@ -77,24 +77,6 @@ impl Todos { todos.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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_id: i32, item: &UpdateTodos) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/create_update_bytes_cow/models/todos/generated.rs b/test/create_update_bytes_cow/models/todos/generated.rs index a7ee8464..8661721d 100644 --- a/test/create_update_bytes_cow/models/todos/generated.rs +++ b/test/create_update_bytes_cow/models/todos/generated.rs @@ -63,24 +63,6 @@ impl Todos { todos.filter(data.eq(param_data)).first::(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) -> diesel::QueryResult> { - 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::(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, item: &UpdateTodos) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/create_update_bytes_slice/models/todos/generated.rs b/test/create_update_bytes_slice/models/todos/generated.rs index 07cb96a4..e4e2d3c8 100644 --- a/test/create_update_bytes_slice/models/todos/generated.rs +++ b/test/create_update_bytes_slice/models/todos/generated.rs @@ -63,24 +63,6 @@ impl Todos { todos.filter(data.eq(param_data)).first::(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) -> diesel::QueryResult> { - 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::(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, item: &UpdateTodos) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/create_update_str_cow/models/todos/generated.rs b/test/create_update_str_cow/models/todos/generated.rs index 6f0c6fc0..dd61ada7 100644 --- a/test/create_update_str_cow/models/todos/generated.rs +++ b/test/create_update_str_cow/models/todos/generated.rs @@ -75,24 +75,6 @@ impl Todos { todos.filter(text.eq(param_text)).first::(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) -> diesel::QueryResult> { - 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::(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_text: String, item: &UpdateTodos) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/create_update_str_str/models/todos/generated.rs b/test/create_update_str_str/models/todos/generated.rs index e020e1c3..886bdf58 100644 --- a/test/create_update_str_str/models/todos/generated.rs +++ b/test/create_update_str_str/models/todos/generated.rs @@ -75,24 +75,6 @@ impl Todos { todos.filter(text.eq(param_text)).first::(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) -> diesel::QueryResult> { - 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::(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_text: String, item: &UpdateTodos) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/custom_model_and_schema_path/data/models/table_a/generated.rs b/test/custom_model_and_schema_path/data/models/table_a/generated.rs index e189aaff..812a89f6 100644 --- a/test/custom_model_and_schema_path/data/models/table_a/generated.rs +++ b/test/custom_model_and_schema_path/data/models/table_a/generated.rs @@ -51,24 +51,6 @@ impl TableA { tableA.filter(_id.eq(param__id)).first::(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) -> diesel::QueryResult> { - use crate::data::schema::tableA::dsl::*; - - let page_size = if page_size < 1 { 1 } else { page_size }; - let total_items = tableA.count().get_result(db)?; - let items = tableA.limit(page_size).offset(page * page_size).load::(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) - }) - } - /// Delete a row in `tableA`, identified by the primary key pub fn delete(db: &mut ConnectionType, param__id: i32) -> diesel::QueryResult { use crate::data::schema::tableA::dsl::*; diff --git a/test/custom_model_and_schema_path/data/models/table_b/generated.rs b/test/custom_model_and_schema_path/data/models/table_b/generated.rs index d48aead6..cdcf4ad3 100644 --- a/test/custom_model_and_schema_path/data/models/table_b/generated.rs +++ b/test/custom_model_and_schema_path/data/models/table_b/generated.rs @@ -64,24 +64,6 @@ impl TableB { tableB.filter(_id.eq(param__id)).first::(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) -> diesel::QueryResult> { - use crate::data::schema::tableB::dsl::*; - - let page_size = if page_size < 1 { 1 } else { page_size }; - let total_items = tableB.count().get_result(db)?; - let items = tableB.limit(page_size).offset(page * page_size).load::(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 `tableB`, identified by the primary key with [`UpdateTableB`] pub fn update(db: &mut ConnectionType, param__id: i32, item: &UpdateTableB) -> diesel::QueryResult { use crate::data::schema::tableB::dsl::*; diff --git a/test/custom_model_path/models/table_a/generated.rs b/test/custom_model_path/models/table_a/generated.rs index 1855353f..65d43a3a 100644 --- a/test/custom_model_path/models/table_a/generated.rs +++ b/test/custom_model_path/models/table_a/generated.rs @@ -51,24 +51,6 @@ impl TableA { tableA.filter(_id.eq(param__id)).first::(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) -> diesel::QueryResult> { - use crate::schema::tableA::dsl::*; - - let page_size = if page_size < 1 { 1 } else { page_size }; - let total_items = tableA.count().get_result(db)?; - let items = tableA.limit(page_size).offset(page * page_size).load::(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) - }) - } - /// Delete a row in `tableA`, identified by the primary key pub fn delete(db: &mut ConnectionType, param__id: i32) -> diesel::QueryResult { use crate::schema::tableA::dsl::*; diff --git a/test/custom_model_path/models/table_b/generated.rs b/test/custom_model_path/models/table_b/generated.rs index 2a848832..d9ab2308 100644 --- a/test/custom_model_path/models/table_b/generated.rs +++ b/test/custom_model_path/models/table_b/generated.rs @@ -64,24 +64,6 @@ impl TableB { tableB.filter(_id.eq(param__id)).first::(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) -> diesel::QueryResult> { - use crate::schema::tableB::dsl::*; - - let page_size = if page_size < 1 { 1 } else { page_size }; - let total_items = tableB.count().get_result(db)?; - let items = tableB.limit(page_size).offset(page * page_size).load::(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 `tableB`, identified by the primary key with [`UpdateTableB`] pub fn update(db: &mut ConnectionType, param__id: i32, item: &UpdateTableB) -> diesel::QueryResult { use crate::schema::tableB::dsl::*; diff --git a/test/manual_primary_keys/models/todos/generated.rs b/test/manual_primary_keys/models/todos/generated.rs index 2c91b48a..5b8b4c13 100644 --- a/test/manual_primary_keys/models/todos/generated.rs +++ b/test/manual_primary_keys/models/todos/generated.rs @@ -51,24 +51,6 @@ impl Todos { todos.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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) - }) - } - /// Delete a row in `todos`, identified by the primary key pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/multiple_primary_keys/models/users/generated.rs b/test/multiple_primary_keys/models/users/generated.rs index 46a6192a..9df11315 100644 --- a/test/multiple_primary_keys/models/users/generated.rs +++ b/test/multiple_primary_keys/models/users/generated.rs @@ -67,24 +67,6 @@ impl Users { users.filter(name.eq(param_name)).filter(address.eq(param_address)).first::(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) -> diesel::QueryResult> { - use crate::schema::users::dsl::*; - - let page_size = if page_size < 1 { 1 } else { page_size }; - let total_items = users.count().get_result(db)?; - let items = users.limit(page_size).offset(page * page_size).load::(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 `users`, identified by the primary keys with [`UpdateUsers`] pub fn update(db: &mut ConnectionType, param_name: String, param_address: String, item: &UpdateUsers) -> diesel::QueryResult { use crate::schema::users::dsl::*; diff --git a/test/no_default_features/models/todos/generated.rs b/test/no_default_features/models/todos/generated.rs index f734cd5a..b184146f 100644 --- a/test/no_default_features/models/todos/generated.rs +++ b/test/no_default_features/models/todos/generated.rs @@ -93,24 +93,6 @@ impl Todos { todos.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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_id: i32, item: &UpdateTodos) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/once_common_structs/models/table1/generated.rs b/test/once_common_structs/models/table1/generated.rs index 5bd29d89..e2fef4b1 100644 --- a/test/once_common_structs/models/table1/generated.rs +++ b/test/once_common_structs/models/table1/generated.rs @@ -29,24 +29,6 @@ impl Table1 { table1.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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) - }) - } - /// Delete a row in `table1`, identified by the primary key pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { use crate::schema::table1::dsl::*; diff --git a/test/once_common_structs/models/table2/generated.rs b/test/once_common_structs/models/table2/generated.rs index af9b5fac..4a9b4403 100644 --- a/test/once_common_structs/models/table2/generated.rs +++ b/test/once_common_structs/models/table2/generated.rs @@ -29,24 +29,6 @@ impl Table2 { table2.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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) - }) - } - /// Delete a row in `table2`, identified by the primary key pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { use crate::schema::table2::dsl::*; diff --git a/test/once_common_structs_once_connection_type/models/table1/generated.rs b/test/once_common_structs_once_connection_type/models/table1/generated.rs index 30b6860f..7d940d4c 100644 --- a/test/once_common_structs_once_connection_type/models/table1/generated.rs +++ b/test/once_common_structs_once_connection_type/models/table1/generated.rs @@ -27,24 +27,6 @@ impl Table1 { table1.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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) - }) - } - /// Delete a row in `table1`, identified by the primary key pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { use crate::schema::table1::dsl::*; diff --git a/test/once_common_structs_once_connection_type/models/table2/generated.rs b/test/once_common_structs_once_connection_type/models/table2/generated.rs index 7fb5035c..ce4aef40 100644 --- a/test/once_common_structs_once_connection_type/models/table2/generated.rs +++ b/test/once_common_structs_once_connection_type/models/table2/generated.rs @@ -27,24 +27,6 @@ impl Table2 { table2.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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) - }) - } - /// Delete a row in `table2`, identified by the primary key pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { use crate::schema::table2::dsl::*; diff --git a/test/once_common_structs_once_connection_type_single_file/models/table1.rs b/test/once_common_structs_once_connection_type_single_file/models/table1.rs index 30b6860f..7d940d4c 100644 --- a/test/once_common_structs_once_connection_type_single_file/models/table1.rs +++ b/test/once_common_structs_once_connection_type_single_file/models/table1.rs @@ -27,24 +27,6 @@ impl Table1 { table1.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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) - }) - } - /// Delete a row in `table1`, identified by the primary key pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { use crate::schema::table1::dsl::*; diff --git a/test/once_common_structs_once_connection_type_single_file/models/table2.rs b/test/once_common_structs_once_connection_type_single_file/models/table2.rs index 7fb5035c..ce4aef40 100644 --- a/test/once_common_structs_once_connection_type_single_file/models/table2.rs +++ b/test/once_common_structs_once_connection_type_single_file/models/table2.rs @@ -27,24 +27,6 @@ impl Table2 { table2.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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) - }) - } - /// Delete a row in `table2`, identified by the primary key pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { use crate::schema::table2::dsl::*; diff --git a/test/once_connection_type/models/table1/generated.rs b/test/once_connection_type/models/table1/generated.rs index 114f019c..03911360 100644 --- a/test/once_connection_type/models/table1/generated.rs +++ b/test/once_connection_type/models/table1/generated.rs @@ -42,24 +42,6 @@ impl Table1 { table1.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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) - }) - } - /// Delete a row in `table1`, identified by the primary key pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { use crate::schema::table1::dsl::*; diff --git a/test/once_connection_type/models/table2/generated.rs b/test/once_connection_type/models/table2/generated.rs index ca83f780..27cbd524 100644 --- a/test/once_connection_type/models/table2/generated.rs +++ b/test/once_connection_type/models/table2/generated.rs @@ -42,24 +42,6 @@ impl Table2 { table2.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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) - }) - } - /// Delete a row in `table2`, identified by the primary key pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { use crate::schema::table2::dsl::*; diff --git a/test/postgres_array_column/models/user/generated.rs b/test/postgres_array_column/models/user/generated.rs index d547eaa9..01c8c6a7 100644 --- a/test/postgres_array_column/models/user/generated.rs +++ b/test/postgres_array_column/models/user/generated.rs @@ -67,24 +67,6 @@ impl User { user.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - use crate::schema::user::dsl::*; - - let page_size = if page_size < 1 { 1 } else { page_size }; - let total_items = user.count().get_result(db)?; - let items = user.limit(page_size).offset(page * page_size).load::(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 `user`, identified by the primary key with [`UpdateUser`] pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateUser) -> diesel::QueryResult { use crate::schema::user::dsl::*; diff --git a/test/readonly/models/normal/generated.rs b/test/readonly/models/normal/generated.rs index 08bb99aa..83f216a6 100644 --- a/test/readonly/models/normal/generated.rs +++ b/test/readonly/models/normal/generated.rs @@ -61,24 +61,6 @@ impl Normal { normal.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - use crate::schema::normal::dsl::*; - - let page_size = if page_size < 1 { 1 } else { page_size }; - let total_items = normal.count().get_result(db)?; - let items = normal.limit(page_size).offset(page * page_size).load::(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 `normal`, identified by the primary key with [`UpdateNormal`] pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateNormal) -> diesel::QueryResult { use crate::schema::normal::dsl::*; diff --git a/test/readonly/models/prefix_table/generated.rs b/test/readonly/models/prefix_table/generated.rs index 9f5bf802..31d53a51 100644 --- a/test/readonly/models/prefix_table/generated.rs +++ b/test/readonly/models/prefix_table/generated.rs @@ -37,22 +37,4 @@ impl PrefixTable { prefixTable.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - use crate::schema::prefixTable::dsl::*; - - let page_size = if page_size < 1 { 1 } else { page_size }; - let total_items = prefixTable.count().get_result(db)?; - let items = prefixTable.limit(page_size).offset(page * page_size).load::(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) - }) - } } diff --git a/test/readonly/models/prefix_table_suffix/generated.rs b/test/readonly/models/prefix_table_suffix/generated.rs index 6e0e86b8..04ecc241 100644 --- a/test/readonly/models/prefix_table_suffix/generated.rs +++ b/test/readonly/models/prefix_table_suffix/generated.rs @@ -37,22 +37,4 @@ impl PrefixTableSuffix { prefixTableSuffix.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - use crate::schema::prefixTableSuffix::dsl::*; - - let page_size = if page_size < 1 { 1 } else { page_size }; - let total_items = prefixTableSuffix.count().get_result(db)?; - let items = prefixTableSuffix.limit(page_size).offset(page * page_size).load::(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) - }) - } } diff --git a/test/readonly/models/table_suffix/generated.rs b/test/readonly/models/table_suffix/generated.rs index cd7ab188..e986e111 100644 --- a/test/readonly/models/table_suffix/generated.rs +++ b/test/readonly/models/table_suffix/generated.rs @@ -37,22 +37,4 @@ impl TableSuffix { tableSuffix.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - use crate::schema::tableSuffix::dsl::*; - - let page_size = if page_size < 1 { 1 } else { page_size }; - let total_items = tableSuffix.count().get_result(db)?; - let items = tableSuffix.limit(page_size).offset(page * page_size).load::(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) - }) - } } diff --git a/test/simple_table/models/todos/generated.rs b/test/simple_table/models/todos/generated.rs index 05aa49e7..64aa5752 100644 --- a/test/simple_table/models/todos/generated.rs +++ b/test/simple_table/models/todos/generated.rs @@ -93,24 +93,6 @@ impl Todos { todos.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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_id: i32, item: &UpdateTodos) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/simple_table_async/models/todos/generated.rs b/test/simple_table_async/models/todos/generated.rs index 6eeb56e5..8f049b19 100644 --- a/test/simple_table_async/models/todos/generated.rs +++ b/test/simple_table_async/models/todos/generated.rs @@ -82,24 +82,6 @@ impl Todos { todos.filter(id.eq(param_id)).first::(db).await } - /// Paginates through the table where page is a 0-based index (i.e. page 0 is the first page) - pub async fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> diesel::QueryResult> { - use crate::schema::todos::dsl::*; - - let page_size = if page_size < 1 { 1 } else { page_size }; - let total_items = todos.count().get_result(db).await?; - let items = todos.limit(page_size).offset(page * page_size).load::(db).await?; - - 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 async fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodos) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/simple_table_custom_schema_path/models/todos/generated.rs b/test/simple_table_custom_schema_path/models/todos/generated.rs index e24e9228..443a08b3 100644 --- a/test/simple_table_custom_schema_path/models/todos/generated.rs +++ b/test/simple_table_custom_schema_path/models/todos/generated.rs @@ -81,24 +81,6 @@ impl Todos { todos.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - use crate::data::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::(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_id: i32, item: &UpdateTodos) -> diesel::QueryResult { use crate::data::schema::todos::dsl::*; diff --git a/test/simple_table_no_serde/models/todos/generated.rs b/test/simple_table_no_serde/models/todos/generated.rs index 45adb060..8171bd47 100644 --- a/test/simple_table_no_serde/models/todos/generated.rs +++ b/test/simple_table_no_serde/models/todos/generated.rs @@ -81,24 +81,6 @@ impl Todos { todos.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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_id: i32, item: &UpdateTodos) -> diesel::QueryResult { use crate::schema::todos::dsl::*; diff --git a/test/single_model_file/models/table1.rs b/test/single_model_file/models/table1.rs index 2b82aa18..5689ac58 100644 --- a/test/single_model_file/models/table1.rs +++ b/test/single_model_file/models/table1.rs @@ -43,24 +43,6 @@ impl Table1 { table1.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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) - }) - } - /// Delete a row in `table1`, identified by the primary key pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { use crate::schema::table1::dsl::*; diff --git a/test/single_model_file/models/table2.rs b/test/single_model_file/models/table2.rs index 116c884c..1f89881b 100644 --- a/test/single_model_file/models/table2.rs +++ b/test/single_model_file/models/table2.rs @@ -43,24 +43,6 @@ impl Table2 { table2.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - 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::(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) - }) - } - /// Delete a row in `table2`, identified by the primary key pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult { use crate::schema::table2::dsl::*; diff --git a/test/use_statements/models/fang_tasks/generated.rs b/test/use_statements/models/fang_tasks/generated.rs index 0901871a..f4592420 100644 --- a/test/use_statements/models/fang_tasks/generated.rs +++ b/test/use_statements/models/fang_tasks/generated.rs @@ -111,24 +111,6 @@ impl FangTasks { fang_tasks.filter(id.eq(param_id)).first::(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) -> diesel::QueryResult> { - use crate::schema::fang_tasks::dsl::*; - - let page_size = if page_size < 1 { 1 } else { page_size }; - let total_items = fang_tasks.count().get_result(db)?; - let items = fang_tasks.limit(page_size).offset(page * page_size).load::(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 `fang_tasks`, identified by the primary key with [`UpdateFangTasks`] pub fn update(db: &mut ConnectionType, param_id: uuid::Uuid, item: &UpdateFangTasks) -> diesel::QueryResult { use crate::schema::fang_tasks::dsl::*;