sea_query/query/
returning.rs

1use crate::{ColumnRef, IntoColumnRef, SimpleExpr};
2
3/// RETURNING clause.
4/// ## Note:
5/// Works on
6/// * PostgreSQL
7/// * SQLite
8///     - SQLite version >= 3.35.0
9///     - **Note that sea-query won't try to enforce either of these constraints**
10#[derive(Clone, Debug, PartialEq)]
11pub enum ReturningClause {
12    All,
13    Columns(Vec<ColumnRef>),
14    Exprs(Vec<SimpleExpr>),
15}
16
17/// Shorthand for constructing [`ReturningClause`]
18#[derive(Clone, Debug, Default)]
19pub struct Returning;
20
21impl Returning {
22    /// Constructs a new [`Returning`].
23    pub fn new() -> Self {
24        Self
25    }
26
27    /// Constructs a new [`ReturningClause::All`].
28    ///
29    /// # Examples
30    ///
31    /// ```
32    /// use sea_query::{tests_cfg::*, *};
33    ///
34    /// let query = Query::delete()
35    ///     .from_table(Glyph::Table)
36    ///     .and_where(Expr::col(Glyph::Id).eq(1))
37    ///     .returning(Query::returning().all())
38    ///     .to_owned();
39    ///
40    /// assert_eq!(
41    ///     query.to_string(PostgresQueryBuilder),
42    ///     r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING *"#
43    /// );
44    /// assert_eq!(
45    ///     query.to_string(SqliteQueryBuilder),
46    ///     r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING *"#
47    /// );
48    /// ```
49    pub fn all(&self) -> ReturningClause {
50        ReturningClause::All
51    }
52
53    /// Constructs a new [`ReturningClause::Columns`].
54    ///
55    /// # Examples
56    ///
57    /// ```
58    /// use sea_query::{tests_cfg::*, *};
59    ///
60    /// let query = Query::delete()
61    ///     .from_table(Glyph::Table)
62    ///     .and_where(Expr::col(Glyph::Id).eq(1))
63    ///     .returning(Query::returning().column(Glyph::Id))
64    ///     .to_owned();
65    ///
66    /// assert_eq!(
67    ///     query.to_string(PostgresQueryBuilder),
68    ///     r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id""#
69    /// );
70    /// assert_eq!(
71    ///     query.to_string(SqliteQueryBuilder),
72    ///     r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id""#
73    /// );
74    /// ```
75    pub fn column<C>(&self, col: C) -> ReturningClause
76    where
77        C: IntoColumnRef,
78    {
79        ReturningClause::Columns(vec![col.into_column_ref()])
80    }
81
82    /// Constructs a new [`ReturningClause::Columns`].
83    ///
84    /// # Examples
85    ///
86    /// ```
87    /// use sea_query::{tests_cfg::*, *};
88    ///
89    /// let query = Query::delete()
90    ///     .from_table(Glyph::Table)
91    ///     .and_where(Expr::col(Glyph::Id).eq(1))
92    ///     .returning(Query::returning().columns([Glyph::Id, Glyph::Image]))
93    ///     .to_owned();
94    ///
95    /// assert_eq!(
96    ///     query.to_string(PostgresQueryBuilder),
97    ///     r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id", "image""#
98    /// );
99    /// assert_eq!(
100    ///     query.to_string(SqliteQueryBuilder),
101    ///     r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id", "image""#
102    /// );
103    /// ```
104    pub fn columns<T, I>(self, cols: I) -> ReturningClause
105    where
106        T: IntoColumnRef,
107        I: IntoIterator<Item = T>,
108    {
109        let cols: Vec<_> = cols.into_iter().map(|c| c.into_column_ref()).collect();
110        ReturningClause::Columns(cols)
111    }
112
113    /// Constructs a new [`ReturningClause::Exprs`].
114    ///
115    /// # Examples
116    ///
117    /// ```
118    /// use sea_query::{tests_cfg::*, *};
119    ///
120    /// let query = Query::delete()
121    ///     .from_table(Glyph::Table)
122    ///     .and_where(Expr::col(Glyph::Id).eq(1))
123    ///     .returning(Query::returning().expr(Expr::col(Glyph::Id)))
124    ///     .to_owned();
125    ///
126    /// assert_eq!(
127    ///     query.to_string(PostgresQueryBuilder),
128    ///     r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id""#
129    /// );
130    /// assert_eq!(
131    ///     query.to_string(SqliteQueryBuilder),
132    ///     r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id""#
133    /// );
134    /// ```
135    pub fn expr<T>(&self, expr: T) -> ReturningClause
136    where
137        T: Into<SimpleExpr>,
138    {
139        ReturningClause::Exprs(vec![expr.into()])
140    }
141
142    /// Constructs a new [`ReturningClause::Exprs`].
143    ///
144    /// # Examples
145    ///
146    /// ```
147    /// use sea_query::{tests_cfg::*, *};
148    ///
149    /// let query = Query::delete()
150    ///     .from_table(Glyph::Table)
151    ///     .and_where(Expr::col(Glyph::Id).eq(1))
152    ///     .returning(Query::returning().exprs([Expr::col(Glyph::Id), Expr::col(Glyph::Image)]))
153    ///     .to_owned();
154    ///
155    /// assert_eq!(
156    ///     query.to_string(PostgresQueryBuilder),
157    ///     r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id", "image""#
158    /// );
159    /// assert_eq!(
160    ///     query.to_string(SqliteQueryBuilder),
161    ///     r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id", "image""#
162    /// );
163    /// ```
164    pub fn exprs<T, I>(self, exprs: I) -> ReturningClause
165    where
166        T: Into<SimpleExpr>,
167        I: IntoIterator<Item = T>,
168    {
169        ReturningClause::Exprs(exprs.into_iter().map(Into::into).collect())
170    }
171}