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
pub mod changeset;
pub mod target;

pub use self::changeset::{Changeset, AsChangeset};
pub use self::target::{UpdateTarget, IntoUpdateTarget};

use backend::{Backend, SupportsReturningClause};
use expression::{Expression, SelectableExpression, NonAggregate};
use query_builder::{Query, AsQuery, QueryFragment, QueryBuilder, BuildQueryResult};
use query_source::Table;
use result::QueryResult;

/// The type returned by [`update`](/diesel/fn.update.html). The only thing you can do
/// with this type is call `set` on it.
#[derive(Debug)]
pub struct IncompleteUpdateStatement<T, U>(UpdateTarget<T, U>);

impl<T, U> IncompleteUpdateStatement<T, U> {
    #[doc(hidden)]
    pub fn new(t: UpdateTarget<T, U>) -> Self {
        IncompleteUpdateStatement(t)
    }
}

impl<T, U> IncompleteUpdateStatement<T, U> {
    pub fn set<V>(self, values: V) -> UpdateStatement<T, U, V::Changeset> where
        T: Table,
        V: changeset::AsChangeset<Target=T>,
        UpdateStatement<T, U, V::Changeset>: AsQuery,
    {
        UpdateStatement {
            table: self.0.table,
            where_clause: self.0.where_clause,
            values: values.as_changeset(),
        }
    }
}

#[derive(Debug, Copy, Clone)]
pub struct UpdateStatement<T, U, V> {
    table: T,
    where_clause: Option<U>,
    values: V,
}

impl<T, U, V, DB> QueryFragment<DB> for UpdateStatement<T, U, V> where
    DB: Backend,
    T: Table,
    T::FromClause: QueryFragment<DB>,
    U: QueryFragment<DB>,
    V: changeset::Changeset<DB>,
{
    fn to_sql(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult {
        if self.values.is_noop() {
            return Err("There are no changes to save. This \
                       query cannot be built".into())
        }

        out.push_sql("UPDATE ");
        try!(self.table.from_clause().to_sql(out));
        out.push_sql(" SET ");
        try!(self.values.to_sql(out));
        if let Some(ref clause) = self.where_clause {
            out.push_sql(" WHERE ");
            try!(clause.to_sql(out));
        }
        Ok(())
    }

    fn collect_binds(&self, out: &mut DB::BindCollector) -> QueryResult<()> {
        try!(self.table.from_clause().collect_binds(out));
        try!(self.values.collect_binds(out));
        if let Some(ref clause) = self.where_clause {
            try!(clause.collect_binds(out));
        }
        Ok(())
    }

    fn is_safe_to_cache_prepared(&self) -> bool {
        false
    }
}

impl_query_id!(noop: UpdateStatement<T, U, V>);

impl<T, U, V> AsQuery for UpdateStatement<T, U, V> where
    T: Table,
    UpdateQuery<T::AllColumns, UpdateStatement<T, U, V>>: Query,
{
    type SqlType = <Self::Query as Query>::SqlType;
    type Query = UpdateQuery<T::AllColumns, Self>;

    fn as_query(self) -> Self::Query {
        UpdateQuery {
            returning: T::all_columns(),
            statement: self,
        }
    }
}

impl<T, U, V> UpdateStatement<T, U, V> {
    /// Specify what expression is returned after execution of the `update`.
    /// # Examples
    ///
    /// ### Updating a single record:
    ///
    /// ```rust
    /// # #[macro_use] extern crate diesel;
    /// # include!("src/doctest_setup.rs");
    /// #
    /// # table! {
    /// #     users {
    /// #         id -> Integer,
    /// #         name -> VarChar,
    /// #     }
    /// # }
    /// #
    /// # #[cfg(feature = "postgres")]
    /// # fn main() {
    /// #     use self::users::dsl::*;
    /// #     let connection = establish_connection();
    /// let updated_name = diesel::update(users.filter(id.eq(1)))
    ///     .set(name.eq("Dean"))
    ///     .returning(name)
    ///     .get_result(&connection);
    /// assert_eq!(Ok("Dean".to_string()), updated_name);
    /// # }
    /// # #[cfg(not(feature = "postgres"))]
    /// # fn main() {}
    /// ```
    pub fn returning<E>(self, returns: E) -> UpdateQuery<E, Self> where
        T: Table,
        UpdateQuery<E, Self>: Query,
    {
        UpdateQuery {
            returning: returns,
            statement: self,
        }
    }
}

#[doc(hidden)]
#[derive(Debug, Copy, Clone)]
pub struct UpdateQuery<T, U> {
    returning: T,
    statement: U,
}

impl<Ret, T, U, V> Query for UpdateQuery<Ret, UpdateStatement<T, U, V>> where
    T: Table,
    Ret: Expression + SelectableExpression<T> + NonAggregate,
{
    type SqlType = Ret::SqlType;
}

impl<T, U, DB> QueryFragment<DB> for UpdateQuery<T, U> where
    DB: Backend + SupportsReturningClause,
    T: QueryFragment<DB>,
    U: QueryFragment<DB>,
{
    fn to_sql(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult {
        try!(self.statement.to_sql(out));
        out.push_sql(" RETURNING ");
        try!(self.returning.to_sql(out));
        Ok(())
    }

    fn collect_binds(&self, out: &mut DB::BindCollector) -> QueryResult<()> {
        try!(self.statement.collect_binds(out));
        try!(self.returning.collect_binds(out));
        Ok(())
    }

    fn is_safe_to_cache_prepared(&self) -> bool {
        false
    }
}

impl_query_id!(noop: UpdateQuery<T, U>);