Skip to main content

diesel/query_builder/upsert/
into_conflict_clause.rs

1use crate::query_builder::insert_statement::{BatchInsert, InsertFromSelect};
2use crate::query_builder::{BoxedSelectStatement, Query, SelectStatement, ValuesClause};
3
4mod sealed {
5    pub trait Sealed {}
6}
7
8/// Represents a type that can be converted into a value clause for an
9/// `ON CONFLICT` statement.
10///
11/// This trait is sealed and cannot be implemented for types outside of Diesel,
12/// and may be used to constrain generic parameters.
13pub trait IntoConflictValueClause: sealed::Sealed {
14    #[doc(hidden)]
15    type ValueClause;
16
17    #[doc(hidden)]
18    fn into_value_clause(self) -> Self::ValueClause;
19}
20
21#[derive(#[automatically_derived]
impl<S: ::core::fmt::Debug> ::core::fmt::Debug for OnConflictSelectWrapper<S>
    {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f,
            "OnConflictSelectWrapper", &&self.0)
    }
}Debug, #[automatically_derived]
impl<S: ::core::clone::Clone> ::core::clone::Clone for
    OnConflictSelectWrapper<S> {
    #[inline]
    fn clone(&self) -> OnConflictSelectWrapper<S> {
        OnConflictSelectWrapper(::core::clone::Clone::clone(&self.0))
    }
}Clone, #[automatically_derived]
impl<S: ::core::marker::Copy> ::core::marker::Copy for
    OnConflictSelectWrapper<S> {
}Copy)]
22pub struct OnConflictSelectWrapper<S>(pub(crate) S);
23
24impl<Q> Query for OnConflictSelectWrapper<Q>
25where
26    Q: Query,
27{
28    type SqlType = Q::SqlType;
29}
30
31impl<Inner, Tab> sealed::Sealed for ValuesClause<Inner, Tab> {}
32impl<Inner, Tab> IntoConflictValueClause for ValuesClause<Inner, Tab> {
33    type ValueClause = Self;
34
35    fn into_value_clause(self) -> Self::ValueClause {
36        self
37    }
38}
39
40impl<V, Tab, QId, const STATIC_QUERY_ID: bool> sealed::Sealed
41    for BatchInsert<V, Tab, QId, STATIC_QUERY_ID>
42{
43}
44impl<V, Tab, QId, const STATIC_QUERY_ID: bool> IntoConflictValueClause
45    for BatchInsert<V, Tab, QId, STATIC_QUERY_ID>
46{
47    type ValueClause = Self;
48
49    fn into_value_clause(self) -> Self::ValueClause {
50        self
51    }
52}
53
54impl<F, S, D, W, O, LOf, G, H, LC, Columns> sealed::Sealed
55    for InsertFromSelect<SelectStatement<F, S, D, W, O, LOf, G, H, LC>, Columns>
56{
57}
58impl<F, S, D, W, O, LOf, G, H, LC, Columns> IntoConflictValueClause
59    for InsertFromSelect<SelectStatement<F, S, D, W, O, LOf, G, H, LC>, Columns>
60{
61    type ValueClause = InsertFromSelect<
62        OnConflictSelectWrapper<SelectStatement<F, S, D, W, O, LOf, G, H, LC>>,
63        Columns,
64    >;
65
66    fn into_value_clause(self) -> Self::ValueClause {
67        let InsertFromSelect { columns, query } = self;
68        InsertFromSelect {
69            query: OnConflictSelectWrapper(query),
70            columns,
71        }
72    }
73}
74
75impl<'a, ST, QS, DB, GB, Columns> sealed::Sealed
76    for InsertFromSelect<BoxedSelectStatement<'a, ST, QS, DB, GB>, Columns>
77{
78}
79impl<'a, ST, QS, DB, GB, Columns> IntoConflictValueClause
80    for InsertFromSelect<BoxedSelectStatement<'a, ST, QS, DB, GB>, Columns>
81{
82    type ValueClause = InsertFromSelect<
83        OnConflictSelectWrapper<BoxedSelectStatement<'a, ST, QS, DB, GB>>,
84        Columns,
85    >;
86
87    fn into_value_clause(self) -> Self::ValueClause {
88        let InsertFromSelect { columns, query } = self;
89        InsertFromSelect {
90            query: OnConflictSelectWrapper(query),
91            columns,
92        }
93    }
94}