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(#[automatically_derived]
impl ::core::fmt::Debug for NoReturningClause {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "NoReturningClause")
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for NoReturningClause {
    #[inline]
    fn clone(&self) -> NoReturningClause { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for NoReturningClause { }Copy, const _: () =
    {
        use diesel;
        #[allow(non_camel_case_types)]
        impl diesel::query_builder::QueryId for NoReturningClause {
            type QueryId = NoReturningClause<>;
            const HAS_STATIC_QUERY_ID: bool = true;
            const IS_WINDOW_FUNCTION: bool = false;
        }
    };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(#[automatically_derived]
impl<Expr: ::core::fmt::Debug> ::core::fmt::Debug for ReturningClause<Expr> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f,
            "ReturningClause", &&self.0)
    }
}Debug, #[automatically_derived]
impl<Expr: ::core::clone::Clone> ::core::clone::Clone for
    ReturningClause<Expr> {
    #[inline]
    fn clone(&self) -> ReturningClause<Expr> {
        ReturningClause(::core::clone::Clone::clone(&self.0))
    }
}Clone, #[automatically_derived]
impl<Expr: ::core::marker::Copy> ::core::marker::Copy for
    ReturningClause<Expr> {
}Copy, const _: () =
    {
        use diesel;
        #[allow(non_camel_case_types)]
        impl<Expr: diesel::query_builder::QueryId>
            diesel::query_builder::QueryId for ReturningClause<Expr> {
            type QueryId =
                ReturningClause<<Expr as
                diesel::query_builder::QueryId>::QueryId>;
            const HAS_STATIC_QUERY_ID: bool =
                <Expr as diesel::query_builder::QueryId>::HAS_STATIC_QUERY_ID
                    && true;
            const IS_WINDOW_FUNCTION: bool =
                <Expr as diesel::query_builder::QueryId>::IS_WINDOW_FUNCTION
                    || false;
        }
    };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}