This commit is contained in:
Billy Chan 2021-11-09 12:11:00 +08:00
parent 429b920ded
commit 24fab66d17
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
3 changed files with 52 additions and 11 deletions

View File

@ -230,26 +230,55 @@ impl<'a> ConnectionTrait<'a> for DatabaseConnection {
#[cfg(feature = "sqlx-mysql")]
DatabaseConnection::SqlxMySqlPoolConnection {
support_returning, ..
} => *support_returning,
} => {
// Supported if it's MariaDB on or after version 10.5.0
// Not supported in all MySQL versions
*support_returning
}
#[cfg(feature = "sqlx-postgres")]
DatabaseConnection::SqlxPostgresPoolConnection(_) => true,
DatabaseConnection::SqlxPostgresPoolConnection(_) => {
// Supported by all Postgres versions
true
}
#[cfg(feature = "sqlx-sqlite")]
DatabaseConnection::SqlxSqlitePoolConnection(_) => false,
DatabaseConnection::SqlxSqlitePoolConnection(_) => {
// Supported by SQLite on or after version 3.35.0 (2021-03-12)
false
}
#[cfg(feature = "mock")]
DatabaseConnection::MockDatabaseConnection(conn) => match conn.get_database_backend() {
DbBackend::MySql => false,
DbBackend::Postgres => true,
DbBackend::Sqlite => false,
},
DatabaseConnection::Disconnected => panic!("Disconnected"),
_ => unimplemented!(),
}
}
fn returning_on_update(&self) -> bool {
match self {
#[cfg(feature = "sqlx-mysql")]
DatabaseConnection::SqlxMySqlPoolConnection { .. } => false,
DatabaseConnection::SqlxMySqlPoolConnection { .. } => {
// Not supported in all MySQL & MariaDB versions
false
}
#[cfg(feature = "sqlx-postgres")]
DatabaseConnection::SqlxPostgresPoolConnection(_) => true,
DatabaseConnection::SqlxPostgresPoolConnection(_) => {
// Supported by all Postgres versions
true
}
#[cfg(feature = "sqlx-sqlite")]
DatabaseConnection::SqlxSqlitePoolConnection(_) => false,
DatabaseConnection::SqlxSqlitePoolConnection(_) => {
// Supported by SQLite on or after version 3.35.0 (2021-03-12)
false
}
#[cfg(feature = "mock")]
DatabaseConnection::MockDatabaseConnection(conn) => match conn.get_database_backend() {
DbBackend::MySql => false,
DbBackend::Postgres => true,
DbBackend::Sqlite => false,
},
DatabaseConnection::Disconnected => panic!("Disconnected"),
_ => unimplemented!(),
}
}

View File

@ -195,6 +195,7 @@ async fn into_db_connection(pool: MySqlPool) -> Result<DatabaseConnection, DbErr
let version: String = query_result.try_get("", "Value")?;
let support_returning = if !version.contains("MariaDB") {
// This is MySQL
// Not supported in all MySQL versions
false
} else {
// This is MariaDB
@ -212,6 +213,7 @@ async fn into_db_connection(pool: MySqlPool) -> Result<DatabaseConnection, DbErr
}
let ver_major = parse_captures!(1);
let ver_minor = parse_captures!(2);
// Supported if it's MariaDB with version 10.5.0 or after
ver_major >= 10 && ver_minor >= 5
};
(version, support_returning)

View File

@ -31,7 +31,7 @@ async fn main() -> Result<(), DbErr> {
(Column::ProfitMargin, 0.5.into()),
])
.and_where(Column::Id.eq(1));
let mut returning = Query::select();
returning.columns(vec![Column::Id, Column::Name, Column::ProfitMargin]);
if db.returning_on_insert() {
@ -43,8 +43,18 @@ async fn main() -> Result<(), DbErr> {
create_tables(db).await?;
println!("db_version: {:#?}", db.version());
db.query_one(builder.build(&insert)).await?;
db.query_one(builder.build(&update)).await?;
let insert_res = db.query_one(builder.build(&insert)).await?.expect("Insert failed");
if db.returning_on_insert() {
let _id: i32 = insert_res.try_get("", "id")?;
let _name: String = insert_res.try_get("", "name")?;
let _profit_margin: f64 = insert_res.try_get("", "profit_margin")?;
}
let update_res = db.query_one(builder.build(&update)).await?.expect("Update filed");
if db.returning_on_update() {
let _id: i32 = insert_res.try_get("", "id")?;
let _name: String = insert_res.try_get("", "name")?;
let _profit_margin: f64 = insert_res.try_get("", "profit_margin")?;
}
ctx.delete().await;
Ok(())