diesel/query_builder/
returning_clause.rs

1use super::DeleteStatement;
2use super::InsertStatement;
3use super::UpdateStatement;
4use super::{AstPass, QueryFragment};
5use crate::backend::{Backend, DieselReserveSpecialization};
6use crate::query_builder::QueryId;
7use crate::result::QueryResult;
8use crate::QuerySource;
9
10#[derive(Debug, Clone, Copy, QueryId)]
11pub struct NoReturningClause;
12
13impl<DB> QueryFragment<DB> for NoReturningClause
14where
15    DB: Backend + DieselReserveSpecialization,
16{
17    fn walk_ast<'b>(&'b self, _: AstPass<'_, 'b, DB>) -> QueryResult<()> {
18        Ok(())
19    }
20}
21
22/// This type represents a SQL `Returning` clause
23///
24/// Custom backends can specialize the [`QueryFragment`]
25/// implementation via
26/// [`SqlDialect::ReturningClause`](crate::backend::SqlDialect::ReturningClause)
27#[cfg_attr(
28    feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes",
29    cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")
30)]
31#[derive(Debug, Clone, Copy, QueryId)]
32pub struct ReturningClause<Expr>(pub Expr);
33
34impl<Expr, DB> QueryFragment<DB> for ReturningClause<Expr>
35where
36    DB: Backend,
37    Self: QueryFragment<DB, DB::ReturningClause>,
38{
39    fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
40        <Self as QueryFragment<DB, DB::ReturningClause>>::walk_ast(self, pass)
41    }
42}
43
44impl<Expr, DB>
45    QueryFragment<DB, crate::backend::sql_dialect::returning_clause::PgLikeReturningClause>
46    for ReturningClause<Expr>
47where
48    DB: Backend<
49        ReturningClause = crate::backend::sql_dialect::returning_clause::PgLikeReturningClause,
50    >,
51    Expr: QueryFragment<DB>,
52{
53    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
54        out.push_sql(" RETURNING ");
55        self.0.walk_ast(out.reborrow())?;
56        Ok(())
57    }
58}
59
60pub trait ReturningClauseHelper<S> {
61    type WithReturning;
62}
63
64impl<S, T, U, Op> ReturningClauseHelper<S> for InsertStatement<T, U, Op>
65where
66    T: QuerySource,
67{
68    type WithReturning = InsertStatement<T, U, Op, ReturningClause<S>>;
69}
70
71impl<S, T, U, V> ReturningClauseHelper<S> for UpdateStatement<T, U, V>
72where
73    T: QuerySource,
74{
75    type WithReturning = UpdateStatement<T, U, V, ReturningClause<S>>;
76}
77
78impl<S, T, U> ReturningClauseHelper<S> for DeleteStatement<T, U>
79where
80    T: QuerySource,
81{
82    type WithReturning = DeleteStatement<T, U, ReturningClause<S>>;
83}