-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathsql.h
727 lines (627 loc) · 17.2 KB
/
sql.h
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
#pragma once
#include <vector>
#include <string>
namespace sql {
class column;
class Param
{
public:
Param (const std::string ¶m) : _param(param) {}
Param (const char *param) : _param(param) {}
public:
std::string operator()() const { return param(); }
inline std::string param() const { return _param; }
private:
const std::string _param;
};
template <typename T>
inline std::string to_value(const T& data) {
return std::to_string(data);
}
template <size_t N>
inline std::string to_value(char const(&data)[N]) {
std::string str("'");
str.append(data);
str.append("'");
return str;
}
template <>
inline std::string to_value<std::string>(const std::string& data) {
std::string str("'");
str.append(data);
str.append("'");
return str;
}
template <>
inline std::string to_value<const char*>(const char* const& data) {
std::string str("'");
str.append(data);
str.append("'");
return str;
}
template <>
inline std::string to_value<Param>(const Param& data) {
return data();
}
template <>
inline std::string to_value<column>(const column& data);
/*
template <>
static std::string sql::to_value<time_t>(const time_t& data) {
char buff[128] = {0};
struct tm* ttime = localtime(&data);
strftime(buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", ttime);
std::string str("'");
str.append(buff);
str.append("'");
return str;
}
*/
template <typename T>
void join_vector(std::string& result, const std::vector<T>& vec, const char* sep) {
size_t size = vec.size();
for(size_t i = 0; i < size; ++i) {
if(i < size - 1) {
result.append(vec[i]);
result.append(sep);
} else {
result.append(vec[i]);
}
}
}
class column
{
public:
column(const std::string& column) {
_cond = column;
}
virtual ~column() {}
column& as(const std::string& s) {
_cond.append(" as ");
_cond.append(s);
return *this;
}
column& is_null() {
_cond.append(" is null");
return *this;
}
column& is_not_null() {
_cond.append(" is not null");
return *this;
}
template <typename T>
column& in(const std::vector<T>& args) {
size_t size = args.size();
if(size == 1) {
_cond.append(" = ");
_cond.append(to_value(args[0]));
} else {
_cond.append(" in (");
for(size_t i = 0; i < size; ++i) {
if(i < size - 1) {
_cond.append(to_value(args[i]));
_cond.append(", ");
} else {
_cond.append(to_value(args[i]));
}
}
_cond.append(")");
}
return *this;
}
template <typename T>
column& not_in(const std::vector<T>& args) {
size_t size = args.size();
if(size == 1) {
_cond.append(" != ");
_cond.append(to_value(args[0]));
} else {
_cond.append(" not in (");
for(size_t i = 0; i < size; ++i) {
if(i < size - 1) {
_cond.append(to_value(args[i]));
_cond.append(", ");
} else {
_cond.append(to_value(args[i]));
}
}
_cond.append(")");
}
return *this;
}
column& operator &&(column& condition) {
std::string str("(");
str.append(_cond);
str.append(") and (");
str.append(condition._cond);
str.append(")");
condition._cond = str;
return condition;
}
column& operator ||(column& condition) {
std::string str("(");
str.append(_cond);
str.append(") or (");
str.append(condition._cond);
str.append(")");
condition._cond = str;
return condition;
}
column& operator &&(const std::string& condition) {
_cond.append(" and ");
_cond.append(condition);
return *this;
}
column& operator ||(const std::string& condition) {
_cond.append(" or ");
_cond.append(condition);
return *this;
}
column& operator &&(const char* condition) {
_cond.append(" and ");
_cond.append(condition);
return *this;
}
column& operator ||(const char* condition) {
_cond.append(" or ");
_cond.append(condition);
return *this;
}
template <typename T>
column& operator ==(const T& data) {
_cond.append(" = ");
_cond.append(to_value(data));
return *this;
}
template <typename T>
column& operator !=(const T& data) {
_cond.append(" != ");
_cond.append(to_value(data));
return *this;
}
template <typename T>
column& operator >=(const T& data) {
_cond.append(" >= ");
_cond.append(to_value(data));
return *this;
}
template <typename T>
column& operator <=(const T& data) {
_cond.append(" <= ");
_cond.append(to_value(data));
return *this;
}
template <typename T>
column& operator >(const T& data) {
_cond.append(" > ");
_cond.append(to_value(data));
return *this;
}
template <typename T>
column& operator <(const T& data) {
_cond.append(" < ");
_cond.append(to_value(data));
return *this;
}
const std::string& str() const {
return _cond;
}
operator bool() {
return true;
}
private:
std::string _cond;
};
template <>
inline std::string to_value<column>(const column& data) {
return data.str();
}
class SqlModel
{
public:
SqlModel() {}
virtual ~SqlModel() {}
virtual const std::string& str() = 0;
const std::string& last_sql() {
return _sql;
}
private:
SqlModel(const SqlModel& m) = delete;
SqlModel& operator =(const SqlModel& data) = delete;
protected:
std::string _sql;
};
class SelectModel : public SqlModel
{
public:
SelectModel() : _distinct(false) {}
virtual ~SelectModel() {}
template <typename... Args>
SelectModel& select(const std::string& str, Args&&... columns) {
_select_columns.push_back(str);
select(columns...);
return *this;
}
// for recursion
SelectModel& select() {
return *this;
}
SelectModel& distinct() {
_distinct = true;
return *this;
}
template <typename... Args>
SelectModel& from(const std::string& table_name, Args&&... tables) {
if(_table_name.empty()) {
_table_name = table_name;
} else {
_table_name.append(", ");
_table_name.append(table_name);
}
from(tables...);
return *this;
}
// for recursion
SelectModel& from() {
return *this;
}
SelectModel& join(const std::string& table_name) {
_join_type = "join";
_join_table = table_name;
return *this;
}
SelectModel& left_join(const std::string& table_name) {
_join_type = "left join";
_join_table = table_name;
return *this;
}
SelectModel& left_outer_join(const std::string& table_name) {
_join_type = "left outer join";
_join_table = table_name;
return *this;
}
SelectModel& right_join(const std::string& table_name) {
_join_type = "right join";
_join_table = table_name;
return *this;
}
SelectModel& right_outer_join(const std::string& table_name) {
_join_type = "right outer join";
_join_table = table_name;
return *this;
}
SelectModel& full_join(const std::string& table_name) {
_join_type = "full join";
_join_table = table_name;
return *this;
}
SelectModel& full_outer_join(const std::string& table_name) {
_join_type = "full outer join";
_join_table = table_name;
return *this;
}
SelectModel& on(const std::string& condition) {
_join_on_condition.push_back(condition);
return *this;
}
SelectModel& on(const column& condition) {
_join_on_condition.push_back(condition.str());
return *this;
}
SelectModel& where(const std::string& condition) {
_where_condition.push_back(condition);
return *this;
}
SelectModel& where(const column& condition) {
_where_condition.push_back(condition.str());
return *this;
}
template <typename... Args>
SelectModel& group_by(const std::string& str, Args&&...columns) {
_groupby_columns.push_back(str);
group_by(columns...);
return *this;
}
// for recursion
SelectModel& group_by() {
return *this;
}
SelectModel& having(const std::string& condition) {
_having_condition.push_back(condition);
return *this;
}
SelectModel& having(const column& condition) {
_having_condition.push_back(condition.str());
return *this;
}
SelectModel& order_by(const std::string& order_by) {
_order_by = order_by;
return *this;
}
template <typename T>
SelectModel& limit(const T& limit) {
_limit = std::to_string(limit);
return *this;
}
template <typename T>
SelectModel& limit(const T& offset, const T& limit) {
_offset = std::to_string(offset);
_limit = std::to_string(limit);
return *this;
}
template <typename T>
SelectModel& offset(const T& offset) {
_offset = std::to_string(offset);
return *this;
}
virtual const std::string& str() override {
_sql.clear();
_sql.append("select ");
if(_distinct) {
_sql.append("distinct ");
}
join_vector(_sql, _select_columns, ", ");
_sql.append(" from ");
_sql.append(_table_name);
if(!_join_type.empty()) {
_sql.append(" ");
_sql.append(_join_type);
_sql.append(" ");
_sql.append(_join_table);
}
if(!_join_on_condition.empty()) {
_sql.append(" on ");
join_vector(_sql, _join_on_condition, " and ");
}
if(!_where_condition.empty()) {
_sql.append(" where ");
join_vector(_sql, _where_condition, " and ");
}
if(!_groupby_columns.empty()) {
_sql.append(" group by ");
join_vector(_sql, _groupby_columns, ", ");
}
if(!_having_condition.empty()) {
_sql.append(" having ");
join_vector(_sql, _having_condition, " and ");
}
if(!_order_by.empty()) {
_sql.append(" order by ");
_sql.append(_order_by);
}
if(!_limit.empty()) {
_sql.append(" limit ");
_sql.append(_limit);
}
if(!_offset.empty()) {
_sql.append(" offset ");
_sql.append(_offset);
}
return _sql;
}
SelectModel& reset() {
_select_columns.clear();
_distinct = false;
_groupby_columns.clear();
_table_name.clear();
_join_type.clear();
_join_table.clear();
_join_on_condition.clear();
_where_condition.clear();
_having_condition.clear();
_order_by.clear();
_limit.clear();
_offset.clear();
return *this;
}
friend inline std::ostream& operator<< (std::ostream& out, SelectModel& mod) {
out<<mod.str();
return out;
}
protected:
std::vector<std::string> _select_columns;
bool _distinct;
std::vector<std::string> _groupby_columns;
std::string _table_name;
std::string _join_type;
std::string _join_table;
std::vector<std::string> _join_on_condition;
std::vector<std::string> _where_condition;
std::vector<std::string> _having_condition;
std::string _order_by;
std::string _limit;
std::string _offset;
};
class InsertModel : public SqlModel
{
public:
InsertModel() {}
virtual ~InsertModel() {}
template <typename T>
InsertModel& insert(const std::string& c, const T& data) {
_columns.push_back(c);
_values.push_back(to_value(data));
return *this;
}
template <typename T>
InsertModel& operator()(const std::string& c, const T& data) {
return insert(c, data);
}
InsertModel& into(const std::string& table_name) {
_table_name = table_name;
return *this;
}
InsertModel& replace(bool var) {
_replace = var;
return *this;
}
virtual const std::string& str() override {
_sql.clear();
std::string v_ss;
if (_replace) {
_sql.append("insert or replace into ");
}else {
_sql.append("insert into ");
}
_sql.append(_table_name);
_sql.append("(");
v_ss.append(" values(");
size_t size = _columns.size();
for(size_t i = 0; i < size; ++i) {
if(i < size - 1) {
_sql.append(_columns[i]);
_sql.append(", ");
v_ss.append(_values[i]);
v_ss.append(", ");
} else {
_sql.append(_columns[i]);
_sql.append(")");
v_ss.append(_values[i]);
v_ss.append(")");
}
}
_sql.append(v_ss);
return _sql;
}
InsertModel& reset() {
_table_name.clear();
_columns.clear();
_values.clear();
return *this;
}
friend inline std::ostream& operator<< (std::ostream& out, InsertModel& mod) {
out<<mod.str();
return out;
}
protected:
bool _replace = false;
std::string _table_name;
std::vector<std::string> _columns;
std::vector<std::string> _values;
};
template <>
inline InsertModel& InsertModel::insert(const std::string& c, const std::nullptr_t&) {
_columns.push_back(c);
_values.push_back("null");
return *this;
}
class UpdateModel : public SqlModel
{
public:
UpdateModel() {}
virtual ~UpdateModel() {}
UpdateModel& update(const std::string& table_name) {
_table_name = table_name;
return *this;
}
template <typename T>
UpdateModel& set(const std::string& c, const T& data) {
std::string str(c);
str.append(" = ");
str.append(to_value(data));
_set_columns.push_back(str);
return *this;
}
template <typename T>
UpdateModel& operator()(const std::string& c, const T& data) {
return set(c, data);
}
UpdateModel& where(const std::string& condition) {
_where_condition.push_back(condition);
return *this;
}
UpdateModel& where(const column& condition) {
_where_condition.push_back(condition.str());
return *this;
}
virtual const std::string& str() override {
_sql.clear();
_sql.append("update ");
_sql.append(_table_name);
_sql.append(" set ");
join_vector(_sql, _set_columns, ", ");
size_t size = _where_condition.size();
if(size > 0) {
_sql.append(" where ");
join_vector(_sql, _where_condition, " and ");
}
return _sql;
}
UpdateModel& reset() {
_table_name.clear();
_set_columns.clear();
_where_condition.clear();
return *this;
}
friend inline std::ostream& operator<< (std::ostream& out, UpdateModel& mod) {
out<<mod.str();
return out;
}
protected:
std::vector<std::string> _set_columns;
std::string _table_name;
std::vector<std::string> _where_condition;
};
template <>
inline UpdateModel& UpdateModel::set(const std::string& c, const std::nullptr_t&) {
std::string str(c);
str.append(" = null");
_set_columns.push_back(str);
return *this;
}
class DeleteModel : public SqlModel
{
public:
DeleteModel() {}
virtual ~DeleteModel() {}
DeleteModel& _delete() {
return *this;
}
template <typename... Args>
DeleteModel& from(const std::string& table_name, Args&&... tables) {
if(_table_name.empty()) {
_table_name = table_name;
} else {
_table_name.append(", ");
_table_name.append(table_name);
}
from(tables...);
return *this;
}
// for recursion
DeleteModel& from() {
return *this;
}
DeleteModel& where(const std::string& condition) {
_where_condition.push_back(condition);
return *this;
}
DeleteModel& where(const column& condition) {
_where_condition.push_back(condition.str());
return *this;
}
virtual const std::string& str() override {
_sql.clear();
_sql.append("delete from ");
_sql.append(_table_name);
size_t size = _where_condition.size();
if(size > 0) {
_sql.append(" where ");
join_vector(_sql, _where_condition, " and ");
}
return _sql;
}
DeleteModel& reset() {
_table_name.clear();
_where_condition.clear();
return *this;
}
friend inline std::ostream& operator<< (std::ostream& out, DeleteModel& mod) {
out<<mod.str();
return out;
}
protected:
std::string _table_name;
std::vector<std::string> _where_condition;
};
}