diff --git a/src/backend/mysql/query.rs b/src/backend/mysql/query.rs index 5538ef7222..810628ce12 100644 --- a/src/backend/mysql/query.rs +++ b/src/backend/mysql/query.rs @@ -1,3 +1,11 @@ use super::*; -impl QueryBuilder for MysqlQueryBuilder {} +impl QueryBuilder for MysqlQueryBuilder { + fn prepare_returning( + &self, + _returning: &[SelectExpr], + _sql: &mut SqlWriter, + _collector: &mut dyn FnMut(Value), + ) { + } +} diff --git a/src/backend/postgres/query.rs b/src/backend/postgres/query.rs index 16c138ee0a..276d3c1dd1 100644 --- a/src/backend/postgres/query.rs +++ b/src/backend/postgres/query.rs @@ -6,24 +6,6 @@ impl QueryBuilder for PostgresQueryBuilder { ("$", true) } - fn prepare_returning( - &self, - returning: &[SelectExpr], - sql: &mut SqlWriter, - collector: &mut dyn FnMut(Value), - ) { - if !returning.is_empty() { - write!(sql, " RETURNING ").unwrap(); - returning.iter().fold(true, |first, expr| { - if !first { - write!(sql, ", ").unwrap() - } - self.prepare_select_expr(expr, sql, collector); - false - }); - } - } - fn if_null_function(&self) -> &str { "COALESCE" } diff --git a/src/backend/query_builder.rs b/src/backend/query_builder.rs index 22f0f08fb9..20af88d531 100644 --- a/src/backend/query_builder.rs +++ b/src/backend/query_builder.rs @@ -793,10 +793,20 @@ pub trait QueryBuilder: QuotedBuilder { /// Hook to insert "RETURNING" statements. fn prepare_returning( &self, - _returning: &[SelectExpr], - _sql: &mut SqlWriter, - _collector: &mut dyn FnMut(Value), + returning: &[SelectExpr], + sql: &mut SqlWriter, + collector: &mut dyn FnMut(Value), ) { + if !returning.is_empty() { + write!(sql, " RETURNING ").unwrap(); + returning.iter().fold(true, |first, expr| { + if !first { + write!(sql, ", ").unwrap() + } + self.prepare_select_expr(expr, sql, collector); + false + }); + } } #[doc(hidden)] diff --git a/src/query/delete.rs b/src/query/delete.rs index d8c83e2e06..bb580e3f4d 100644 --- a/src/query/delete.rs +++ b/src/query/delete.rs @@ -100,7 +100,15 @@ impl DeleteStatement { self } - /// RETURNING expressions. Postgres only. + /// RETURNING expressions. + /// + /// ## Note: + /// Works on + /// * PostgreSQL + /// * SQLite + /// - SQLite version >= 3.35.0 and + /// - Not with sqlx, see [issue](https://github.com/launchbadge/sqlx/issues/1531) + /// - **Note that sea-query won't try to enforce either of these constraints** /// /// ``` /// use sea_query::{tests_cfg::*, *}; @@ -121,7 +129,7 @@ impl DeleteStatement { /// ); /// assert_eq!( /// query.to_string(SqliteQueryBuilder), - /// r#"DELETE FROM `glyph` WHERE `id` = 1"# + /// r#"DELETE FROM `glyph` WHERE `id` = 1 RETURNING `id`"# /// ); /// ``` pub fn returning(&mut self, select: SelectStatement) -> &mut Self { @@ -129,9 +137,17 @@ impl DeleteStatement { self } - /// RETURNING a column after delete. Postgres only. + /// RETURNING a column after delete. /// Wrapper over [`DeleteStatement::returning()`]. /// + /// ## Note: + /// Works on + /// * PostgreSQL + /// * SQLite + /// - SQLite version >= 3.35.0 and + /// - Not with sqlx, see [issue](https://github.com/launchbadge/sqlx/issues/1531) + /// - **Note that sea-query won't try to enforce either of these constraints** + /// /// ``` /// use sea_query::{tests_cfg::*, *}; /// @@ -151,7 +167,7 @@ impl DeleteStatement { /// ); /// assert_eq!( /// query.to_string(SqliteQueryBuilder), - /// r#"DELETE FROM `glyph` WHERE `id` = 1"# + /// r#"DELETE FROM `glyph` WHERE `id` = 1 RETURNING `id`"# /// ); /// ``` pub fn returning_col(&mut self, col: C) -> &mut Self diff --git a/src/query/insert.rs b/src/query/insert.rs index 39903b9532..4860e8e6c7 100644 --- a/src/query/insert.rs +++ b/src/query/insert.rs @@ -179,7 +179,15 @@ impl InsertStatement { self.exprs(values).unwrap() } - /// RETURNING expressions. Postgres only. + /// RETURNING expressions. + /// + /// ## Note: + /// Works on + /// * PostgreSQL + /// * SQLite + /// - SQLite version >= 3.35.0 and + /// - Not with sqlx, see [issue](https://github.com/launchbadge/sqlx/issues/1531) + /// - **Note that sea-query won't try to enforce either of these constraints** /// /// ``` /// use sea_query::{tests_cfg::*, *}; @@ -201,7 +209,7 @@ impl InsertStatement { /// ); /// assert_eq!( /// query.to_string(SqliteQueryBuilder), - /// "INSERT INTO `glyph` (`image`) VALUES ('12A')" + /// "INSERT INTO `glyph` (`image`) VALUES ('12A') RETURNING `id`" /// ); /// ``` pub fn returning(&mut self, select: SelectStatement) -> &mut Self { @@ -209,9 +217,17 @@ impl InsertStatement { self } - /// RETURNING a column after insertion. Postgres only. This is equivalent to MySQL's LAST_INSERT_ID. + /// RETURNING a column after insertion. This is equivalent to MySQL's LAST_INSERT_ID. /// Wrapper over [`InsertStatement::returning()`]. /// + /// ## Note: + /// Works on + /// * PostgreSQL + /// * SQLite + /// - SQLite version >= 3.35.0 and + /// - Not with sqlx, see [issue](https://github.com/launchbadge/sqlx/issues/1531) + /// - **Note that sea-query won't try to enforce either of these constraints** + /// /// ``` /// use sea_query::{tests_cfg::*, *}; /// @@ -232,7 +248,7 @@ impl InsertStatement { /// ); /// assert_eq!( /// query.to_string(SqliteQueryBuilder), - /// "INSERT INTO `glyph` (`image`) VALUES ('12A')" + /// "INSERT INTO `glyph` (`image`) VALUES ('12A') RETURNING `id`" /// ); /// ``` pub fn returning_col(&mut self, col: C) -> &mut Self diff --git a/src/query/update.rs b/src/query/update.rs index 5c8af74956..8152f2311e 100644 --- a/src/query/update.rs +++ b/src/query/update.rs @@ -223,7 +223,15 @@ impl UpdateStatement { self } - /// RETURNING expressions. Postgres only. + /// RETURNING expressions. + /// + /// ## Note: + /// Works on + /// * PostgreSQL + /// * SQLite + /// - SQLite version >= 3.35.0 and + /// - Not with sqlx, see [issue](https://github.com/launchbadge/sqlx/issues/1531) + /// - **Note that sea-query won't try to enforce either of these constraints** /// /// ``` /// use sea_query::{tests_cfg::*, *}; @@ -246,7 +254,7 @@ impl UpdateStatement { /// ); /// assert_eq!( /// query.to_string(SqliteQueryBuilder), - /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1"# + /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1 RETURNING `id`"# /// ); /// ``` pub fn returning(&mut self, select: SelectStatement) -> &mut Self { @@ -254,9 +262,17 @@ impl UpdateStatement { self } - /// RETURNING a column after update. Postgres only. + /// RETURNING a column after update. /// Wrapper over [`UpdateStatement::returning()`]. /// + /// ## Note: + /// Works on + /// * PostgreSQL + /// * SQLite + /// - SQLite version >= 3.35.0 and + /// - Not with sqlx, see [issue](https://github.com/launchbadge/sqlx/issues/1531) + /// - **Note that sea-query won't try to enforce either of these constraints** + /// /// ``` /// use sea_query::{tests_cfg::*, *}; /// @@ -279,7 +295,7 @@ impl UpdateStatement { /// ); /// assert_eq!( /// query.to_string(SqliteQueryBuilder), - /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1"# + /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1 RETURNING `id`"# /// ); /// ``` pub fn returning_col(&mut self, col: C) -> &mut Self