Debian Codegraph @

src/query_builder/upsert/on_conflict_actions.rs

raw
1 use std::marker::PhantomData;
2
3 use crate::backend::sql_dialect::on_conflict_clause;
4 use crate::expression::{AppearsOnTable, Expression};
5 use crate::query_builder::*;
6 use crate::query_source::*;
7
8 #[doc(hidden)]
9 #[derive(Debug, Clone, Copy, QueryId)]
10 pub struct DoNothing<T>(PhantomData<T>);
11
12 impl<T> DoNothing<T> {
13 pub(crate) fn new() -> Self {
14 Self(PhantomData)
15 }
16 }
17
18 impl<T, DB> QueryFragment<DB> for DoNothing<T>
19 where
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
28 impl<DB, T, SD> QueryFragment<DB, SD> for DoNothing<T>
29 where
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(Debug, Clone, Copy, QueryId)]
41 pub struct DoUpdate<T, Tab> {
42 pub(crate) changeset: T,
43 tab: PhantomData<Tab>,
44 }
45
46 impl<T, Tab> DoUpdate<T, Tab> {
47 pub(crate) fn new(changeset: T) -> Self {
48 DoUpdate {
49 changeset,
50 tab: PhantomData,
51 }
52 }
53 }
54
55 impl<DB, T, Tab> QueryFragment<DB> for DoUpdate<T, Tab>
56 where
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
65 impl<DB, T, Tab, SD> QueryFragment<DB, SD> for DoUpdate<T, Tab>
66 where
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(Debug, Clone, Copy, QueryId)]
85 pub struct Excluded<T>(T);
86
87 impl<T> Excluded<T> {
88 pub(crate) fn new(t: T) -> Self {
89 Excluded(t)
90 }
91 }
92
93 impl<DB, T> QueryFragment<DB> for Excluded<T>
94 where
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
103 impl<DB, T, SD> QueryFragment<DB, SD> for Excluded<T>
104 where
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
116 impl<T> Expression for Excluded<T>
117 where
118 T: Expression,
119 {
120 type SqlType = T::SqlType;
121 }
122
123 impl<T> AppearsOnTable<T::Table> for Excluded<T>
124 where
125 T: Column,
126 Excluded<T>: Expression,
127 {
128 }