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::*;
use crate::{query_ast::*, query_graph::QueryGraph, ParsedField};
use query_structure::{Model, PrismaValue};
use std::{collections::HashMap, convert::TryInto};

pub(crate) fn execute_raw(graph: &mut QueryGraph, field: ParsedField<'_>) -> QueryGraphBuilderResult<()> {
    let raw_query = Query::Write(WriteQuery::ExecuteRaw(raw_query(None, None, field)?));

    graph.create_node(raw_query);
    Ok(())
}

pub(crate) fn query_raw(
    graph: &mut QueryGraph,
    model: Option<Model>,
    query_type: Option<String>,
    field: ParsedField<'_>,
) -> QueryGraphBuilderResult<()> {
    let raw_query = Query::Write(WriteQuery::QueryRaw(raw_query(model, query_type, field)?));

    graph.create_node(raw_query);
    Ok(())
}

fn raw_query(
    model: Option<Model>,
    query_type: Option<String>,
    field: ParsedField<'_>,
) -> QueryGraphBuilderResult<RawQuery> {
    let inputs = field
        .arguments
        .into_iter()
        .map(|arg| {
            let parsed_arg_value: PrismaValue = arg.value.try_into()?;

            Ok((arg.name, parsed_arg_value))
        })
        .collect::<QueryGraphBuilderResult<HashMap<_, _>>>()?;

    Ok(RawQuery {
        model,
        inputs,
        query_type,
    })
}