From 86f2d7e32718c402c5f3835aafa383c05cbd4fae Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Sep 2021 11:45:54 +0800 Subject: [PATCH 1/7] fix #200 * adapter postgrel url scheme * refactor DbScheme --- .gitignore | 3 ++- sea-orm-cli/src/main.rs | 2 +- src/database/mod.rs | 25 ++++++++++++++++++++++--- src/driver/mock.rs | 10 +++++----- src/driver/sqlx_mysql.rs | 4 ++-- src/driver/sqlx_postgres.rs | 4 ++-- src/driver/sqlx_sqlite.rs | 4 ++-- 7 files changed, 36 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 950c227c..3eaf3f74 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/sea-orm-cli/src/main.rs b/sea-orm-cli/src/main.rs index e7041ef0..527e1264 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 bool { + match self { + DbScheme::Postgres => { + base_url.starts_with("postgres://") || base_url.starts_with("postgresql://") + } + DbScheme::Mysql => base_url.starts_with("mysql://"), + DbScheme::Sqlite => base_url.starts_with("sqlite:"), + } + } +} + #[derive(Debug, Default)] pub struct Database; impl Database { pub async fn connect(string: &str) -> Result { #[cfg(feature = "sqlx-mysql")] - if crate::SqlxMySqlConnector::accepts(string) { + if DbScheme::Mysql::starts_with(string) { return crate::SqlxMySqlConnector::connect(string).await; } #[cfg(feature = "sqlx-postgres")] - if crate::SqlxPostgresConnector::accepts(string) { + if DbScheme::Postgres::starts_with(string) { return crate::SqlxPostgresConnector::connect(string).await; } #[cfg(feature = "sqlx-sqlite")] - if crate::SqlxSqliteConnector::accepts(string) { + if DbScheme::Sqlite::starts_with(string) { return crate::SqlxSqliteConnector::connect(string).await; } #[cfg(feature = "mock")] diff --git a/src/driver/mock.rs b/src/driver/mock.rs index 823ddb32..f3bbe6f8 100644 --- a/src/driver/mock.rs +++ b/src/driver/mock.rs @@ -1,6 +1,6 @@ use crate::{ - debug_print, error::*, DatabaseConnection, DbBackend, ExecResult, MockDatabase, QueryResult, - Statement, Transaction, + debug_print, error::*, DatabaseConnection, DbBackend, ExecResult, MockDatabase, + QueryResult, Statement, Transaction, }; use std::fmt::Debug; use std::sync::{ @@ -31,15 +31,15 @@ impl MockDatabaseConnector { #[allow(unused_variables)] pub fn accepts(string: &str) -> bool { #[cfg(feature = "sqlx-mysql")] - if crate::SqlxMySqlConnector::accepts(string) { + if DbScheme::Mysql::accepts(string) { return true; } #[cfg(feature = "sqlx-postgres")] - if crate::SqlxPostgresConnector::accepts(string) { + if DbScheme::Postgres::accepts(string) { return true; } #[cfg(feature = "sqlx-sqlite")] - if crate::SqlxSqliteConnector::accepts(string) { + if DbScheme::Sqlite::accepts(string) { return true; } false diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index be590b6e..41365c88 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::Mysql::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 e7b9032a..8dd28808 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::Postgres::starts_with(string) } pub async fn connect(string: &str) -> Result { diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index bd6f0399..5f865ef2 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -6,7 +6,7 @@ use sqlx::{ sea_query::sea_query_driver_sqlite!(); use sea_query_driver_sqlite::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 SqlxSqlitePoolConnection { impl SqlxSqliteConnector { pub fn accepts(string: &str) -> bool { - string.starts_with("sqlite:") + DbScheme::Sqlite::starts_with(string) } pub async fn connect(string: &str) -> Result { From 2ccc8c527a6ec0054648ac6d51345201775611be Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Sep 2021 11:49:20 +0800 Subject: [PATCH 2/7] cargo fmt --- src/driver/mock.rs | 4 ++-- src/entity/column.rs | 10 ++-------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/driver/mock.rs b/src/driver/mock.rs index f3bbe6f8..50248a54 100644 --- a/src/driver/mock.rs +++ b/src/driver/mock.rs @@ -1,6 +1,6 @@ use crate::{ - debug_print, error::*, DatabaseConnection, DbBackend, ExecResult, MockDatabase, - QueryResult, Statement, Transaction, + debug_print, error::*, DatabaseConnection, DbBackend, ExecResult, MockDatabase, QueryResult, + Statement, Transaction, }; use std::fmt::Debug; use std::sync::{ diff --git a/src/entity/column.rs b/src/entity/column.rs index 91d34bab..32500630 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 1cf5cec1c53cd9aea70fdb5e16caa9825360d7b8 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Sep 2021 12:13:06 +0800 Subject: [PATCH 3/7] refactor code --- src/database/connection.rs | 10 ++++++++++ src/database/mod.rs | 25 +++---------------------- src/driver/mock.rs | 6 +++--- src/driver/sqlx_mysql.rs | 4 ++-- src/driver/sqlx_postgres.rs | 4 ++-- src/driver/sqlx_sqlite.rs | 4 ++-- 6 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/database/connection.rs b/src/database/connection.rs index 995dde9d..6d02781d 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -129,6 +129,16 @@ impl DatabaseConnection { } impl DbBackend { + pub fn url_starts_with(self, base_url: &str) -> bool { + match self { + Self::Postgres => { + base_url.starts_with("postgres://") || base_url.starts_with("postgresql://") + } + Self::MySql => base_url.starts_with("mysql://"), + Self::Sqlite => base_url.starts_with("sqlite:"), + } + } + pub fn build(&self, statement: &S) -> Statement where S: StatementBuilder, diff --git a/src/database/mod.rs b/src/database/mod.rs index 20435ca5..083307e2 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -12,40 +12,21 @@ pub use transaction::*; use crate::DbErr; -#[derive(Debug)] -pub enum DbScheme { - Postgres, - Mysql, - Sqlite, -} - -impl DbScheme { - pub fn starts_with(self, base_url: &str) -> bool { - match self { - DbScheme::Postgres => { - base_url.starts_with("postgres://") || base_url.starts_with("postgresql://") - } - DbScheme::Mysql => base_url.starts_with("mysql://"), - DbScheme::Sqlite => base_url.starts_with("sqlite:"), - } - } -} - #[derive(Debug, Default)] pub struct Database; impl Database { pub async fn connect(string: &str) -> Result { #[cfg(feature = "sqlx-mysql")] - if DbScheme::Mysql::starts_with(string) { + if DbBackend::MySql::starts_with(string) { return crate::SqlxMySqlConnector::connect(string).await; } #[cfg(feature = "sqlx-postgres")] - if DbScheme::Postgres::starts_with(string) { + if DbBackend::Postgres::starts_with(string) { return crate::SqlxPostgresConnector::connect(string).await; } #[cfg(feature = "sqlx-sqlite")] - if DbScheme::Sqlite::starts_with(string) { + if DbBackend::Sqlite::starts_with(string) { return crate::SqlxSqliteConnector::connect(string).await; } #[cfg(feature = "mock")] diff --git a/src/driver/mock.rs b/src/driver/mock.rs index 50248a54..626d8502 100644 --- a/src/driver/mock.rs +++ b/src/driver/mock.rs @@ -31,15 +31,15 @@ impl MockDatabaseConnector { #[allow(unused_variables)] pub fn accepts(string: &str) -> bool { #[cfg(feature = "sqlx-mysql")] - if DbScheme::Mysql::accepts(string) { + if DbBackend::MySql::accepts(string) { return true; } #[cfg(feature = "sqlx-postgres")] - if DbScheme::Postgres::accepts(string) { + if DbBackend::Postgres::accepts(string) { return true; } #[cfg(feature = "sqlx-sqlite")] - if DbScheme::Sqlite::accepts(string) { + if DbBackend::Sqlite::accepts(string) { return true; } false diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index 41365c88..4aeb77e1 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, DbScheme, Statement}; +use crate::{debug_print, error::*, executor::*, DatabaseConnection, DbBackend, Statement}; use super::sqlx_common::*; @@ -20,7 +20,7 @@ pub struct SqlxMySqlPoolConnection { impl SqlxMySqlConnector { pub fn accepts(string: &str) -> bool { - DbScheme::Mysql::starts_with(string) + DbBackend::MySql::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 8dd28808..ecd68133 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, DbScheme, Statement}; +use crate::{debug_print, error::*, executor::*, DatabaseConnection, DbBackend, Statement}; use super::sqlx_common::*; @@ -20,7 +20,7 @@ pub struct SqlxPostgresPoolConnection { impl SqlxPostgresConnector { pub fn accepts(string: &str) -> bool { - DbScheme::Postgres::starts_with(string) + DbBackend::Postgres::starts_with(string) } pub async fn connect(string: &str) -> Result { diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index 5f865ef2..995d6b98 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -6,7 +6,7 @@ use sqlx::{ sea_query::sea_query_driver_sqlite!(); use sea_query_driver_sqlite::bind_query; -use crate::{debug_print, error::*, executor::*, DatabaseConnection, DbScheme, Statement}; +use crate::{debug_print, error::*, executor::*, DatabaseConnection, DbBackend, Statement}; use super::sqlx_common::*; @@ -20,7 +20,7 @@ pub struct SqlxSqlitePoolConnection { impl SqlxSqliteConnector { pub fn accepts(string: &str) -> bool { - DbScheme::Sqlite::starts_with(string) + DbBackend::Sqlite::starts_with(string) } pub async fn connect(string: &str) -> Result { From 203a0d603add74d77623c806248c06060f3b26d0 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Sep 2021 12:19:36 +0800 Subject: [PATCH 4/7] fix build error --- src/database/mod.rs | 6 +++--- src/driver/mock.rs | 6 +++--- src/driver/sqlx_mysql.rs | 2 +- src/driver/sqlx_postgres.rs | 2 +- src/driver/sqlx_sqlite.rs | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/database/mod.rs b/src/database/mod.rs index 083307e2..2e0f9ac8 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -18,15 +18,15 @@ pub struct Database; impl Database { pub async fn connect(string: &str) -> Result { #[cfg(feature = "sqlx-mysql")] - if DbBackend::MySql::starts_with(string) { + if DbBackend::MySql.url_starts_with(string) { return crate::SqlxMySqlConnector::connect(string).await; } #[cfg(feature = "sqlx-postgres")] - if DbBackend::Postgres::starts_with(string) { + if DbBackend::Postgres.url_starts_with(string) { return crate::SqlxPostgresConnector::connect(string).await; } #[cfg(feature = "sqlx-sqlite")] - if DbBackend::Sqlite::starts_with(string) { + if DbBackend::Sqlite.url_starts_with(string) { return crate::SqlxSqliteConnector::connect(string).await; } #[cfg(feature = "mock")] diff --git a/src/driver/mock.rs b/src/driver/mock.rs index 626d8502..45e521a5 100644 --- a/src/driver/mock.rs +++ b/src/driver/mock.rs @@ -31,15 +31,15 @@ impl MockDatabaseConnector { #[allow(unused_variables)] pub fn accepts(string: &str) -> bool { #[cfg(feature = "sqlx-mysql")] - if DbBackend::MySql::accepts(string) { + if DbBackend::MySql.url_starts_with(string) { return true; } #[cfg(feature = "sqlx-postgres")] - if DbBackend::Postgres::accepts(string) { + if DbBackend::Postgres.url_starts_with(string) { return true; } #[cfg(feature = "sqlx-sqlite")] - if DbBackend::Sqlite::accepts(string) { + if DbBackend::Sqlite.url_starts_with(string) { return true; } false diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index 4aeb77e1..f754edaf 100644 --- a/src/driver/sqlx_mysql.rs +++ b/src/driver/sqlx_mysql.rs @@ -20,7 +20,7 @@ pub struct SqlxMySqlPoolConnection { impl SqlxMySqlConnector { pub fn accepts(string: &str) -> bool { - DbBackend::MySql::starts_with(string) + DbBackend::MySql.url_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 ecd68133..4a1aa807 100644 --- a/src/driver/sqlx_postgres.rs +++ b/src/driver/sqlx_postgres.rs @@ -20,7 +20,7 @@ pub struct SqlxPostgresPoolConnection { impl SqlxPostgresConnector { pub fn accepts(string: &str) -> bool { - DbBackend::Postgres::starts_with(string) + DbBackend::Postgres.url_starts_with(string) } pub async fn connect(string: &str) -> Result { diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index 995d6b98..a083929a 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -20,7 +20,7 @@ pub struct SqlxSqlitePoolConnection { impl SqlxSqliteConnector { pub fn accepts(string: &str) -> bool { - DbBackend::Sqlite::starts_with(string) + DbBackend::Sqlite.url_starts_with(string) } pub async fn connect(string: &str) -> Result { From ae9e1c48ceeff94853d071a1d3b1ea42a94009cd Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Sep 2021 12:34:53 +0800 Subject: [PATCH 5/7] make url_starts_with access level --- src/database/connection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/database/connection.rs b/src/database/connection.rs index 6d02781d..382872e2 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -129,7 +129,7 @@ impl DatabaseConnection { } impl DbBackend { - pub fn url_starts_with(self, base_url: &str) -> bool { + pub(crate) fn url_starts_with(self, base_url: &str) -> bool { match self { Self::Postgres => { base_url.starts_with("postgres://") || base_url.starts_with("postgresql://") From d78a067b2df1d573a564a33e09b713ee61de14ff Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Sep 2021 16:41:47 +0800 Subject: [PATCH 6/7] 'url_starts_with' replace with 'is_prefix_of' method --- src/database/connection.rs | 2 +- src/database/mod.rs | 6 +++--- src/driver/mock.rs | 6 +++--- src/driver/sqlx_mysql.rs | 2 +- src/driver/sqlx_postgres.rs | 2 +- src/driver/sqlx_sqlite.rs | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/database/connection.rs b/src/database/connection.rs index 382872e2..c673fca9 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -129,7 +129,7 @@ impl DatabaseConnection { } impl DbBackend { - pub(crate) fn url_starts_with(self, base_url: &str) -> bool { + pub(crate) fn is_prefix_of(self, base_url: &str) -> bool { match self { Self::Postgres => { base_url.starts_with("postgres://") || base_url.starts_with("postgresql://") diff --git a/src/database/mod.rs b/src/database/mod.rs index 2e0f9ac8..f61343c1 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -18,15 +18,15 @@ pub struct Database; impl Database { pub async fn connect(string: &str) -> Result { #[cfg(feature = "sqlx-mysql")] - if DbBackend::MySql.url_starts_with(string) { + if DbBackend::MySql.is_prefix_of(string) { return crate::SqlxMySqlConnector::connect(string).await; } #[cfg(feature = "sqlx-postgres")] - if DbBackend::Postgres.url_starts_with(string) { + if DbBackend::Postgres.is_prefix_of(string) { return crate::SqlxPostgresConnector::connect(string).await; } #[cfg(feature = "sqlx-sqlite")] - if DbBackend::Sqlite.url_starts_with(string) { + if DbBackend::Sqlite.is_prefix_of(string) { return crate::SqlxSqliteConnector::connect(string).await; } #[cfg(feature = "mock")] diff --git a/src/driver/mock.rs b/src/driver/mock.rs index 45e521a5..0e398586 100644 --- a/src/driver/mock.rs +++ b/src/driver/mock.rs @@ -31,15 +31,15 @@ impl MockDatabaseConnector { #[allow(unused_variables)] pub fn accepts(string: &str) -> bool { #[cfg(feature = "sqlx-mysql")] - if DbBackend::MySql.url_starts_with(string) { + if DbBackend::MySql.is_prefix_of(string) { return true; } #[cfg(feature = "sqlx-postgres")] - if DbBackend::Postgres.url_starts_with(string) { + if DbBackend::Postgres.is_prefix_of(string) { return true; } #[cfg(feature = "sqlx-sqlite")] - if DbBackend::Sqlite.url_starts_with(string) { + if DbBackend::Sqlite.is_prefix_of(string) { return true; } false diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index f754edaf..c542a9b4 100644 --- a/src/driver/sqlx_mysql.rs +++ b/src/driver/sqlx_mysql.rs @@ -20,7 +20,7 @@ pub struct SqlxMySqlPoolConnection { impl SqlxMySqlConnector { pub fn accepts(string: &str) -> bool { - DbBackend::MySql.url_starts_with(string) + DbBackend::MySql.is_prefix_of(string) } pub async fn connect(string: &str) -> Result { diff --git a/src/driver/sqlx_postgres.rs b/src/driver/sqlx_postgres.rs index 4a1aa807..fb5402eb 100644 --- a/src/driver/sqlx_postgres.rs +++ b/src/driver/sqlx_postgres.rs @@ -20,7 +20,7 @@ pub struct SqlxPostgresPoolConnection { impl SqlxPostgresConnector { pub fn accepts(string: &str) -> bool { - DbBackend::Postgres.url_starts_with(string) + DbBackend::Postgres.is_prefix_of(string) } pub async fn connect(string: &str) -> Result { diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index a083929a..b02f4408 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -20,7 +20,7 @@ pub struct SqlxSqlitePoolConnection { impl SqlxSqliteConnector { pub fn accepts(string: &str) -> bool { - DbBackend::Sqlite.url_starts_with(string) + DbBackend::Sqlite.is_prefix_of(string) } pub async fn connect(string: &str) -> Result { From 8d69b034a0453078036411e710a24f8051e5833a Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 27 Sep 2021 16:45:25 +0800 Subject: [PATCH 7/7] 'url_starts_with' replace with 'is_prefix_of' method --- src/database/connection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/database/connection.rs b/src/database/connection.rs index c673fca9..e5ec4e2c 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -129,7 +129,7 @@ impl DatabaseConnection { } impl DbBackend { - pub(crate) fn is_prefix_of(self, base_url: &str) -> bool { + pub fn is_prefix_of(self, base_url: &str) -> bool { match self { Self::Postgres => { base_url.starts_with("postgres://") || base_url.starts_with("postgresql://")