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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use super::*;
use crate::query_graph_builder::write::write_args_parser::*;
use crate::{
    query_ast::*,
    query_graph::{Node, NodeRef, QueryGraph, QueryGraphDependency},
    ArgumentListLookup, ParsedField, ParsedInputMap,
};
use psl::datamodel_connector::ConnectorCapability;
use query_structure::{Filter, IntoFilter, Model};
use schema::{constants::args, QuerySchema};
use std::convert::TryInto;

/// Creates an update record query and adds it to the query graph, together with it's nested queries and companion read query.
pub(crate) fn update_record(
    graph: &mut QueryGraph,
    query_schema: &QuerySchema,
    model: Model,
    mut field: ParsedField<'_>,
) -> QueryGraphBuilderResult<()> {
    // "where"
    let where_arg: ParsedInputMap<'_> = field.arguments.lookup(args::WHERE).unwrap().value.try_into()?;
    let filter = extract_unique_filter(where_arg, &model)?;

    // "data"
    let data_argument = field.arguments.lookup(args::DATA).unwrap();
    let data_map: ParsedInputMap<'_> = data_argument.value.try_into()?;

    let can_use_atomic_update = can_use_atomic_update(query_schema, &model, &data_map, &field);

    let update_node = update_record_node(
        graph,
        query_schema,
        filter.clone(),
        model.clone(),
        data_map,
        Some(&field),
    )?;

    if query_schema.relation_mode().is_prisma() {
        let read_parent_node = graph.create_node(utils::read_ids_infallible(
            model.clone(),
            model.primary_identifier(),
            filter,
        ));

        utils::insert_emulated_on_update(graph, query_schema, &model, &read_parent_node, &update_node)?;

        graph.create_edge(
            &read_parent_node,
            &update_node,
            QueryGraphDependency::ProjectedDataDependency(
                model.primary_identifier(),
                Box::new(move |mut update_node, parent_ids| {
                    if let Node::Query(Query::Write(WriteQuery::UpdateRecord(ref mut ur))) = update_node {
                        ur.set_record_filter(parent_ids.into());
                    }

                    Ok(update_node)
                }),
            ),
        )?;
    }

    // If the update can be done in a single operation (which includes getting the result back),
    // then make that node the result node.
    if can_use_atomic_update {
        graph.add_result_node(&update_node);

        let check_node = graph.create_node(Node::Empty);

        graph.create_edge(
            &update_node,
            &check_node,
            QueryGraphDependency::ProjectedDataDependency(
                model.primary_identifier(),
                Box::new(move |read_node, parent_ids| {
                    if parent_ids.is_empty() {
                        return Err(QueryGraphBuilderError::RecordNotFound(
                            "Record to update not found.".to_string(),
                        ));
                    }

                    Ok(read_node)
                }),
            ),
        )?;
    // Otherwise, perform the update, and then do an additional read.
    } else {
        graph.flag_transactional();

        let read_query = read::find_unique(field, model.clone(), query_schema)?;
        let read_node = graph.create_node(Query::Read(read_query));

        graph.add_result_node(&read_node);

        graph.create_edge(
            &update_node,
            &read_node,
            QueryGraphDependency::ProjectedDataDependency(
                model.primary_identifier(),
                Box::new(move |mut read_node, mut parent_ids| {
                    let parent_id = match parent_ids.pop() {
                        Some(pid) => Ok(pid),
                        None => Err(QueryGraphBuilderError::RecordNotFound(
                            "Record to update not found.".to_string(),
                        )),
                    }?;

                    if let Node::Query(Query::Read(ReadQuery::RecordQuery(ref mut rq))) = read_node {
                        rq.add_filter(parent_id.filter());
                    };

                    Ok(read_node)
                }),
            ),
        )?;
    }

    Ok(())
}

/// Creates an update many record query and adds it to the query graph.
pub fn update_many_records(
    graph: &mut QueryGraph,
    query_schema: &QuerySchema,
    model: Model,
    mut field: ParsedField<'_>,
) -> QueryGraphBuilderResult<()> {
    graph.flag_transactional();

    // "where"
    let filter = match field.arguments.lookup(args::WHERE) {
        Some(where_arg) => extract_filter(where_arg.value.try_into()?, &model)?,
        None => Filter::empty(),
    };

    // "data"
    let data_argument = field.arguments.lookup(args::DATA).unwrap();
    let data_map: ParsedInputMap<'_> = data_argument.value.try_into()?;

    if query_schema.relation_mode().uses_foreign_keys() {
        update_many_record_node(graph, query_schema, filter, model, data_map)?;
    } else {
        let pre_read_node = graph.create_node(utils::read_ids_infallible(
            model.clone(),
            model.primary_identifier(),
            filter,
        ));
        let update_many_node = update_many_record_node(graph, query_schema, Filter::empty(), model.clone(), data_map)?;

        utils::insert_emulated_on_update(graph, query_schema, &model, &pre_read_node, &update_many_node)?;

        graph.create_edge(
            &pre_read_node,
            &update_many_node,
            QueryGraphDependency::ProjectedDataDependency(
                model.primary_identifier(),
                Box::new(move |mut update_node, parent_ids| {
                    if let Node::Query(Query::Write(WriteQuery::UpdateManyRecords(ref mut ur))) = update_node {
                        ur.record_filter = parent_ids.into();
                    }

                    Ok(update_node)
                }),
            ),
        )?;
    }

    Ok(())
}

