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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
use crate::error::{ConnectorError, ErrorKind};
use indexmap::{map::Keys, IndexMap};
use query_structure::{
    CompositeFieldRef, Field, Filter, Model, ModelProjection, PrismaValue, ScalarFieldRef, SelectedField,
    SelectionResult,
};
use std::{borrow::Borrow, convert::TryInto, ops::Deref};

/// WriteArgs represent data to be written to an underlying data source.
#[derive(Debug, PartialEq, Clone)]
pub struct WriteArgs {
    pub request_now: PrismaValue,
    pub args: IndexMap<DatasourceFieldName, WriteOperation>,
}

/// Wrapper struct to force a bit of a reflection whether or not the string passed
/// to the write arguments is the data source field name, not the model field name.
/// Also helps to avoid errors with convenient from-field conversions.
#[derive(Debug, PartialEq, Clone, Hash, Eq)]
pub struct DatasourceFieldName(pub String);

impl Deref for DatasourceFieldName {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Borrow<str> for DatasourceFieldName {
    fn borrow(&self) -> &str {
        &self.0
    }
}

impl From<&ScalarFieldRef> for DatasourceFieldName {
    fn from(sf: &ScalarFieldRef) -> Self {
        DatasourceFieldName(sf.db_name().to_owned())
    }
}

impl From<&CompositeFieldRef> for DatasourceFieldName {
    fn from(cf: &CompositeFieldRef) -> Self {
        DatasourceFieldName(cf.db_name().to_owned())
    }
}

/// A WriteExpression allows to express more complex operations on how the data is written,
/// like field or inter-field arithmetic.
#[derive(Debug, PartialEq, Clone)]
pub enum WriteOperation {
    Scalar(ScalarWriteOperation),
    Composite(CompositeWriteOperation),
}

impl WriteOperation {
    pub fn scalar_set(pv: PrismaValue) -> Self {
        Self::Scalar(ScalarWriteOperation::Set(pv))
    }

    pub fn scalar_unset(should_unset: bool) -> Self {
        Self::Scalar(ScalarWriteOperation::Unset(should_unset))
    }

    pub fn scalar_add(pv: PrismaValue) -> Self {
        Self::Scalar(ScalarWriteOperation::Add(pv))
    }

    pub fn scalar_substract(pv: PrismaValue) -> Self {
        Self::Scalar(ScalarWriteOperation::Substract(pv))
    }

    pub fn scalar_multiply(pv: PrismaValue) -> Self {
        Self::Scalar(ScalarWriteOperation::Multiply(pv))
    }

    pub fn scalar_divide(pv: PrismaValue) -> Self {
        Self::Scalar(ScalarWriteOperation::Divide(pv))
    }

    pub fn composite_set(pv: PrismaValue) -> Self {
        Self::Composite(CompositeWriteOperation::Set(pv))
    }

    pub fn composite_unset(should_unset: bool) -> Self {
        Self::Composite(CompositeWriteOperation::Unset(should_unset))
    }

    pub fn composite_update(writes: Vec<(DatasourceFieldName, WriteOperation)>) -> Self {
        Self::Composite(CompositeWriteOperation::Update(NestedWrite { writes }))
    }

    pub fn composite_push(pv: PrismaValue) -> Self {
        Self::Composite(CompositeWriteOperation::Push(pv))
    }

    pub fn composite_upsert(set: CompositeWriteOperation, update: CompositeWriteOperation) -> Self {
        Self::Composite(CompositeWriteOperation::Upsert {
            set: Box::new(set),
            update: Box::new(update),
        })
    }

    pub fn composite_update_many(filter: Filter, update: CompositeWriteOperation) -> Self {
        Self::Composite(CompositeWriteOperation::UpdateMany {
            filter,
            update: Box::new(update),
        })
    }

    pub fn composite_delete_many(filter: Filter) -> Self {
        Self::Composite(CompositeWriteOperation::DeleteMany { filter })
    }

