refactor code

This commit is contained in:
baoyachi 2021-09-27 12:13:06 +08:00
parent 2ccc8c527a
commit 1cf5cec1c5
6 changed files with 22 additions and 31 deletions

View File

@ -129,6 +129,16 @@ impl DatabaseConnection {
} }
impl DbBackend { 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<S>(&self, statement: &S) -> Statement pub fn build<S>(&self, statement: &S) -> Statement
where where
S: StatementBuilder, S: StatementBuilder,

View File

@ -12,40 +12,21 @@ pub use transaction::*;
use crate::DbErr; 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)] #[derive(Debug, Default)]
pub struct Database; pub struct Database;
impl Database { impl Database {
pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> { pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> {
#[cfg(feature = "sqlx-mysql")] #[cfg(feature = "sqlx-mysql")]
if DbScheme::Mysql::starts_with(string) { if DbBackend::MySql::starts_with(string) {
return crate::SqlxMySqlConnector::connect(string).await; return crate::SqlxMySqlConnector::connect(string).await;
} }
#[cfg(feature = "sqlx-postgres")] #[cfg(feature = "sqlx-postgres")]
if DbScheme::Postgres::starts_with(string) { if DbBackend::Postgres::starts_with(string) {
return crate::SqlxPostgresConnector::connect(string).await; return crate::SqlxPostgresConnector::connect(string).await;
} }
#[cfg(feature = "sqlx-sqlite")] #[cfg(feature = "sqlx-sqlite")]
if DbScheme::Sqlite::starts_with(string) { if DbBackend::Sqlite::starts_with(string) {
return crate::SqlxSqliteConnector::connect(string).await; return crate::SqlxSqliteConnector::connect(string).await;
} }
#[cfg(feature = "mock")] #[cfg(feature = "mock")]

View File

@ -31,15 +31,15 @@ impl MockDatabaseConnector {
#[allow(unused_variables)] #[allow(unused_variables)]
pub fn accepts(string: &str) -> bool { pub fn accepts(string: &str) -> bool {
#[cfg(feature = "sqlx-mysql")] #[cfg(feature = "sqlx-mysql")]
if DbScheme::Mysql::accepts(string) { if DbBackend::MySql::accepts(string) {
return true; return true;
} }
#[cfg(feature = "sqlx-postgres")] #[cfg(feature = "sqlx-postgres")]
if DbScheme::Postgres::accepts(string) { if DbBackend::Postgres::accepts(string) {
return true; return true;
} }
#[cfg(feature = "sqlx-sqlite")] #[cfg(feature = "sqlx-sqlite")]
if DbScheme::Sqlite::accepts(string) { if DbBackend::Sqlite::accepts(string) {
return true; return true;
} }
false false

View File

@ -6,7 +6,7 @@ use sqlx::{
sea_query::sea_query_driver_mysql!(); sea_query::sea_query_driver_mysql!();
use sea_query_driver_mysql::bind_query; 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::*; use super::sqlx_common::*;
@ -20,7 +20,7 @@ pub struct SqlxMySqlPoolConnection {
impl SqlxMySqlConnector { impl SqlxMySqlConnector {
pub fn accepts(string: &str) -> bool { pub fn accepts(string: &str) -> bool {
DbScheme::Mysql::starts_with(string) DbBackend::MySql::starts_with(string)
} }
pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> { pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> {

View File

@ -6,7 +6,7 @@ use sqlx::{
sea_query::sea_query_driver_postgres!(); sea_query::sea_query_driver_postgres!();
use sea_query_driver_postgres::bind_query; 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::*; use super::sqlx_common::*;
@ -20,7 +20,7 @@ pub struct SqlxPostgresPoolConnection {
impl SqlxPostgresConnector { impl SqlxPostgresConnector {
pub fn accepts(string: &str) -> bool { pub fn accepts(string: &str) -> bool {
DbScheme::Postgres::starts_with(string) DbBackend::Postgres::starts_with(string)
} }
pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> { pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> {

View File

@ -6,7 +6,7 @@ use sqlx::{
sea_query::sea_query_driver_sqlite!(); sea_query::sea_query_driver_sqlite!();
use sea_query_driver_sqlite::bind_query; 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::*; use super::sqlx_common::*;
@ -20,7 +20,7 @@ pub struct SqlxSqlitePoolConnection {
impl SqlxSqliteConnector { impl SqlxSqliteConnector {
pub fn accepts(string: &str) -> bool { pub fn accepts(string: &str) -> bool {
DbScheme::Sqlite::starts_with(string) DbBackend::Sqlite::starts_with(string)
} }
pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> { pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> {