From 4377bd49ad17a31ea0b602a37d643c8ed53e20c1 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 23 Sep 2021 13:28:26 +0800 Subject: [PATCH 01/15] add debug_query make get raw_sql becomes simply. --- .gitignore | 3 ++- src/query/traits.rs | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 950c227c7..0156ca9a0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ target Cargo.lock *.sublime* -.vscode \ No newline at end of file +.vscode +.idea/ \ No newline at end of file diff --git a/src/query/traits.rs b/src/query/traits.rs index f0045f524..5f1e634cb 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -1,4 +1,4 @@ -use crate::{DbBackend, Statement}; +use crate::{DatabaseConnection, DbBackend, Statement}; use sea_query::QueryStatementBuilder; pub trait QueryTrait { @@ -22,3 +22,15 @@ pub trait QueryTrait { ) } } + +/// Make get raw_sql becomes simply. It does not need to specify a specific `DbBackend`, +/// but can be obtained through `get_database_backend` with `DatabaseConnection`. +/// Return a Statement type. +pub fn debug_query(query: &impl QueryTrait, conn: &DatabaseConnection) -> Statement { + query.build(conn.get_database_backend()) +} + +/// Use `debug_query` get raw_sql. +pub fn debug_query_fmt(query: &impl QueryTrait, conn: &DatabaseConnection) -> String { + debug_query(query, conn).to_string() +} From 8f0852958ffe216ab75c276063852c1d30da2dd4 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 23 Sep 2021 21:30:53 +0800 Subject: [PATCH 02/15] refactor code with macro debug_query! ref:https://github.com/SeaQL/sea-orm/issues/145 --- src/query/traits.rs | 93 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 89 insertions(+), 4 deletions(-) diff --git a/src/query/traits.rs b/src/query/traits.rs index 5f1e634cb..a0f533795 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -23,14 +23,99 @@ pub trait QueryTrait { } } +#[derive(Debug)] +pub struct DebugQuery<'a, Q, T> { + pub query: &'a Q, + pub value: T, +} + +impl<'a, Q> DebugQuery<'a, Q, DbBackend> +where + Q: QueryTrait, +{ + pub fn build(&self) -> Statement { + self.query.build(self.value) + } +} + +impl<'a, Q> DebugQuery<'a, Q, &DatabaseConnection> +where + Q: QueryTrait, +{ + pub fn build(&self) -> Statement { + self.query.build(self.value.get_database_backend()) + } +} + /// Make get raw_sql becomes simply. It does not need to specify a specific `DbBackend`, /// but can be obtained through `get_database_backend` with `DatabaseConnection`. /// Return a Statement type. -pub fn debug_query(query: &impl QueryTrait, conn: &DatabaseConnection) -> Statement { - query.build(conn.get_database_backend()) +/// +/// +/// # Example +/// +/// ``` +/// # #[cfg(feature = "mock")] +/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend}; +/// # +/// # let conn = MockDatabase::new(DbBackend::Postgres) +/// # .into_connection(); +/// # +/// use sea_orm::{entity::*, query::*, tests_cfg::cake,debug_query}; +/// +/// let c = cake::Entity::insert( +/// cake::ActiveModel { +/// id: ActiveValue::set(1), +/// name: ActiveValue::set("Apple Pie".to_owned()), +/// }); +/// +/// let raw_sql = debug_query!(&c,&conn).to_string(); +/// assert_eq!(raw_sql,r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#); +/// +/// let raw_sql = debug_query!(&c,DbBackend::MySql).to_string(); +/// assert_eq!(raw_sql,r#"INSERT INTO `cake` (`id`, `name`) VALUES (1, 'Apple Pie')"#); +/// +/// ``` +#[macro_export] +macro_rules! debug_query { + ($query:expr,$value:expr) => { + $crate::DebugQuery { + query: $query, + value: $value, + } + .build(); + }; } /// Use `debug_query` get raw_sql. -pub fn debug_query_fmt(query: &impl QueryTrait, conn: &DatabaseConnection) -> String { - debug_query(query, conn).to_string() +/// +/// # Example +/// +/// ``` +/// # #[cfg(feature = "mock")] +/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend}; +/// # +/// # let conn = MockDatabase::new(DbBackend::Postgres) +/// # .into_connection(); +/// # +/// use sea_orm::{entity::*, query::*, tests_cfg::cake,debug_query_fmt}; +/// +/// let c = cake::Entity::insert( +/// cake::ActiveModel { +/// id: ActiveValue::set(1), +/// name: ActiveValue::set("Apple Pie".to_owned()), +/// }); +/// +/// let raw_sql = debug_query_fmt!(&c,&conn); +/// assert_eq!(raw_sql,r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#); +/// +/// let raw_sql = debug_query_fmt!(&c,DbBackend::Sqlite); +/// assert_eq!(raw_sql,r#"INSERT INTO `cake` (`id`, `name`) VALUES (1, 'Apple Pie')"#); +/// +/// ``` +#[macro_export] +macro_rules! debug_query_fmt { + ($query:expr,$value:expr) => { + $crate::debug_query!($query, $value).to_string(); + }; } From aa7e053ba04146e5bdcfbab11a7f9c695be62641 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 23 Sep 2021 21:31:54 +0800 Subject: [PATCH 03/15] update doc --- src/query/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/query/traits.rs b/src/query/traits.rs index a0f533795..acd207a23 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -87,7 +87,7 @@ macro_rules! debug_query { }; } -/// Use `debug_query` get raw_sql. +/// Use `debug_query` macro get raw_sql. /// /// # Example /// From 10eeca16876f2f06ee0aee83e92b593b1f3f68b4 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 23 Sep 2021 22:10:53 +0800 Subject: [PATCH 04/15] add DbConnection with DebugQuey build overload method. Working for #145 --- src/query/traits.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/query/traits.rs b/src/query/traits.rs index acd207a23..546ce008d 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -47,6 +47,15 @@ where } } +impl<'a, Q> DebugQuery<'a, Q, DatabaseConnection> +where + Q: QueryTrait, +{ + pub fn build(&self) -> Statement { + self.query.build(self.value.get_database_backend()) + } +} + /// Make get raw_sql becomes simply. It does not need to specify a specific `DbBackend`, /// but can be obtained through `get_database_backend` with `DatabaseConnection`. /// Return a Statement type. @@ -69,7 +78,7 @@ where /// name: ActiveValue::set("Apple Pie".to_owned()), /// }); /// -/// let raw_sql = debug_query!(&c,&conn).to_string(); +/// let raw_sql = debug_query!(&c,conn).to_string(); /// assert_eq!(raw_sql,r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#); /// /// let raw_sql = debug_query!(&c,DbBackend::MySql).to_string(); From 536a9002caacbc08cd6fb1b71150494fb3394a4b Mon Sep 17 00:00:00 2001 From: baoyachi Date: Fri, 24 Sep 2021 14:24:41 +0800 Subject: [PATCH 05/15] all rename with DbBackEnd --- src/database/connection.rs | 4 +--- tests/common/setup/mod.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/database/connection.rs b/src/database/connection.rs index 995dde9d4..e79002633 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -17,14 +17,12 @@ pub enum DatabaseConnection { pub type DbConn = DatabaseConnection; #[derive(Debug, Copy, Clone, PartialEq)] -pub enum DatabaseBackend { +pub enum DbBackend { MySql, Postgres, Sqlite, } -pub type DbBackend = DatabaseBackend; - impl Default for DatabaseConnection { fn default() -> Self { Self::Disconnected diff --git a/tests/common/setup/mod.rs b/tests/common/setup/mod.rs index d982b2b7b..d6b98ac13 100644 --- a/tests/common/setup/mod.rs +++ b/tests/common/setup/mod.rs @@ -1,4 +1,4 @@ -use sea_orm::{Database, DatabaseBackend, DatabaseConnection, Statement}; +use sea_orm::{Database, DbBackend, DatabaseConnection, Statement}; pub mod schema; pub use schema::*; @@ -8,14 +8,14 @@ pub async fn setup(base_url: &str, db_name: &str) -> DatabaseConnection { let db = Database::connect(&url).await.unwrap(); let _drop_db_result = db .execute(Statement::from_string( - DatabaseBackend::MySql, + DbBackend::MySql, format!("DROP DATABASE IF EXISTS `{}`;", db_name), )) .await; let _create_db_result = db .execute(Statement::from_string( - DatabaseBackend::MySql, + DbBackend::MySql, format!("CREATE DATABASE `{}`;", db_name), )) .await; @@ -27,14 +27,14 @@ pub async fn setup(base_url: &str, db_name: &str) -> DatabaseConnection { let db = Database::connect(&url).await.unwrap(); let _drop_db_result = db .execute(Statement::from_string( - DatabaseBackend::Postgres, + DbBackend::Postgres, format!("DROP DATABASE IF EXISTS \"{}\";", db_name), )) .await; let _create_db_result = db .execute(Statement::from_string( - DatabaseBackend::Postgres, + DbBackend::Postgres, format!("CREATE DATABASE \"{}\";", db_name), )) .await; @@ -63,7 +63,7 @@ pub async fn tear_down(base_url: &str, db_name: &str) { let db = Database::connect(&url).await.unwrap(); let _ = db .execute(Statement::from_string( - DatabaseBackend::MySql, + DbBackend::MySql, format!("DROP DATABASE IF EXISTS \"{}\";", db_name), )) .await; @@ -72,7 +72,7 @@ pub async fn tear_down(base_url: &str, db_name: &str) { let db = Database::connect(&url).await.unwrap(); let _ = db .execute(Statement::from_string( - DatabaseBackend::Postgres, + DbBackend::Postgres, format!("DROP DATABASE IF EXISTS \"{}\";", db_name), )) .await; From 9af9637cf42fca20b28bc954fc25423793ad17fb Mon Sep 17 00:00:00 2001 From: baoyachi Date: Fri, 24 Sep 2021 14:30:53 +0800 Subject: [PATCH 06/15] define IntoDbBackEnd trait --- src/database/connection.rs | 43 +++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/database/connection.rs b/src/database/connection.rs index e79002633..cf02790da 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -16,6 +16,14 @@ pub enum DatabaseConnection { pub type DbConn = DatabaseConnection; +pub trait IntoDbBackend{ + fn build(&self, statement: &S) -> Statement + where + S: StatementBuilder; + + fn get_query_builder(&self) -> Box; +} + #[derive(Debug, Copy, Clone, PartialEq)] pub enum DbBackend { MySql, @@ -23,6 +31,24 @@ pub enum DbBackend { Sqlite, } +impl IntoDbBackend for DbBackend { + fn build(&self, statement: &S) -> Statement + where + S: StatementBuilder, + { + statement.build(self) + } + + fn get_query_builder(&self) -> Box { + match self { + Self::MySql => Box::new(MysqlQueryBuilder), + Self::Postgres => Box::new(PostgresQueryBuilder), + Self::Sqlite => Box::new(SqliteQueryBuilder), + } + } +} + + impl Default for DatabaseConnection { fn default() -> Self { Self::Disconnected @@ -126,23 +152,6 @@ impl DatabaseConnection { } } -impl DbBackend { - pub fn build(&self, statement: &S) -> Statement - where - S: StatementBuilder, - { - statement.build(self) - } - - pub fn get_query_builder(&self) -> Box { - match self { - Self::MySql => Box::new(MysqlQueryBuilder), - Self::Postgres => Box::new(PostgresQueryBuilder), - Self::Sqlite => Box::new(SqliteQueryBuilder), - } - } -} - #[cfg(test)] mod tests { use crate::DatabaseConnection; From 849ffeb7d03ff6e6bbb34da8e4b4078bbfa4b66f Mon Sep 17 00:00:00 2001 From: baoyachi Date: Fri, 24 Sep 2021 15:00:10 +0800 Subject: [PATCH 07/15] fix build error --- src/database/statement.rs | 2 +- src/executor/delete.rs | 4 +--- src/executor/insert.rs | 5 +---- src/executor/paginator.rs | 2 +- src/executor/select.rs | 6 +----- src/executor/update.rs | 4 +--- src/query/traits.rs | 2 +- 7 files changed, 7 insertions(+), 18 deletions(-) diff --git a/src/database/statement.rs b/src/database/statement.rs index 12b074873..d1e7f430b 100644 --- a/src/database/statement.rs +++ b/src/database/statement.rs @@ -1,4 +1,4 @@ -use crate::DbBackend; +use crate::{DbBackend, IntoDbBackend}; use sea_query::{inject_parameters, MysqlQueryBuilder, PostgresQueryBuilder, SqliteQueryBuilder}; pub use sea_query::{Value, Values}; use std::fmt; diff --git a/src/executor/delete.rs b/src/executor/delete.rs index 807bc5442..c5c61c2af 100644 --- a/src/executor/delete.rs +++ b/src/executor/delete.rs @@ -1,6 +1,4 @@ -use crate::{ - error::*, ActiveModelTrait, DatabaseConnection, DeleteMany, DeleteOne, EntityTrait, Statement, -}; +use crate::{error::*, ActiveModelTrait, DatabaseConnection, DeleteMany, DeleteOne, EntityTrait, Statement, IntoDbBackend}; use sea_query::DeleteStatement; use std::future::Future; diff --git a/src/executor/insert.rs b/src/executor/insert.rs index 1f4936ba4..b30ac979c 100644 --- a/src/executor/insert.rs +++ b/src/executor/insert.rs @@ -1,7 +1,4 @@ -use crate::{ - error::*, ActiveModelTrait, DatabaseConnection, EntityTrait, Insert, PrimaryKeyTrait, - Statement, TryFromU64, -}; +use crate::{error::*, ActiveModelTrait, DatabaseConnection, EntityTrait, Insert, PrimaryKeyTrait, Statement, TryFromU64, IntoDbBackend}; use sea_query::InsertStatement; use std::{future::Future, marker::PhantomData}; diff --git a/src/executor/paginator.rs b/src/executor/paginator.rs index 24822111d..b2b2e80a4 100644 --- a/src/executor/paginator.rs +++ b/src/executor/paginator.rs @@ -1,4 +1,4 @@ -use crate::{error::*, DatabaseConnection, SelectorTrait}; +use crate::{error::*, DatabaseConnection, SelectorTrait, IntoDbBackend}; use async_stream::stream; use futures::Stream; use sea_query::{Alias, Expr, SelectStatement}; diff --git a/src/executor/select.rs b/src/executor/select.rs index 959091c01..55b297ada 100644 --- a/src/executor/select.rs +++ b/src/executor/select.rs @@ -1,8 +1,4 @@ -use crate::{ - error::*, DatabaseConnection, EntityTrait, FromQueryResult, IdenStatic, Iterable, JsonValue, - ModelTrait, Paginator, PrimaryKeyToColumn, QueryResult, Select, SelectA, SelectB, SelectTwo, - SelectTwoMany, Statement, -}; +use crate::{error::*, DatabaseConnection, EntityTrait, FromQueryResult, IdenStatic, Iterable, JsonValue, ModelTrait, Paginator, PrimaryKeyToColumn, QueryResult, Select, SelectA, SelectB, SelectTwo, SelectTwoMany, Statement, IntoDbBackend}; use sea_query::SelectStatement; use std::marker::PhantomData; diff --git a/src/executor/update.rs b/src/executor/update.rs index 6c7a98733..a7cf23c71 100644 --- a/src/executor/update.rs +++ b/src/executor/update.rs @@ -1,6 +1,4 @@ -use crate::{ - error::*, ActiveModelTrait, DatabaseConnection, EntityTrait, Statement, UpdateMany, UpdateOne, -}; +use crate::{error::*, ActiveModelTrait, DatabaseConnection, EntityTrait, Statement, UpdateMany, UpdateOne, IntoDbBackend}; use sea_query::UpdateStatement; use std::future::Future; diff --git a/src/query/traits.rs b/src/query/traits.rs index 546ce008d..a0b9bd32d 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -1,4 +1,4 @@ -use crate::{DatabaseConnection, DbBackend, Statement}; +use crate::{DatabaseConnection, DbBackend, Statement, IntoDbBackend}; use sea_query::QueryStatementBuilder; pub trait QueryTrait { From a1bbb72b2d6153b31eabd96eb66f2d988a6311a7 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Fri, 24 Sep 2021 15:15:03 +0800 Subject: [PATCH 08/15] fix build error --- src/database/connection.rs | 11 +++++------ src/executor/delete.rs | 5 ++++- src/executor/insert.rs | 5 ++++- src/executor/paginator.rs | 4 ++-- src/executor/select.rs | 6 +++++- src/executor/update.rs | 5 ++++- src/query/traits.rs | 2 +- tests/common/setup/mod.rs | 2 +- tests/common/setup/schema.rs | 4 +++- 9 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/database/connection.rs b/src/database/connection.rs index cf02790da..0f95f3302 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -16,10 +16,10 @@ pub enum DatabaseConnection { pub type DbConn = DatabaseConnection; -pub trait IntoDbBackend{ +pub trait IntoDbBackend { fn build(&self, statement: &S) -> Statement - where - S: StatementBuilder; + where + S: StatementBuilder; fn get_query_builder(&self) -> Box; } @@ -33,8 +33,8 @@ pub enum DbBackend { impl IntoDbBackend for DbBackend { fn build(&self, statement: &S) -> Statement - where - S: StatementBuilder, + where + S: StatementBuilder, { statement.build(self) } @@ -48,7 +48,6 @@ impl IntoDbBackend for DbBackend { } } - impl Default for DatabaseConnection { fn default() -> Self { Self::Disconnected diff --git a/src/executor/delete.rs b/src/executor/delete.rs index c5c61c2af..fddede0d7 100644 --- a/src/executor/delete.rs +++ b/src/executor/delete.rs @@ -1,4 +1,7 @@ -use crate::{error::*, ActiveModelTrait, DatabaseConnection, DeleteMany, DeleteOne, EntityTrait, Statement, IntoDbBackend}; +use crate::{ + error::*, ActiveModelTrait, DatabaseConnection, DeleteMany, DeleteOne, EntityTrait, + IntoDbBackend, Statement, +}; use sea_query::DeleteStatement; use std::future::Future; diff --git a/src/executor/insert.rs b/src/executor/insert.rs index b30ac979c..eeaa1fa87 100644 --- a/src/executor/insert.rs +++ b/src/executor/insert.rs @@ -1,4 +1,7 @@ -use crate::{error::*, ActiveModelTrait, DatabaseConnection, EntityTrait, Insert, PrimaryKeyTrait, Statement, TryFromU64, IntoDbBackend}; +use crate::{ + error::*, ActiveModelTrait, DatabaseConnection, EntityTrait, Insert, IntoDbBackend, + PrimaryKeyTrait, Statement, TryFromU64, +}; use sea_query::InsertStatement; use std::{future::Future, marker::PhantomData}; diff --git a/src/executor/paginator.rs b/src/executor/paginator.rs index b2b2e80a4..2682fdef8 100644 --- a/src/executor/paginator.rs +++ b/src/executor/paginator.rs @@ -1,4 +1,4 @@ -use crate::{error::*, DatabaseConnection, SelectorTrait, IntoDbBackend}; +use crate::{error::*, DatabaseConnection, IntoDbBackend, SelectorTrait}; use async_stream::stream; use futures::Stream; use sea_query::{Alias, Expr, SelectStatement}; @@ -159,7 +159,7 @@ where mod tests { use crate::entity::prelude::*; use crate::tests_cfg::*; - use crate::{DatabaseConnection, DbBackend, MockDatabase, Transaction}; + use crate::{DatabaseConnection, DbBackend, IntoDbBackend, MockDatabase, Transaction}; use futures::TryStreamExt; use sea_query::{Alias, Expr, SelectStatement, Value}; diff --git a/src/executor/select.rs b/src/executor/select.rs index 55b297ada..eb88a4eaa 100644 --- a/src/executor/select.rs +++ b/src/executor/select.rs @@ -1,4 +1,8 @@ -use crate::{error::*, DatabaseConnection, EntityTrait, FromQueryResult, IdenStatic, Iterable, JsonValue, ModelTrait, Paginator, PrimaryKeyToColumn, QueryResult, Select, SelectA, SelectB, SelectTwo, SelectTwoMany, Statement, IntoDbBackend}; +use crate::{ + error::*, DatabaseConnection, EntityTrait, FromQueryResult, IdenStatic, IntoDbBackend, + Iterable, JsonValue, ModelTrait, Paginator, PrimaryKeyToColumn, QueryResult, Select, SelectA, + SelectB, SelectTwo, SelectTwoMany, Statement, +}; use sea_query::SelectStatement; use std::marker::PhantomData; diff --git a/src/executor/update.rs b/src/executor/update.rs index a7cf23c71..d71801301 100644 --- a/src/executor/update.rs +++ b/src/executor/update.rs @@ -1,4 +1,7 @@ -use crate::{error::*, ActiveModelTrait, DatabaseConnection, EntityTrait, Statement, UpdateMany, UpdateOne, IntoDbBackend}; +use crate::{ + error::*, ActiveModelTrait, DatabaseConnection, EntityTrait, IntoDbBackend, Statement, + UpdateMany, UpdateOne, +}; use sea_query::UpdateStatement; use std::future::Future; diff --git a/src/query/traits.rs b/src/query/traits.rs index a0b9bd32d..226f49845 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -1,4 +1,4 @@ -use crate::{DatabaseConnection, DbBackend, Statement, IntoDbBackend}; +use crate::{DatabaseConnection, DbBackend, IntoDbBackend, Statement}; use sea_query::QueryStatementBuilder; pub trait QueryTrait { diff --git a/tests/common/setup/mod.rs b/tests/common/setup/mod.rs index d6b98ac13..03982376f 100644 --- a/tests/common/setup/mod.rs +++ b/tests/common/setup/mod.rs @@ -1,4 +1,4 @@ -use sea_orm::{Database, DbBackend, DatabaseConnection, Statement}; +use sea_orm::{Database, DatabaseConnection, DbBackend, Statement}; pub mod schema; pub use schema::*; diff --git a/tests/common/setup/schema.rs b/tests/common/setup/schema.rs index 64f31dfe7..ccc7b6174 100644 --- a/tests/common/setup/schema.rs +++ b/tests/common/setup/schema.rs @@ -1,6 +1,8 @@ pub use super::super::bakery_chain::*; use pretty_assertions::assert_eq; -use sea_orm::{error::*, sea_query, DbBackend, DbConn, EntityTrait, ExecResult, Schema}; +use sea_orm::{ + error::*, sea_query, DbBackend, DbConn, EntityTrait, ExecResult, IntoDbBackend, Schema, +}; use sea_query::{ Alias, ColumnDef, ForeignKey, ForeignKeyAction, Index, Table, TableCreateStatement, }; From 55ecf469a1316fd7d93e33f886cf0080635442ed Mon Sep 17 00:00:00 2001 From: baoyachi Date: Sun, 26 Sep 2021 00:34:10 +0800 Subject: [PATCH 09/15] refactor code IntoDbBackend --- src/database/connection.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/database/connection.rs b/src/database/connection.rs index 0f95f3302..615570a7f 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -17,9 +17,7 @@ pub enum DatabaseConnection { pub type DbConn = DatabaseConnection; pub trait IntoDbBackend { - fn build(&self, statement: &S) -> Statement - where - S: StatementBuilder; + fn build(&self, statement: &impl StatementBuilder) -> Statement; fn get_query_builder(&self) -> Box; } @@ -32,9 +30,7 @@ pub enum DbBackend { } impl IntoDbBackend for DbBackend { - fn build(&self, statement: &S) -> Statement - where - S: StatementBuilder, + fn build(&self, statement: &impl StatementBuilder) -> Statement { statement.build(self) } From 08f19cc4d8d7655d2af5654205a3e5324f168b33 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Sep 2021 00:53:38 +0800 Subject: [PATCH 10/15] cargo fmt --- src/database/connection.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/database/connection.rs b/src/database/connection.rs index 615570a7f..bf91f94b1 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -30,8 +30,7 @@ pub enum DbBackend { } impl IntoDbBackend for DbBackend { - fn build(&self, statement: &impl StatementBuilder) -> Statement - { + fn build(&self, statement: &impl StatementBuilder) -> Statement { statement.build(self) } From ba6d2d9a1b47ed8c28220a0cd9d0c59cf824ce80 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Sep 2021 00:58:47 +0800 Subject: [PATCH 11/15] rename marco --- src/query/traits.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/query/traits.rs b/src/query/traits.rs index 226f49845..cbf34b507 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -70,7 +70,7 @@ where /// # let conn = MockDatabase::new(DbBackend::Postgres) /// # .into_connection(); /// # -/// use sea_orm::{entity::*, query::*, tests_cfg::cake,debug_query}; +/// use sea_orm::{entity::*, query::*, tests_cfg::cake,gen_statement}; /// /// let c = cake::Entity::insert( /// cake::ActiveModel { @@ -78,15 +78,18 @@ where /// name: ActiveValue::set("Apple Pie".to_owned()), /// }); /// -/// let raw_sql = debug_query!(&c,conn).to_string(); +/// let raw_sql = gen_statement!(&c,&conn).to_string(); +/// assert_eq!(raw_sql,r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#); +/// +/// let raw_sql = gen_statement!(&c,conn).to_string(); /// assert_eq!(raw_sql,r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#); /// -/// let raw_sql = debug_query!(&c,DbBackend::MySql).to_string(); +/// let raw_sql = gen_statement!(&c,DbBackend::MySql).to_string(); /// assert_eq!(raw_sql,r#"INSERT INTO `cake` (`id`, `name`) VALUES (1, 'Apple Pie')"#); /// /// ``` #[macro_export] -macro_rules! debug_query { +macro_rules! gen_statement { ($query:expr,$value:expr) => { $crate::DebugQuery { query: $query, @@ -107,7 +110,7 @@ macro_rules! debug_query { /// # let conn = MockDatabase::new(DbBackend::Postgres) /// # .into_connection(); /// # -/// use sea_orm::{entity::*, query::*, tests_cfg::cake,debug_query_fmt}; +/// use sea_orm::{entity::*, query::*, tests_cfg::cake,debug_query}; /// /// let c = cake::Entity::insert( /// cake::ActiveModel { @@ -115,16 +118,19 @@ macro_rules! debug_query { /// name: ActiveValue::set("Apple Pie".to_owned()), /// }); /// -/// let raw_sql = debug_query_fmt!(&c,&conn); +/// let raw_sql = debug_query!(&c,&conn); +/// assert_eq!(raw_sql,r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#); +/// +/// let raw_sql = debug_query!(&c,conn); /// assert_eq!(raw_sql,r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#); /// -/// let raw_sql = debug_query_fmt!(&c,DbBackend::Sqlite); +/// let raw_sql = debug_query!(&c,DbBackend::Sqlite); /// assert_eq!(raw_sql,r#"INSERT INTO `cake` (`id`, `name`) VALUES (1, 'Apple Pie')"#); /// /// ``` #[macro_export] -macro_rules! debug_query_fmt { +macro_rules! debug_query { ($query:expr,$value:expr) => { - $crate::debug_query!($query, $value).to_string(); + $crate::gen_statement!($query, $value).to_string(); }; } From ece6b625299096920256858050dfa2acd1605c0c Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Sep 2021 01:01:21 +0800 Subject: [PATCH 12/15] cargo fmt --- src/entity/column.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/entity/column.rs b/src/entity/column.rs index 91d34baba..32500630d 100644 --- a/src/entity/column.rs +++ b/src/entity/column.rs @@ -438,14 +438,8 @@ mod tests { impl ActiveModelBehavior for ActiveModel {} } - assert_eq!( - hello::Column::One.def(), - ColumnType::Integer.def() - ); - assert_eq!( - hello::Column::Two.def(), - ColumnType::Integer.def().unique() - ); + assert_eq!(hello::Column::One.def(), ColumnType::Integer.def()); + assert_eq!(hello::Column::Two.def(), ColumnType::Integer.def().unique()); assert_eq!( hello::Column::Three.def(), ColumnType::Integer.def().indexed() From d2702dd8003c081ece7650ed00dbcae3b899491a Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Sep 2021 09:42:46 +0800 Subject: [PATCH 13/15] fix build test error --- tests/basic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/basic.rs b/tests/basic.rs index a0763d451..7b0b5d00a 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -1,6 +1,6 @@ pub mod common; -pub use sea_orm::{entity::*, error::*, sea_query, tests_cfg::*, Database, DbConn}; +pub use sea_orm::{entity::*, error::*, sea_query, tests_cfg::*, Database, DbConn, IntoDbBackend}; // cargo test --features sqlx-sqlite,runtime-async-std-native-tls --test basic #[sea_orm_macros::test] From 2ad56f35767a38baec039b496e039432145f8cbe Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Sep 2021 11:01:58 +0800 Subject: [PATCH 14/15] fix #200 --- sea-orm-cli/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sea-orm-cli/src/main.rs b/sea-orm-cli/src/main.rs index e7041ef01..527e1264d 100644 --- a/sea-orm-cli/src/main.rs +++ b/sea-orm-cli/src/main.rs @@ -56,7 +56,7 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box Date: Mon, 27 Sep 2021 11:21:50 +0800 Subject: [PATCH 15/15] fix #200 --- src/database/mod.rs | 11 +++++++++++ src/driver/sqlx_mysql.rs | 4 ++-- src/driver/sqlx_postgres.rs | 4 ++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/database/mod.rs b/src/database/mod.rs index 36f825bb1..b5ed64127 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -12,6 +12,17 @@ pub use transaction::*; use crate::DbErr; +#[derive(Debug)] +pub struct DbScheme; + +impl DbScheme { + pub fn starts_with(base_url: &str) -> bool { + base_url.starts_with("postgres://") + || base_url.starts_with("postgresql://") + || base_url.starts_with("mysql://") + } +} + #[derive(Debug, Default)] pub struct Database; diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index be590b6ea..849068bba 100644 --- a/src/driver/sqlx_mysql.rs +++ b/src/driver/sqlx_mysql.rs @@ -6,7 +6,7 @@ use sqlx::{ sea_query::sea_query_driver_mysql!(); use sea_query_driver_mysql::bind_query; -use crate::{debug_print, error::*, executor::*, DatabaseConnection, Statement}; +use crate::{debug_print, error::*, executor::*, DatabaseConnection, DbScheme, Statement}; use super::sqlx_common::*; @@ -20,7 +20,7 @@ pub struct SqlxMySqlPoolConnection { impl SqlxMySqlConnector { pub fn accepts(string: &str) -> bool { - string.starts_with("mysql://") + DbScheme::starts_with(string) } pub async fn connect(string: &str) -> Result { diff --git a/src/driver/sqlx_postgres.rs b/src/driver/sqlx_postgres.rs index e7b9032ac..542525b10 100644 --- a/src/driver/sqlx_postgres.rs +++ b/src/driver/sqlx_postgres.rs @@ -6,7 +6,7 @@ use sqlx::{ sea_query::sea_query_driver_postgres!(); use sea_query_driver_postgres::bind_query; -use crate::{debug_print, error::*, executor::*, DatabaseConnection, Statement}; +use crate::{debug_print, error::*, executor::*, DatabaseConnection, DbScheme, Statement}; use super::sqlx_common::*; @@ -20,7 +20,7 @@ pub struct SqlxPostgresPoolConnection { impl SqlxPostgresConnector { pub fn accepts(string: &str) -> bool { - string.starts_with("postgres://") + DbScheme::starts_with(string) } pub async fn connect(string: &str) -> Result {