blitz_query_cpp
116 строк · 3.4 Кб
1#include "processing/sql_query_resolver.hpp"
2#include "data/sql/sql_expr.hpp"
3#include "struct/context_operations.hpp"
4
5#include <algorithm>
6#include <ranges>
7
8using namespace blitz_query_cpp;
9using namespace blitz_query_cpp::sql;
10
11namespace ranges = std::ranges;
12
13void sql_query_resolver::process(query_context &context)
14{
15if (context.data.contains(Key))
16return;
17
18if (!context.document.operation)
19{
20context.report_error("Operation definition is missing");
21return;
22}
23
24switch (context.document.operation->operation_type)
25{
26case operation_type_t::Query:
27process_query(context);
28break;
29case operation_type_t::Mutation:
30process_mutation(context);
31break;
32default:
33break;
34}
35}
36
37bool sql_query_resolver::process_field(query_context &, const field &field_decl)
38{
39std::string_view column_name = field_decl.name;
40if (auto column_dir = field_decl.find_directive(ColumnDirective))
41{
42auto column_param = column_dir->parameters.find("name");
43if (column_param != column_dir->parameters.end())
44column_name = column_param->string_value;
45}
46if (current_selection_alias.empty())
47_query |= column(_schema_name, _table_name, column_name);
48else
49_query |= column(current_selection_alias, column_name);
50return true;
51}
52
53bool sql_query_resolver::process_query(query_context &context)
54{
55auto object = get_root_selection_node(context);
56if (object == nullptr)
57return false;
58
59auto field = get_root_selection_field(context, object->name);
60if (field == nullptr)
61return false;
62
63auto type = field->field_type.type;
64auto table_schema_dir = type->find_directive(TableDirective);
65
66// not SQL type, handle else where
67if (!table_schema_dir)
68return true;
69
70auto table_param = table_schema_dir->parameters.find("table");
71if (table_param != table_schema_dir->parameters.end())
72_table_name = table_param->string_value;
73if (_table_name.empty())
74_table_name = object->name;
75
76auto schema_param = table_schema_dir->parameters.find("schema");
77if (schema_param != table_schema_dir->parameters.end())
78_schema_name = schema_param->string_value;
79if (_schema_name.empty())
80_schema_name = DefaultSchama;
81
82_query = select();
83auto selection_set = object->selection_set;
84
85current_selection_alias = next_alias_name();
86context.data[type->name] = current_selection_alias;
87
88for (auto field_node : selection_set->children)
89{
90auto field_decl = type->fields.find(field_node->name);
91if (field_decl == type->fields.end())
92return context.report_error("Field '{}' is not found in object '{}'", field_node->name, object->name);
93if (!process_field(context, *field_decl))
94return false;
95}
96
97for (auto &field_decl : type->fields)
98{
99if (always_projected(field_decl))
100{
101if (!process_field(context, field_decl))
102return false;
103}
104}
105
106_query |= from(alias(table(_schema_name, _table_name), current_selection_alias));
107
108context.objects["sql_expr"] = std::move(_query);
109
110return true;
111}
112
113bool sql_query_resolver::process_mutation(query_context &)
114{
115return true;
116}