Repository navigation
Expand file tree
/
Copy pathsql_dialect_helper_probe.cpp
More file actions
69 lines (67 loc) · 2.94 KB
/
Copy pathsql_dialect_helper_probe.cpp
File metadata and controls
69 lines (67 loc) · 2.94 KB
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
#include "fixture_sql.hpp"
#include <iostream>
static Json evaluate(Memory owner, const Json& input) {
auto output = detail::model_json(owner);
auto mode = text(field(input, "mode"));
if (mode == "in_direct") {
FixtureDialect dialect;
dialect.numbered = std::get<bool>(field(input, "numbered").data);
auto descriptor = entity(owner, field(input, "entity"));
auto left = expression(owner, field(input, "left"));
auto right = expression(owner, field(input, "right"));
auto op = static_cast<BinaryOp>(integer(field(input, "op")));
SqlBindings params(owner);
try {
detail::model_put(
output, "sql", dialect.compile_in(owner, descriptor, left, op, right, params));
} catch (const SqlCompileError& error) {
detail::model_put(output, "error", error.to_string(owner));
} catch (const std::logic_error& error) {
if (std::string_view(error.what()) != "Invalid direct IN operator") {
throw;
}
detail::model_put(output, "panic", "operator");
}
Json::Array values(owner), policies(owner);
for (const auto& item : params.values()) {
values.emplace_back(item.to_json_value(owner));
}
auto log = params.log_context(owner, dialect.kind());
for (auto policy : log.parameter_policies) {
policies.emplace_back(static_cast<unsigned>(policy));
}
detail::model_put(output, "values", values);
detail::model_put(output, "policies", policies);
} else if (mode == "quote") {
auto code = static_cast<char32_t>(integer(field(input, "quote_code")));
detail::model_put(
output,
"quoted",
quote_identifier_if_needed(owner, text(field(input, "identifier")), code));
} else {
detail::model_put(output,
"length",
storage_length_covers(integer(field(input, "expected_length")),
uint_option(input, "actual_length")));
detail::model_put(output,
"numeric",
storage_numeric_covers(integer(field(input, "expected_precision")),
integer(field(input, "expected_scale")),
uint_option(input, "actual_precision"),
uint_option(input, "actual_scale")));
}
return output;
}
int main() {
GlobalContext global;
std::string line;
while (std::getline(std::cin, line)) {
auto user = global.make_user_context();
auto owner = user->memory_resource();
auto input = Json::parse(owner, line);
if (!input) {
throw std::invalid_argument("Invalid dialect helper input");
}
std::cout << evaluate(owner, *input).dump(owner) << '\n';
}
}