Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ pub mod derives {
pub const DEBUG: &str = "Debug";
pub const DEFAULT: &str = "Default";
pub const CLONE: &str = "Clone";
pub const QUERYABLE: &str = "Queryable";
pub const INSERTABLE: &str = "Insertable";
pub const QUERYABLE: &str = "diesel::Queryable";
pub const INSERTABLE: &str = "diesel::Insertable";
pub const SERIALIZE: &str = "serde::Serialize";
pub const DESERIALIZE: &str = "serde::Deserialize";
pub const ASCHANGESET: &str = "AsChangeset";
pub const SELECTABLE: &str = "Selectable";
pub const IDENTIFIABLE: &str = "Identifiable";
pub const ASSOCIATIONS: &str = "Associations";
pub const ASCHANGESET: &str = "diesel::AsChangeset";
pub const SELECTABLE: &str = "diesel::Selectable";
pub const IDENTIFIABLE: &str = "diesel::Identifiable";
pub const ASSOCIATIONS: &str = "diesel::Associations";
#[cfg(feature = "derive-queryablebyname")]
pub const QUERYABLEBYNAME: &str = "QueryableByName";
pub const QUERYABLEBYNAME: &str = "diesel::QueryableByName";
pub const PARTIALEQ: &str = "PartialEq";
}

