diesel/pg/upsert/
on_conflict_actions.rs

1use expression::{AppearsOnTable, Expression};
2use pg::Pg;
3use query_builder::*;
4use query_source::*;
5use result::QueryResult;
6
7/// Represents `excluded.column` in an `ON CONFLICT DO UPDATE` clause.
8pub fn excluded<T>(excluded: T) -> Excluded<T> {
9    Excluded(excluded)
10}
11
12#[doc(hidden)]
13#[derive(Debug, Clone, Copy)]
14pub struct DoNothing;
15
16impl QueryFragment<Pg> for DoNothing {
17    fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> {
18        out.push_sql(" DO NOTHING");
19        Ok(())
20    }
21}
22
23#[doc(hidden)]
24#[derive(Debug, Clone, Copy)]
25pub struct DoUpdate<T> {
26    changeset: T,
27}
28
29impl<T> DoUpdate<T> {
30    pub(crate) fn new(changeset: T) -> Self {
31        DoUpdate { changeset }
32    }
33}
34
35impl<T> QueryFragment<Pg> for DoUpdate<T>
36where
37    T: QueryFragment<Pg>,
38{
39    fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> {
40        out.unsafe_to_cache_prepared();
41        if self.changeset.is_noop()? {
42            out.push_sql(" DO NOTHING");
43        } else {
44            out.push_sql(" DO UPDATE SET ");
45            self.changeset.walk_ast(out.reborrow())?;
46        }
47        Ok(())
48    }
49}
50
51#[doc(hidden)]
52#[derive(Debug, Clone, Copy)]
53pub struct Excluded<T>(T);
54
55impl<T> QueryFragment<Pg> for Excluded<T>
56where
57    T: Column,
58{
59    fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> {
60        out.push_sql("excluded.");
61        out.push_identifier(T::NAME)?;
62        Ok(())
63    }
64}
65
66impl<T> Expression for Excluded<T>
67where
68    T: Expression,
69{
70    type SqlType = T::SqlType;
71}
72
73impl<T> AppearsOnTable<T::Table> for Excluded<T>
74where
75    T: Column,
76    Excluded<T>: Expression,
77{
78}