zino_orm/
mutation.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/// Generates SQL `SET` expressions.
use super::{query::QueryExt, DatabaseDriver, EncodeColumn, Entity, IntoSqlValue, Schema};
use std::marker::PhantomData;
use zino_core::{
    datetime::DateTime,
    extension::JsonObjectExt,
    model::{Mutation, Query},
    JsonValue, Map,
};

/// A mutation builder for the model entity.
///
/// # Examples
/// ```rust,ignore
/// use crate::model::{User, UserColumn};
/// use zino_orm::{MutationBuilder, QueryBuilder, Schema};
///
/// let query = QueryBuilder::<User>::new()
///     .primary_key("01936dc6-e48c-7d22-8e69-b29f85682fac")
///     .and_not_in(UserColumn::Status, ["Deleted", "Locked"])
///     .build();
/// let mut mutation = MutationBuilder::<User>::new()
///     .set(UserColumn::Status, "Active")
///     .set_now(UserColumn::UpdatedAt)
///     .inc_one(UserColumn::Version)
///     .build();
/// let ctx = User::update_one(&query, &mut mutation).await?;
/// ```
#[derive(Debug, Clone)]
pub struct MutationBuilder<E: Entity> {
    /// The mutation updates.
    updates: Map,
    /// `$inc` operations.
    inc_ops: Map,
    /// `$mul` operations.
    mul_ops: Map,
    /// `$min` operations.
    min_ops: Map,
    /// `$max` operations.
    max_ops: Map,
    /// The phantom data.
    phantom: PhantomData<E>,
}

impl<E: Entity> MutationBuilder<E> {
    /// Creates a new instance.
    #[inline]
    pub fn new() -> Self {
        Self {
            updates: Map::new(),
            inc_ops: Map::new(),
            mul_ops: Map::new(),
            min_ops: Map::new(),
            max_ops: Map::new(),
            phantom: PhantomData,
        }
    }

    /// Sets the value of a column.
    #[inline]
    pub fn set(mut self, col: E::Column, value: impl IntoSqlValue) -> Self {
        self.updates.upsert(col.as_ref(), value.into_sql_value());
        self
    }

    /// Sets the value of a column if the value is not null.
    #[inline]
    pub fn set_if_not_null(mut self, col: E::Column, value: impl IntoSqlValue) -> Self {
        let value = value.into_sql_value();
        if !value.is_null() {
            self.updates.upsert(col.as_ref(), value);
        }
        self
    }

    /// Sets the value of a column if the value is not none.
    #[inline]
    pub fn set_if_some<T: IntoSqlValue>(mut self, col: E::Column, value: Option<T>) -> Self {
        if let Some(value) = value {
            self.updates.upsert(col.as_ref(), value.into_sql_value());
        }
        self
    }

    /// Sets the value of a column to null.
    #[inline]
    pub fn set_null(mut self, col: E::Column) -> Self {
        self.updates.upsert(col.as_ref(), JsonValue::Null);
        self
    }

    /// Sets the value of a column to the current date time.
    #[inline]
    pub fn set_now(mut self, col: E::Column) -> Self {
        self.updates
            .upsert(col.as_ref(), DateTime::now().into_sql_value());
        self
    }

    /// Increments the value of a column.
    #[inline]
    pub fn inc(mut self, col: E::Column, value: impl IntoSqlValue) -> Self {
        self.inc_ops.upsert(col.as_ref(), value.into_sql_value());
        self
    }

    /// Increments the value of a column by 1.
    #[inline]
    pub fn inc_one(mut self, col: E::Column) -> Self {
        self.inc_ops.upsert(col.as_ref(), 1);
        self
    }

    /// Multiplies the value of a column by a number.
    #[inline]
    pub fn mul(mut self, col: E::Column, value: impl IntoSqlValue) -> Self {
        self.mul_ops.upsert(col.as_ref(), value.into_sql_value());
        self
    }

    /// Updates the value of a column to a specified value
    /// if the specified value is less than the current value of the column.
    #[inline]
    pub fn min(mut self, col: E::Column, value: impl IntoSqlValue) -> Self {
        self.min_ops.upsert(col.as_ref(), value.into_sql_value());
        self
    }

