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
|
#include "statement.h"
#include <sstream>
#include <utility>
#include "filter.h"
#include "util.h"
#include "notion.h"
#include "word.h"
#include "group.h"
#include "frame.h"
#include "lemma.h"
#include "form.h"
#include "pronunciation.h"
namespace verbly {
statement::statement(
object context,
filter queryFilter) :
statement(getTableForContext(context), queryFilter.normalize(context))
{
}
std::string statement::getQueryString(std::list<std::string> select, bool random, int limit) const
{
std::stringstream queryStream;
if (!withs_.empty())
{
queryStream << "WITH RECURSIVE ";
std::list<std::string> ctes;
for (const with& cte : withs_)
{
std::stringstream cteStream;
cteStream << cte.getIdentifier();
cteStream << " AS (SELECT ";
cteStream << cte.getTopTable();
cteStream << ".* FROM ";
cteStream << cte.getTableForId(cte.getTopTable());
cteStream << " AS ";
cteStream << cte.getTopTable();
for (const join& j : cte.getJoins())
{
cteStream << " ";
cteStream << j;
}
if (cte.getCondition().getType() != condition::type::empty)
{
cteStream << " WHERE ";
cteStream << cte.getCondition().toSql();
}
if (cte.isRecursive())
{
cteStream << " UNION SELECT l.* FROM ";
cteStream << cte.getIdentifier();
cteStream << " AS t INNER JOIN ";
cteStream << cte.getField().getTable();
cteStream << " AS j ON t.";
cteStream << cte.getField().getColumn();
cteStream << " = j.";
cteStream << cte.getField().getForeignJoinColumn();
cteStream << " INNER JOIN ";
cteStream << cte.getTableForId(cte.getTopTable());
cteStream << " AS l ON j.";
cteStream << cte.getField().getJoinColumn();
cteStream << " = l.";
cteStream << cte.getField().getColumn();
}
cteStream << ")";
ctes.push_back(cteStream.str());
}
queryStream << implode(std::begin(ctes), std::end(ctes), ", ");
queryStream << " ";
}
std::list<std::string> realSelect;
for (std::string& s : select)
{
realSelect.push_back(topTable_ + "." + s);
}
queryStream << "SELECT ";
queryStream << implode(std::begin(realSelect), std::end(realSelect), ", ");
queryStream << " FROM ";
queryStream << tables_.at(topTable_);
queryStream << " AS ";
queryStream << topTable_;
for (const join& j : joins_)
{
queryStream << " ";
queryStream << j;
}
if (topCondition_.getType() != condition::type::empty)
{
queryStream << " WHERE ";
queryStream << topCondition_.toSql();
}
if (random)
{
queryStream << " ORDER BY RANDOM()";
}
if (limit > 0)
{
queryStream << " LIMIT ";
queryStream << limit;
}
return queryStream.str();
}
std::list<binding> statement::getBindings() const
{
std::list<binding> result;
for (const with& w : withs_)
{
for (binding value : w.getCondition().flattenBindings())
{
result.push_back(std::move(value));
}
}
for (binding value : topCondition_.flattenBindings())
{
result.push_back(std::move(value));
}
return result;
}
statement::statement(
std::string tableName,
filter clause,
int nextTableId,
int nextWithId) :
nextTableId_(nextTableId),
nextWithId_(nextWithId),
topTable_(instantiateTable(std::move(tableName))),
topCondition_(parseFilter(std::move(clause)))
{
}
/**
* This function recursively parses the query's filter condition. It is not
* idempotent. It returns a condition object representing the passed filter,
* but it also modifies the statement object, specifically by adding any joins
* and CTEs that may be required to represent the passed filter. This may also
* involve instantiating tables.
*/
statement::condition statement::parseFilter(filter clause)
{
switch (clause.getType())
{
case filter::type::empty:
{
return {};
}
case filter::type::singleton:
{
switch (clause.getField().getType())
{
case field::type::undefined:
{
return {};
}
// For primitive type filters, all we need to do is translate the
// filter object directly into a condition object. No joins are
// necessary.
case field::type::string:
case field::type::integer:
case field::type::boolean:
{
switch (clause.getComparison())
{
case filter::comparison::is_null:
{
return condition(topTable_, clause.getField().getColumn(), true);
}
case filter::comparison::is_not_null:
{
return condition(topTable_, clause.getField().getColumn(), false);
}
case filter::comparison::int_equals:
{
return condition(topTable_, clause.getField().getColumn(), condition::comparison::equals, clause.getIntegerArgument());
}
case filter::comparison::int_does_not_equal:
{
return condition(topTable_, clause.getField().getColumn(), condition::comparison::does_not_equal, clause.getIntegerArgument());
}
case filter::comparison::int_is_at_least:
{
return condition(topTable_, clause.getField().getColumn(), condition::comparison::is_at_least, clause.getIntegerArgument());
}
case filter::comparison::int_is_greater_than:
{
return condition(topTable_, clause.getField().getColumn(), condition::comparison::is_greater_than, clause.getIntegerArgument());
}
case filter::comparison::int_is_at_most:
{
return condition(topTable_, clause.getField().getColumn(), condition::comparison::is_at_most, clause.getIntegerArgument());
}
case filter::comparison::int_is_less_than:
{
return condition(topTable_, clause.getField().getColumn(), condition::comparison::is_less_than, clause.getIntegerArgument());
}
case filter::comparison::boolean_equals:
{
return condition(topTable_, clause.getField().getColumn(), condition::comparison::equals, clause.getBooleanArgument() ? 1 : 0);
}
case filter::comparison::string_equals:
{
return condition(topTable_, clause.getField().getColumn(), condition::comparison::equals, clause.getStringArgument());
}
case filter::comparison::string_does_not_equal:
{
return condition(topTable_, clause.getField().getColumn(), condition::comparison::does_not_equal, clause.getStringArgument());
}
case filter::comparison::string_is_like:
{
return condition(topTable_, clause.getField().getColumn(), condition::comparison::is_like, clause.getStringArgument());
}
case filter::comparison::string_is_not_like:
{
return condition(topTable_, clause.getField().getColumn(), condition::comparison::is_not_like, clause.getStringArgument());
}
case filter::comparison::matches:
case filter::comparison::does_not_match:
case filter::comparison::hierarchally_matches:
case filter::comparison::does_not_hierarchally_match:
{
throw std::logic_error("Invalid comparison type for field");
}
}
}
case field::type::join:
{
// First, figure out what table we need to join against.
std::string joinTableName;
if (clause.getField().hasTable())
{
joinTableName = clause.getField().getTable();
} else {
joinTableName = getTableForContext(clause.getField().getJoinObject());
}
// Recursively parse the subquery, and therefore obtain an
// instantiated table to join against, as well as any joins or CTEs
// that the subquery may require to function.
statement joinStmt(
joinTableName,
clause.getJoinCondition().normalize(clause.getField().getJoinObject()),
nextTableId_,
nextWithId_);
std::string joinTable = joinStmt.topTable_;
if (clause.getComparison() == filter::comparison::does_not_match)
{
// If the comparison is actually a negative filter, we can't just
// integrate the subquery statement into this statement and then
// join against it. Even if we LEFT JOIN against the subquery's
// top level table and then condition on the join column being
// NULL, if that table joins against any other table, the query
// will return zero results. Instead, we create a non-recursive
// CTE that represents the subquery, then LEFT JOIN against it and
// condition on the join column being NULL as before.
std::string withName = instantiateWith(clause.getField().getTable());
std::string withInstName = instantiateTable(withName);
// LEFT JOIN against the CTE.
joins_.emplace_back(
true,
withName,
topTable_,
clause.getField().getColumn(),
withInstName,
clause.getField().getColumn());
// All CTEs have to be in the main statement, so integrate any
// CTEs that our subquery uses. Also, retrieve the table mapping,
// joins list, and subquery condition, and use them to create the
// CTE.
std::map<std::string, std::string> cteTables = std::move(joinStmt.tables_);
std::list<join> cteJoins = std::move(joinStmt.joins_);
condition cteCondition = integrate(std::move(joinStmt), true);
withs_.emplace_back(
std::move(withName),
clause.getField(),
std::move(cteTables),
std::move(joinTable),
std::move(cteCondition),
std::move(cteJoins),
false);
// Condition on the join column being NULL, which causes the query
// to only return results that do not match the subquery.
return condition(std::move(withInstName), clause.getField().getColumn(), true);
} else {
// INNER JOIN against the top table of the subquery.
joins_.emplace_back(
false,
std::move(joinTableName),
topTable_,
clause.getField().getColumn(),
std::move(joinTable),
clause.getField().getColumn());
// Integrate the subquery's table mappings, joins, and CTEs into
// this statement, and return the subquery condition as our
// condition.
return integrate(std::move(joinStmt));
}
}
case field::type::join_through:
{
// Recursively parse the subquery, and therefore obtain an
// instantiated table to join against, as well as any joins or CTEs
// that the subquery may require to function.
statement joinStmt(
getTableForContext(clause.getField().getJoinObject()),
clause.getJoinCondition().normalize(clause.getField().getJoinObject()),
nextTableId_,
nextWithId_);
std::string joinTable = joinStmt.topTable_;
if (clause.getComparison() == filter::comparison::does_not_match)
{
// If the comparison is actually a negative filter, we can't just
// integrate the subquery statement into this statement and then
// join against it. Even if we LEFT JOIN against the subquery's
// through table and then condition on the join column being NULL,
// the query will return zero results because the through table
// joins against the subquery's top level table. Instead, we
// create a non-recursive CTE that represents the through table
// joined against the subquery, then LEFT JOIN against it and
// condition on the join column being NULL as before.
std::string withName = instantiateWith(clause.getField().getTable());
std::string withInstName = instantiateTable(withName);
// LEFT JOIN against the CTE.
joins_.emplace_back(
true,
withName,
topTable_,
clause.getField().getColumn(),
withInstName,
clause.getField().getJoinColumn());
// Modify the substatement such that the through table is the top
// table, and such that it joins against the previous top table.
std::string throughTable = joinStmt.instantiateTable(clause.getField().getTable());
joinStmt.joins_.emplace_back(
false,
getTableForContext(clause.getField().getJoinObject()),
throughTable,
clause.getField().getForeignJoinColumn(),
std::move(joinTable),
clause.getField().getForeignColumn());
joinStmt.topTable_ = throughTable;
// All CTEs have to be in the main statement, so integrate any
// CTEs that our subquery uses. Also, retrieve the table mapping,
// joins list, and subquery condition, and use them to create the
// CTE.
std::map<std::string, std::string> cteTables = std::move(joinStmt.tables_);
std::list<join> cteJoins = std::move(joinStmt.joins_);
condition cteCondition = integrate(std::move(joinStmt), true);
withs_.emplace_back(
std::move(withName),
clause.getField(),
std::move(cteTables),
std::move(throughTable),
std::move(cteCondition),
std::move(cteJoins),
false);
// Condition on the join column being NULL, which causes the query
// to only return results that do not match the subquery.
return condition(std::move(withInstName), clause.getField().getJoinColumn(), true);
} else {
// Instantiate the through table.
std::string throughTable = instantiateTable(clause.getField().getTable());
// INNER JOIN against the through table.
joins_.emplace_back(
false,
clause.getField().getTable(),
topTable_,
clause.getField().getColumn(),
throughTable,
clause.getField().getJoinColumn());
// INNER JOIN from the through table to the top table of the subquery.
joins_.emplace_back(
false,
getTableForContext(clause.getField().getJoinObject()),
std::move(throughTable),
clause.getField().getForeignJoinColumn(),
std::move(joinTable),
clause.getField().getForeignColumn());
// Integrate the subquery's table mappings, joins, and CTEs into
// this statement, and return the subquery condition as our
// condition.
return integrate(std::move(joinStmt));
}
}
case field::type::hierarchal_join:
{
// Create a recursive CTE that represents the results of the subquery.
std::string withName = instantiateWith(clause.getField().getTable());
std::string withInstName = instantiateTable(withName);
// If we are matching against the subquery, we INNER JOIN with the
// CTE. If we are negatively matching the subquery, we LEFT JOIN
// with the CTE.
bool outer = false;
if (clause.getComparison() == filter::comparison::does_not_hierarchally_match)
{
outer = true;
}
// Join against the CTE.
joins_.emplace_back(
outer,
withName,
topTable_,
clause.getField().getColumn(),
withInstName,
clause.getField().getColumn());
// Recursively parse the subquery in order to create the CTE.
statement withStmt(
getTableForContext(clause.getField().getObject()),
clause.getJoinCondition().normalize(clause.getField().getObject()),
nextTableId_,
nextWithId_);
// All CTEs have to be in the main statement, so integrate any CTEs
// that our subquery uses. Also, retrieve the table mapping, joins
// list, and subquery condition, and use them to create the CTE.
std::string cteTopTable = std::move(withStmt.topTable_);
std::map<std::string, std::string> cteTables = std::move(withStmt.tables_);
std::list<join> cteJoins = std::move(withStmt.joins_);
condition cteCondition = integrate(std::move(withStmt), true);
withs_.emplace_back(
std::move(withName),
clause.getField(),
std::move(cteTables),
std::move(cteTopTable),
std::move(cteCondition),
std::move(cteJoins),
true);
// If we are matching against the subquery, no condition is
// necessary. If we are negatively matching the subquery, we
// condition on the join column being NULL.
if (clause.getComparison() == filter::comparison::does_not_hierarchally_match)
{
return condition(withInstName, clause.getField().getColumn(), true);
} else {
return {};
}
}
}
}
case filter::type::group:
{
condition grp(clause.getOrlogic());
for (const filter& child : clause)
{
condition newChild = parseFilter(child);
if (newChild.getType() != condition::type::empty)
{
grp += std::move(newChild);
}
}
if (grp.getChildren().empty())
{
grp = {};
}
return grp;
}
}
}
std::string statement::instantiateTable(std::string name)
{
std::string identifier = name + "_" + std::to_string(nextTableId_++);
tables_[identifier] = name;
return identifier;
}
std::string statement::instantiateWith(std::string name)
{
return name + "_tree_" + std::to_string(nextWithId_++);
}
/**
* This method integrates the parts of a recursively generated statement into
* this statement. This is used because filters are recursive objects, but
* statements need to be flat to be compiled into a SQL query. Thus, all CTEs
* have to be in the main statement, and all table mappings & joins that
* aren't part of a CTE have to be in the main statement as well. Finally, we
* need to copy up the next ID fields in order to properly prevent ID reuse.
*/
statement::condition statement::integrate(statement subStmt, bool cte)
{
if (!cte)
{
for (auto& mapping : subStmt.tables_)
{
tables_[mapping.first] = mapping.second;
}
for (auto& j : subStmt.joins_)
{
joins_.push_back(j);
}
}
for (auto& w : subStmt.withs_)
{
withs_.push_back(w);
}
nextTableId_ = subStmt.nextTableId_;
nextWithId_ = subStmt.nextWithId_;
return subStmt.topCondition_;
}
std::ostream& operator<<(std::ostream& oss, const statement::join& j)
{
if (j.isOuterJoin())
{
oss << "LEFT";
} else {
oss << "INNER";
}
return oss
<< " JOIN "
<< j.getForeignTableName()
<< " AS "
<< j.getForeignTable()
<< " ON "
<< j.getForeignTable()
<< "."
<< j.getForeignColumn()
<< " = "
<< j.getJoinTable()
<< "."
<< j.getJoinColumn();
}
statement::condition::condition(const condition& other)
{
type_ = other.type_;
switch (type_)
{
case type::empty:
{
break;
}
case type::singleton:
{
new(&singleton_.table_) std::string(other.singleton_.table_);
new(&singleton_.column_) std::string(other.singleton_.column_);
singleton_.comparison_ = other.singleton_.comparison_;
new(&singleton_.value_) binding(other.singleton_.value_);
break;
}
case type::group:
{
new(&group_.children_) std::list<condition>(other.group_.children_);
group_.orlogic_ = other.group_.orlogic_;
break;
}
}
}
statement::condition::condition(condition&& other) : condition()
{
swap(*this, other);
}
statement::condition& statement::condition::operator=(condition other)
{
swap(*this, other);
return *this;
}
void swap(statement::condition& first, statement::condition& second)
{
using type = statement::condition::type;
using condition = statement::condition;
type tempType = first.type_;
std::string tempTable;
std::string tempColumn;
condition::comparison tempComparison;
binding tempBinding;
std::list<condition> tempChildren;
bool tempOrlogic;
switch (tempType)
{
case type::empty:
{
break;
}
case type::singleton:
{
tempTable = std::move(first.singleton_.table_);
tempColumn = std::move(first.singleton_.column_);
tempComparison = first.singleton_.comparison_;
tempBinding = std::move(first.singleton_.value_);
break;
}
case type::group:
{
tempChildren = std::move(first.group_.children_);
tempOrlogic = first.group_.orlogic_;
break;
}
}
first.~condition();
first.type_ = second.type_;
switch (first.type_)
{
case type::empty:
{
break;
}
case type::singleton:
{
new(&first.singleton_.table_) std::string(std::move(second.singleton_.table_));
new(&first.singleton_.column_) std::string(std::move(second.singleton_.column_));
first.singleton_.comparison_ = second.singleton_.comparison_;
new(&first.singleton_.value_) binding(std::move(second.singleton_.value_));
break;
}
case type::group:
{
new(&first.group_.children_) std::list<condition>(std::move(second.group_.children_));
first.group_.orlogic_ = second.group_.orlogic_;
break;
}
}
second.~condition();
second.type_ = tempType;
switch (second.type_)
{
case type::empty:
{
break;
}
case type::singleton:
{
new(&second.singleton_.table_) std::string(std::move(tempTable));
new(&second.singleton_.column_) std::string(std::move(tempColumn));
second.singleton_.comparison_ = tempComparison;
new(&second.singleton_.value_) binding(std::move(tempBinding));
break;
}
case type::group:
{
new(&second.group_.children_) std::list<condition>(std::move(tempChildren));
second.group_.orlogic_ = tempOrlogic;
break;
}
}
}
statement::condition::~condition()
{
switch (type_)
{
case type::empty:
{
break;
}
case type::singleton:
{
using string_type = std::string;
singleton_.table_.~string_type();
singleton_.column_.~string_type();
singleton_.value_.~binding();
break;
}
case type::group:
{
using list_type = std::list<condition>;
group_.children_.~list_type();
break;
}
}
}
statement::condition::condition() : type_(type::empty)
{
}
statement::condition::condition(
std::string table,
std::string column,
bool isNull) :
type_(type::singleton)
{
new(&singleton_.table_) std::string(std::move(table));
new(&singleton_.column_) std::string(std::move(column));
if (isNull)
{
singleton_.comparison_ = comparison::is_null;
} else {
singleton_.comparison_ = comparison::is_not_null;
}
}
statement::condition::condition(
std::string table,
std::string column,
comparison comp,
binding value) :
type_(type::singleton)
{
new(&singleton_.table_) std::string(std::move(table));
new(&singleton_.column_) std::string(std::move(column));
singleton_.comparison_ = comp;
new(&singleton_.value_) binding(std::move(value));
}
std::string statement::condition::toSql() const
{
switch (type_)
{
case type::empty:
{
return "";
}
case type::singleton:
{
switch (singleton_.comparison_)
{
case comparison::equals:
{
return singleton_.table_ + "." + singleton_.column_ + " = ?";
}
case comparison::does_not_equal:
{
return singleton_.table_ + "." + singleton_.column_ + " != ?";
}
case comparison::is_greater_than:
{
return singleton_.table_ + "." + singleton_.column_ + " > ?";
}
case comparison::is_at_most:
{
return singleton_.table_ + "." + singleton_.column_ + " <= ?";
}
case comparison::is_less_than:
{
return singleton_.table_ + "." + singleton_.column_ + " < ?";
}
case comparison::is_at_least:
{
return singleton_.table_ + "." + singleton_.column_ + " >= ?";
}
case comparison::is_like:
{
return singleton_.table_ + "." + singleton_.column_ + " LIKE ?";
}
case comparison::is_not_like:
{
return singleton_.table_ + "." + singleton_.column_ + " NOT LIKE ?";
}
case comparison::is_not_null:
{
return singleton_.table_ + "." + singleton_.column_ + " IS NOT NULL";
}
case comparison::is_null:
{
return singleton_.table_ + "." + singleton_.column_ + " IS NULL";
}
}
}
case type::group:
{
std::list<std::string> clauses;
for (const condition& cond : group_.children_)
{
clauses.push_back(cond.toSql());
}
return implode(std::begin(clauses), std::end(clauses), group_.orlogic_ ? " OR " : " AND ");
}
}
}
std::list<binding> statement::condition::flattenBindings() const
{
switch (type_)
{
case type::empty:
{
return {};
}
case type::singleton:
{
switch (singleton_.comparison_)
{
case comparison::equals:
case comparison::does_not_equal:
case comparison::is_greater_than:
case comparison::is_at_most:
case comparison::is_less_than:
case comparison::is_at_least:
case comparison::is_like:
case comparison::is_not_like:
{
return {singleton_.value_};
}
case comparison::is_not_null:
case comparison::is_null:
{
return {};
}
}
}
case type::group:
{
std::list<binding> bindings;
for (const condition& cond : group_.children_)
{
for (binding value : cond.flattenBindings())
{
bindings.push_back(std::move(value));
}
}
return bindings;
}
}
}
statement::condition::condition(bool orlogic) : type_(type::group)
{
new(&group_.children_) std::list<condition>();
group_.orlogic_ = orlogic;
}
statement::condition& statement::condition::operator+=(condition n)
{
if (type_ == type::group)
{
group_.children_.push_back(std::move(n));
return *this;
} else {
throw std::domain_error("Cannot add condition to non-group condition");
}
}
statement::condition& statement::condition::operator&=(condition n)
{
switch (type_)
{
case type::empty:
{
*this = std::move(n);
break;
}
case type::singleton:
{
condition grp(false);
grp += *this;
grp += std::move(n);
*this = grp;
break;
}
case type::group:
{
*this += std::move(n);
break;
}
}
return *this;
}
const std::list<statement::condition>& statement::condition::getChildren() const
{
if (type_ == type::group)
{
return group_.children_;
} else {
throw std::domain_error("Cannot get children of non-group condition");
}
}
};
|