forked from six-ddc/sql-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.h
More file actions
710 lines (599 loc) · 20.7 KB
/
sql.h
File metadata and controls
710 lines (599 loc) · 20.7 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
#pragma once
#include <vector>
#include <string>
#include <sstream>
#include <map>
#include <iomanip>
using string = std::string;
namespace sql {
std::string add_single_quotes(std::string const str){
std::stringstream ss;
ss << std::quoted(str,'\'');
return ss.str();
}
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;
};
//BEGIN to_value блок. --------------------------------------------------
//Нужно разобраться в семантической надобности этих функций и привести
//их в нормальное состояние.
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]) {
return add_single_quotes(data);
}
template <>
inline std::string to_value<std::string>(const std::string& data) {
return add_single_quotes(data);
}
template <>
inline std::string to_value<const char*>(const char* const& data) {
return add_single_quotes(data);
}
template <>
inline std::string to_value<Param>(const Param& data) {
return data();
}
template <>
inline std::string to_value<column>(const column& data);
//END to_value блок. ----------------------------------------------------
template <typename T>
void join_vector(std::string& result, const std::vector<T>& vec, const char* sep) {
for (auto item : vec)
result.append(item)
.append(sep);
if (!vec.empty())
result.erase(//erase extra sep
std::begin(result) + result.rfind(sep),
std::end(result)
);
}
class column
{
public:
column(const std::string& column): _cond(column) {}
virtual ~column() {}
operator std::string() const {return _cond;}
column& as(const std::string& s) {
_cond.append(" as ")
.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& add_args(const std::vector<T>& input_args, bool in_condition = true) {
const string delimiter {", "};
if(input_args.size() == 1)
_cond.append(in_condition ? " " : " !")
.append("= ")
.append(to_value(input_args.at(0)));
else if (input_args.size() > 1) {
_cond.append(in_condition ? " " : " not ")
.append("in (");
//input args with delimiter ', '
for (auto arg:input_args)
_cond.append(to_value(arg))
.append(delimiter);
//remove extra delimiter
_cond.pop_back();
_cond.pop_back();
_cond.append(")");
}
return *this;
}
template <typename T>
column& in(const std::vector<T>& input_args) {
return add_args(input_args);
}
template <typename T>
column& not_in(const std::vector<T>& input_args) {
return add_args(input_args, false);
}
column& operators(column& condition, std::string oper) {
std::string str{"("+_cond+")"};
str.append(oper)
.append("("+condition._cond+")");
condition._cond = std::move(str);
return condition;
}
column& operator &&(column& condition) {
return operators(condition, " and ");
}
column& operator ||(column& condition) {
return operators(condition, " or ");
}
template <typename T>
column& operator ==(const T& data) {
_cond.append(" = ")
.append(to_value(data));
return *this;
}
template <typename T>
column& operator !=(const T& data) {
_cond.append(" != ")
.append(to_value(data));
return *this;
}
template <typename T>
column& operator >=(const T& data) {
_cond.append(" >= ")
.append(to_value(data));
return *this;
}
template <typename T>
column& operator <=(const T& data) {
_cond.append(" <= ")
.append(to_value(data));
return *this;
}
template <typename T>
column& operator >(const T& data) {
_cond.append(" > ")
.append(to_value(data));
return *this;
}
template <typename T>
column& operator <(const T& data) {
_cond.append(" < ")
.append(to_value(data));
return *this;
}
operator bool() {
return !_cond.empty();
}
private:
std::string _cond;
};
template <>
inline std::string to_value<column>(const column& data) {
return data;
}
template<typename Model>
class SqlModel
{
public:
SqlModel() {}
SqlModel(std::string table): _table_name(table) {}
SqlModel(SqlModel& other)
: _where_condition(other._where_condition)
, _table_name(other._table_name)
, _flags(other._flags){}
SqlModel(Model& other)
: _where_condition(other._where_condition)
, _table_name(other._table_name)
, _flags(other._flags){}
virtual ~SqlModel() {}
// virtual std::string querry()=0;
friend std::ostream& operator<< (std::ostream& out, Model& mod) {
out<<mod.querry();
return out;
}
virtual bool operator == (std::string arg){
return arg.compare(dynamic_cast<Model*>(this)->querry());
}
template<typename T>
Model& where(T& condition) {
_where_condition.push_back(condition);
return *(static_cast<Model*>(this));
}
Model& operator =(const SqlModel& other){
*this = other;
return *(static_cast<Model*>(this));
};
SqlModel& reset(){
_where_condition.clear();
_flags.at("complette") = false;
_flags.at("distinct") = false;
_flags.at("replace") = false;
return *this;
}
SqlModel& clear(){
_table_name.clear();
return reset();
}
std::string table(){
return _table_name;
}
protected:
std::vector<std::string> _where_condition;
std::string _table_name;
std::map<std::string, bool> _flags{{"complette",false},
{"distinct",false},
{"replace",false}};
};
class SelectModel : public SqlModel<SelectModel>
{
public:
SelectModel() {}
SelectModel(string table_name)
: SqlModel(table_name){}
SelectModel(SelectModel& other)
: SqlModel(other)
, _select_columns(other._select_columns)
, _groupby_columns(other._groupby_columns)
, _join_on_condition(other._join_on_condition)
, _having_condition(other._having_condition)
, _select_specs(other._select_specs){}
// SelectModel(SqlModel& other):SqlModel(other){}
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() {
_flags.at("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(", ")
.append(table_name);
from(tables...);
return *this;
}
// for recursion
SelectModel& from() {
return *this;
}
template<typename T>
SelectModel& join(const T& table_name, std::string join_type = "join") {
_select_specs.insert({"_join_type", join_type});
_select_specs.insert({"_join_table", table_name});
return *this;
}
template<typename T>
SelectModel& left_join(const T& table_name) {
return join(table_name, "left join");
}
template<typename T>
SelectModel& left_outer_join(const T& table_name) {
return join(table_name, "left outer join");
}
template<typename T>
SelectModel& right_join(const T& table_name) {
return join(table_name, "right join");
}
template<typename T>
SelectModel& right_outer_join(const T& table_name) {
return join(table_name, "right outer join");
}
template<typename T>
SelectModel& full_join(const T& table_name) {
return join(table_name, "full join");
}
template<typename T>
SelectModel& full_outer_join(const T& table_name) {
return join(table_name, "full outer join");
}
template<typename T>
SelectModel& on(const T& condition) {
_join_on_condition.push_back(condition);
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;
}
template <typename T>
SelectModel& having(const T& condition) {
_having_condition.push_back(condition);
return *this;
}
SelectModel& order_by(const std::string& order_by) {
_select_specs.insert({"order_by", order_by});
return *this;
}
template <typename T>
SelectModel& limit(const T& limit) {
_select_specs.insert({"limit", std::to_string(limit)});
return *this;
}
template <typename T>
SelectModel& limit(const T& offset, const T& limit) {
_select_specs.insert({"offset", std::to_string(offset)});
_select_specs.insert({"limit", std::to_string(limit)});
return *this;
}
template <typename T>
SelectModel& offset(const T& offset) {
_select_specs.insert({"offset", std::to_string(offset)});
return *this;
}
std::string querry() {
std::string _sql{""};
if(!_table_name.empty() && !_select_columns.empty()){
_sql.append("select ");
if(_flags.at("distinct"))
_sql.append("distinct ");
join_vector(_sql, _select_columns, ", ");
_sql.append(" from ")
.append(_table_name);
if(_select_specs.contains("_join_type")
&& _select_specs.contains("_join_table"))
_sql.append(" ")
.append(_select_specs.at("_join_type"))
.append(" ")
.append(_select_specs.at("_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(_select_specs.contains("order_by"))
_sql.append(" order by ")
.append(_select_specs.at("order_by"));
if(_select_specs.contains("limit"))
_sql.append(" limit ")
.append(_select_specs.at("limit"));
if(_select_specs.contains("offset"))
_sql.append(" offset ")
.append(_select_specs.at("offset"));
}
return _sql;
}
SelectModel& reset() {
SqlModel::reset();
_select_columns.clear();
_groupby_columns.clear();
_join_on_condition.clear();
_having_condition.clear();
_select_specs.clear();
return *this;
}
SelectModel& clear() {
SqlModel::clear();
return SelectModel::reset();
}
private:
std::vector<std::string> _select_columns;
std::vector<std::string> _groupby_columns;
std::vector<std::string> _join_on_condition;
std::vector<std::string> _having_condition;
std::map<std::string, std::string> _select_specs;
};
class InsertModel : public SqlModel<InsertModel>
{
public:
InsertModel() {}
InsertModel(std::string targetTable):SqlModel(targetTable){}
InsertModel(SqlModel& other):SqlModel(other){}
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& insert(const std::vector<T>& data) {
if (!data.empty()){
std::vector<std::string> new_values;
new_values.reserve( _values.size() + data.size() ); // preallocate memory
new_values.insert( std::end(new_values), std::begin(_values), std::end(_values) );
new_values.insert( std::end(new_values), std::begin(data), std::end(data) );
_values = std::move( new_values );
}
return *this;
}
template <typename T>
InsertModel& operator()(const std::string& c, const T& data) {
return insert(c, data);
}
template <typename T>
InsertModel& operator()(const T& data) {
_values.push_back(to_value(data));
return *this;
}
InsertModel& into(const std::string& table_name) {
_table_name = table_name;
return *this;
}
InsertModel& set_default(const std::string& column) {
_columns.push_back(column);
_values.push_back("DEFAULT");
return *this;
}
InsertModel& replace(bool var) {
_flags.at("replace") = var;
return *this;
}
std::string vec_to_str(std::vector<std::string> vec, std::string delimiter = ", "){
std::string result{""};
for(auto element:vec)
result.append((result.empty() ? element : (delimiter + element)));
return result;
}
std::string get_values(){
string result = std::move(vec_to_str(_values));
if (!result.empty())
result = (" values ("+result+")");
return result;
}
std::string get_columns(){
string result = std::move(vec_to_str(_columns));
if (!result.empty())
result = (" ("+result+")");
return result;
}
std::string querry() {
std::string _sql;
if (!_values.empty() //not empty values
&& (_columns.size() == _values.size() //and (columns and values sizes eqaul)
|| _columns.empty())){//or values empty
_sql.append("insert")
.append(_flags.at("replace") ? " or replace " : " ")
.append("into ")
.append(_table_name)
.append(std::move(get_columns()))
.append(std::move(get_values()));
}
return _sql;
}
InsertModel& reset() {
SqlModel::reset();
_columns.clear();
_values.clear();
return *this;
}
InsertModel& clear() {
SqlModel::clear();
InsertModel::reset();
return *this;
}
template<typename T>
InsertModel& where(T& condition) = delete;
protected:
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<UpdateModel>
{
public:
UpdateModel() {}
UpdateModel(std::string targetTable):SqlModel(targetTable) {}
UpdateModel(SqlModel& other):SqlModel(other){}
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(" = ")
.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);
}
std::string querry() {
std::string _sql;
_sql.append("update ")
.append(_table_name)
.append(" set ");
join_vector(_sql, _set_columns, ", ");
if(_where_condition.size() > 0) {
_sql.append(" where ");
join_vector(_sql, _where_condition, " and ");
}
return _sql;
}
UpdateModel& reset() {
SqlModel::reset();
_set_columns.clear();
_where_condition.clear();
return *this;
}
UpdateModel& clear() {
SqlModel::clear();
UpdateModel::reset();
return *this;
}
protected:
std::vector<std::string> _set_columns;//нужо разделить на _columns и _values как это сделано в InsertModel
};
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<DeleteModel>
{
public:
DeleteModel() {}
DeleteModel(std::string targetTable):SqlModel(targetTable) {}
DeleteModel(SqlModel& other):SqlModel(other){}
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;
}
std::string querry() {
std::string _sql{"delete from "};
_sql.append(_table_name);
if(_where_condition.size() > 0) {
_sql.append(" where ");
join_vector(_sql, _where_condition, " and ");
}
return _sql;
}
DeleteModel& clear() {
SqlModel::clear();
return *this;
}
DeleteModel& reset() {
SqlModel::reset();
return *this;
}
};
}