forked from six-ddc/sql-builder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcol.hpp
347 lines (288 loc) · 7.52 KB
/
col.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
#pragma once
#include <string>
#include <vector>
#include "adapter.hpp"
namespace boosql
{
class col
{
public:
enum witch {field, value, other};
struct item {
public:
witch type;
std::string val;
};
col() {}
col(const std::string & c)
{
name(c);
}
col(const std::string & c, const std::string & tn)
{
name(c);
table_name(tn);
}
col(const col & c)
{
_items = c._items;
_table_name = c._table_name;
}
virtual ~col() {}
col & name(const std::string & c)
{
if (c != "") {
_items.push_back(item{ field, c });
}
return *this;
}
col & table_name(const std::string & tn)
{
_table_name = tn;
return *this;
}
col& as(const std::string& s)
{
_items.push_back(item{other, "AS " + s});
return *this;
}
col& is_null() {
_items.push_back(item{other, "IS NULL"});
return *this;
}
col& is_not_null() {
_items.push_back(item{other, "IS NOT NULL"});
return *this;
}
template <typename T>
col& in(const std::vector<T>& args) {
return in_or_not(args, "IN");
}
template <typename T>
col& not_in(const std::vector<T>& args) {
return in_or_not(args, "NOT IN");
}
template<typename T>
col & in_or_not(const std::vector<T>& args, std::string in)
{
size_t size = args.size();
if(size == 1) {
_items.push_back(item{other, "="});
std::ostringstream str;
str << args[0];
_items.push_back(item{value, str.str()});
} else {
_items.push_back(item{other, in + " ("});
for(size_t i = 0; i < size; ++i) {
std::ostringstream str;
str << args[i];
_items.push_back(item{value, str.str()});
if(i < size - 1) {
_items.push_back(item{other, ","});
}
}
_items.push_back(item{other, ")"});
}
return *this;
}
col & o_and()
{
_items.push_back(item{other, "AND"});
return *this;
}
col & o_or()
{
_items.push_back(item{other, "OR"});
return *this;
}
col & quote_begin()
{
_items.push_back(item{other, "("});
return *this;
}
col & quote_end()
{
_items.push_back(item{other, ")"});
return *this;
}
col& operator ()(const std::string & any)
{
_items.push_back(item{other, any});
return *this;
}
template<typename T>
col & val(const T& data)
{
std::ostringstream str;
str << data;
_items.push_back(item{value, str.str()});
return *this;
}
template<typename T>
col & val(const std::vector<T> &data)
{
for (auto i = data.begin(); i != data.end(); ++i) {
std::ostringstream str;
str << *i;
_items.push_back(item{value, str.str()});
}
return *this;
}
col& operator &&(col & condition)
{
o_and();
merge(condition);
return *this;
}
col& operator ||(col& condition)
{
o_or();
merge(condition);
return *this;
}
void merge(col & condition)
{
for (auto i = condition._items.begin(); i != condition._items.end(); ++i) {
_items.push_back(*i);
}
}
bool empty()
{
return _items.empty();
}
const item & last()
{
return _items.back();
}
col& operator &&(const std::string& condition)
{
quote_begin();
_items.push_back(item{other, condition});
quote_end();
return *this;
}
col& operator ||(const std::string& condition) {
quote_begin();
_items.push_back(item{other, condition});
quote_end();
return *this;
}
col& operator &&(const char* condition) {
return operator &&(std::string(condition));
}
col& operator ||(const char* condition) {
return operator ||(std::string(condition));
}
col& operator [] (const char * data) {
return with_operator(data, "LIKE");
}
col& operator [] (const std::string & data) {
return with_operator(data, "LIKE");
}
col & escape(const std::string & data) {
_items.push_back(item{other, "{ ESCAPE "});
_items.push_back(item{value, data});
_items.push_back(item{other, "}"});
return *this;
}
template <typename T>
col& operator ==(const T& data) {
return with_operator(data, "=");
}
template <typename T>
col& operator !=(const T& data) {
return with_operator(data, "!=");
}
template <typename T>
col& operator >=(const T& data) {
return with_operator(data, ">=");
}
template <typename T>
col& operator <=(const T& data) {
return with_operator(data, "<=");
}
template <typename T>
col& operator >(const T& data) {
return with_operator(data, ">");
}
template <typename T>
col& operator <(const T& data) {
return with_operator(data, "<");
}
template <typename T>
col & with_operator(const T & data, const std::string & oper)
{
std::ostringstream str;
str << data;
_items.push_back(item{other, oper});
_items.push_back(item{value, str.str()});
return *this;
}
std::string str(adapter * adapter) const
{
return str(adapter, "");
}
std::string str(adapter * adapter, std::vector<std::string> & params) const
{
return str(adapter, "", params);
}
std::string str(adapter * adapter, std::string table_name) const {
std::string ret = "";
std::string pre_col = "";
if (table_name != "") {
pre_col = adapter->quote_field(table_name) + ".";
} else if (_table_name != "") {
pre_col = adapter->quote_field(_table_name) + ".";
}
for(auto i = _items.begin(); i != _items.end(); ++i) {
auto it = *i;
switch(it.type) {
case field:
ret += pre_col + adapter->quote_field(it.val);
break;
case value:
ret += adapter->quote_value(it.val);
break;
case other:
ret += " " + it.val + " ";
}
}
return ret;
}
std::string str(adapter * adapter, std::string table_name, std::vector<std::string> & params) const
{
std::string ret = "";
std::string pre_col = "";
if (table_name != "") {
pre_col = adapter->quote_field(table_name) + ".";
} else if (_table_name != "") {
pre_col = adapter->quote_field(_table_name) + ".";
}
for(auto i = _items.begin(); i != _items.end(); ++i) {
auto it = *i;
switch(it.type) {
case field:
ret += pre_col + adapter->quote_field(it.val);
break;
case value:
ret += adapter->placeholder();
params.push_back(it.val);
break;
case other:
ret += " " + it.val + " ";
}
}
return ret;
}
static col pre_quote(const std::string & c)
{
return col().quote_begin().name(c);
}
operator bool() {
return true;
}
private:
std::vector<item> _items;
std::string _table_name = "";
};
}