Skip to content

Commit

Permalink
fix(code): rename "type Connection" to "type ConnectionType"
Browse files Browse the repository at this point in the history
to lessen conflicts with diesel's "Connection" trait
  • Loading branch information
hasezoey committed Sep 4, 2023
1 parent cabf20f commit 20cf314
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 94 deletions.
14 changes: 7 additions & 7 deletions src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl {struct_name} {{
if create_struct.has_fields() {
buffer.push_str(&format!(
r##"
pub{async_keyword} fn create(db: &mut Connection, item: &{create_struct_identifier}) -> QueryResult<Self> {{
pub{async_keyword} fn create(db: &mut ConnectionType, item: &{create_struct_identifier}) -> QueryResult<Self> {{
use {schema_path}{table_name}::dsl::*;
insert_into({table_name}).values(item).get_result::<Self>(db){await_keyword}
Expand All @@ -380,7 +380,7 @@ impl {struct_name} {{
} else {
buffer.push_str(&format!(
r##"
pub{async_keyword} fn create(db: &mut Connection) -> QueryResult<Self> {{
pub{async_keyword} fn create(db: &mut ConnectionType) -> QueryResult<Self> {{
use {schema_path}{table_name}::dsl::*;
insert_into({table_name}).default_values().get_result::<Self>(db){await_keyword}
Expand All @@ -391,7 +391,7 @@ impl {struct_name} {{

buffer.push_str(&format!(
r##"
pub{async_keyword} fn read(db: &mut Connection, {item_id_params}) -> QueryResult<Self> {{
pub{async_keyword} fn read(db: &mut ConnectionType, {item_id_params}) -> QueryResult<Self> {{
use {schema_path}{table_name}::dsl::*;
{table_name}.{item_id_filters}.first::<Self>(db){await_keyword}
Expand All @@ -401,7 +401,7 @@ impl {struct_name} {{

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 Connection, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> {{
pub{async_keyword} fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> {{
use {schema_path}{table_name}::dsl::*;
let page_size = if page_size < 1 {{ 1 }} else {{ page_size }};
Expand Down Expand Up @@ -429,7 +429,7 @@ impl {struct_name} {{
// we should generate an update() method.

buffer.push_str(&format!(r##"
pub{async_keyword} fn update(db: &mut Connection, {item_id_params}, item: &{update_struct_identifier}) -> QueryResult<Self> {{
pub{async_keyword} fn update(db: &mut ConnectionType, {item_id_params}, item: &{update_struct_identifier}) -> QueryResult<Self> {{
use {schema_path}{table_name}::dsl::*;
diesel::update({table_name}.{item_id_filters}).set(item).get_result(db){await_keyword}
Expand All @@ -439,7 +439,7 @@ impl {struct_name} {{

buffer.push_str(&format!(
r##"
pub{async_keyword} fn delete(db: &mut Connection, {item_id_params}) -> QueryResult<usize> {{
pub{async_keyword} fn delete(db: &mut ConnectionType, {item_id_params}) -> QueryResult<usize> {{
use {schema_path}{table_name}::dsl::*;
diesel::delete({table_name}.{item_id_filters}).execute(db){await_keyword}
Expand Down Expand Up @@ -496,7 +496,7 @@ fn build_imports(table: &ParsedTableMacro, config: &GenerationConfig) -> String

let connection_type_alias = if table_options.get_fns() {
format!(
"\ntype Connection = {connection_type};",
"\ntype ConnectionType = {connection_type};",
connection_type = config.connection_type,
)
} else {
Expand Down
12 changes: 6 additions & 6 deletions test/autogenerated_all/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use diesel::QueryResult;
use serde::{Deserialize, Serialize};


type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Selectable)]
#[diesel(table_name=todos, primary_key(id))]
Expand Down Expand Up @@ -35,20 +35,20 @@ pub struct PaginationResult<T> {

impl Todo {

pub fn create(db: &mut Connection) -> QueryResult<Self> {
pub fn create(db: &mut ConnectionType) -> QueryResult<Self> {
use crate::schema::todos::dsl::*;

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

pub fn read(db: &mut Connection, param_id: i32) -> QueryResult<Self> {
pub fn read(db: &mut ConnectionType, param_id: i32) -> 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 Connection, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> {
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 };
Expand All @@ -65,13 +65,13 @@ impl Todo {
})
}

pub fn update(db: &mut Connection, param_id: i32, item: &UpdateTodo) -> QueryResult<Self> {
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodo) -> QueryResult<Self> {
use crate::schema::todos::dsl::*;

diesel::update(todos.filter(id.eq(param_id))).set(item).get_result(db)
}

pub fn delete(db: &mut Connection, param_id: i32) -> QueryResult<usize> {
pub fn delete(db: &mut ConnectionType, param_id: i32) -> QueryResult<usize> {
use crate::schema::todos::dsl::*;

diesel::delete(todos.filter(id.eq(param_id))).execute(db)
Expand Down
12 changes: 6 additions & 6 deletions test/autogenerated_attributes/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use diesel::QueryResult;
use serde::{Deserialize, Serialize};


type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Selectable)]
#[diesel(table_name=todos, primary_key(id))]
Expand Down Expand Up @@ -40,20 +40,20 @@ pub struct PaginationResult<T> {

impl Todo {

pub fn create(db: &mut Connection, item: &CreateTodo) -> QueryResult<Self> {
pub fn create(db: &mut ConnectionType, item: &CreateTodo) -> QueryResult<Self> {
use crate::schema::todos::dsl::*;

insert_into(todos).values(item).get_result::<Self>(db)
}

pub fn read(db: &mut Connection, param_id: i32) -> QueryResult<Self> {
pub fn read(db: &mut ConnectionType, param_id: i32) -> 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 Connection, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> {
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 };
Expand All @@ -70,13 +70,13 @@ impl Todo {
})
}

pub fn update(db: &mut Connection, param_id: i32, item: &UpdateTodo) -> QueryResult<Self> {
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodo) -> QueryResult<Self> {
use crate::schema::todos::dsl::*;

diesel::update(todos.filter(id.eq(param_id))).set(item).get_result(db)
}

pub fn delete(db: &mut Connection, param_id: i32) -> QueryResult<usize> {
pub fn delete(db: &mut ConnectionType, param_id: i32) -> QueryResult<usize> {
use crate::schema::todos::dsl::*;

diesel::delete(todos.filter(id.eq(param_id))).execute(db)
Expand Down
12 changes: 6 additions & 6 deletions test/autogenerated_primary_keys/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use diesel::QueryResult;
use serde::{Deserialize, Serialize};


type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Selectable)]
#[diesel(table_name=todos, primary_key(id))]
Expand Down Expand Up @@ -40,20 +40,20 @@ pub struct PaginationResult<T> {

impl Todo {

pub fn create(db: &mut Connection, item: &CreateTodo) -> QueryResult<Self> {
pub fn create(db: &mut ConnectionType, item: &CreateTodo) -> QueryResult<Self> {
use crate::schema::todos::dsl::*;

insert_into(todos).values(item).get_result::<Self>(db)
}

pub fn read(db: &mut Connection, param_id: i32) -> QueryResult<Self> {
pub fn read(db: &mut ConnectionType, param_id: i32) -> 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 Connection, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> {
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 };
Expand All @@ -70,13 +70,13 @@ impl Todo {
})
}

pub fn update(db: &mut Connection, param_id: i32, item: &UpdateTodo) -> QueryResult<Self> {
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodo) -> QueryResult<Self> {
use crate::schema::todos::dsl::*;

diesel::update(todos.filter(id.eq(param_id))).set(item).get_result(db)
}

pub fn delete(db: &mut Connection, param_id: i32) -> QueryResult<usize> {
pub fn delete(db: &mut ConnectionType, param_id: i32) -> QueryResult<usize> {
use crate::schema::todos::dsl::*;

diesel::delete(todos.filter(id.eq(param_id))).execute(db)
Expand Down
12 changes: 6 additions & 6 deletions test/cleanup_generated_content/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use diesel::QueryResult;
use serde::{Deserialize, Serialize};


type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Selectable)]
#[diesel(table_name=todos, primary_key(id))]
Expand Down Expand Up @@ -48,20 +48,20 @@ pub struct PaginationResult<T> {

impl Todo {

pub fn create(db: &mut Connection, item: &CreateTodo) -> QueryResult<Self> {
pub fn create(db: &mut ConnectionType, item: &CreateTodo) -> QueryResult<Self> {
use crate::schema::todos::dsl::*;

insert_into(todos).values(item).get_result::<Self>(db)
}

pub fn read(db: &mut Connection, param_id: i32) -> QueryResult<Self> {
pub fn read(db: &mut ConnectionType, param_id: i32) -> 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 Connection, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> {
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 };
Expand All @@ -78,13 +78,13 @@ impl Todo {
})
}

pub fn update(db: &mut Connection, param_id: i32, item: &UpdateTodo) -> QueryResult<Self> {
pub fn update(db: &mut ConnectionType, param_id: i32, item: &UpdateTodo) -> QueryResult<Self> {
use crate::schema::todos::dsl::*;

diesel::update(todos.filter(id.eq(param_id))).set(item).get_result(db)
}

pub fn delete(db: &mut Connection, param_id: i32) -> QueryResult<usize> {
pub fn delete(db: &mut ConnectionType, param_id: i32) -> QueryResult<usize> {
use crate::schema::todos::dsl::*;

diesel::delete(todos.filter(id.eq(param_id))).execute(db)
Expand Down
10 changes: 5 additions & 5 deletions test/custom_model_and_schema_path/models/tableA/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use diesel::QueryResult;
use serde::{Deserialize, Serialize};


type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, Selectable)]
#[diesel(table_name=tableA, primary_key(_id))]
Expand Down Expand Up @@ -34,20 +34,20 @@ pub struct PaginationResult<T> {

impl TableA {

pub fn create(db: &mut Connection, item: &CreateTableA) -> QueryResult<Self> {
pub fn create(db: &mut ConnectionType, item: &CreateTableA) -> QueryResult<Self> {
use crate::data::schema::tableA::dsl::*;

insert_into(tableA).values(item).get_result::<Self>(db)
}

pub fn read(db: &mut Connection, param__id: i32) -> QueryResult<Self> {
pub fn read(db: &mut ConnectionType, param__id: i32) -> QueryResult<Self> {
use crate::data::schema::tableA::dsl::*;

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

let page_size = if page_size < 1 { 1 } else { page_size };
Expand All @@ -64,7 +64,7 @@ impl TableA {
})
}

pub fn delete(db: &mut Connection, param__id: i32) -> QueryResult<usize> {
pub fn delete(db: &mut ConnectionType, param__id: i32) -> QueryResult<usize> {
use crate::data::schema::tableA::dsl::*;

diesel::delete(tableA.filter(_id.eq(param__id))).execute(db)
Expand Down
12 changes: 6 additions & 6 deletions test/custom_model_and_schema_path/models/tableB/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use diesel::QueryResult;
use serde::{Deserialize, Serialize};
use crate::data::models::table_a::TableA;

type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, AsChangeset, Identifiable, Associations, Selectable)]
#[diesel(table_name=tableB, primary_key(_id), belongs_to(TableA, foreign_key=link))]
Expand Down Expand Up @@ -41,20 +41,20 @@ pub struct PaginationResult<T> {

impl TableB {

pub fn create(db: &mut Connection, item: &CreateTableB) -> QueryResult<Self> {
pub fn create(db: &mut ConnectionType, item: &CreateTableB) -> QueryResult<Self> {
use crate::data::schema::tableB::dsl::*;

insert_into(tableB).values(item).get_result::<Self>(db)
}

pub fn read(db: &mut Connection, param__id: i32) -> QueryResult<Self> {
pub fn read(db: &mut ConnectionType, param__id: i32) -> QueryResult<Self> {
use crate::data::schema::tableB::dsl::*;

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

let page_size = if page_size < 1 { 1 } else { page_size };
Expand All @@ -71,13 +71,13 @@ impl TableB {
})
}

pub fn update(db: &mut Connection, param__id: i32, item: &UpdateTableB) -> QueryResult<Self> {
pub fn update(db: &mut ConnectionType, param__id: i32, item: &UpdateTableB) -> QueryResult<Self> {
use crate::data::schema::tableB::dsl::*;

diesel::update(tableB.filter(_id.eq(param__id))).set(item).get_result(db)
}

pub fn delete(db: &mut Connection, param__id: i32) -> QueryResult<usize> {
pub fn delete(db: &mut ConnectionType, param__id: i32) -> QueryResult<usize> {
use crate::data::schema::tableB::dsl::*;

diesel::delete(tableB.filter(_id.eq(param__id))).execute(db)
Expand Down
10 changes: 5 additions & 5 deletions test/custom_model_path/models/tableA/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use diesel::QueryResult;
use serde::{Deserialize, Serialize};


type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
type ConnectionType = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;

#[derive(Debug, Serialize, Deserialize, Clone, Queryable, Insertable, Selectable)]
#[diesel(table_name=tableA, primary_key(_id))]
Expand Down Expand Up @@ -34,20 +34,20 @@ pub struct PaginationResult<T> {

impl TableA {

pub fn create(db: &mut Connection, item: &CreateTableA) -> QueryResult<Self> {
pub fn create(db: &mut ConnectionType, item: &CreateTableA) -> QueryResult<Self> {
use crate::schema::tableA::dsl::*;

insert_into(tableA).values(item).get_result::<Self>(db)
}

pub fn read(db: &mut Connection, param__id: i32) -> QueryResult<Self> {
pub fn read(db: &mut ConnectionType, param__id: i32) -> QueryResult<Self> {
use crate::schema::tableA::dsl::*;

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

let page_size = if page_size < 1 { 1 } else { page_size };
Expand All @@ -64,7 +64,7 @@ impl TableA {
})
}

pub fn delete(db: &mut Connection, param__id: i32) -> QueryResult<usize> {
pub fn delete(db: &mut ConnectionType, param__id: i32) -> QueryResult<usize> {
use crate::schema::tableA::dsl::*;

diesel::delete(tableA.filter(_id.eq(param__id))).execute(db)
Expand Down
Loading

0 comments on commit 20cf314

Please sign in to comment.