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 {