blitz_query_cpp

Форк
0
/
sql_query_resolver.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

8
using namespace blitz_query_cpp;
9
using namespace blitz_query_cpp::sql;
10

11
namespace ranges = std::ranges;
12

13
void sql_query_resolver::process(query_context &context)
14
{
15
    if (context.data.contains(Key))
16
        return;
17

18
    if (!context.document.operation)
19
    {
20
        context.report_error("Operation definition is missing");
21
        return;
22
    }
23

24
    switch (context.document.operation->operation_type)
25
    {
26
    case operation_type_t::Query:
27
        process_query(context);
28
        break;
29
    case operation_type_t::Mutation:
30
        process_mutation(context);
31
        break;
32
    default:
33
        break;
34
    }
35
}
36

37
bool sql_query_resolver::process_field(query_context &, const field &field_decl)
38
{
39
    std::string_view column_name = field_decl.name;
40
    if (auto column_dir = field_decl.find_directive(ColumnDirective))
41
    {
42
        auto column_param = column_dir->parameters.find("name");
43
        if (column_param != column_dir->parameters.end())
44
            column_name = column_param->string_value;
45
    }
46
    if (current_selection_alias.empty())
47
        _query |= column(_schema_name, _table_name, column_name);
48
    else
49
        _query |= column(current_selection_alias, column_name);
50
    return true;
51
}
52

53
bool sql_query_resolver::process_query(query_context &context)
54
{
55
    auto object = get_root_selection_node(context);
56
    if (object == nullptr)
57
        return false;
58

59
    auto field = get_root_selection_field(context, object->name);
60
    if (field == nullptr)
61
        return false;
62

63
    auto type = field->field_type.type;
64
    auto table_schema_dir = type->find_directive(TableDirective);
65

66
    // not SQL type, handle else where
67
    if (!table_schema_dir)
68
        return true;
69

70
    auto table_param = table_schema_dir->parameters.find("table");
71
    if (table_param != table_schema_dir->parameters.end())
72
        _table_name = table_param->string_value;
73
    if (_table_name.empty())
74
        _table_name = object->name;
75

76
    auto schema_param = table_schema_dir->parameters.find("schema");
77
    if (schema_param != table_schema_dir->parameters.end())
78
        _schema_name = schema_param->string_value;
79
    if (_schema_name.empty())
80
        _schema_name = DefaultSchama;
81

82
    _query = select();
83
    auto selection_set = object->selection_set;
84

85
    current_selection_alias = next_alias_name();
86
    context.data[type->name] = current_selection_alias;
87

88
    for (auto field_node : selection_set->children)
89
    {
90
        auto field_decl = type->fields.find(field_node->name);
91
        if (field_decl == type->fields.end())
92
            return context.report_error("Field '{}' is not found in object '{}'", field_node->name, object->name);
93
        if (!process_field(context, *field_decl))
94
            return false;
95
    }
96

97
    for (auto &field_decl : type->fields)
98
    {
99
        if (always_projected(field_decl))
100
        {
101
            if (!process_field(context, field_decl))
102
                return false;
103
        }
104
    }
105

106
    _query |= from(alias(table(_schema_name, _table_name), current_selection_alias));
107

108
    context.objects["sql_expr"] = std::move(_query);
109

110
    return true;
111
}
112

113
bool sql_query_resolver::process_mutation(query_context &)
114
{
115
    return true;
116
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.