@@ -12,7 +12,7 @@ use query_engine_metadata::metadata::database;
12
12
use query_engine_sql:: sql;
13
13
use std:: collections:: BTreeMap ;
14
14
15
- use super :: common:: { self , default_constraint , CheckArgument } ;
15
+ use super :: common:: { self , get_nullable_predicate_argument , CheckArgument } ;
16
16
17
17
/// A representation of an auto-generated update mutation.
18
18
///
@@ -109,9 +109,9 @@ pub fn translate(
109
109
UpdateMutation :: UpdateByKey ( mutation) => {
110
110
let object = arguments
111
111
. get ( & mutation. update_columns_argument_name )
112
- . ok_or ( Error :: ArgumentNotFound (
113
- mutation. update_columns_argument_name . clone ( ) ,
114
- ) ) ?;
112
+ . ok_or_else ( || {
113
+ Error :: ArgumentNotFound ( mutation. update_columns_argument_name . clone ( ) )
114
+ } ) ?;
115
115
116
116
let set = parse_update_columns ( env, state, mutation, object) ?;
117
117
@@ -156,37 +156,16 @@ pub fn translate(
156
156
current_table : table_name_and_reference,
157
157
} ;
158
158
159
- // Set default constrainst
160
- let default_constraint = default_constraint ( ) ;
161
-
162
159
// Build the `pre_constraint` argument boolean expression.
163
- let pre_predicate_json = arguments
164
- . get ( & mutation. pre_check . argument_name )
165
- . unwrap_or ( & default_constraint) ;
166
-
167
- let pre_predicate: models:: Expression =
168
- serde_json:: from_value ( pre_predicate_json. clone ( ) ) . map_err ( |_| {
169
- Error :: UnexpectedStructure ( format ! (
170
- "Argument '{}' should have an ndc-spec Expression structure" ,
171
- mutation. pre_check. argument_name. clone( )
172
- ) )
173
- } ) ?;
160
+ let pre_predicate =
161
+ get_nullable_predicate_argument ( & mutation. pre_check . argument_name , arguments) ?;
174
162
175
163
let pre_predicate_expression =
176
164
filtering:: translate ( env, state, & root_and_current_tables, & pre_predicate) ?;
177
165
178
166
// Build the `post_constraint` argument boolean expression.
179
- let post_predicate_json = arguments
180
- . get ( & mutation. post_check . argument_name )
181
- . unwrap_or ( & default_constraint) ;
182
-
183
- let post_predicate: models:: Expression =
184
- serde_json:: from_value ( post_predicate_json. clone ( ) ) . map_err ( |_| {
185
- Error :: UnexpectedStructure ( format ! (
186
- "Argument '{}' should have an ndc-spec Expression structure" ,
187
- mutation. post_check. argument_name. clone( )
188
- ) )
189
- } ) ?;
167
+ let post_predicate =
168
+ get_nullable_predicate_argument ( & mutation. post_check . argument_name , arguments) ?;
190
169
191
170
let post_predicate_expression =
192
171
filtering:: translate ( env, state, & root_and_current_tables, & post_predicate) ?;
@@ -234,17 +213,18 @@ fn parse_update_columns(
234
213
// For each field, look up the column name in the table
235
214
// and update it and the value into the map.
236
215
for ( name, value) in object {
237
- let column_info = mutation. table_columns . get ( name. as_str ( ) ) . ok_or (
216
+ let column_info = mutation. table_columns . get ( name. as_str ( ) ) . ok_or_else ( || {
238
217
Error :: ColumnNotFoundInCollection (
239
218
name. clone ( ) . into ( ) ,
240
219
mutation. collection_name . clone ( ) ,
241
- ) ,
242
- ) ?;
220
+ )
221
+ } ) ?;
243
222
244
- columns_to_values. insert (
245
- sql:: ast:: ColumnName ( column_info. name . clone ( ) ) ,
246
- parse_update_column ( env, state, & name. as_str ( ) . into ( ) , column_info, value) ?,
247
- ) ;
223
+ if let Some ( value) =
224
+ parse_update_column ( env, state, & name. as_str ( ) . into ( ) , column_info, value) ?
225
+ {
226
+ columns_to_values. insert ( sql:: ast:: ColumnName ( column_info. name . clone ( ) ) , value) ;
227
+ }
248
228
}
249
229
Ok ( ( ) )
250
230
}
@@ -275,7 +255,7 @@ fn parse_update_column(
275
255
column_name : & models:: FieldName ,
276
256
column_info : & metadata:: database:: ColumnInfo ,
277
257
object : & serde_json:: Value ,
278
- ) -> Result < sql:: ast:: MutationValueExpression , Error > {
258
+ ) -> Result < Option < sql:: ast:: MutationValueExpression > , Error > {
279
259
match object {
280
260
serde_json:: Value :: Object ( object) => {
281
261
let vec = object. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
@@ -289,9 +269,9 @@ fn parse_update_column(
289
269
}
290
270
// _set operation.
291
271
if * operation == "_set" {
292
- Ok ( sql:: ast:: MutationValueExpression :: Expression (
272
+ Ok ( Some ( sql:: ast:: MutationValueExpression :: Expression (
293
273
values:: translate ( env, state, value, & column_info. r#type ) ?,
294
- ) )
274
+ ) ) )
295
275
}
296
276
// Operation is not supported.
297
277
else {
@@ -304,6 +284,7 @@ fn parse_update_column(
304
284
}
305
285
}
306
286
}
287
+ serde_json:: Value :: Null => Ok ( None ) ,
307
288
// Unexpected structures.
308
289
serde_json:: Value :: Array ( _) => Err ( Error :: UnexpectedStructure ( format ! (
309
290
"array structure in update column '{column_name}' argument. Expecting an object." ,
0 commit comments