Repository navigation
Expand file tree
/
Copy pathto_sql.cpp
More file actions
807 lines (598 loc) · 26.6 KB
/
Copy pathto_sql.cpp
File metadata and controls
807 lines (598 loc) · 26.6 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
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
#include "sqlgen/postgres/to_sql.hpp"
#include <ranges>
#include <rfl.hpp>
#include <sstream>
#include <stdexcept>
#include <type_traits>
#include "sqlgen/dynamic/Join.hpp"
#include "sqlgen/dynamic/Operation.hpp"
#include "sqlgen/internal/collect/vector.hpp"
#include "sqlgen/internal/strings/strings.hpp"
namespace sqlgen::postgres {
std::string aggregation_to_sql(
const dynamic::Aggregation& _aggregation) noexcept;
std::string column_or_value_to_sql(const dynamic::ColumnOrValue& _col) noexcept;
std::string condition_to_sql(const dynamic::Condition& _cond) noexcept;
template <class ConditionType>
std::string condition_to_sql_impl(const ConditionType& _condition) noexcept;
std::string column_to_sql_definition(const dynamic::Column& _col) noexcept;
std::string create_index_to_sql(const dynamic::CreateIndex& _stmt) noexcept;
std::string create_table_to_sql(const dynamic::CreateTable& _stmt) noexcept;
std::string create_as_to_sql(const dynamic::CreateAs& _stmt) noexcept;
std::string delete_from_to_sql(const dynamic::DeleteFrom& _stmt) noexcept;
std::string drop_to_sql(const dynamic::Drop& _stmt) noexcept;
std::string escape_single_quote(const std::string& _str) noexcept;
std::string field_to_str(const dynamic::SelectFrom::Field& _field) noexcept;
std::vector<std::string> get_primary_keys(
const dynamic::CreateTable& _stmt) noexcept;
std::string insert_to_sql(const dynamic::Insert& _stmt) noexcept;
std::string join_to_sql(const dynamic::Join& _stmt) noexcept;
std::string operation_to_sql(const dynamic::Operation& _stmt) noexcept;
std::string properties_to_sql(
const dynamic::types::Properties& _properties) noexcept;
std::string select_from_to_sql(const dynamic::SelectFrom& _stmt) noexcept;
std::string table_or_query_to_sql(
const dynamic::SelectFrom::TableOrQueryType& _table_or_query) noexcept;
std::string type_to_sql(const dynamic::Type& _type) noexcept;
std::string update_to_sql(const dynamic::Update& _stmt) noexcept;
std::string write_to_sql(const dynamic::Write& _stmt) noexcept;
// ----------------------------------------------------------------------------
inline std::string get_name(const dynamic::Column& _col) { return _col.name; }
inline std::string wrap_in_quotes(const std::string& _name) noexcept {
return "\"" + _name + "\"";
}
// ----------------------------------------------------------------------------
std::string aggregation_to_sql(
const dynamic::Aggregation& _aggregation) noexcept {
return _aggregation.val.visit([](const auto& _agg) -> std::string {
using Type = std::remove_cvref_t<decltype(_agg)>;
std::stringstream stream;
if constexpr (std::is_same_v<Type, dynamic::Aggregation::Avg>) {
stream << "AVG(" << operation_to_sql(*_agg.val) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Aggregation::Count>) {
const auto val =
std::string(_agg.val && _agg.distinct ? "DISTINCT " : "") +
(_agg.val ? column_or_value_to_sql(*_agg.val) : std::string("*"));
stream << "COUNT(" << val << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Aggregation::Max>) {
stream << "MAX(" << operation_to_sql(*_agg.val) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Aggregation::Min>) {
stream << "MIN(" << operation_to_sql(*_agg.val) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Aggregation::Sum>) {
stream << "SUM(" << operation_to_sql(*_agg.val) << ")";
} else {
static_assert(rfl::always_false_v<Type>, "Not all cases were covered.");
}
return stream.str();
});
}
std::string column_or_value_to_sql(
const dynamic::ColumnOrValue& _col) noexcept {
const auto handle_value = [](const auto& _v) -> std::string {
using Type = std::remove_cvref_t<decltype(_v)>;
if constexpr (std::is_same_v<Type, dynamic::String>) {
return "'" + escape_single_quote(_v.val) + "'";
} else if constexpr (std::is_same_v<Type, dynamic::Duration>) {
return "INTERVAL '" + std::to_string(_v.val) + " " +
rfl::enum_to_string(_v.unit) + "'";
} else if constexpr (std::is_same_v<Type, dynamic::Timestamp>) {
return "to_timestamp(" + std::to_string(_v.seconds_since_unix) + ")";
} else {
return std::to_string(_v.val);
}
};
return _col.visit([&](const auto& _c) -> std::string {
using Type = std::remove_cvref_t<decltype(_c)>;
if constexpr (std::is_same_v<Type, dynamic::Column>) {
if (_c.alias) {
return *_c.alias + "." + wrap_in_quotes(_c.name);
} else {
return wrap_in_quotes(_c.name);
}
} else {
return _c.val.visit(handle_value);
}
});
}
std::string condition_to_sql(const dynamic::Condition& _cond) noexcept {
return _cond.val.visit(
[&](const auto& _c) { return condition_to_sql_impl(_c); });
}
template <class ConditionType>
std::string condition_to_sql_impl(const ConditionType& _condition) noexcept {
using C = std::remove_cvref_t<ConditionType>;
std::stringstream stream;
if constexpr (std::is_same_v<C, dynamic::Condition::And>) {
stream << "(" << condition_to_sql(*_condition.cond1) << ") AND ("
<< condition_to_sql(*_condition.cond2) << ")";
} else if constexpr (std::is_same_v<C, dynamic::Condition::Equal>) {
stream << operation_to_sql(_condition.op1) << " = "
<< operation_to_sql(_condition.op2);
} else if constexpr (std::is_same_v<C, dynamic::Condition::GreaterEqual>) {
stream << operation_to_sql(_condition.op1)
<< " >= " << operation_to_sql(_condition.op2);
} else if constexpr (std::is_same_v<C, dynamic::Condition::GreaterThan>) {
stream << operation_to_sql(_condition.op1) << " > "
<< operation_to_sql(_condition.op2);
} else if constexpr (std::is_same_v<C, dynamic::Condition::IsNull>) {
stream << operation_to_sql(_condition.op) << " IS NULL";
} else if constexpr (std::is_same_v<C, dynamic::Condition::IsNotNull>) {
stream << operation_to_sql(_condition.op) << " IS NOT NULL";
} else if constexpr (std::is_same_v<C, dynamic::Condition::LesserEqual>) {
stream << operation_to_sql(_condition.op1)
<< " <= " << operation_to_sql(_condition.op2);
} else if constexpr (std::is_same_v<C, dynamic::Condition::LesserThan>) {
stream << operation_to_sql(_condition.op1) << " < "
<< operation_to_sql(_condition.op2);
} else if constexpr (std::is_same_v<C, dynamic::Condition::Like>) {
stream << operation_to_sql(_condition.op) << " LIKE "
<< column_or_value_to_sql(_condition.pattern);
} else if constexpr (std::is_same_v<C, dynamic::Condition::Not>) {
stream << "NOT (" << condition_to_sql(*_condition.cond) << ")";
} else if constexpr (std::is_same_v<C, dynamic::Condition::NotEqual>) {
stream << operation_to_sql(_condition.op1)
<< " != " << operation_to_sql(_condition.op2);
} else if constexpr (std::is_same_v<C, dynamic::Condition::NotLike>) {
stream << operation_to_sql(_condition.op) << " NOT LIKE "
<< column_or_value_to_sql(_condition.pattern);
} else if constexpr (std::is_same_v<C, dynamic::Condition::Or>) {
stream << "(" << condition_to_sql(*_condition.cond1) << ") OR ("
<< condition_to_sql(*_condition.cond2) << ")";
} else {
static_assert(rfl::always_false_v<C>, "Not all cases were covered.");
}
return stream.str();
}
std::string column_to_sql_definition(const dynamic::Column& _col) noexcept {
return wrap_in_quotes(_col.name) + " " + type_to_sql(_col.type) +
properties_to_sql(
_col.type.visit([](const auto& _t) { return _t.properties; }));
}
std::string create_index_to_sql(const dynamic::CreateIndex& _stmt) noexcept {
using namespace std::ranges::views;
std::stringstream stream;
if (_stmt.unique) {
stream << "CREATE UNIQUE INDEX ";
} else {
stream << "CREATE INDEX ";
}
if (_stmt.if_not_exists) {
stream << "IF NOT EXISTS ";
}
stream << "\"" << _stmt.name << "\" ";
stream << "ON ";
if (_stmt.table.schema) {
stream << "\"" << *_stmt.table.schema << "\".";
}
stream << "\"" << _stmt.table.name << "\"";
stream << "(";
stream << internal::strings::join(
", ",
internal::collect::vector(_stmt.columns | transform(wrap_in_quotes)));
stream << ")";
if (_stmt.where) {
stream << " WHERE " << condition_to_sql(*_stmt.where);
}
stream << ";";
return stream.str();
}
std::string create_table_to_sql(const dynamic::CreateTable& _stmt) noexcept {
using namespace std::ranges::views;
const auto col_to_sql = [&](const auto& _col) {
return column_to_sql_definition(_col);
};
std::stringstream stream;
stream << "CREATE TABLE ";
if (_stmt.if_not_exists) {
stream << "IF NOT EXISTS ";
}
if (_stmt.table.schema) {
stream << wrap_in_quotes(*_stmt.table.schema) << ".";
}
stream << wrap_in_quotes(_stmt.table.name) << " ";
stream << "(";
stream << internal::strings::join(
", ", internal::collect::vector(_stmt.columns | transform(col_to_sql)));
const auto primary_keys = get_primary_keys(_stmt);
if (primary_keys.size() != 0) {
stream << ", PRIMARY KEY (" << internal::strings::join(", ", primary_keys)
<< ")";
}
stream << ");";
return stream.str();
}
std::string create_as_to_sql(const dynamic::CreateAs& _stmt) noexcept {
std::stringstream stream;
stream << "CREATE ";
if (_stmt.or_replace) {
stream << "OR REPLACE ";
}
stream << internal::strings::replace_all(
internal::strings::to_upper(rfl::enum_to_string(_stmt.what)),
"_", " ")
<< " ";
if (_stmt.if_not_exists) {
stream << "IF NOT EXISTS ";
}
if (_stmt.table_or_view.schema) {
stream << wrap_in_quotes(*_stmt.table_or_view.schema) << ".";
}
stream << wrap_in_quotes(_stmt.table_or_view.name) << " AS ";
stream << select_from_to_sql(_stmt.query);
return stream.str();
}
std::string delete_from_to_sql(const dynamic::DeleteFrom& _stmt) noexcept {
std::stringstream stream;
stream << "DELETE FROM ";
if (_stmt.table.schema) {
stream << wrap_in_quotes(*_stmt.table.schema) << ".";
}
stream << wrap_in_quotes(_stmt.table.name);
if (_stmt.where) {
stream << " WHERE " << condition_to_sql(*_stmt.where);
}
stream << ";";
return stream.str();
}
std::string drop_to_sql(const dynamic::Drop& _stmt) noexcept {
std::stringstream stream;
stream << "DROP "
<< internal::strings::replace_all(
internal::strings::to_upper(rfl::enum_to_string(_stmt.what)),
"_", " ")
<< " ";
if (_stmt.if_exists) {
stream << "IF EXISTS ";
}
if (_stmt.table.schema) {
stream << wrap_in_quotes(*_stmt.table.schema) << ".";
}
stream << wrap_in_quotes(_stmt.table.name);
if (_stmt.cascade) {
stream << " CASCADE";
}
stream << ";";
return stream.str();
}
std::string escape_single_quote(const std::string& _str) noexcept {
return internal::strings::replace_all(_str, "'", "''");
}
std::string field_to_str(const dynamic::SelectFrom::Field& _field) noexcept {
std::stringstream stream;
stream << operation_to_sql(_field.val);
if (_field.as) {
stream << " AS " << wrap_in_quotes(*_field.as);
}
return stream.str();
}
std::vector<std::string> get_primary_keys(
const dynamic::CreateTable& _stmt) noexcept {
using namespace std::ranges::views;
const auto is_primary_key = [](const auto& _col) -> bool {
return _col.type.visit(
[](const auto& _t) -> bool { return _t.properties.primary; });
};
return internal::collect::vector(_stmt.columns | filter(is_primary_key) |
transform(get_name) |
transform(wrap_in_quotes));
}
std::string insert_to_sql(const dynamic::Insert& _stmt) noexcept {
using namespace std::ranges::views;
const auto to_placeholder = [](const size_t _i) -> std::string {
return "$" + std::to_string(_i + 1);
};
std::stringstream stream;
stream << "INSERT INTO ";
if (_stmt.table.schema) {
stream << "\"" << *_stmt.table.schema << "\".";
}
stream << "\"" << _stmt.table.name << "\"";
stream << " (";
stream << internal::strings::join(
", ",
internal::collect::vector(_stmt.columns | transform(wrap_in_quotes)));
stream << ")";
stream << " VALUES (";
stream << internal::strings::join(
", ", internal::collect::vector(
iota(static_cast<size_t>(0), _stmt.columns.size()) |
transform(to_placeholder)));
stream << ");";
return stream.str();
}
std::string join_to_sql(const dynamic::Join& _stmt) noexcept {
std::stringstream stream;
stream << internal::strings::to_upper(internal::strings::replace_all(
rfl::enum_to_string(_stmt.how), "_", " "))
<< " " << table_or_query_to_sql(_stmt.table_or_query) << " "
<< _stmt.alias << " ";
if (_stmt.on) {
stream << "ON " << condition_to_sql(*_stmt.on);
} else {
stream << "ON 1 = 1";
}
return stream.str();
}
std::string operation_to_sql(const dynamic::Operation& _stmt) noexcept {
using namespace std::ranges::views;
return _stmt.val.visit([](const auto& _s) -> std::string {
using Type = std::remove_cvref_t<decltype(_s)>;
std::stringstream stream;
if constexpr (std::is_same_v<Type, dynamic::Operation::Abs>) {
stream << "abs(" << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Aggregation>) {
stream << aggregation_to_sql(_s);
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Cast>) {
stream << "cast(" << operation_to_sql(*_s.op1) << " as "
<< type_to_sql(_s.target_type) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Coalesce>) {
stream << "coalesce("
<< internal::strings::join(
", ", internal::collect::vector(
_s.ops | transform([](const auto& _op) {
return operation_to_sql(*_op);
})))
<< ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Ceil>) {
stream << "ceil(" << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Column>) {
stream << column_or_value_to_sql(_s);
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Concat>) {
stream << "("
<< internal::strings::join(
" || ", internal::collect::vector(
_s.ops | transform([](const auto& _op) {
return operation_to_sql(*_op);
})))
<< ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Cos>) {
stream << "cos(" << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type,
dynamic::Operation::DatePlusDuration>) {
stream << operation_to_sql(*_s.date) << " + "
<< internal::strings::join(
" + ",
internal::collect::vector(
_s.durations | transform([](const auto& _d) {
return column_or_value_to_sql(dynamic::Value{_d});
})));
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Day>) {
stream << "extract(DAY from " << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type,
dynamic::Operation::DaysBetween>) {
stream << "cast(" << operation_to_sql(*_s.op2) << " as DATE) - cast("
<< operation_to_sql(*_s.op1) << " as DATE)";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Divides>) {
stream << "(" << operation_to_sql(*_s.op1) << ") / ("
<< operation_to_sql(*_s.op2) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Exp>) {
stream << "exp(" << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Floor>) {
stream << "floor(" << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Hour>) {
stream << "extract(HOUR from " << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Length>) {
stream << "length(" << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Ln>) {
stream << "ln(" << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Log2>) {
stream << "log(2.0, " << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Lower>) {
stream << "lower(" << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::LTrim>) {
stream << "ltrim(" << operation_to_sql(*_s.op1) << ", "
<< operation_to_sql(*_s.op2) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Minus>) {
stream << "(" << operation_to_sql(*_s.op1) << ") - ("
<< operation_to_sql(*_s.op2) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Minute>) {
stream << "extract(MINUTE from " << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Mod>) {
stream << "mod(" << operation_to_sql(*_s.op1) << ", "
<< operation_to_sql(*_s.op2) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Month>) {
stream << "extract(MONTH from " << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Multiplies>) {
stream << "(" << operation_to_sql(*_s.op1) << ") * ("
<< operation_to_sql(*_s.op2) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Plus>) {
stream << "(" << operation_to_sql(*_s.op1) << ") + ("
<< operation_to_sql(*_s.op2) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Replace>) {
stream << "replace(" << operation_to_sql(*_s.op1) << ", "
<< operation_to_sql(*_s.op2) << ", " << operation_to_sql(*_s.op3)
<< ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Round>) {
stream << "round(" << operation_to_sql(*_s.op1) << ", "
<< operation_to_sql(*_s.op2) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::RTrim>) {
stream << "rtrim(" << operation_to_sql(*_s.op1) << ", "
<< operation_to_sql(*_s.op2) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Second>) {
stream << "extract(SECOND from " << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Sin>) {
stream << "sin(" << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Sqrt>) {
stream << "sqrt(" << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Tan>) {
stream << "tan(" << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Trim>) {
stream << "trim(" << operation_to_sql(*_s.op1) << ", "
<< operation_to_sql(*_s.op2) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Unixepoch>) {
stream << "extract(EPOCH FROM " << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Upper>) {
stream << "upper(" << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Value>) {
stream << column_or_value_to_sql(_s);
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Weekday>) {
stream << "extract(DOW from " << operation_to_sql(*_s.op1) << ")";
} else if constexpr (std::is_same_v<Type, dynamic::Operation::Year>) {
stream << "extract(YEAR from " << operation_to_sql(*_s.op1) << ")";
} else {
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
}
return stream.str();
});
}
std::string properties_to_sql(const dynamic::types::Properties& _p) noexcept {
return [&]() -> std::string {
return std::string(_p.auto_incr ? " GENERATED ALWAYS AS IDENTITY" : "") +
std::string(_p.nullable ? "" : " NOT NULL") +
std::string(_p.unique ? " UNIQUE" : "");
}() + [&]() -> std::string {
if (!_p.foreign_key_reference) {
return "";
}
const auto& ref = *_p.foreign_key_reference;
return " REFERENCES " + wrap_in_quotes(ref.table) + "(" +
wrap_in_quotes(ref.column) + ")";
}();
}
std::string select_from_to_sql(const dynamic::SelectFrom& _stmt) noexcept {
using namespace std::ranges::views;
const auto order_by_to_str = [](const auto& _w) -> std::string {
return column_or_value_to_sql(_w.column) + (_w.desc ? " DESC" : "");
};
std::stringstream stream;
stream << "SELECT ";
stream << internal::strings::join(
", ", internal::collect::vector(_stmt.fields | transform(field_to_str)));
stream << " FROM " << table_or_query_to_sql(_stmt.table_or_query);
if (_stmt.alias) {
stream << " " << *_stmt.alias;
}
if (_stmt.joins) {
stream << " "
<< internal::strings::join(
" ", internal::collect::vector(*_stmt.joins |
transform(join_to_sql)));
}
if (_stmt.where) {
stream << " WHERE " << condition_to_sql(*_stmt.where);
}
if (_stmt.group_by) {
stream << " GROUP BY "
<< internal::strings::join(
", ",
internal::collect::vector(_stmt.group_by->columns |
transform(column_or_value_to_sql)));
}
if (_stmt.order_by) {
stream << " ORDER BY "
<< internal::strings::join(
", ", internal::collect::vector(_stmt.order_by->columns |
transform(order_by_to_str)));
}
if (_stmt.limit) {
stream << " LIMIT " << _stmt.limit->val;
}
return stream.str();
}
std::string table_or_query_to_sql(
const dynamic::SelectFrom::TableOrQueryType& _table_or_query) noexcept {
return _table_or_query.visit([](const auto& _t) -> std::string {
using Type = std::remove_cvref_t<decltype(_t)>;
if constexpr (std::is_same_v<Type, dynamic::Table>) {
return wrap_in_quotes(_t.name);
} else {
return "(" + select_from_to_sql(*_t) + ")";
}
});
}
std::string to_sql_impl(const dynamic::Statement& _stmt) noexcept {
return _stmt.visit([&](const auto& _s) -> std::string {
using S = std::remove_cvref_t<decltype(_s)>;
if constexpr (std::is_same_v<S, dynamic::CreateIndex>) {
return create_index_to_sql(_s);
} else if constexpr (std::is_same_v<S, dynamic::CreateTable>) {
return create_table_to_sql(_s);
} else if constexpr (std::is_same_v<S, dynamic::CreateAs>) {
return create_as_to_sql(_s);
} else if constexpr (std::is_same_v<S, dynamic::DeleteFrom>) {
return delete_from_to_sql(_s);
} else if constexpr (std::is_same_v<S, dynamic::Drop>) {
return drop_to_sql(_s);
} else if constexpr (std::is_same_v<S, dynamic::Insert>) {
return insert_to_sql(_s);
} else if constexpr (std::is_same_v<S, dynamic::SelectFrom>) {
return select_from_to_sql(_s);
} else if constexpr (std::is_same_v<S, dynamic::Update>) {
return update_to_sql(_s);
} else if constexpr (std::is_same_v<S, dynamic::Write>) {
return write_to_sql(_s);
} else {
static_assert(rfl::always_false_v<S>, "Unsupported type.");
}
});
}
std::string type_to_sql(const dynamic::Type& _type) noexcept {
return _type.visit([](const auto _t) -> std::string {
using T = std::remove_cvref_t<decltype(_t)>;
if constexpr (std::is_same_v<T, dynamic::types::Boolean>) {
return "BOOLEAN";
} else if constexpr (std::is_same_v<T, dynamic::types::Dynamic>) {
return _t.type_name;
} else if constexpr (std::is_same_v<T, dynamic::types::Int8> ||
std::is_same_v<T, dynamic::types::Int16> ||
std::is_same_v<T, dynamic::types::UInt8> ||
std::is_same_v<T, dynamic::types::UInt16>) {
return "SMALLINT";
} else if constexpr (std::is_same_v<T, dynamic::types::Int32> ||
std::is_same_v<T, dynamic::types::UInt32>) {
return "INTEGER";
} else if constexpr (std::is_same_v<T, dynamic::types::Int64> ||
std::is_same_v<T, dynamic::types::UInt64>) {
return "BIGINT";
} else if constexpr (std::is_same_v<T, dynamic::types::Float32> ||
std::is_same_v<T, dynamic::types::Float64>) {
return "NUMERIC";
} else if constexpr (std::is_same_v<T, dynamic::types::Text>) {
return "TEXT";
} else if constexpr (std::is_same_v<T, dynamic::types::VarChar>) {
return "VARCHAR(" + std::to_string(_t.length) + ")";
} else if constexpr (std::is_same_v<T, dynamic::types::Date>) {
return "DATE";
} else if constexpr (std::is_same_v<T, dynamic::types::Timestamp>) {
return "TIMESTAMP";
} else if constexpr (std::is_same_v<T, dynamic::types::TimestampWithTZ>) {
return "TIMESTAMP WITH TIME ZONE";
} else if constexpr (std::is_same_v<T, dynamic::types::Unknown>) {
return "TEXT";
} else {
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
}
});
}
std::string update_to_sql(const dynamic::Update& _stmt) noexcept {
using namespace std::ranges::views;
const auto to_str = [](const auto& _set) -> std::string {
return wrap_in_quotes(_set.col.name) + " = " +
column_or_value_to_sql(_set.to);
};
std::stringstream stream;
stream << "UPDATE ";
if (_stmt.table.schema) {
stream << wrap_in_quotes(*_stmt.table.schema) << ".";
}
stream << wrap_in_quotes(_stmt.table.name);
stream << " SET ";
stream << internal::strings::join(
", ", internal::collect::vector(_stmt.sets | transform(to_str)));
if (_stmt.where) {
stream << " WHERE " << condition_to_sql(*_stmt.where);
}
stream << ";";
return stream.str();
}
std::string write_to_sql(const dynamic::Write& _stmt) noexcept {
using namespace std::ranges::views;
const auto schema = wrap_in_quotes(_stmt.table.schema.value_or("public"));
const auto table = wrap_in_quotes(_stmt.table.name);
const auto colnames = internal::strings::join(
", ",
internal::collect::vector(_stmt.columns | transform(wrap_in_quotes)));
return "COPY " + schema + "." + table + "(" + colnames +
") FROM STDIN WITH DELIMITER '\t' NULL '\e' CSV QUOTE '\a';";
}
} // namespace sqlgen::postgres