diesel/query_builder/update_statement/
changeset.rs

1use backend::Backend;
2use expression::operators::Eq;
3use expression::AppearsOnTable;
4use query_builder::*;
5use query_source::{Column, QuerySource};
6use result::QueryResult;
7
8/// Types which can be passed to
9/// [`update.set`](struct.UpdateStatement.html#method.set).
10///
11/// ### Deriving
12///
13/// This trait can be automatically derived using by adding `#[derive(AsChangeset)]`
14/// to your struct.  Structs which derive this trait must be annotated with
15/// `#[table_name = "something"]`. If the field name of your struct differs
16/// from the name of the column, you can annotate the field with
17/// `#[column_name = "some_column_name"]`.
18///
19/// By default, any `Option` fields on the struct are skipped if their value is
20/// `None`. If you would like to assign `NULL` to the field instead, you can
21/// annotate your struct with `#[changeset_options(treat_none_as_null =
22/// "true")]`.
23pub trait AsChangeset {
24    /// The table which `Self::Changeset` will be updating
25    type Target: QuerySource;
26
27    /// The update statement this type represents
28    type Changeset;
29
30    /// Convert `self` into the actual update statement being executed
31    fn as_changeset(self) -> Self::Changeset;
32}
33
34impl<T: AsChangeset> AsChangeset for Option<T> {
35    type Target = T::Target;
36    type Changeset = Option<T::Changeset>;
37
38    fn as_changeset(self) -> Self::Changeset {
39        self.map(|v| v.as_changeset())
40    }
41}
42
43impl<Left, Right> AsChangeset for Eq<Left, Right>
44where
45    Left: Column,
46    Right: AppearsOnTable<Left::Table>,
47{
48    type Target = Left::Table;
49    type Changeset = Assign<Left, Right>;
50
51    fn as_changeset(self) -> Self::Changeset {
52        Assign {
53            _column: self.left,
54            expr: self.right,
55        }
56    }
57}
58
59#[derive(Debug, Clone, Copy)]
60pub struct Assign<Col, Expr> {
61    _column: Col,
62    expr: Expr,
63}
64
65impl<T, U, DB> QueryFragment<DB> for Assign<T, U>
66where
67    DB: Backend,
68    T: Column,
69    U: QueryFragment<DB>,
70{
71    fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
72        out.push_identifier(T::NAME)?;
73        out.push_sql(" = ");
74        QueryFragment::walk_ast(&self.expr, out)
75    }
76}