diff --git a/src/database/db_connection.rs b/src/database/db_connection.rs index 90a472e4..f04b67c7 100644 --- a/src/database/db_connection.rs +++ b/src/database/db_connection.rs @@ -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!(), } } diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index b66e1078..55f46a94 100644 --- a/src/driver/sqlx_mysql.rs +++ b/src/driver/sqlx_mysql.rs @@ -195,6 +195,7 @@ async fn into_db_connection(pool: MySqlPool) -> Result Result= 10 && ver_minor >= 5 }; (version, support_returning) diff --git a/tests/returning_tests.rs b/tests/returning_tests.rs index 561ba2c5..d67612fe 100644 --- a/tests/returning_tests.rs +++ b/tests/returning_tests.rs @@ -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(())