    pub fn as_scalar(&self) -> Option<&ScalarWriteOperation> {
        if let Self::Scalar(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn as_composite(&self) -> Option<&CompositeWriteOperation> {
        if let Self::Composite(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn try_into_scalar(self) -> Option<ScalarWriteOperation> {
        if let Self::Scalar(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn try_into_composite(self) -> Option<CompositeWriteOperation> {
        if let Self::Composite(v) = self {
            Some(v)
        } else {
            None
        }
    }
}

#[derive(Debug, PartialEq, Clone)]
pub enum ScalarWriteOperation {
    /// Reference to another field on the same model (unused at the moment).
    Field(DatasourceFieldName),

    /// Write plain value to field.
    Set(PrismaValue),

    /// Unsets a field (only for MongoDB for now)
    Unset(bool),

    /// Add value to field.
    Add(PrismaValue),

    /// Substract value from field
    Substract(PrismaValue),

    /// Multiply field by value.
    Multiply(PrismaValue),

    /// Divide field by value.
    Divide(PrismaValue),
}

#[derive(Debug, PartialEq, Clone)]
pub enum CompositeWriteOperation {
    Set(PrismaValue),
    Push(PrismaValue),
    Unset(bool),
    Update(NestedWrite),
    Upsert {
        set: Box<CompositeWriteOperation>,
        update: Box<CompositeWriteOperation>,
    },
    UpdateMany {
        filter: Filter,
        update: Box<CompositeWriteOperation>,
    },
    DeleteMany {
        filter: Filter,
    },
}

#[derive(Debug, PartialEq, Clone)]
pub struct NestedWrite {
    pub writes: Vec<(DatasourceFieldName, WriteOperation)>,
}

#[derive(Debug, Clone, Default)]
pub struct FieldPath {
    pub alias: Option<String>,
    pub path: Vec<String>,
}

impl FieldPath {
    pub fn new_from_segment(field: &Field) -> Self {
        let mut path = Self::default();
        path.add_segment(field);

        path
    }

    pub fn new_from_alias(alias: impl Into<String>) -> Self {
        Self {
            alias: Some(alias.into()),
            path: vec![],
        }
    }

    pub fn add_segment(&mut self, field: &Field) {
        self.path.push(field.db_name().to_owned());
    }

    /// Keep only the last element of the path
    pub fn keep_last(&mut self) {
        self.path.drain(0..self.path.len() - 1);
    }

    pub fn take(&mut self, n: usize) {
        self.path = self.path[0..n].to_vec();
    }

    pub fn path(&self, include_alias: bool) -> String {
        let rendered_path = self.path.join(".");

        if !include_alias {
            return rendered_path;
        }

        if let Some(alias) = &self.alias {
            if self.path.is_empty() {
                alias.to_owned()
            } else {
                format!("${alias}.{rendered_path}")
            }
        } else {
            rendered_path
        }
    }

    pub fn dollar_path(&self, include_alias: bool) -> String {
        format!("${}", self.path(include_alias))
    }

    pub fn identifier(&self) -> String {
        let rendered_path = self.path.join("_");

        if let Some(alias) = &self.alias {
            if self.path.is_empty() {
                alias.to_owned()
            } else {
                format!("{alias}_{rendered_path}")
            }
        } else {
            rendered_path
        }
    }
}

impl NestedWrite {
    /// Unfolds nested writes into a flat list of `WriteOperation`s.
    ///
    /// Given the following `NestedWrite`:
    /// ```text
    /// Vec [(
    ///       "field_a",
    ///       WriteOperation::Composite(Update(Vec[("field_b", WriteOperation::Composite(Set("3")))]))
    ///     )]
    /// ```
    /// `unfold` will roughly return:
    /// ```text
    /// Vec[(Set("3"), Field("field_b"), "field_a.field_b")]
    /// ```
    /// where:
    ///  - `Set("3")` is the write operation to execute
    ///  - `Field("field_b")` is the field on which to execute the write operation
    /// - `"field_a.field_b"` is the path for MongoDB to access the nested field
    pub fn unfold(self, field: &Field, field_path: FieldPath) -> Vec<(WriteOperation, Field, FieldPath)> {
        self.unfold_internal(field.clone(), field_path)
    }

    fn unfold_internal(self, field: Field, field_path: FieldPath) -> Vec<(WriteOperation, Field, FieldPath)> {
        let mut nested_writes: Vec<(WriteOperation, Field, FieldPath)> = vec![];

        for (DatasourceFieldName(db_name), write) in self.writes {
            let nested_ct = field.as_composite().unwrap().typ();
            let nested_field = nested_ct.find_field_by_db_name(&db_name).unwrap();
            let mut new_path = field_path.clone();

            new_path.add_segment(&nested_field);

            match write {
                WriteOperation::Composite(CompositeWriteOperation::Update(nested_write)) => {
                    nested_writes.extend(nested_write.unfold_internal(nested_field.clone(), new_path));
                }
                _ => {
                    nested_writes.push((write, nested_field.clone(), new_path));
                }
            }
        }

        nested_writes
    }
}

impl From<(&ScalarFieldRef, PrismaValue)> for WriteOperation {
    fn from((_, pv): (&ScalarFieldRef, PrismaValue)) -> Self {
        WriteOperation::scalar_set(pv)
    }
}

impl From<(&CompositeFieldRef, PrismaValue)> for WriteOperation {
    fn from((_, pv): (&CompositeFieldRef, PrismaValue)) -> Self {
        WriteOperation::composite_set(pv)
    }
}

impl From<(&SelectedField, PrismaValue)> for WriteOperation {
    fn from((selection, pv): (&SelectedField, PrismaValue)) -> Self {
        match selection {
            SelectedField::Scalar(sf) => (sf, pv).into(),
            SelectedField::Composite(cs) => (&cs.field, pv).into(),
            SelectedField::Relation(_) => todo!(),
            SelectedField::Virtual(_) => todo!(),
        }
    }
}

impl TryInto<PrismaValue> for WriteOperation {
    type Error = ConnectorError;

    fn try_into(self) -> Result<PrismaValue, Self::Error> {
        match self {
            WriteOperation::Scalar(ScalarWriteOperation::Set(pv)) => Ok(pv),
            WriteOperation::Composite(CompositeWriteOperation::Set(pv)) => Ok(pv),
            x => Err(ConnectorError::from_kind(ErrorKind::InternalConversionError(format!(
                "Unable to convert write expression {x:?} into prisma value."
            )))),
        }
    }
}

impl WriteArgs {
    pub fn new(args: IndexMap<DatasourceFieldName, WriteOperation>, request_now: PrismaValue) -> Self {
        Self { args, request_now }
    }

    pub fn new_empty(request_now: PrismaValue) -> Self {
        Self {
            args: Default::default(),
            request_now,
        }
    }

    pub fn insert<T, V>(&mut self, key: T, arg: V)
    where
        T: Into<DatasourceFieldName>,
        V: Into<WriteOperation>,
    {
        self.args.insert(key.into(), arg.into());
    }

    pub fn has_arg_for(&self, field: &str) -> bool {
        self.args.contains_key(field)
    }

    pub fn get_field_value(&self, field: &str) -> Option<&WriteOperation> {
        self.args.get(field)
    }

    pub fn take_field_value(&mut self, field: &str) -> Option<WriteOperation> {
        self.args.remove(field)
    }

    pub fn keys(&self) -> Keys<DatasourceFieldName, WriteOperation> {
        self.args.keys()
    }

    pub fn is_empty(&self) -> bool {
        self.args.is_empty()
    }

    pub fn len(&self) -> usize {
        self.args.len()
    }

    // @updatedAt
    pub fn add_datetimes(&mut self, model: &Model) {
        let updated_at_fields = model.fields().updated_at();
        let value = &self.request_now;

        for f in updated_at_fields {
            if self.args.get(f.db_name()).is_none() {
                self.args.insert((&f).into(), WriteOperation::scalar_set(value.clone()));
            }
        }
    }

    pub fn update_datetimes(&mut self, model: &Model) {
        if !self.args.is_empty() {
            self.add_datetimes(model)
        }
    }

    pub fn as_selection_result(&self, model_projection: ModelProjection) -> Option<SelectionResult> {
        let pairs: Vec<_> = model_projection
            .scalar_fields()
            .map(|field| {
                let val: PrismaValue = match self.get_field_value(field.db_name()) {
                    Some(val) => {
                        // Important: This causes write expressions that are not plain values to produce
                        // null values. At the moment, this function is used to extract an ID for
                        // create record calls, which only operate on plain values _for now_. As soon
                        // as that changes we need to revisit the whole ID extraction on create / update topic.
                        let p: Option<PrismaValue> = val.clone().try_into().ok();
                        match p {
                            Some(p) => p,
                            None => PrismaValue::Null,
                        }
                    }
                    None => PrismaValue::Null,
                };

                (field, val)
            })
            .collect();

        Some(pairs.into())
    }
}

/// Picks all arguments out of `args` that are updating a value for a field
/// contained in `projection`, as those need to be merged into the records later on.
pub fn pick_args(projection: &ModelProjection, args: &WriteArgs) -> WriteArgs {
    let pairs = projection
        .scalar_fields()
        .filter_map(|field| {
            args.get_field_value(field.db_name())
                .map(|v| (DatasourceFieldName::from(&field), v.clone()))
        })
        .collect();

    WriteArgs::new(pairs, args.request_now.clone())
}

/// Merges the incoming write argument values into the given, already loaded, ids. Overwrites existing values.
pub fn merge_write_args(loaded_ids: Vec<SelectionResult>, incoming_args: WriteArgs) -> Vec<SelectionResult> {
    if loaded_ids.is_empty() || incoming_args.is_empty() {
        return loaded_ids;
    }

    // Contains all positions that need to be updated with the given expression.
    let positions: IndexMap<usize, &WriteOperation> = loaded_ids
        .first()
        .unwrap()
        .pairs
        .iter()
        .enumerate()
        .filter_map(|(i, (selection, _))| incoming_args.get_field_value(&selection.db_name()).map(|val| (i, val)))
        .collect();

    loaded_ids
        .into_iter()
        .map(|mut id| {
            for (position, write_op) in positions.iter() {
                let current_val = id.pairs[*position].1.clone();
                id.pairs[*position].1 = apply_expression(current_val, (*write_op.as_scalar().unwrap()).clone());
            }

            id
        })
        .collect()
}

pub fn apply_expression(val: PrismaValue, scalar_write: ScalarWriteOperation) -> PrismaValue {
    match scalar_write {
        ScalarWriteOperation::Field(_) => unimplemented!(),
        ScalarWriteOperation::Set(pv) => pv,
        ScalarWriteOperation::Add(rhs) => val + rhs,
        ScalarWriteOperation::Substract(rhs) => val - rhs,
        ScalarWriteOperation::Multiply(rhs) => val * rhs,
        ScalarWriteOperation::Divide(rhs) => val / rhs,
        ScalarWriteOperation::Unset(_) => unimplemented!(),
    }
}