-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathquery.hpp
699 lines (571 loc) · 16 KB
/
query.hpp
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#pragma once
#include <array>
#include <string>
#include <string_view>
#include <type_traits>
#include "cexpr/string.hpp"
#include "ra/cross.hpp"
#include "ra/join.hpp"
#include "ra/natural.hpp"
#include "ra/projection.hpp"
#include "ra/relation.hpp"
#include "ra/rename.hpp"
#include "ra/selection.hpp"
#include "sql/column.hpp"
#include "sql/tokens.hpp"
#include "sql/predicate.hpp"
#include "sql/row.hpp"
namespace sql
{
// anonymous namespace to hold helper data structures and functions
namespace
{
template <std::size_t Pos, typename Node>
struct context
{
using node = Node;
static constexpr std::size_t pos = Pos;
};
template <typename Type, std::size_t Name, std::size_t Next>
struct colinfo
{
using type = Type;
static constexpr std::size_t name = Name;
static constexpr std::size_t next = Next;
};
template <cexpr::string Name, typename Row>
constexpr bool exists() noexcept
{
if constexpr (std::is_same_v<Row, sql::void_row>)
{
return false;
}
else
{
if constexpr (Row::column::name == Name)
{
return true;
}
else
{
return exists<Name, typename Row::next>();
}
}
}
template <typename Type, typename Char, std::size_t N>
constexpr value<Type> convert(cexpr::string<Char, N> const& str) noexcept
{
auto curr{ str.cbegin() }, end{ str.cend() };
constexpr Char dot{ '.' }, zro{ '0' }, min{ '-' };
Type acc{}, sign{ 1 }, scalar{ 10 };
if (*curr == min)
{
sign = -1;
++curr;
}
while (curr != end && *curr != dot)
{
acc = (acc * scalar) + (*curr - zro);
++curr;
}
if (curr != end && *curr == dot)
{
scalar = 1;
++curr;
while(curr != end)
{
acc += (*curr - zro) * (scalar /= Type{ 10 });
++curr;
}
}
return value<Type>{ acc * sign };
}
inline constexpr bool isquote(std::string_view const& tv) noexcept
{
return tv == "\"" || tv == "'";
}
inline constexpr bool isor(std::string_view const& tv) noexcept
{
return tv == "OR" || tv == "or";
}
inline constexpr bool isand(std::string_view const& tv) noexcept
{
return tv == "AND" || tv == "and";
}
inline constexpr bool isnot(std::string_view const& tv) noexcept
{
return tv == "NOT" || tv == "not";
}
inline constexpr bool isnatural(std::string_view const& tv) noexcept
{
return tv == "NATURAL" || tv == "natural";
}
inline constexpr bool isjoin(std::string_view const& tv) noexcept
{
return tv == "JOIN" || tv == "join";
}
inline constexpr bool iswhere(std::string_view const& tv) noexcept
{
return tv == "WHERE" || tv == "where";
}
inline constexpr bool isfrom(std::string_view const& tv) noexcept
{
return tv == "FROM" || tv == "from";
}
inline constexpr bool isas(std::string_view const& tv) noexcept
{
return tv == "AS" || tv == "as";
}
inline constexpr bool isselect(std::string_view const& tv) noexcept
{
return tv == "SELECT" || tv == "select";
}
inline constexpr bool iscomma(std::string_view const& tv) noexcept
{
return tv == ",";
}
constexpr bool isintegral(std::string_view const& tv) noexcept
{
bool result{ false };
for (auto c : tv)
{
result |= (c == '.');
}
return !result;
}
constexpr bool isdigit(char c) noexcept
{
return (c >= '0' && c <= '9') || c == '-' || c == '.';
}
constexpr bool iscomp(char c) noexcept
{
return c == '=' || c == '!' || c == '<' || c == '>';
}
constexpr bool iscolumn(std::string_view const& tv) noexcept
{
return !iscomma(tv) && !isas(tv) && !isfrom(tv);
}
constexpr bool isseparator(std::string_view const& tv) noexcept
{
return iscomma(tv) || isfrom(tv);
}
} // namespace
// structured binding compliant iterator for query objects
template <typename Expr>
class query_iterator
{
public:
using row_type = std::remove_cvref_t<typename Expr::output_type>;
// seeds row datamember for first dereference
query_iterator(bool end) : end_{ end }, row_{}
{
operator++();
}
inline bool operator==(query_iterator const& it) const noexcept
{
return end_ == it.end_;
}
inline bool operator!=(query_iterator const& it) const noexcept
{
return !(*this == it);
}
inline row_type const& operator*() const noexcept
{
return row_;
}
query_iterator& operator++()
{
if (!end_)
{
try
{
row_ = Expr::next();
}
catch (ra::data_end const& e)
{
end_ = true;
}
}
return *this;
}
private:
bool end_{};
row_type row_{};
};
template <cexpr::string Str, typename... Schemas>
class query
{
private:
// where predicate terminal parsing
template <std::size_t Pos, typename Row>
static constexpr auto parse_terms()
{
if constexpr (tokens_[Pos] == "(")
{
constexpr auto next{ parse_or<Pos + 1, Row>() };
using node = typename decltype(next)::node;
static_assert(tokens_[next.pos] == ")", "No closing paranthesis found.");
return context<next.pos + 1, node>{};
}
else if constexpr (isquote(tokens_[Pos]))
{
constexpr cexpr::string<char, tokens_[Pos + 1].length() + 1> name{ tokens_[Pos + 1] };
using str = decltype(name);
using node = sql::constant<value<str>{ name }, Row>;
static_assert(isquote(tokens_[Pos + 2]), "No closing quote found.");
return context<Pos + 3, node>{};
}
else if constexpr (isdigit(tokens_[Pos][0]))
{
constexpr cexpr::string<char, tokens_[Pos].length() + 1> name{ tokens_[Pos] };
using val = decltype(isintegral(tokens_[Pos]) ? std::int64_t{} : double{});
using node = sql::constant<sql::convert<val>(name), Row>;
return context<Pos + 1, node>{};
}
else
{
constexpr cexpr::string<char, tokens_[Pos].length() + 1> name{ tokens_[Pos] };
using node = sql::variable<name, Row>;
return context<Pos + 1, node>{};
}
}
// parses a single compare operation
template <typename Left, typename Row>
static constexpr auto recurse_comparison()
{
if constexpr (!iscomp(tokens_[Left::pos][0]))
{
return Left{};
}
else
{
constexpr auto next{ parse_terms<Left::pos + 1, Row>() };
constexpr cexpr::string<char, tokens_[Left::pos].length() + 1> name{ tokens_[Left::pos] };
using ranode = typename decltype(next)::node;
using node = sql::operation<name, Row, typename Left::node, ranode>;
return context<next.pos, node>{};
}
}
// descend further and attempt to parse a compare operation
template <std::size_t Pos, typename Row>
static constexpr auto parse_comparison()
{
using left = decltype(parse_terms<Pos, Row>());
return recurse_comparison<left, Row>();
}
// attempt to parse a negation operation then descend further
template <std::size_t Pos, typename Row>
static constexpr auto parse_negation()
{
if constexpr (isnot(tokens_[Pos]))
{
constexpr auto next{ parse_comparison<Pos + 1, Row>() };
using ranode = typename decltype(next)::node;
using node = sql::operation<"NOT", Row, ranode>;
return context<next.pos, node>{};
}
else
{
return parse_comparison<Pos, Row>();
}
}
// recursively parse chained AND operations
template <typename Left, typename Row>
static constexpr auto recurse_and()
{
if constexpr (!isand(tokens_[Left::pos]))
{
return Left{};
}
else
{
constexpr auto next{ parse_negation<Left::pos + 1, Row>() };
using ranode = typename decltype(next)::node;
using node = sql::operation<"AND", Row, typename Left::node, ranode>;
return recurse_and<context<next.pos, node>, Row>();
}
}
// descend further then attempt to parse AND operations
template <std::size_t Pos, typename Row>
static constexpr auto parse_and()
{
using left = decltype(parse_negation<Pos, Row>());
return recurse_and<left, Row>();
}
// recursively parse chained OR operations
template <typename Left, typename Row>
static constexpr auto recurse_or()
{
if constexpr (!isor(tokens_[Left::pos]))
{
return Left{};
}
else
{
constexpr auto next{ parse_and<Left::pos + 1, Row>() };
using ranode = typename decltype(next)::node;
using node = sql::operation<"OR", Row, typename Left::node, ranode>;
return recurse_or<context<next.pos, node>, Row>();
}
}
// descend further then attempt to parse OR operations
template <std::size_t Pos, typename Row>
static constexpr auto parse_or()
{
using left = decltype(parse_and<Pos, Row>());
return recurse_or<left, Row>();
}
// find correct schema for terminal relation
template <cexpr::string Name, std::size_t Id, typename Schema, typename... Others>
static constexpr auto recurse_schemas()
{
if constexpr (Name == Schema::name)
{
return ra::relation<Schema, Id>{};
}
else
{
static_assert(sizeof...(Others) != 0, "Schema name used in JOIN was not provided.");
return recurse_schemas<Name, Id, Others...>();
}
}
// wrapper function to determine terminal relation
template <std::size_t Pos>
static constexpr auto parse_schema()
{
if constexpr (tokens_[Pos] == "(")
{
constexpr auto next{ parse_root<Pos + 1>() };
using node = typename decltype(next)::node;
static_assert(tokens_[next.pos] == ")", "No closing paranthesis found.");
return context<next.pos + 1, node>{};
}
else
{
constexpr cexpr::string<char, tokens_[Pos].length() + 1> name{ tokens_[Pos] };
using node = decltype(recurse_schemas<name, Pos, Schemas...>());
return context<Pos + 1, node>{};
}
}
// stub which will choose the specific join RA node
template <std::size_t Pos, typename Left, typename Right>
static constexpr auto choose_join()
{
if constexpr (isnatural(tokens_[Pos]))
{
return ra::natural<Left, Right>{};
}
else
{
return ra::cross<Left, Right>{};
}
}
// parses join colinfo if a join is present else returns the single relation terminal
template <std::size_t Pos>
static constexpr auto parse_join()
{
constexpr auto lnext{ parse_schema<Pos>() };
using lnode = typename decltype(lnext)::node;
if constexpr (lnext.pos + 2 < tokens_.count() && isjoin(tokens_[lnext.pos + 1]))
{
constexpr auto rnext{ parse_schema<lnext.pos + 2>() };
using rnode = typename decltype(rnext)::node;
using join = decltype(choose_join<lnext.pos, lnode, rnode>());
return context<rnext.pos, join>{};
}
else
{
return context<lnext.pos, lnode>{};
}
}
// starting point to parse everything after the from keyword
template <std::size_t Pos>
static constexpr auto parse_from()
{
static_assert(isfrom(tokens_[Pos]), "Expected 'FROM' token not found.");
constexpr auto next{ parse_join<Pos + 1>() };
using node = typename decltype(next)::node;
if constexpr (next.pos < tokens_.count() && iswhere(tokens_[next.pos]))
{
using output = std::remove_cvref_t<typename node::output_type>;
constexpr auto predicate{ parse_or<next.pos + 1, output>() };
using pnext = typename decltype(predicate)::node;
using snode = ra::selection<pnext, node>;
return context<predicate.pos, snode>{};
}
else
{
return context<next.pos, node>{};
}
}
// recursively searches all schemas for the a matching column
template <cexpr::string Name, typename Schema, typename... Others>
static constexpr auto recurse_types()
{
if constexpr (sql::exists<Name, typename Schema::row_type>())
{
return decltype(sql::get<Name>(typename Schema::row_type{})){};
}
else
{
static_assert(sizeof...(Others) != 0, "Column name was not present in any schema.");
return recurse_types<Name, Others...>();
}
}
// wrapper to determine the type for the the column
template <std::size_t Pos>
static constexpr auto column_type()
{
constexpr cexpr::string<char, tokens_[Pos].length() + 1> name{ tokens_[Pos] };
return recurse_types<name, Schemas...>();
}
// asserts token is column separator, and if comma returns one past the comma else start position
template <std::size_t Pos>
static constexpr std::size_t next_column()
{
static_assert(isseparator(tokens_[Pos]), "Expected ',' or 'FROM' token following column.");
if constexpr (iscomma(tokens_[Pos]))
{
return Pos + 1;
}
else
{
return Pos;
}
}
template <std::size_t Pos, bool Rename>
static constexpr auto parse_colinfo()
{
static_assert(iscolumn(tokens_[Pos]), "Invalid token starting column delcaration.");
constexpr bool rename{ isas(tokens_[Pos + 1]) && iscolumn(tokens_[Pos + 2]) };
using col = decltype(column_type<Pos>());
if constexpr (Rename && rename)
{
constexpr auto next{ next_column<Pos + 3>() };
return colinfo<col, Pos + 2, next>{};
}
else if constexpr (rename)
{
constexpr auto next{ next_column<Pos + 3>() };
return colinfo<col, Pos, next>{};
}
else
{
constexpr auto next{ next_column<Pos + 1>() };
return colinfo<col, Pos, next>{};
}
}
// recursively parse all columns projected/renamed in the query
template <std::size_t Pos, bool Rename>
static constexpr auto recurse_columns()
{
if constexpr (isfrom(tokens_[Pos]))
{
return context<Pos, sql::void_row>{};
}
else
{
constexpr auto info{ parse_colinfo<Pos, Rename>() };
constexpr cexpr::string<char, tokens_[info.name].length() + 1> name{ tokens_[info.name] };
constexpr auto child{ recurse_columns<info.next, Rename>() };
using next = std::remove_const_t<typename decltype(child)::node>;
using col = std::remove_const_t<decltype(sql::column<name, typename decltype(info)::type>{})>;
using node = sql::row<col, next>;
return context<child.pos, node>{};
}
}
// wrapper to parse columns as a projection RA node
template <std::size_t Pos>
static constexpr auto parse_projection()
{
constexpr auto proj{ recurse_columns<Pos, false>() };
constexpr auto next{ parse_from<proj.pos>() };
using ranode = typename decltype(proj)::node;
using node = ra::projection<ranode, typename decltype(next)::node>;
return context<next.pos, node>{};
}
// wrapper to parse columns as a rename RA node
template <std::size_t Pos>
static constexpr auto parse_rename()
{
constexpr auto next = parse_projection<Pos>();
using ranode = typename decltype(recurse_columns<Pos, true>())::node;
using node = ra::rename<ranode, typename decltype(next)::node>;
return context<next.pos, node>{};
}
// attempts to match column rename operation pattern on a column
template <std::size_t Pos>
static constexpr bool has_rename()
{
if constexpr (isfrom(tokens_[Pos]) || isfrom(tokens_[Pos + 2]))
{
return false;
}
else if constexpr (iscolumn(tokens_[Pos]) && isas(tokens_[Pos + 1]) && iscolumn(tokens_[Pos + 2]))
{
return true;
}
else
{
constexpr bool comma{ iscomma(tokens_[Pos + 1]) };
static_assert(comma || isfrom(tokens_[Pos + 1]), "Expected ',' or 'FROM' token following column.");
if constexpr (comma)
{
return has_rename<Pos + 2>();
}
else
{
return has_rename<Pos + 1>();
}
}
}
// decide RA node to root the expression tree
template <std::size_t Pos>
static constexpr auto parse_root()
{
static_assert(isselect(tokens_[Pos]), "Expected 'SELECT' token not found.");
if constexpr (tokens_[Pos + 1] == "*")
{
return parse_from<Pos + 2>();
}
else if constexpr (has_rename<Pos + 1>())
{
return parse_rename<Pos + 1>();
}
else
{
return parse_projection<Pos + 1>();
}
}
static constexpr sql::tokens<char, sql::preprocess(Str)> tokens_{ Str };
using expression = typename decltype(parse_root<0>())::node;
bool empty_;
public:
using iterator = query_iterator<expression>;
using row_type = expression::output_type;
query(Schemas const&... tables)
{
try
{
expression::seed(tables...);
empty_ = false;
}
catch(ra::data_end const& e)
{
empty_ = true;
}
}
~query()
{
expression::reset();
}
inline iterator begin() const
{
return iterator{ empty_ };
}
inline iterator end() const
{
return iterator{ true };
}
};
} // namespace sql