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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use expression::{AppearsOnTable, Expression};
use pg::Pg;
use query_builder::*;
use query_source::*;
use result::QueryResult;

/// Used in conjunction with
/// [`on_conflict`](trait.OnConflictExtension.html#method.on_conflict) to write
/// a query in the form `ON CONFLICT (name) DO NOTHING`. If you want to do
/// nothing when *any* constraint conflicts, use
/// [`on_conflict_do_nothing()`](trait.OnConflictExtension.html#method.on_conflict_do_nothing)
/// instead.
pub fn do_nothing() -> DoNothing {
    DoNothing
}

/// Used to create a query in the form `ON CONFLICT (...) DO UPDATE ...`
///
/// Call `.set` on the result of this function with the changes you want to
/// apply. The argument to `set` can be anything that implements `AsChangeset`
/// (e.g. anything you could pass to `set` on a normal update statement).
///
/// Note: When inserting more than one row at a time, this query can still fail
/// if the rows being inserted conflict with each other.
///
/// # Examples
///
/// ## Set specific value on conflict
///
/// ```rust
/// # #[macro_use] extern crate diesel;
/// # #[macro_use] extern crate diesel_codegen;
/// # include!("src/pg/upsert/on_conflict_docs_setup.rs");
/// #
/// # fn main() {
/// #     use self::users::dsl::*;
/// use self::diesel::pg::upsert::*;
///
/// #     let conn = establish_connection();
/// #     conn.execute("TRUNCATE TABLE users").unwrap();
/// let user = User { id: 1, name: "Pascal" };
/// let user2 = User { id: 1, name: "Sean" };
///
/// assert_eq!(Ok(1), diesel::insert(&user).into(users).execute(&conn));
///
/// let insert_count = diesel::insert(
///     &user2.on_conflict(id, do_update().set(name.eq("I DONT KNOW ANYMORE")))
/// ).into(users).execute(&conn);
/// assert_eq!(Ok(1), insert_count);
///
/// let users_in_db = users.load(&conn);
/// assert_eq!(Ok(vec![(1, "I DONT KNOW ANYMORE".to_string())]), users_in_db);
/// # }
/// ```
///
/// ## Set `AsChangeset` struct on conflict
///
/// ```rust
/// # #[macro_use] extern crate diesel;
/// # #[macro_use] extern crate diesel_codegen;
/// # include!("src/pg/upsert/on_conflict_docs_setup.rs");
/// #
/// # fn main() {
/// #     use self::users::dsl::*;
/// use self::diesel::pg::upsert::*;
///
/// #     let conn = establish_connection();
/// #     conn.execute("TRUNCATE TABLE users").unwrap();
/// let user = User { id: 1, name: "Pascal" };
/// let user2 = User { id: 1, name: "Sean" };
///
/// assert_eq!(Ok(1), diesel::insert(&user).into(users).execute(&conn));
///
/// let insert_count = diesel::insert(
///     &user2.on_conflict(id, do_update().set(&user2))
/// ).into(users).execute(&conn);
/// assert_eq!(Ok(1), insert_count);
///
/// let users_in_db = users.load(&conn);
/// assert_eq!(Ok(vec![(1, "Sean".to_string())]), users_in_db);
/// # }
/// ```
///
/// ## Use `excluded` to get the rejected value
///
/// ```rust
/// # #[macro_use] extern crate diesel;
/// # #[macro_use] extern crate diesel_codegen;
/// # include!("src/pg/upsert/on_conflict_docs_setup.rs");
/// #
/// # fn main() {
/// #     use self::users::dsl::*;
/// use self::diesel::pg::upsert::*;
///
/// #     let conn = establish_connection();
/// #     conn.execute("TRUNCATE TABLE users").unwrap();
/// let user = User { id: 1, name: "Pascal" };
/// let user2 = User { id: 1, name: "Sean" };
/// let user3 = User { id: 2, name: "Tess" };
///
/// assert_eq!(Ok(1), diesel::insert(&user).into(users).execute(&conn));
///
/// let insert_count = diesel::insert(&vec![user2, user3]
///     .on_conflict(id, do_update().set(name.eq(excluded(name))))
/// ).into(users).execute(&conn);
/// assert_eq!(Ok(2), insert_count);
///
/// let users_in_db = users.load(&conn);
/// assert_eq!(Ok(vec![(1, "Sean".to_string()), (2, "Tess".to_string())]), users_in_db);
/// # }
pub fn do_update() -> IncompleteDoUpdate {
    IncompleteDoUpdate
}

/// Represents `excluded.column` in an `ON CONFLICT DO UPDATE` clause.
pub fn excluded<T>(excluded: T) -> Excluded<T> {
    Excluded(excluded)
}

#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct DoNothing;

impl QueryFragment<Pg> for DoNothing {
    fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> {
        out.push_sql(" DO NOTHING");
        Ok(())
    }
}

#[derive(Debug, Clone, Copy)]
pub struct IncompleteDoUpdate;

impl IncompleteDoUpdate {
    pub fn set<T: AsChangeset>(self, changeset: T) -> DoUpdate<T> {
        DoUpdate {
            changeset: changeset,
        }
    }
}

#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct DoUpdate<T> {
    changeset: T,
}

impl<T> QueryFragment<Pg> for DoUpdate<T> where
    T: Changeset<Pg>,
{
    fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> {
        out.unsafe_to_cache_prepared();
        if self.changeset.is_noop() {
            out.push_sql(" DO NOTHING");
        } else {
            out.push_sql(" DO UPDATE SET ");
            self.changeset.walk_ast(out.reborrow())?;
        }
        Ok(())
    }
}

#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct Excluded<T>(T);

impl<T> QueryFragment<Pg> for Excluded<T> where
    T: Column,
{
    fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> {
        out.push_sql("excluded.");
        try!(out.push_identifier(T::name()));
        Ok(())
    }
}

impl<T> Expression for Excluded<T> where
    T: Expression,
{
    type SqlType = T::SqlType;
}

impl<T> AppearsOnTable<T::Table> for Excluded<T> where
    T: Column,
    Excluded<T>: Expression,
{
}

#[doc(hidden)]
pub trait IntoConflictAction<T> {
    type Action: QueryFragment<Pg>;

    fn into_conflict_action(self) -> Self::Action;
}

impl<T> IntoConflictAction<T> for DoNothing {
    type Action = Self;

    fn into_conflict_action(self) -> Self::Action {
        self
    }
}

impl<Table, Changes> IntoConflictAction<Table> for DoUpdate<Changes> where
    Table: QuerySource,
    Changes: AsChangeset<Target=Table>,
    DoUpdate<Changes::Changeset>: QueryFragment<Pg>,
{
    type Action = DoUpdate<Changes::Changeset>;

    fn into_conflict_action(self) -> Self::Action {
        DoUpdate {
            changeset: self.changeset.as_changeset()
        }
    }
}