diesel/query_builder/upsert/
on_conflict_clause.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use super::on_conflict_actions::*;
use super::on_conflict_target::*;
use crate::backend::sql_dialect;
use crate::insertable::*;
use crate::query_builder::where_clause::{NoWhereClause, WhereClause};
use crate::query_builder::*;

#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct OnConflictValues<Values, Target, Action, WhereClause = NoWhereClause> {
    pub(crate) values: Values,
    pub(crate) target: Target,
    pub(crate) action: Action,
    /// Allow to apply filters on ON CONFLICT ... DO UPDATE ... WHERE ...
    pub(crate) where_clause: WhereClause,
}

impl<Values, Target, Action, WhereClause> QueryId
    for OnConflictValues<Values, Target, Action, WhereClause>
{
    type QueryId = ();

    const HAS_STATIC_QUERY_ID: bool = false;
}

impl<Values, T> OnConflictValues<Values, NoConflictTarget, DoNothing<T>, NoWhereClause> {
    pub(crate) fn do_nothing(values: Values) -> Self {
        Self::new(values, NoConflictTarget, DoNothing::new(), NoWhereClause)
    }
}

impl<Values, Target, Action, WhereClause> OnConflictValues<Values, Target, Action, WhereClause> {
    pub(crate) fn new(
        values: Values,
        target: Target,
        action: Action,
        where_clause: WhereClause,
    ) -> Self {
        OnConflictValues {
            values,
            target,
            action,
            where_clause,
        }
    }

    pub(crate) fn replace_where<Where, F>(
        self,
        f: F,
    ) -> OnConflictValues<Values, Target, Action, Where>
    where
        F: FnOnce(WhereClause) -> Where,
    {
        OnConflictValues::new(self.values, self.target, self.action, f(self.where_clause))
    }
}

impl<DB, Values, Target, Action, WhereClause> CanInsertInSingleQuery<DB>
    for OnConflictValues<Values, Target, Action, WhereClause>
where
    DB: Backend,
    DB::OnConflictClause: sql_dialect::on_conflict_clause::SupportsOnConflictClause,
    Values: CanInsertInSingleQuery<DB>,
{
    fn rows_to_insert(&self) -> Option<usize> {
        self.values.rows_to_insert()
    }
}

impl<DB, Values, Target, Action> QueryFragment<DB>
    for OnConflictValues<Values, Target, Action, NoWhereClause>
where
    DB: Backend,
    DB::OnConflictClause: sql_dialect::on_conflict_clause::SupportsOnConflictClause,
    Self: QueryFragment<DB, DB::OnConflictClause>,
{
    fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        <Self as QueryFragment<DB, DB::OnConflictClause>>::walk_ast(self, pass)
    }
}

impl<DB, Values, Target, Action, SD> QueryFragment<DB, SD>
    for OnConflictValues<Values, Target, Action, NoWhereClause>
where
    DB: Backend<OnConflictClause = SD>,
    SD: sql_dialect::on_conflict_clause::PgLikeOnConflictClause,
    Values: QueryFragment<DB>,
    Target: QueryFragment<DB>,
    Action: QueryFragment<DB>,
{
    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        self.values.walk_ast(out.reborrow())?;
        out.push_sql(" ON CONFLICT");
        self.target.walk_ast(out.reborrow())?;
        self.action.walk_ast(out.reborrow())?;
        Ok(())
    }
}

impl<DB, Values, Target, Action, Expr> QueryFragment<DB>
    for OnConflictValues<Values, Target, Action, WhereClause<Expr>>
where
    DB: Backend,
    DB::OnConflictClause: sql_dialect::on_conflict_clause::SupportsOnConflictClause,
    DB::OnConflictClause: sql_dialect::on_conflict_clause::SupportsOnConflictClauseWhere,
    Values: QueryFragment<DB>,
    Target: QueryFragment<DB>,
    Action: QueryFragment<DB>,
    WhereClause<Expr>: QueryFragment<DB>,
{
    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        self.values.walk_ast(out.reborrow())?;
        out.push_sql(" ON CONFLICT");
        self.target.walk_ast(out.reborrow())?;
        self.action.walk_ast(out.reborrow())?;
        self.where_clause.walk_ast(out.reborrow())?;
        Ok(())
    }
}