diesel/query_builder/upsert/
on_conflict_actions.rs

1use std::marker::PhantomData;
2
3use crate::backend::sql_dialect::on_conflict_clause;
4use crate::expression::{AppearsOnTable, Expression};
5use crate::query_builder::*;
6use crate::query_source::*;
7
8#[doc(hidden)]
9#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for DoNothing<T> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "DoNothing",
            &&self.0)
    }
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for DoNothing<T> {
    #[inline]
    fn clone(&self) -> DoNothing<T> {
        DoNothing(::core::clone::Clone::clone(&self.0))
    }
}Clone, #[automatically_derived]
impl<T: ::core::marker::Copy> ::core::marker::Copy for DoNothing<T> { }Copy, const _: () =
    {
        use diesel;
        #[allow(non_camel_case_types)]
        impl<T: diesel::query_builder::QueryId> diesel::query_builder::QueryId
            for DoNothing<T> {
            type QueryId =
                DoNothing<<T as diesel::query_builder::QueryId>::QueryId>;
            const HAS_STATIC_QUERY_ID: bool =
                <T as diesel::query_builder::QueryId>::HAS_STATIC_QUERY_ID &&
                    true;
            const IS_WINDOW_FUNCTION: bool =
                <T as diesel::query_builder::QueryId>::IS_WINDOW_FUNCTION ||
                    false;
        }
    };QueryId)]
10pub struct DoNothing<T>(PhantomData<T>);
11
12impl<T> DoNothing<T> {
13    pub(crate) fn new() -> Self {
14        Self(PhantomData)
15    }
16}
17
18impl<T, DB> QueryFragment<DB> for DoNothing<T>
19where
20    DB: Backend,
21    Self: QueryFragment<DB, DB::OnConflictClause>,
22{
23    fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
24        <Self as QueryFragment<DB, DB::OnConflictClause>>::walk_ast(self, pass)
25    }
26}
27
28impl<DB, T, SD> QueryFragment<DB, SD> for DoNothing<T>
29where
30    DB: Backend<OnConflictClause = SD>,
31    SD: on_conflict_clause::PgLikeOnConflictClause,
32{
33    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
34        out.push_sql(" DO NOTHING");
35        Ok(())
36    }
37}
38
39#[doc(hidden)]
40#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug, Tab: ::core::fmt::Debug> ::core::fmt::Debug for
    DoUpdate<T, Tab> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "DoUpdate",
            "changeset", &self.changeset, "tab", &&self.tab)
    }
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone, Tab: ::core::clone::Clone> ::core::clone::Clone
    for DoUpdate<T, Tab> {
    #[inline]
    fn clone(&self) -> DoUpdate<T, Tab> {
        DoUpdate {
            changeset: ::core::clone::Clone::clone(&self.changeset),
            tab: ::core::clone::Clone::clone(&self.tab),
        }
    }
}Clone, #[automatically_derived]
impl<T: ::core::marker::Copy, Tab: ::core::marker::Copy> ::core::marker::Copy
    for DoUpdate<T, Tab> {
}Copy, const _: () =
    {
        use diesel;
        #[allow(non_camel_case_types)]
        impl<T: diesel::query_builder::QueryId,
            Tab: diesel::query_builder::QueryId>
            diesel::query_builder::QueryId for DoUpdate<T, Tab> {
            type QueryId =
                DoUpdate<<T as diesel::query_builder::QueryId>::QueryId,
                <Tab as diesel::query_builder::QueryId>::QueryId>;
            const HAS_STATIC_QUERY_ID: bool =
                <T as diesel::query_builder::QueryId>::HAS_STATIC_QUERY_ID &&
                        <Tab as diesel::query_builder::QueryId>::HAS_STATIC_QUERY_ID
                    && true;
            const IS_WINDOW_FUNCTION: bool =
                <T as diesel::query_builder::QueryId>::IS_WINDOW_FUNCTION ||
                        <Tab as diesel::query_builder::QueryId>::IS_WINDOW_FUNCTION
                    || false;
        }
    };QueryId)]
41pub struct DoUpdate<T, Tab> {
42    pub(crate) changeset: T,
43    tab: PhantomData<Tab>,
44}
45
46impl<T, Tab> DoUpdate<T, Tab> {
47    pub(crate) fn new(changeset: T) -> Self {
48        DoUpdate {
49            changeset,
50            tab: PhantomData,
51        }
52    }
53}
54
55impl<DB, T, Tab> QueryFragment<DB> for DoUpdate<T, Tab>
56where
57    DB: Backend,
58    Self: QueryFragment<DB, DB::OnConflictClause>,
59{
60    fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
61        <Self as QueryFragment<DB, DB::OnConflictClause>>::walk_ast(self, pass)
62    }
63}
64
65impl<DB, T, Tab, SD> QueryFragment<DB, SD> for DoUpdate<T, Tab>
66where
67    DB: Backend<OnConflictClause = SD>,
68    T: QueryFragment<DB>,
69    SD: on_conflict_clause::PgLikeOnConflictClause,
70{
71    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
72        out.unsafe_to_cache_prepared();
73        if self.changeset.is_noop(out.backend())? {
74            out.push_sql(" DO NOTHING");
75        } else {
76            out.push_sql(" DO UPDATE SET ");
77            self.changeset.walk_ast(out.reborrow())?;
78        }
79        Ok(())
80    }
81}
82
83#[doc(hidden)]
84#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for Excluded<T> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Excluded",
            &&self.0)
    }
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for Excluded<T> {
    #[inline]
    fn clone(&self) -> Excluded<T> {
        Excluded(::core::clone::Clone::clone(&self.0))
    }
}Clone, #[automatically_derived]
impl<T: ::core::marker::Copy> ::core::marker::Copy for Excluded<T> { }Copy, const _: () =
    {
        use diesel;
        #[allow(non_camel_case_types)]
        impl<T: diesel::query_builder::QueryId> diesel::query_builder::QueryId
            for Excluded<T> {
            type QueryId =
                Excluded<<T as diesel::query_builder::QueryId>::QueryId>;
            const HAS_STATIC_QUERY_ID: bool =
                <T as diesel::query_builder::QueryId>::HAS_STATIC_QUERY_ID &&
                    true;
            const IS_WINDOW_FUNCTION: bool =
                <T as diesel::query_builder::QueryId>::IS_WINDOW_FUNCTION ||
                    false;
        }
    };QueryId)]
85pub struct Excluded<T>(T);
86
87impl<T> Excluded<T> {
88    pub(crate) fn new(t: T) -> Self {
89        Excluded(t)
90    }
91}
92
93impl<DB, T> QueryFragment<DB> for Excluded<T>
94where
95    DB: Backend,
96    Self: QueryFragment<DB, DB::OnConflictClause>,
97{
98    fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
99        <Self as QueryFragment<DB, DB::OnConflictClause>>::walk_ast(self, pass)
100    }
101}
102
103impl<DB, T, SD> QueryFragment<DB, SD> for Excluded<T>
104where
105    DB: Backend<OnConflictClause = SD>,
106    T: Column,
107    SD: on_conflict_clause::PgLikeOnConflictClause,
108{
109    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
110        out.push_sql("excluded.");
111        out.push_identifier(T::NAME)?;
112        Ok(())
113    }
114}
115
116impl<T> Expression for Excluded<T>
117where
118    T: Expression,
119{
120    type SqlType = T::SqlType;
121}
122
123impl<T> AppearsOnTable<T::Table> for Excluded<T>
124where
125    T: Column,
126    Excluded<T>: Expression,
127{
128}