Try
This commit is contained in:
parent
429b920ded
commit
24fab66d17
@ -230,26 +230,55 @@ impl<'a> ConnectionTrait<'a> for DatabaseConnection {
|
|||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
DatabaseConnection::SqlxMySqlPoolConnection {
|
DatabaseConnection::SqlxMySqlPoolConnection {
|
||||||
support_returning, ..
|
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")]
|
#[cfg(feature = "sqlx-postgres")]
|
||||||
DatabaseConnection::SqlxPostgresPoolConnection(_) => true,
|
DatabaseConnection::SqlxPostgresPoolConnection(_) => {
|
||||||
|
// Supported by all Postgres versions
|
||||||
|
true
|
||||||
|
}
|
||||||
#[cfg(feature = "sqlx-sqlite")]
|
#[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"),
|
DatabaseConnection::Disconnected => panic!("Disconnected"),
|
||||||
_ => unimplemented!(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn returning_on_update(&self) -> bool {
|
fn returning_on_update(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
DatabaseConnection::SqlxMySqlPoolConnection { .. } => false,
|
DatabaseConnection::SqlxMySqlPoolConnection { .. } => {
|
||||||
|
// Not supported in all MySQL & MariaDB versions
|
||||||
|
false
|
||||||
|
}
|
||||||
#[cfg(feature = "sqlx-postgres")]
|
#[cfg(feature = "sqlx-postgres")]
|
||||||
DatabaseConnection::SqlxPostgresPoolConnection(_) => true,
|
DatabaseConnection::SqlxPostgresPoolConnection(_) => {
|
||||||
|
// Supported by all Postgres versions
|
||||||
|
true
|
||||||
|
}
|
||||||
#[cfg(feature = "sqlx-sqlite")]
|
#[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"),
|
DatabaseConnection::Disconnected => panic!("Disconnected"),
|
||||||
_ => unimplemented!(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,6 +195,7 @@ async fn into_db_connection(pool: MySqlPool) -> Result<DatabaseConnection, DbErr
|
|||||||
let version: String = query_result.try_get("", "Value")?;
|
let version: String = query_result.try_get("", "Value")?;
|
||||||
let support_returning = if !version.contains("MariaDB") {
|
let support_returning = if !version.contains("MariaDB") {
|
||||||
// This is MySQL
|
// This is MySQL
|
||||||
|
// Not supported in all MySQL versions
|
||||||
false
|
false
|
||||||
} else {
|
} else {
|
||||||
// This is MariaDB
|
// 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_major = parse_captures!(1);
|
||||||
let ver_minor = parse_captures!(2);
|
let ver_minor = parse_captures!(2);
|
||||||
|
// Supported if it's MariaDB with version 10.5.0 or after
|
||||||
ver_major >= 10 && ver_minor >= 5
|
ver_major >= 10 && ver_minor >= 5
|
||||||
};
|
};
|
||||||
(version, support_returning)
|
(version, support_returning)
|
||||||
|
@ -31,7 +31,7 @@ async fn main() -> Result<(), DbErr> {
|
|||||||
(Column::ProfitMargin, 0.5.into()),
|
(Column::ProfitMargin, 0.5.into()),
|
||||||
])
|
])
|
||||||
.and_where(Column::Id.eq(1));
|
.and_where(Column::Id.eq(1));
|
||||||
|
|
||||||
let mut returning = Query::select();
|
let mut returning = Query::select();
|
||||||
returning.columns(vec![Column::Id, Column::Name, Column::ProfitMargin]);
|
returning.columns(vec![Column::Id, Column::Name, Column::ProfitMargin]);
|
||||||
if db.returning_on_insert() {
|
if db.returning_on_insert() {
|
||||||
@ -43,8 +43,18 @@ async fn main() -> Result<(), DbErr> {
|
|||||||
|
|
||||||
create_tables(db).await?;
|
create_tables(db).await?;
|
||||||
println!("db_version: {:#?}", db.version());
|
println!("db_version: {:#?}", db.version());
|
||||||
db.query_one(builder.build(&insert)).await?;
|
let insert_res = db.query_one(builder.build(&insert)).await?.expect("Insert failed");
|
||||||
db.query_one(builder.build(&update)).await?;
|
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;
|
ctx.delete().await;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user