Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Nov 9, 2021
1 parent c1fae1b commit cc035d7
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 54 deletions.
16 changes: 0 additions & 16 deletions src/database/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,22 +292,6 @@ impl DatabaseConnection {
}
}

impl DatabaseConnection {
/// Get database version
pub fn version(&self) -> String {
match self {
#[cfg(feature = "sqlx-mysql")]
DatabaseConnection::SqlxMySqlPoolConnection(conn) => conn.version.to_string(),
#[cfg(feature = "sqlx-postgres")]
DatabaseConnection::SqlxPostgresPoolConnection(_) => "".to_string(),
#[cfg(feature = "sqlx-sqlite")]
DatabaseConnection::SqlxSqlitePoolConnection(conn) => conn.version.to_string(),
DatabaseConnection::Disconnected => panic!("Disconnected"),
_ => unimplemented!(),
}
}
}

impl DbBackend {
/// Check if the URI is the same as the specified database backend.
/// Returns true if they match.
Expand Down
30 changes: 3 additions & 27 deletions src/database/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,37 +367,13 @@ impl<'a> ConnectionTrait<'a> for DatabaseTransaction {
}

fn returning_on_insert(&self) -> bool {
match self.backend {
DbBackend::MySql => {
// Supported if it's MariaDB on or after version 10.5.0
// Not supported in all MySQL versions
self.support_returning
}
DbBackend::Postgres => {
// Supported by all Postgres versions
true
}
DbBackend::Sqlite => {
// Supported by SQLite on or after version 3.35.0 (2021-03-12)
self.support_returning
}
}
self.support_returning
}

fn returning_on_update(&self) -> bool {
match self.backend {
DbBackend::MySql => {
// Not supported in all MySQL & MariaDB versions
false
}
DbBackend::Postgres => {
// Supported by all Postgres versions
true
}
DbBackend::Sqlite => {
// Supported by SQLite on or after version 3.35.0 (2021-03-12)
self.support_returning
}
DbBackend::MySql => false,
_ => self.support_returning,
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/driver/sqlx_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub struct SqlxMySqlConnector;
#[derive(Debug, Clone)]
pub struct SqlxMySqlPoolConnection {
pool: MySqlPool,
pub(crate) version: String,
pub(crate) support_returning: bool,
}

Expand Down Expand Up @@ -186,17 +185,16 @@ pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, MySql, MySq
}

async fn into_db_connection(pool: MySqlPool) -> Result<DatabaseConnection, DbErr> {
let (version, support_returning) = parse_support_returning(&pool).await?;
let support_returning = parse_support_returning(&pool).await?;
Ok(DatabaseConnection::SqlxMySqlPoolConnection(
SqlxMySqlPoolConnection {
pool,
version,
support_returning,
},
))
}

async fn parse_support_returning(pool: &MySqlPool) -> Result<(String, bool), DbErr> {
async fn parse_support_returning(pool: &MySqlPool) -> Result<bool, DbErr> {
let stmt = Statement::from_string(
DbBackend::MySql,
r#"SHOW VARIABLES LIKE "version""#.to_owned(),
Expand Down Expand Up @@ -232,5 +230,5 @@ async fn parse_support_returning(pool: &MySqlPool) -> Result<(String, bool), DbE
};
debug_print!("db_version: {}", version);
debug_print!("db_support_returning: {}", support_returning);
Ok((version, support_returning))
Ok(support_returning)
}
8 changes: 3 additions & 5 deletions src/driver/sqlx_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub struct SqlxSqliteConnector;
#[derive(Debug, Clone)]
pub struct SqlxSqlitePoolConnection {
pool: SqlitePool,
pub(crate) version: String,
pub(crate) support_returning: bool,
}

Expand Down Expand Up @@ -190,17 +189,16 @@ pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, Sqlite, Sql
}

async fn into_db_connection(pool: SqlitePool) -> Result<DatabaseConnection, DbErr> {
let (version, support_returning) = parse_support_returning(&pool).await?;
let support_returning = parse_support_returning(&pool).await?;
Ok(DatabaseConnection::SqlxSqlitePoolConnection(
SqlxSqlitePoolConnection {
pool,
version,
support_returning,
},
))
}

async fn parse_support_returning(pool: &SqlitePool) -> Result<(String, bool), DbErr> {
async fn parse_support_returning(pool: &SqlitePool) -> Result<bool, DbErr> {
let stmt = Statement::from_string(
DbBackend::Sqlite,
r#"SELECT sqlite_version() AS version"#.to_owned(),
Expand Down Expand Up @@ -229,5 +227,5 @@ async fn parse_support_returning(pool: &SqlitePool) -> Result<(String, bool), Db
let support_returning = ver_major >= 3 && ver_minor >= 35;
debug_print!("db_version: {}", version);
debug_print!("db_support_returning: {}", support_returning);
Ok((version, support_returning))
Ok(support_returning)
}
1 change: 0 additions & 1 deletion tests/returning_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ async fn main() -> Result<(), DbErr> {
returning.columns(vec![Column::Id, Column::Name, Column::ProfitMargin]);

create_tables(db).await?;
println!("db_version: {:#?}", db.version());

if db.returning_on_insert() {
insert.returning(returning.clone());
Expand Down

0 comments on commit cc035d7

Please sign in to comment.