Skip to content

Commit

Permalink
Merge pull request SeaQL#194 from SpyrosRoum/returning-for-sqlite
Browse files Browse the repository at this point in the history
Implement `RETURNING` for SQLite
  • Loading branch information
tyt2y3 authored Jan 1, 2022
2 parents 8ad6fbf + 8730f5c commit 0ccd839
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 34 deletions.
10 changes: 9 additions & 1 deletion src/backend/mysql/query.rs
Original file line number Diff line number Diff line change
@@ -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),
) {
}
}
18 changes: 0 additions & 18 deletions src/backend/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
16 changes: 13 additions & 3 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
24 changes: 20 additions & 4 deletions src/query/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*, *};
Expand All @@ -121,17 +129,25 @@ 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 {
self.returning = select.selects;
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::*, *};
///
Expand All @@ -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<C>(&mut self, col: C) -> &mut Self
Expand Down
24 changes: 20 additions & 4 deletions src/query/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*, *};
Expand All @@ -201,17 +209,25 @@ 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 {
self.returning = select.selects;
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::*, *};
///
Expand All @@ -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<C>(&mut self, col: C) -> &mut Self
Expand Down
24 changes: 20 additions & 4 deletions src/query/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*, *};
Expand All @@ -246,17 +254,25 @@ 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 {
self.returning = select.selects;
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::*, *};
///
Expand All @@ -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<C>(&mut self, col: C) -> &mut Self
Expand Down

0 comments on commit 0ccd839

Please sign in to comment.