-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.cpp
306 lines (266 loc) · 8.29 KB
/
url.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/sqlite/extension.hpp>
#include <boost/sqlite/vtable.hpp>
#include <boost/system/result.hpp>
#include <boost/url.hpp>
using namespace boost;
constexpr int pct_subtype = static_cast<int>('U');
void tag_invoke(sqlite::set_result_tag, sqlite3_context * ctx, urls::pct_string_view value)
{
using boost::sqlite::ext::sqlite3_api;
sqlite3_result_text(ctx, value.data(), value.size(), nullptr);
sqlite3_result_subtype(ctx, pct_subtype);
}
void tag_invoke(sqlite::set_result_tag, sqlite3_context * ctx, const urls::segments_encoded_view & value)
{
using boost::sqlite::ext::sqlite3_api;
sqlite3_result_text(ctx, value.buffer().data(), value.buffer().size(), nullptr);
sqlite3_result_subtype(ctx, pct_subtype);
}
struct url_cursor final
: sqlite::vtab::cursor<
variant2::variant<variant2::monostate, core::string_view, urls::pct_string_view>
>
{
url_cursor(urls::url_view view) : view(view ) {}
urls::url_view view;
bool done{false};
sqlite::result<void> next() { done = true; return {};}
sqlite::result<sqlite3_int64> row_id() {return static_cast<sqlite3_int64>(0);}
sqlite::result<column_type> column(int i, bool nochange)
{
nochange = true;
switch (i)
{
case 0: return view.scheme();
case 1: return view.encoded_user();
case 2: return view.encoded_password();
case 3: return view.encoded_host();
case 4: return view.port();
case 5: return view.encoded_path();
case 6: return view.encoded_query();
case 7: return view.encoded_fragment();
case 8: return view.buffer();
default:
return variant2::monostate{};
}
}
sqlite::result<void> filter(int idx, const char * idxStr, span<sqlite::value> values)
{
if (values.size() > 0u)
view = urls::parse_uri(values[0].get_text()).value();
return {};
}
bool eof() noexcept {return done || view.empty();}
};
struct url_wrapper final : sqlite::vtab::table<url_cursor>
{
urls::url value;
const char * declaration() override
{
return R"(
create table url(
scheme text,
user text,
password text,
host text,
port text,
path text,
query text,
fragment text,
url text hidden);)";
}
sqlite::result<url_cursor> open() override
{
return url_cursor{value};
}
sqlite::result<void> best_index(sqlite::vtab::index_info & info) override
{
for (const auto constraint : info.constraints())
{
if (constraint.iColumn == 8 && constraint.usable)
{
if (constraint.op != SQLITE_INDEX_CONSTRAINT_EQ)
return {SQLITE_MISUSE,
sqlite::error_info("query only support equality constraints")};
info.usage_of(constraint).argvIndex = 1;
info.set_index(1);
}
}
return {};
}
};
struct url_module final : sqlite::vtab::eponymous_module<url_wrapper>
{
sqlite::result<url_wrapper> connect(sqlite::connection db,
int argc, const char * const *argv)
{
return url_wrapper{};
}
};
struct segements_cursor final : sqlite::vtab::cursor<
variant2::variant<variant2::monostate, int, core::string_view, urls::segments_encoded_view>>
{
segements_cursor(urls::segments_encoded_view view) : view(view) {}
urls::segments_encoded_view view;
urls::segments_encoded_view::const_iterator itr{view.begin()};
sqlite::result<void> next() override { itr++; return {};}
sqlite::result<sqlite3_int64> row_id() override {return std::distance(view.begin(), itr);}
sqlite::result<column_type> column(int i, bool nochange) override
{
nochange = true;
switch (i)
{
case 0: return std::distance(view.begin(), itr);
case 1: return *itr;
case 2: return view;
default:
return variant2::monostate{};
}
}
sqlite::result<void> filter(int idx, const char * idxStr,
span<sqlite::value> values) override
{
if (values.size() > 0u)
view = urls::segments_encoded_view(values[0].get_text());
itr = view.begin();
return {};
}
bool eof() noexcept override {return itr == view.end();}
};
struct segment_wrapper final : sqlite::vtab::table<segements_cursor>
{
urls::segments_encoded_view value;
const char * declaration() override
{
return R"(
create table segments(
idx integer,
segment text,
segments text hidden);)";
}
sqlite::result<segements_cursor> open() override
{
return segements_cursor{value};
}
sqlite::result<void> best_index(sqlite::vtab::index_info & info) override
{
for (auto & constraint : info.constraints())
{
if (constraint.iColumn == 2
&& constraint.usable)
{
if (constraint.op != SQLITE_INDEX_CONSTRAINT_EQ)
return {SQLITE_OK, sqlite::error_info("segments only support equality constraints")};
info.usage_of(constraint).argvIndex = 1;
info.set_index(1);
}
}
return {};
}
};
struct segments_module final : sqlite::vtab::eponymous_module<segment_wrapper>
{
sqlite::result<segment_wrapper> connect(sqlite::connection conn,
int argc, const char * const *argv)
{
return segment_wrapper{};
}
};
struct query_cursor final : sqlite::vtab::cursor<
variant2::variant<variant2::monostate, int, core::string_view, urls::pct_string_view>
>
{
urls::params_encoded_view view;
urls::params_encoded_view::const_iterator itr{view.begin()};
sqlite::result<void> next() override { itr++; return {};}
sqlite::result<sqlite3_int64> row_id() override {return std::distance(view.begin(), itr);}
sqlite::result<column_type> column(int i, bool nochange) override
{
nochange = true;
switch (i)
{
case 0: return std::distance(view.begin(), itr);
case 1: return itr->key;
case 2:
if (!itr->has_value)
return variant2::monostate{};
else
return itr->value;
case 3: return view.buffer();
default:
return variant2::monostate{};
}
}
sqlite::result<void> filter(int idx, const char * idxStr,
span<sqlite::value> values) override
{
if (values.size() > 0u)
view = urls::params_encoded_view(values[0].get_text());
itr = view.begin();
return {};
}
bool eof() noexcept override {return itr == view.end();}
};
struct query_wrapper final : sqlite::vtab::table<query_cursor>
{
const char * declaration() override
{
return R"(
create table queries(
idx integer,
name text,
value text,
query_string text hidden);)";
}
sqlite::result<query_cursor> open() override
{
return query_cursor{};
}
sqlite::result<void> best_index(sqlite::vtab::index_info & info) override
{
for (auto & constraint : info.constraints())
{
if (constraint.iColumn == 3
&& constraint.usable)
{
if (constraint.op != SQLITE_INDEX_CONSTRAINT_EQ)
return sqlite::error{SQLITE_OK, "query only support equality constraints"};
info.usage_of(constraint).argvIndex = 1;
info.set_index(1);
}
}
return {};
}
};
struct query_module final : sqlite::vtab::eponymous_module<query_wrapper>
{
sqlite::result<query_wrapper> connect(sqlite::connection conn,
int argc, const char * const *argv)
{
return query_wrapper{};
}
};
BOOST_SQLITE_EXTENSION(url, conn)
{
sqlite::create_module(conn, "url", url_module{});
sqlite::create_module(conn, "segments", segments_module());
sqlite::create_module(conn, "query", query_module());
sqlite::create_scalar_function(
conn, "pct_decode",
+[](boost::sqlite::context<> , boost::span<boost::sqlite::value, 1u> s)
{
return urls::pct_string_view(s[0].get_text()).decode();
});
sqlite::create_scalar_function(
conn, "pct_encode",
+[](boost::sqlite::context<> , boost::span<boost::sqlite::value, 1u> s)
{
return urls::encode(s[0].get_text(), urls::pchars);
});
}