-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsql.hpp
1890 lines (1562 loc) · 39.6 KB
/
sql.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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <array>
#include <cstddef>
#include <exception>
#include <fstream>
#include <locale>
#include <set>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
namespace cexpr
{
template <typename Char, std::size_t N>
class string
{
public:
using char_type = Char;
constexpr string() noexcept : size_{ 0 }, string_{ 0 }
{}
constexpr string(const Char(&s)[N]) noexcept : string{}
{
for(; s[size_]; ++size_)
{
string_[size_] = s[size_];
}
}
constexpr string(cexpr::string<Char, N> const& s) noexcept : string{}
{
for (; s[size_]; ++size_)
{
string_[size_] = s[size_];
}
}
constexpr string(std::basic_string_view<Char> const& s) noexcept : string{}
{
if (s.length() < N)
{
for (; size_ < s.length(); ++size_)
{
string_[size_] = s[size_];
}
}
}
constexpr void fill(const Char* begin, const Char* end) noexcept
{
fill_from(begin, end, begin());
}
constexpr void fill_from(const Char* begin, const Char* end, Char* start) noexcept
{
if (end - begin < N)
{
for (auto curr{ start }; begin != end; ++begin, ++curr)
{
*curr = *begin;
}
}
}
inline constexpr std::size_t capacity() const noexcept
{
return N - 1;
}
inline constexpr std::size_t size() const noexcept
{
return size_;
}
inline constexpr Char* begin() noexcept
{
return string_;
}
inline constexpr const Char* cbegin() const noexcept
{
return string_;
}
inline constexpr Char* end() noexcept
{
return &string_[size_];
}
inline constexpr const Char* cend() const noexcept
{
return &string_[size_];
}
inline constexpr Char& operator[](std::size_t i)
{
return string_[i];
}
inline constexpr Char const& operator[](std::size_t i) const
{
return string_[i];
}
template <typename OtherChar, std::size_t OtherN>
constexpr bool operator==(string<OtherChar, OtherN> const& other) const noexcept
{
if constexpr (N != OtherN)
{
return false;
}
std::size_t i{};
for (; i < N && string_[i] == other[i]; ++i);
return i == N;
}
template <typename OtherChar, std::size_t OtherN>
constexpr bool operator==(const OtherChar(&other)[OtherN]) const noexcept
{
if constexpr (N != OtherN)
{
return false;
}
std::size_t i{};
for (; i < N && string_[i] == other[i]; ++i);
return i == N;
}
template <typename OtherChar>
inline bool operator==(std::basic_string<OtherChar> const& other) const noexcept
{
return other == string_;
}
template <typename OtherChar>
inline bool operator!=(std::basic_string<OtherChar> const& other) const noexcept
{
return !(other == string_);
}
private:
std::size_t size_;
Char string_[N];
};
template <typename Char, std::size_t N>
string(const Char[N]) -> string<Char, N>;
template <typename Char, std::size_t N>
inline bool operator==(std::basic_string<Char> const& str, string<Char, N> const& cstr) noexcept
{
return cstr == str;
}
template <typename Char, std::size_t N>
inline bool operator!=(std::basic_string<Char> const& str, string<Char, N> const& cstr) noexcept
{
return cstr != str;
}
} // namespace cexpr
namespace sql
{
template <cexpr::string Name, typename Type>
struct column
{
static constexpr auto name{ Name };
using type = Type;
};
} // namespace sql
namespace sql
{
struct void_row
{
static constexpr std::size_t depth{ 0 };
};
template <typename Col, typename Next>
class row
{
public:
using column = Col;
using next = Next;
static constexpr std::size_t depth{ 1 + next::depth };
row() = default;
template <typename... ColTs>
row(column::type const& val, ColTs const&... vals) : value_{ val }, next_{ vals... }
{}
template <typename... ColTs>
row(column::type&& val, ColTs&&... vals) : value_{ std::forward<column::type>(val) }, next_{ std::forward<ColTs>(vals)... }
{}
inline constexpr next const& tail() const noexcept
{
return next_;
}
inline constexpr next& tail() noexcept
{
return next_;
}
inline constexpr column::type const& head() const noexcept
{
return value_;
}
inline constexpr column::type& head() noexcept
{
return value_;
}
private:
column::type value_;
next next_;
};
template <typename Col, typename... Cols>
struct variadic_row
{
private:
static inline constexpr auto resolve() noexcept
{
if constexpr (sizeof...(Cols) != 0)
{
return typename variadic_row<Cols...>::row_type{};
}
else
{
return void_row{};
}
}
public:
using row_type = row<Col, decltype(resolve())>;
};
// user function to query row elements by column name
template <cexpr::string Name, typename Row>
constexpr auto const& get(Row const& r) noexcept
{
static_assert(!std::is_same_v<Row, sql::void_row>, "Name does not match a column name.");
if constexpr (Row::column::name == Name)
{
return r.head();
}
else
{
return get<Name>(r.tail());
}
}
// compiler function used by structured binding declaration
template <std::size_t Pos, typename Row>
constexpr auto const& get(Row const& r) noexcept
{
static_assert(Pos < Row::depth, "Position is larger than number of row columns.");
if constexpr (Pos == 0)
{
return r.head();
}
else
{
return get<Pos - 1>(r.tail());
}
}
// function to assign a value to a column's value in a row
template <cexpr::string Name, typename Row, typename Type>
constexpr void set(Row& r, Type const& value)
{
static_assert(!std::is_same_v<Row, sql::void_row>, "Name does not match a column name.");
if constexpr (Row::column::name == Name)
{
r.head() = value;
}
else
{
set<Name>(r.tail(), value);
}
}
} // namespace sql
// STL injections to allow row to be used in structured binding declarations
namespace std
{
template <typename Col, typename Next>
class tuple_size<sql::row<Col, Next>> : public integral_constant<size_t, sql::row<Col, Next>::depth>
{};
template <size_t Index, typename Col, typename Next>
struct tuple_element<Index, sql::row<Col, Next>>
{
using type = decltype(sql::get<Index>(sql::row<Col, Next>{}));
};
} // namespace std
namespace sql
{
template <cexpr::string... Columns>
struct index
{
template <typename Row>
struct comparator
{
bool operator()(Row const& left, Row const& right) const noexcept
{
return compare<Columns...>(left, right);
}
private:
template <cexpr::string Col, cexpr::string... Cols>
bool compare(Row const& left, Row const& right) const noexcept
{
auto const& l{ sql::get<Col>(left) };
auto const& r{ sql::get<Col>(right) };
if constexpr (sizeof...(Cols) != 0)
{
if (l == r)
{
return compare<Cols...>(left, right);
}
}
return l < r;
}
};
};
} // namespace sql
namespace sql
{
template <cexpr::string Name, typename Index, typename... Cols>
class schema
{
public:
static constexpr auto name{ Name };
using row_type = sql::variadic_row<Cols...>::row_type;
using container = typename
std::conditional_t<
std::is_same_v<Index, sql::index<>>,
std::vector<row_type>,
std::multiset<row_type, typename Index::template comparator<row_type>>
>;
using const_iterator = typename container::const_iterator;
schema() = default;
template <typename Type, typename... Types>
schema(std::vector<Type> const& col, Types const&... cols) : schema{}
{
insert(col, cols...);
}
template <typename Type, typename... Types>
schema(std::vector<Type>&& col, Types&&... cols) : schema{}
{
insert(std::forward<Type>(col), std::forward<Types>(cols)...);
}
template <typename... Types>
inline void emplace(Types const&... vals)
{
if constexpr (std::is_same_v<Index, sql::index<>>)
{
table_.emplace_back(vals...);
}
else
{
table_.emplace(vals...);
}
}
template <typename... Types>
inline void emplace(Types&&... vals)
{
if constexpr (std::is_same_v<Index, sql::index<>>)
{
table_.emplace_back(vals...);
}
else
{
table_.emplace(vals...);
}
}
template <typename Type, typename... Types>
void insert(std::vector<Type> const& col, Types const&... cols)
{
for (std::size_t i{}; i < col.size(); ++i)
{
emplace(col[i], cols[i]...);
}
}
template <typename Type, typename... Types>
void insert(std::vector<Type>&& col, Types&&... cols)
{
for (std::size_t i{}; i < col.size(); ++i)
{
emplace(std::forward<Type>(col[i]), std::forward<Types>(cols[i])...);
}
}
void insert(row_type const& row)
{
if constexpr (std::is_same_v<Index, sql::index<>>)
{
table_.push_back(row);
}
else
{
table_.insert(row);
}
}
void insert(row_type&& row)
{
if constexpr (std::is_same_v<Index, sql::index<>>)
{
table_.push_back(std::forward<row_type>(row));
}
else
{
table_.insert(std::forward<row_type>(row));
}
}
inline const_iterator begin() const noexcept
{
return table_.begin();
}
inline const_iterator end() const noexcept
{
return table_.end();
}
private:
container table_;
};
namespace
{
template <typename Row>
void fill(std::fstream& fstr, Row& row, [[maybe_unused]] char delim)
{
if constexpr (!std::is_same_v<Row, sql::void_row>)
{
if constexpr (std::is_same_v<typename Row::column::type, std::string>)
{
if constexpr (std::is_same_v<typename Row::next, sql::void_row>)
{
std::getline(fstr, row.head());
}
else
{
std::getline(fstr, row.head(), delim);
}
}
else
{
fstr >> row.head();
}
fill<typename Row::next>(fstr, row.tail(), delim);
}
}
template <typename Row>
void fill(std::fstream& fstr, Row const& row, char delim)
{
if constexpr (!std::is_same_v<Row, sql::void_row>)
{
fstr << row.head();
if constexpr (std::is_same_v<typename Row::next, sql::void_row>)
{
fstr << '\n';
}
else
{
fstr << delim;
}
fill<typename Row::next>(fstr, row.tail(), delim);
}
}
} // namespace
// helper function for users to load a data into a schema from a file
template <typename Schema>
Schema load(std::string const& file, char delim)
{
auto fstr{ std::fstream(file, fstr.in) };
Schema table{};
typename Schema::row_type row{};
while (fstr)
{
fill<typename Schema::row_type>(fstr, row, delim);
table.insert(std::move(row));
// in case last stream extraction did not remove newline
if (fstr.get() != '\n')
{
fstr.unget();
}
}
return table;
}
// for compat with previous versions
template <typename Schema, char Delim>
inline Schema load(std::string const& file)
{
return load<Schema>(file, Delim);
}
// will work with schema and query objects
template <typename Type>
void store(Type const& data, std::string const& file, char delim)
{
auto fstr{ std::fstream(file, fstr.out) };
for (auto const& row : data)
{
fill<typename Type::row_type>(fstr, row, delim);
}
}
// for devs who want to use the previous format
template <typename Type, char Delim>
inline void store(Type const& data, std::string const& file)
{
store<Type>(data, file, Delim);
}
} // namespace sql
namespace ra
{
template <typename Input>
class unary
{
public:
using input_type = std::remove_cvref_t<decltype(Input::next())>;
template <typename... Inputs>
static inline void seed(Inputs const&... rs)
{
Input::seed(rs...);
}
static inline void reset()
{
Input::reset();
}
};
template <typename LeftInput, typename RightInput>
class binary
{
public:
using left_type = std::remove_cvref_t<decltype(LeftInput::next())>;
using right_type = std::remove_cvref_t<decltype(RightInput::next())>;
template <typename... Inputs>
static inline void seed(Inputs const&... rs)
{
LeftInput::seed(rs...);
RightInput::seed(rs...);
}
static inline void reset()
{
LeftInput::reset();
RightInput::reset();
}
};
} // namespace ra
namespace ra
{
namespace
{
template <typename Left, typename Right>
constexpr auto recr_merge()
{
if constexpr (std::is_same_v<Left, sql::void_row>)
{
return Right{};
}
else
{
using next = decltype(recr_merge<typename Left::next, Right>());
return sql::row<typename Left::column, next>{};
}
}
template <typename Left, typename Right>
inline constexpr auto merge()
{
if constexpr (Left::column::name == Right::column::name)
{
return recr_merge<Left, typename Right::next>();
}
else
{
return recr_merge<Left, Right>();
}
}
template <typename Dest, typename Row>
constexpr void recr_copy(Dest& dest, Row const& src)
{
if constexpr (std::is_same_v<Row, sql::void_row>)
{
return;
}
else
{
dest.head() = src.head();
recr_copy(dest.tail(), src.tail());
}
}
template <typename Dest, typename Row>
inline constexpr void copy(Dest& dest, Row const& src)
{
if constexpr (Dest::column::name == Row::column::name)
{
recr_copy(dest, src);
}
else
{
copy(dest.tail(), src);
}
}
} // namespace
template <typename LeftInput, typename RightInput>
class join : public ra::binary<LeftInput, RightInput>
{
using binary_type = ra::binary<LeftInput, RightInput>;
using left_type = typename binary_type::left_type;
using right_type = typename binary_type::right_type;
public:
using output_type = decltype(merge<left_type, right_type>());
template <typename... Inputs>
static inline void seed(Inputs const&... rs)
{
binary_type::seed(rs...);
copy(output_row, LeftInput::next());
}
static inline void reset()
{
binary_type::reset();
copy(output_row, LeftInput::next());
}
static output_type output_row;
};
template <typename LeftInput, typename RightInput>
typename join<LeftInput, RightInput>::output_type join<LeftInput, RightInput>::output_row{};
} // namespace ra
namespace ra
{
struct data_end : std::exception
{};
// Id template parameter allows unique ra::relation types to be instantiated from
// a single sql::schema type (for queries referencing a schema multiple times).
template <typename Schema, std::size_t Id>
class relation
{
public:
using output_type = Schema::row_type&;
static auto& next()
{
if (curr != end)
{
return *curr++;
}
else
{
throw ra::data_end{};
}
}
template <typename Input, typename... Inputs>
static void seed(Input const& r, Inputs const&... rs) noexcept
{
if constexpr (std::is_same_v<Input, Schema>)
{
curr = r.begin();
begin = r.begin();
end = r.end();
}
else
{
seed(rs...);
}
}
static inline void reset() noexcept
{
curr = begin;
}
private:
static Schema::const_iterator curr;
static Schema::const_iterator begin;
static Schema::const_iterator end;
};
template <typename Schema, std::size_t Id>
Schema::const_iterator relation<Schema, Id>::curr{};
template <typename Schema, std::size_t Id>
Schema::const_iterator relation<Schema, Id>::begin{};
template <typename Schema, std::size_t Id>
Schema::const_iterator relation<Schema, Id>::end{};
} // namespace ra
namespace ra
{
template <typename LeftInput, typename RightInput>
class cross : public ra::join<LeftInput, RightInput>
{
using join_type = ra::join<LeftInput, RightInput>;
public:
using output_type = join_type::output_type;
static auto&& next()
{
try
{
copy(join_type::output_row, RightInput::next());
}
catch(ra::data_end const& e)
{
copy(join_type::output_row, LeftInput::next());
RightInput::reset();
copy(join_type::output_row, RightInput::next());
}
return std::move(join_type::output_row);
}
};
} // namespace ra
namespace ra
{
template <typename LeftInput, typename RightInput>
class natural : public ra::join<LeftInput, RightInput>
{
using join_type = ra::join<LeftInput, RightInput>;
using key_type = std::remove_cvref_t<decltype(LeftInput::next().head())>;
using value_type = std::vector<std::remove_cvref_t<decltype(RightInput::next().tail())>>;
using map_type = std::unordered_map<key_type, value_type>;
public:
using output_type = join_type::output_type;
template <typename... Inputs>
static void seed(Inputs const&... rs)
{
join_type::seed(rs...);
if (row_cache.empty())
{
try
{
for (;;)
{
auto const& row{ RightInput::next() };
row_cache[row.head()].push_back(row.tail());
}
}
catch(ra::data_end const& e)
{
RightInput::reset();
}
}
auto const& active{ row_cache[join_type::output_row.head()] };
curr = active.cbegin();
end = active.cend();
}
static auto&& next()
{
while (curr == end)
{
copy(join_type::output_row, LeftInput::next());
auto const& active{ row_cache[join_type::output_row.head()] };
curr = active.cbegin();
end = active.cend();
}
copy(join_type::output_row, *curr++);
return std::move(join_type::output_row);
}
private:
static map_type row_cache;
static value_type::const_iterator curr;
static value_type::const_iterator end;
};
template <typename LeftInput, typename RightInput>
typename natural<LeftInput, RightInput>::map_type natural<LeftInput, RightInput>::row_cache{};
template <typename LeftInput, typename RightInput>
typename natural<LeftInput, RightInput>::value_type::const_iterator natural<LeftInput, RightInput>::curr;
template <typename LeftInput, typename RightInput>
typename natural<LeftInput, RightInput>::value_type::const_iterator natural<LeftInput, RightInput>::end;
} // namespace ra
namespace ra
{
template <typename Output, typename Input>
class projection : public ra::unary<Input>
{
using input_type = typename ra::unary<Input>::input_type;
public:
using output_type = Output;
static auto&& next()
{
fold<output_type>(output_row, Input::next());
return std::move(output_row);
}
private:
template <typename Dest>
static inline constexpr void fold(Dest& dest, input_type const& src)
{
if constexpr (Dest::depth == 0)
{
return;
}
else
{
dest.head() = sql::get<Dest::column::name>(src);
fold<typename Dest::next>(dest.tail(), src);
}
}
static output_type output_row;
};
template <typename Output, typename Input>
typename projection<Output, Input>::output_type projection<Output, Input>::output_row{};
} // namespace ra
namespace ra
{
template <typename Output, typename Input>
class rename : public ra::unary<Input>
{
using input_type = typename ra::unary<Input>::input_type;
public:
using output_type = Output;
static auto&& next()
{
fold<output_type, input_type>(output_row, Input::next());
return std::move(output_row);
}
private:
template <typename Dest, typename Src>
static inline constexpr void fold(Dest& dest, Src const& src)
{
if constexpr (Dest::depth == 0)
{
return;
}
else
{
dest.head() = src.head();
fold<typename Dest::next, typename Src::next>(dest.tail(), src.tail());
}
}
static output_type output_row;
};
template <typename Output, typename Input>
typename rename<Output, Input>::output_type rename<Output, Input>::output_row{};
} // namespace ra
namespace ra
{
template <typename Predicate, typename Input>
class selection : public ra::unary<Input>
{
using input_type = typename ra::unary<Input>::input_type;
public:
using output_type = input_type;
static auto&& next()
{
output_row = Input::next();
while(!Predicate::eval(output_row))
{
output_row = Input::next();
}
return std::move(output_row);
}
private:
static output_type output_row;
};
template <typename Output, typename Input>
typename selection<Output, Input>::output_type selection<Output, Input>::output_row{};
} // namespace ra
namespace sql
{
namespace
{
template <typename Char>
constexpr bool whitespace(Char curr)
{
return curr == Char{ ' ' } || curr == Char{ '\t' } || curr == Char{ '\n' };
}
template <typename Char>
constexpr bool syntax(Char curr)
{
return curr == Char{ ',' } || curr == Char{ '(' } || curr == Char{ ')' } ||
curr == Char{ '\'' } || curr == Char{ '\"' } || curr == Char{ '=' };
}