Refactoring
This commit is contained in:
parent
c1fae1bc86
commit
cc035d7aa7
@ -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 {
|
impl DbBackend {
|
||||||
/// Check if the URI is the same as the specified database backend.
|
/// Check if the URI is the same as the specified database backend.
|
||||||
/// Returns true if they match.
|
/// Returns true if they match.
|
||||||
|
@ -367,37 +367,13 @@ impl<'a> ConnectionTrait<'a> for DatabaseTransaction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn returning_on_insert(&self) -> bool {
|
fn returning_on_insert(&self) -> bool {
|
||||||
match self.backend {
|
self.support_returning
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn returning_on_update(&self) -> bool {
|
fn returning_on_update(&self) -> bool {
|
||||||
match self.backend {
|
match self.backend {
|
||||||
DbBackend::MySql => {
|
DbBackend::MySql => false,
|
||||||
// Not supported in all MySQL & MariaDB versions
|
_ => self.support_returning,
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ pub struct SqlxMySqlConnector;
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SqlxMySqlPoolConnection {
|
pub struct SqlxMySqlPoolConnection {
|
||||||
pool: MySqlPool,
|
pool: MySqlPool,
|
||||||
pub(crate) version: String,
|
|
||||||
pub(crate) support_returning: bool,
|
pub(crate) support_returning: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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> {
|
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(
|
Ok(DatabaseConnection::SqlxMySqlPoolConnection(
|
||||||
SqlxMySqlPoolConnection {
|
SqlxMySqlPoolConnection {
|
||||||
pool,
|
pool,
|
||||||
version,
|
|
||||||
support_returning,
|
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(
|
let stmt = Statement::from_string(
|
||||||
DbBackend::MySql,
|
DbBackend::MySql,
|
||||||
r#"SHOW VARIABLES LIKE "version""#.to_owned(),
|
r#"SHOW VARIABLES LIKE "version""#.to_owned(),
|
||||||
@ -232,5 +230,5 @@ async fn parse_support_returning(pool: &MySqlPool) -> Result<(String, bool), DbE
|
|||||||
};
|
};
|
||||||
debug_print!("db_version: {}", version);
|
debug_print!("db_version: {}", version);
|
||||||
debug_print!("db_support_returning: {}", support_returning);
|
debug_print!("db_support_returning: {}", support_returning);
|
||||||
Ok((version, support_returning))
|
Ok(support_returning)
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ pub struct SqlxSqliteConnector;
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SqlxSqlitePoolConnection {
|
pub struct SqlxSqlitePoolConnection {
|
||||||
pool: SqlitePool,
|
pool: SqlitePool,
|
||||||
pub(crate) version: String,
|
|
||||||
pub(crate) support_returning: bool,
|
pub(crate) support_returning: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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> {
|
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(
|
Ok(DatabaseConnection::SqlxSqlitePoolConnection(
|
||||||
SqlxSqlitePoolConnection {
|
SqlxSqlitePoolConnection {
|
||||||
pool,
|
pool,
|
||||||
version,
|
|
||||||
support_returning,
|
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(
|
let stmt = Statement::from_string(
|
||||||
DbBackend::Sqlite,
|
DbBackend::Sqlite,
|
||||||
r#"SELECT sqlite_version() AS version"#.to_owned(),
|
r#"SELECT sqlite_version() AS version"#.to_owned(),
|
||||||
@ -229,5 +227,5 @@ async fn parse_support_returning(pool: &SqlitePool) -> Result<(String, bool), Db
|
|||||||
let support_returning = ver_major >= 3 && ver_minor >= 35;
|
let support_returning = ver_major >= 3 && ver_minor >= 35;
|
||||||
debug_print!("db_version: {}", version);
|
debug_print!("db_version: {}", version);
|
||||||
debug_print!("db_support_returning: {}", support_returning);
|
debug_print!("db_support_returning: {}", support_returning);
|
||||||
Ok((version, support_returning))
|
Ok(support_returning)
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,6 @@ async fn main() -> Result<(), DbErr> {
|
|||||||
returning.columns(vec![Column::Id, Column::Name, Column::ProfitMargin]);
|
returning.columns(vec![Column::Id, Column::Name, Column::ProfitMargin]);
|
||||||
|
|
||||||
create_tables(db).await?;
|
create_tables(db).await?;
|
||||||
println!("db_version: {:#?}", db.version());
|
|
||||||
|
|
||||||
if db.returning_on_insert() {
|
if db.returning_on_insert() {
|
||||||
insert.returning(returning.clone());
|
insert.returning(returning.clone());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user