Skip to content

Commit

Permalink
Fix SeaQL#120
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhannadAlrusayni committed Sep 14, 2021
1 parent bef0faf commit ed19261
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ pub trait QueryBuilder: QuotedBuilder {
write!(sql, " LIMIT ").unwrap();
self.prepare_value(limit, sql, collector);
}

self.prepare_returning(&delete.returning, sql, collector);
}

/// Translate [`SimpleExpr`] into SQL statement.
Expand Down
65 changes: 64 additions & 1 deletion src/query/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
query::{condition::*, OrderedStatement},
types::*,
value::*,
QueryStatementBuilder,
Query, QueryStatementBuilder, SelectExpr, SelectStatement,
};

/// Delete existing rows from the table
Expand Down Expand Up @@ -39,6 +39,7 @@ pub struct DeleteStatement {
pub(crate) wherei: ConditionHolder,
pub(crate) orders: Vec<OrderExpr>,
pub(crate) limit: Option<Value>,
pub(crate) returning: Vec<SelectExpr>,
}

impl Default for DeleteStatement {
Expand All @@ -55,6 +56,7 @@ impl DeleteStatement {
wherei: ConditionHolder::new(),
orders: Vec::new(),
limit: None,
returning: Vec::new(),
}
}

Expand Down Expand Up @@ -97,6 +99,67 @@ impl DeleteStatement {
self.limit = Some(Value::BigUnsigned(Some(limit)));
self
}

/// RETURNING expressions. Postgres only.
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::delete()
/// .from_table(Glyph::Table)
/// .and_where(Expr::col(Glyph::Id).eq(1))
/// .returning(Query::select().column(Glyph::Id).take())
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"DELETE FROM `glyph` WHERE `id` = 1"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"DELETE FROM `glyph` WHERE `id` = 1"#
/// );
/// ```
pub fn returning(&mut self, select: SelectStatement) -> &mut Self {
self.returning = select.selects;
self
}

/// RETURNING a column after delete. Postgres only.
/// Wrapper over [`DeleteStatement::returning()`].
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::delete()
/// .from_table(Glyph::Table)
/// .and_where(Expr::col(Glyph::Id).eq(1))
/// .returning_col(Glyph::Id)
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"DELETE FROM `glyph` WHERE `id` = 1"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"DELETE FROM `glyph` WHERE `id` = 1"#
/// );
/// ```
pub fn returning_col<C>(&mut self, col: C) -> &mut Self
where
C: IntoIden,
{
self.returning(Query::select().column(col.into_iden()).take())
}
}

impl QueryStatementBuilder for DeleteStatement {
Expand Down

0 comments on commit ed19261

Please sign in to comment.