Repository navigation
Expand file tree
/
Copy pathmod.cpp
More file actions
141 lines (129 loc) · 4.97 KB
/
Copy pathmod.cpp
File metadata and controls
141 lines (129 loc) · 4.97 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <teaql/runtime/detail/data_service/data_service.hpp>
#include <charconv>
namespace teaql::detail {
void record_query_metadata(const UserContext&, SqlIntentRedactions*, const ExecutionMetadata&);
namespace {
struct ContextObserver {
using allocator_type = Allocator;
ResourceBorrow<const UserContext> context;
std::optional<ResourceBorrow<SqlIntentRedactions>> query_privacy;
ContextObserver(std::allocator_arg_t,
Allocator a,
const UserContext& context,
const std::optional<ResourceBorrow<SqlIntentRedactions>>& privacy = {})
: context(std::allocator_arg, a, context, resource_arena(context.memory_resource())),
query_privacy(clone_in(a.resource(), privacy)) {}
ContextObserver(std::allocator_arg_t, Allocator a, const ContextObserver& other)
: context(std::allocator_arg, a, other.context),
query_privacy(clone_in(a.resource(), other.query_privacy)) {}
void operator()(ExecutionMetadata value) const {
record_query_metadata(
context.get(), query_privacy ? &query_privacy->get() : nullptr, value);
}
};
}
void UserContextMetadata::with_query_privacy(SqlIntentRedactions& privacy) {
auto r = memory_resource();
auto entry = ContextResources::entry(resource_arena(r), privacy.memory_resource(), privacy);
query_privacy_.emplace(std::allocator_arg, Allocator(r), privacy, *entry.owner, entry.slot);
}
void UserContextMetadata::record_metadata_log(const ExecutionMetadata& value) const {
record_query_metadata(context(), query_privacy_ ? &query_privacy_->get() : nullptr, value);
}
Vector<const EntityDescriptor*> UserContextMetadata::all_entities(Memory r) const {
if (!resource_arena(context().memory_resource()).can_lend_to(resource_arena(r))) {
throw std::invalid_argument("Context metadata view cannot escape its owning Context");
}
auto entities = context().all_entities();
return Vector<const EntityDescriptor*>(entities.begin(), entities.end(), r);
}
std::optional<ExecutionObserver> UserContextMetadata::query_diagnostic_observer(Memory r) const {
if (!capture_execution_metadata()) {
return {};
}
return ExecutionObserver(
std::allocator_arg,
Allocator(r),
ContextObserver(std::allocator_arg, Allocator(r), context(), query_privacy_));
}
std::optional<ExecutionObserver> UserContextMetadata::mutation_diagnostic_observer(Memory r) const {
if (!context().sql_log_options().mutation) {
return {};
}
return ExecutionObserver(std::allocator_arg,
Allocator(r),
ContextObserver(std::allocator_arg, Allocator(r), context()));
}
std::optional<String> resolve_final_comment(const UserContext& context,
std::span<const TraceNode> trace,
std::optional<std::string_view> comment) {
auto r = context.memory_resource();
String business(r);
if (!trace.empty()) {
for (const auto& node : trace) {
if (!business.empty()) {
business += " -> ";
}
business += node.entity_type;
business += '(';
if (node.entity_id) {
char buffer[32];
auto result = std::to_chars(buffer, buffer + sizeof(buffer), *node.entity_id);
business.append(buffer, result.ptr);
} else {
business += "pending";
}
business += "): ";
business += node.comment;
}
} else if (comment) {
business = *comment;
}
auto user = context.user_identifier();
String out(r);
if (user && !user->empty()) {
out += '[';
out += *user;
out += ']';
if (!business.empty()) {
out += ' ';
out += business;
}
} else {
out = std::move(business);
}
if (out.empty()) {
return {};
}
return out;
}
}
namespace teaql::detail {
void record_query_metadata(const UserContext& context,
SqlIntentRedactions* intent,
const ExecutionMetadata& value) {
if (!intent) {
context.record_metadata_log(value);
return;
}
auto r = context.memory_resource();
auto metadata = clone_in(r, value);
auto inherit = [&](auto&& self, ExecutionMetadata& item) -> void {
if (!item.statements.empty()) {
for (auto& child : item.statements) {
self(self, child);
}
return;
}
item.sql_log.intent_redactions.extend(*intent);
*intent = SqlIntentRedactions::from_bindings(
r,
item.sql_log,
item.params,
item.parameterized_query ? std::string_view(*item.parameterized_query)
: std::string_view{});
};
inherit(inherit, metadata);
context.record_metadata_log(metadata);
}
}