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
use super::ScalarFieldExt;
use crate::context::Context;
use quaint::Value;
use query_structure::{PrismaValue, SelectedField, SelectionResult};

pub(crate) trait SelectionResultExt {
    fn misses_autogen_value(&self) -> bool;
    fn db_values<'a>(&self, ctx: &Context<'_>) -> Vec<Value<'a>>;

    fn add_autogen_value<V>(&mut self, value: V) -> bool
    where
        V: Into<PrismaValue>;
}

impl SelectionResultExt for SelectionResult {
    fn misses_autogen_value(&self) -> bool {
        self.pairs.iter().any(|p| p.1.is_null())
    }

    fn add_autogen_value<V>(&mut self, value: V) -> bool
    where
        V: Into<PrismaValue>,
    {
        for pair in self.pairs.iter_mut() {
            if pair.1.is_null() {
                pair.1 = value.into();
                return true;
            }
        }

        false
    }

    fn db_values<'a>(&self, ctx: &Context<'_>) -> Vec<Value<'a>> {
        self.pairs
            .iter()
            .filter_map(|(selection, v)| match selection {
                SelectedField::Scalar(sf) => Some(sf.value(v.clone(), ctx)),
                SelectedField::Composite(_) => None,
                SelectedField::Relation(_) => None,
                SelectedField::Virtual(_) => None,
            })
            .collect()
    }
}