Expand Down Expand Up @@ -482,21 +482,21 @@ fn build_table_fns(
buffer.push_str(&format!(
r##"
/// Insert a new row into `{table_name}` with a given [`{create_struct_identifier}`]
pub{async_keyword} fn create(db: &mut ConnectionType, item: &{create_struct_identifier}) -> QueryResult<Self> {{
pub{async_keyword} fn create(db: &mut ConnectionType, item: &{create_struct_identifier}) -> diesel::QueryResult<Self> {{
use {schema_path}{table_name}::dsl::*;

insert_into({table_name}).values(item).get_result::<Self>(db){await_keyword}
diesel::insert_into({table_name}).values(item).get_result::<Self>(db){await_keyword}
}}
"##
));
} else {
buffer.push_str(&format!(
r##"
/// Insert a new row into `{table_name}` with all default values
pub{async_keyword} fn create(db: &mut ConnectionType) -> QueryResult<Self> {{
pub{async_keyword} fn create(db: &mut ConnectionType) -> diesel::QueryResult<Self> {{
use {schema_path}{table_name}::dsl::*;

insert_into({table_name}).default_values().get_result::<Self>(db){await_keyword}
diesel::insert_into({table_name}).default_values().get_result::<Self>(db){await_keyword}
}}
"##
));
Expand All @@ -513,7 +513,7 @@ fn build_table_fns(
buffer.push_str(&format!(
r##"
/// Get a row from `{table_name}`, identified by the primary {key_maybe_multiple}
pub{async_keyword} fn read(db: &mut ConnectionType, {item_id_params}) -> QueryResult<Self> {{
pub{async_keyword} fn read(db: &mut ConnectionType, {item_id_params}) -> diesel::QueryResult<Self> {{
use {schema_path}{table_name}::dsl::*;

{table_name}.{item_id_filters}.first::<Self>(db){await_keyword}
Expand All @@ -523,7 +523,7 @@ fn build_table_fns(

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) -> QueryResult<PaginationResult<Self>> {{
pub{async_keyword} fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> diesel::QueryResult<PaginationResult<Self>> {{
use {schema_path}{table_name}::dsl::*;

let page_size = if page_size < 1 {{ 1 }} else {{ page_size }};
Expand Down Expand Up @@ -552,7 +552,7 @@ fn build_table_fns(

buffer.push_str(&format!(r##"
/// Update a row in `{table_name}`, identified by the primary {key_maybe_multiple} with [`{update_struct_identifier}`]
pub{async_keyword} fn update(db: &mut ConnectionType, {item_id_params}, item: &{update_struct_identifier}) -> QueryResult<Self> {{
pub{async_keyword} fn update(db: &mut ConnectionType, {item_id_params}, item: &{update_struct_identifier}) -> diesel::QueryResult<Self> {{
use {schema_path}{table_name}::dsl::*;

diesel::update({table_name}.{item_id_filters}).set(item).get_result(db){await_keyword}
Expand All @@ -564,7 +564,7 @@ fn build_table_fns(
buffer.push_str(&format!(
r##"
/// Delete a row in `{table_name}`, identified by the primary {key_maybe_multiple}
pub{async_keyword} fn delete(db: &mut ConnectionType, {item_id_params}) -> QueryResult<usize> {{
pub{async_keyword} fn delete(db: &mut ConnectionType, {item_id_params}) -> diesel::QueryResult<usize> {{
use {schema_path}{table_name}::dsl::*;

diesel::delete({table_name}.{item_id_filters}).execute(db){await_keyword}
Expand Down Expand Up @@ -646,10 +646,6 @@ fn build_imports(table: &ParsedTableMacro, config: &GenerationConfig) -> String
// no "::" because that is already included in the schema_path
imports_vec.push(format!("use {}*;", config.schema_path));

if table_options.get_fns() {
imports_vec.push("use diesel::QueryResult;".into());
};

if config.once_common_structs || config.once_connection_type {
imports_vec.push(format!("use {}common::*;", config.model_path));
};
Expand Down
17 changes: 8 additions & 9 deletions test/autogenerated_all/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

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)]
#[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`
Expand All @@ -17,7 +16,7 @@ pub struct Todos {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, AsChangeset, PartialEq, Default)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, diesel::AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `created_at`
Expand All @@ -41,21 +40,21 @@ pub struct PaginationResult<T> {

impl Todos {
/// Insert a new row into `todos` with all default values
pub fn create(db: &mut ConnectionType) -> QueryResult<Self> {
pub fn create(db: &mut ConnectionType) -> diesel::QueryResult<Self> {
use crate::schema::todos::dsl::*;

insert_into(todos).default_values().get_result::<Self>(db)
diesel::insert_into(todos).default_values().get_result::<Self>(db)
}

/// Get a row from `todos`, identified by the primary key
pub fn read(db: &mut ConnectionType, param_id: i32) -> QueryResult<Self> {
pub fn read(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult<Self> {
use crate::schema::todos::dsl::*;

todos.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>> {
pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> diesel::QueryResult<PaginationResult<Self>> {
use crate::schema::todos::dsl::*;

let page_size = if page_size < 1 { 1 } else { page_size };
Expand All @@ -73,14 +72,14 @@ impl Todos {
}

/// Update a row in `todos`, identified by the primary key with [`UpdateTodos`]
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodos) -> QueryResult<Self> {
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodos) -> diesel::QueryResult<Self> {
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) -> QueryResult<usize> {
pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult<usize> {
use crate::schema::todos::dsl::*;

diesel::delete(todos.filter(id.eq(param_id))).execute(db)
Expand Down
19 changes: 9 additions & 10 deletions test/autogenerated_attributes/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

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)]
#[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`
Expand All @@ -17,15 +16,15 @@ pub struct Todos {
}

/// Create Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Insertable)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, diesel::Insertable)]
#[diesel(table_name=todos)]
pub struct CreateTodos {
/// Field representing column `id`
pub id: i32,
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, AsChangeset, PartialEq, Default)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, diesel::AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `created_at`
Expand All @@ -49,21 +48,21 @@ pub struct PaginationResult<T> {

impl Todos {
/// Insert a new row into `todos` with a given [`CreateTodos`]
pub fn create(db: &mut ConnectionType, item: &CreateTodos) -> QueryResult<Self> {
pub fn create(db: &mut ConnectionType, item: &CreateTodos) -> diesel::QueryResult<Self> {
use crate::schema::todos::dsl::*;

insert_into(todos).values(item).get_result::<Self>(db)
diesel::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_id: i32) -> QueryResult<Self> {
pub fn read(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult<Self> {
use crate::schema::todos::dsl::*;

todos.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>> {
pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> diesel::QueryResult<PaginationResult<Self>> {
use crate::schema::todos::dsl::*;

let page_size = if page_size < 1 { 1 } else { page_size };
Expand All @@ -81,14 +80,14 @@ impl Todos {
}

/// Update a row in `todos`, identified by the primary key with [`UpdateTodos`]
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodos) -> QueryResult<Self> {
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodos) -> diesel::QueryResult<Self> {
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) -> QueryResult<usize> {
pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult<usize> {
use crate::schema::todos::dsl::*;

diesel::delete(todos.filter(id.eq(param_id))).execute(db)
Expand Down
19 changes: 9 additions & 10 deletions test/autogenerated_primary_keys/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

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)]
#[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`
Expand All @@ -17,15 +16,15 @@ pub struct Todos {
}

/// Create Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Insertable)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, diesel::Insertable)]
#[diesel(table_name=todos)]
pub struct CreateTodos {
/// Field representing column `text`
pub text: String,
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, AsChangeset, PartialEq, Default)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, diesel::AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `text`
Expand All @@ -49,21 +48,21 @@ pub struct PaginationResult<T> {

impl Todos {
/// Insert a new row into `todos` with a given [`CreateTodos`]
pub fn create(db: &mut ConnectionType, item: &CreateTodos) -> QueryResult<Self> {
pub fn create(db: &mut ConnectionType, item: &CreateTodos) -> diesel::QueryResult<Self> {
use crate::schema::todos::dsl::*;

insert_into(todos).values(item).get_result::<Self>(db)
diesel::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_id: i32) -> QueryResult<Self> {
pub fn read(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult<Self> {
use crate::schema::todos::dsl::*;

todos.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>> {
pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> diesel::QueryResult<PaginationResult<Self>> {
use crate::schema::todos::dsl::*;

let page_size = if page_size < 1 { 1 } else { page_size };
Expand All @@ -81,14 +80,14 @@ impl Todos {
}

/// Update a row in `todos`, identified by the primary key with [`UpdateTodos`]
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodos) -> QueryResult<Self> {
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodos) -> diesel::QueryResult<Self> {
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) -> QueryResult<usize> {
pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult<usize> {
use crate::schema::todos::dsl::*;

diesel::delete(todos.filter(id.eq(param_id))).execute(db)
Expand Down
19 changes: 9 additions & 10 deletions test/cleanup_generated_content/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

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)]
#[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`
Expand All @@ -23,7 +22,7 @@ pub struct Todos {
}

/// Create Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Insertable)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, diesel::Insertable)]
#[diesel(table_name=todos)]
pub struct CreateTodos {
/// Field representing column `id`
Expand All @@ -35,7 +34,7 @@ pub struct CreateTodos {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, AsChangeset, PartialEq, Default)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, diesel::AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `text`
Expand Down Expand Up @@ -65,21 +64,21 @@ pub struct PaginationResult<T> {

impl Todos {
/// Insert a new row into `todos` with a given [`CreateTodos`]
pub fn create(db: &mut ConnectionType, item: &CreateTodos) -> QueryResult<Self> {
pub fn create(db: &mut ConnectionType, item: &CreateTodos) -> diesel::QueryResult<Self> {
use crate::schema::todos::dsl::*;

insert_into(todos).values(item).get_result::<Self>(db)
diesel::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_id: i32) -> QueryResult<Self> {
pub fn read(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult<Self> {
use crate::schema::todos::dsl::*;

todos.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>> {
pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> diesel::QueryResult<PaginationResult<Self>> {
use crate::schema::todos::dsl::*;

let page_size = if page_size < 1 { 1 } else { page_size };
Expand All @@ -97,14 +96,14 @@ impl Todos {
}

/// Update a row in `todos`, identified by the primary key with [`UpdateTodos`]
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodos) -> QueryResult<Self> {
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodos) -> diesel::QueryResult<Self> {
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) -> QueryResult<usize> {
pub fn delete(db: &mut ConnectionType, param_id: i32) -> diesel::QueryResult<usize> {
use crate::schema::todos::dsl::*;

diesel::delete(todos.filter(id.eq(param_id))).execute(db)
Expand Down
Loading