    /// Updates the value of a column to a specified value
    /// if the specified value is greater than the current value of the column.
    #[inline]
    pub fn max(mut self, col: E::Column, value: impl IntoSqlValue) -> Self {
        self.max_ops.upsert(col.as_ref(), value.into_sql_value());
        self
    }

    /// Builds the model mutation.
    pub fn build(self) -> Mutation {
        let mut updates = self.updates;
        let inc_ops = self.inc_ops;
        let mul_ops = self.mul_ops;
        let min_ops = self.min_ops;
        let max_ops = self.max_ops;
        if !inc_ops.is_empty() {
            updates.upsert("$inc", inc_ops);
        }
        if !mul_ops.is_empty() {
            updates.upsert("$mul", mul_ops);
        }
        if !min_ops.is_empty() {
            updates.upsert("$min", min_ops);
        }
        if !max_ops.is_empty() {
            updates.upsert("$max", max_ops);
        }
        Mutation::new(updates)
    }
}

impl<E: Entity> Default for MutationBuilder<E> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

/// Extension trait for [`Mutation`](crate::model::Mutation).
pub(super) trait MutationExt<DB> {
    /// Formats the updates to generate SQL `SET` expression.
    fn format_updates<M: Schema>(&self) -> String;
}

impl MutationExt<DatabaseDriver> for Mutation {
    fn format_updates<M: Schema>(&self) -> String {
        let updates = self.updates();
        if updates.is_empty() {
            return String::new();
        }

        let fields = self.fields();
        let permissive = fields.is_empty();
        let mut mutations = Vec::new();
        for (key, value) in updates.iter() {
            match key.as_str() {
                "$inc" => {
                    if let Some(update) = value.as_object() {
                        for (key, value) in update.iter() {
                            if permissive || fields.contains(key) {
                                if let Some(col) = M::get_writable_column(key) {
                                    let key = Query::format_field(key);
                                    let value = col.encode_value(Some(value));
                                    let mutation = format!(r#"{key} = {value} + {key}"#);
                                    mutations.push(mutation);
                                }
                            }
                        }
                    }
                }
                "$mul" => {
                    if let Some(update) = value.as_object() {
                        for (key, value) in update.iter() {
                            if permissive || fields.contains(key) {
                                if let Some(col) = M::get_writable_column(key) {
                                    let key = Query::format_field(key);
                                    let value = col.encode_value(Some(value));
                                    let mutation = format!(r#"{key} = {value} * {key}"#);
                                    mutations.push(mutation);
                                }
                            }
                        }
                    }
                }
                "$min" => {
                    if let Some(update) = value.as_object() {
                        for (key, value) in update.iter() {
                            if permissive || fields.contains(key) {
                                if let Some(col) = M::get_writable_column(key) {
                                    let key = Query::format_field(key);
                                    let value = col.encode_value(Some(value));
                                    let mutation = if cfg!(feature = "orm-sqlite") {
                                        format!(r#"{key} = MIN({value}, {key})"#)
                                    } else {
                                        format!(r#"{key} = LEAST({value}, {key})"#)
                                    };
                                    mutations.push(mutation);
                                }
                            }
                        }
                    }
                }
                "$max" => {
                    if let Some(update) = value.as_object() {
                        for (key, value) in update.iter() {
                            if permissive || fields.contains(key) {
                                if let Some(col) = M::get_writable_column(key) {
                                    let key = Query::format_field(key);
                                    let value = col.encode_value(Some(value));
                                    let mutation = if cfg!(feature = "orm-sqlite") {
                                        format!(r#"{key} = MAX({value}, {key})"#)
                                    } else {
                                        format!(r#"{key} = GREATEST({value}, {key})"#)
                                    };
                                    mutations.push(mutation);
                                }
                            }
                        }
                    }
                }
                _ => {
                    if permissive || fields.contains(key) {
                        if let Some(col) = M::get_writable_column(key) {
                            let key = Query::format_field(key);
                            let mutation = if let Some(subquery) =
                                value.as_object().and_then(|m| m.get_str("$subquery"))
                            {
                                format!(r#"{key} = {subquery}"#)
                            } else {
                                let value = col.encode_value(Some(value));
                                format!(r#"{key} = {value}"#)
                            };
                            mutations.push(mutation);
                        }
                    }
                }
            }
        }
        mutations.join(", ")
    }
}