/// Creates an update record write node and adds it to the query graph.
pub fn update_record_node<T: Clone>(
    graph: &mut QueryGraph,
    query_schema: &QuerySchema,
    filter: T,
    model: Model,
    data_map: ParsedInputMap<'_>,
    field: Option<&ParsedField<'_>>,
) -> QueryGraphBuilderResult<NodeRef>
where
    T: Into<Filter>,
{
    let update_args = WriteArgsParser::from(&model, data_map)?;
    let mut args = update_args.args;

    args.update_datetimes(&model);

    let filter: Filter = filter.into();

    // If the connector can use `RETURNING`, always use it as it may:
    // 1. Save a SELECT statement
    // 2. Avoid from computing the ids in memory if they are updated. See `update_one_without_selection` function.
    let update_parent = if query_schema.has_capability(ConnectorCapability::UpdateReturning) {
        // If there's a selected field, fulfill the scalar selection set.
        if let Some(field) = field.cloned() {
            let nested_fields = field.nested_fields.unwrap().fields;
            let selection_order: Vec<String> = read::utils::collect_selection_order(&nested_fields);
            let selected_fields = read::utils::collect_selected_scalars(&nested_fields, &model);

            Query::Write(WriteQuery::UpdateRecord(UpdateRecord::WithSelection(
                UpdateRecordWithSelection {
                    name: field.name,
                    model: model.clone(),
                    record_filter: filter.into(),
                    args,
                    selected_fields,
                    selection_order,
                },
            )))
        // Otherwise, fallback to the primary identifier, that will be used to fulfill other nested operations requirements
        } else {
            let selected_fields = model.primary_identifier();
            let selection_order = selected_fields.db_names().collect();

            Query::Write(WriteQuery::UpdateRecord(UpdateRecord::WithSelection(
                UpdateRecordWithSelection {
                    name: String::new(), // This node will not be serialized so we don't need a name.
                    model: model.clone(),
                    record_filter: filter.into(),
                    args,
                    selected_fields,
                    selection_order,
                },
            )))
        }
    } else {
        Query::Write(WriteQuery::UpdateRecord(UpdateRecord::WithoutSelection(
            UpdateRecordWithoutSelection {
                model: model.clone(),
                record_filter: filter.into(),
                args,
            },
        )))
    };

    let update_node = graph.create_node(update_parent);

    for (relation_field, data_map) in update_args.nested {
        nested::connect_nested_query(graph, query_schema, update_node, relation_field, data_map)?;
    }

    Ok(update_node)
}

/// Creates an update many record query node and adds it to the query graph.
pub fn update_many_record_node<T>(
    graph: &mut QueryGraph,
    query_schema: &QuerySchema,
    filter: T,
    model: Model,
    data_map: ParsedInputMap<'_>,
) -> QueryGraphBuilderResult<NodeRef>
where
    T: Into<Filter>,
{
    graph.flag_transactional();

    let filter = filter.into();
    let record_filter = filter.into();
    let update_args = WriteArgsParser::from(&model, data_map)?;
    let mut args = update_args.args;

    args.update_datetimes(&model);

    let update_many = UpdateManyRecords {
        model,
        record_filter,
        args,
    };

    let update_many_node = graph.create_node(Query::Write(WriteQuery::UpdateManyRecords(update_many)));

    for (relation_field, data_map) in update_args.nested {
        nested::connect_nested_query(graph, query_schema, update_many_node, relation_field, data_map)?;
    }

    Ok(update_many_node)
}

/// An atomic update is an update performed in a single operation.
/// It uses `UPDATE ... RETURNING` when the connector supports it.
///
/// We only perform such update when:
/// 1. The connector supports returning updated values
/// 2. The selection set contains no relation
fn can_use_atomic_update(
    query_schema: &QuerySchema,
    model: &Model,
    data_map: &ParsedInputMap<'_>,
    field: &ParsedField<'_>,
) -> bool {
    // If the connector does not support RETURNING at all
    if !query_schema.has_capability(ConnectorCapability::UpdateReturning) {
        return false;
    }

    // If the operation has nested selection sets
    if field.has_nested_selection() {
        return false;
    }

    // If the operation has nested operations
    if WriteArgsParser::has_nested_operation(model, data_map) {
        return false;
    }

    true
}