My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
#include "Sql.h"

NAMESPACE_UPP

String SqlStatement::Get(int dialect) const {
ASSERT(dialect == ORACLE || dialect == SQLITE3 || dialect == MY_SQL || dialect == MSSQL ||
dialect == PGSQL || dialect == FIREBIRD || dialect == DB2);
return SqlCompile(dialect, text);
}

#ifndef flagNOAPPSQL
String SqlStatement::Get() const
{
return Get(SQL.GetDialect());
}
#endif

SqlSelect& SqlSelect::SetOp(const SqlSelect& s2, const char *op)
{
String q;
q << SqlCase(SQLITE3, "")("((")
<< text
<< SqlCase(SQLITE3, "")(")")
<< op
<< SqlCase(SQLITE3, "")("(")
<< s2.text
<< SqlCase(SQLITE3, "")("))")
;
text = q;
return *this;
}

SqlSelect& SqlSelect::operator|=(const SqlSelect& s2) {
return SetOp(s2, " union ");
}

SqlSelect& SqlSelect::operator&=(const SqlSelect& s2) {
return SetOp(s2, " intersect ");
}

SqlSelect& SqlSelect::operator-=(const SqlSelect& s2) {
return SetOp(s2, SqlCase(MSSQL|PGSQL|SQLITE3," except ")(" minus "));
}

SqlSelect operator|(const SqlSelect& s1, const SqlSelect& s2) {
SqlSelect s = s1;
s |= s2;
return s;
}

SqlSelect operator&(const SqlSelect& s1, const SqlSelect& s2) {
SqlSelect s = s1;
s &= s2;
return s;
}

SqlSelect operator-(const SqlSelect& s1, const SqlSelect& s2) {
SqlSelect s = s1;
s -= s2;
return s;
}

SqlSelect& SqlSelect::Where(const SqlBool& exp) {
if(!exp.IsTrue() && !exp.IsEmpty())
text << " where " << ~exp;
return *this;
}

SqlSelect& SqlSelect::StartWith(const SqlBool& exp) {
text << " start with " << ~exp;
return *this;
}

SqlSelect& SqlSelect::ConnectBy(const SqlBool& exp) {
text << " connect by " << ~exp;
return *this;
}

SqlSelect& SqlSelect::GroupBy(const SqlSet& set) {
text << " group by " << ~set;
return *this;
}

SqlSelect& SqlSelect::Having(const SqlBool& exp) {
text << " having " << ~exp;
return *this;
}

SqlSelect& SqlSelect::OrderBy(const SqlSet& set) {
if(!set.IsEmpty())
text << " order by " << ~set;
return *this;
}

SqlSelect& SqlSelect::ForUpdate() {
text << " for update";
return *this;
}

SqlSelect& SqlSelect::NoWait() {
text << " nowait";
return *this;
}

SqlSelect& SqlSelect::Limit(int limit) {
text << " limit " << limit;
return *this;
}

SqlSelect& SqlSelect::Limit(int64 offset, int limit) {
text << " limit " << offset << ", " << limit;
return *this;
}

SqlSelect& SqlSelect::Offset(int64 offset) {
text << " offset " << offset;
return *this;
}

SqlSelect& SqlSelect::operator()(const SqlVal& val)
{
if(text.GetCount())
text << ", ";
text << ~val;
return *this;
}

SqlSelect& SqlSelect::Hint(const char *hint)
{
text = "/*+ " + String(hint) + " */ " + text;
return *this;
}

SqlSelect& SqlSelect::Get() {
text = "select " + text + SqlCase(ORACLE, " from DUAL")("");
return *this;
}

SqlSelect& SqlSelect::From(const SqlSet& table) {
String ts = table(SqlSet::SETOP + 1);
text = "select " + text + " from " + ts;
tables << ',' << Filter(ts, CharFilterNotWhitespace);
on = false;
return *this;
}

SqlSelect& SqlSelect::From(const SqlId& table) {
const String& t1 = table.ToString();
text = String().Cat() << "select " << text << " from \t" << t1 << '\t';
tables << ',' << t1;
on = false;
return *this;
}

SqlSelect& SqlSelect::From(const SqlId& table1, const SqlId& table2) {
const String& t1 = table1.ToString();
const String& t2 = table2.ToString();
text = String().Cat() << "select " << text << " from \t" << t1 << "\t, \t" << t2 << '\t';
tables << ',' << t1 << ',' << t2;
on = false;
return *this;
}

SqlSelect& SqlSelect::From(const SqlId& table1, const SqlId& table2, const SqlId& table3) {
const String& t1 = table1.ToString();
const String& t2 = table2.ToString();
const String& t3 = table3.ToString();
text = String().Cat() << "select " << text << " from \t" << t1 << "\t, \t" << t2 << "\t, \t" << t3 << '\t';
tables << ',' << t1 << ',' << t2 << ',' << t3;
on = false;
return *this;
}

SqlSelect& SqlSelect::InnerJoin0(const String& table) {
text << " inner join " << table;
tables << ',' << table;
on = false;
return *this;
}

SqlSelect& SqlSelect::LeftJoin0(const String& table) {
text << " left outer join " << table;
tables << ',' << table;
on = false;
return *this;
}

SqlSelect& SqlSelect::RightJoin0(const String& table) {
text << " right outer join " << table;
tables << ',' << table;
on = false;
return *this;
}

SqlSelect& SqlSelect::FullJoin0(const String& table) {
text << " full outer join " << table;
tables << ',' << table;
on = false;
return *this;
}

SqlSelect& SqlSelect::InnerJoinRef(const SqlId& table)
{
InnerJoin(table);
On(FindSchJoin(tables));
on = true;
return *this;
}

SqlSelect& SqlSelect::LeftJoinRef(const SqlId& table)
{
LeftJoin(table);
On(FindSchJoin(tables));
on = true;
return *this;
}

SqlSelect& SqlSelect::RightJoinRef(const SqlId& table)
{
RightJoin(table);
On(FindSchJoin(tables));
on = true;
return *this;
}

SqlSelect& SqlSelect::FullJoinRef(const SqlId& table)
{
FullJoin(table);
On(FindSchJoin(tables));
on = true;
return *this;
}

SqlSelect& SqlSelect::On(const SqlBool& exp) {
if(!exp.IsTrue() && !exp.IsEmpty())
text << (on ? " and " : " on ") << ~exp;
return *this;
}

SqlVal SqlSelect::AsValue() const
{
return SqlVal(String("(").Cat() << text << ")", SqlVal::LOW);
}

SqlSet SqlSelect::AsTable(const SqlId& tab) const
{
StringBuffer t;
t << SqlCase(MSSQL, "")("(")
<< "(" << text << ") as \t" << tab.ToString() << '\t'
<< SqlCase(MSSQL, "")(")");
return SqlSet(String(t), SqlSet::HIGH);
}

SqlSelect::SqlSelect(Fields f)
{
on = false;
SqlSet set(f);
text = ~set;
}

#define E__SCat(I) set.Cat(p##I)

#define E__QSqlSelectF(I) \
SqlSelect::SqlSelect(__List##I(E__SqlVal)) { \
on = false; \
SqlSet set; \
__List##I(E__SCat); \
text = ~set; \
}
__Expand(E__QSqlSelectF);

#define E__QSelectF(I) \
SqlSelect Select(__List##I(E__SqlVal)) { \
SqlSet set; \
__List##I(E__SCat); \
return Select(set); \
}
__Expand(E__QSelectF);


// -------------------------------

SqlDelete::SqlDelete(SqlVal table) {
text = "delete from " + ~table;
}

SqlDelete& SqlDelete::Where(const SqlBool& b) {
text << " where " << ~b;
return *this;
}

void SqlInsert::Column(const SqlId& column, SqlVal val) {
set1.Cat(column);
set2.Cat(val);
if(keycolumn.IsNull()) keycolumn = column;
if(keyvalue.IsEmpty()) keyvalue =val;
}

SqlInsert::operator SqlStatement() const {
String s = "insert into " + table.Quoted();
if(!set1.IsEmpty()) {
s << set1();
if(from.IsEmpty()) {
if(!set2.IsEmpty())
s << " values " << set2();
}
else
s << ' ' + SqlStatement(Select(set2).From(from).Where(where)).GetText();
}
return SqlStatement(s);
}

SqlInsert& SqlInsert::From(const SqlId& from) {
return From(SqlSet(from));
}

struct InsertFieldOperator : public FieldOperator {
SqlInsert *insert;
bool nokey;

virtual void Field(const char *name, Ref f) {
if(!nokey)
insert->Column(name, (Value)f);
nokey = false;
}

InsertFieldOperator() { nokey = false; }
};

SqlInsert::SqlInsert(Fields f, bool nokey) {
InsertFieldOperator ifo;
ifo.insert = this;
ifo.nokey = nokey;
f(ifo);
table = ifo.table;
}

SqlInsert& SqlInsert::operator()(Fields f, bool nokey)
{
InsertFieldOperator ifo;
ifo.insert = this;
ifo.nokey = nokey;
f(ifo);
return *this;
}

struct UpdateFieldOperator : public FieldOperator {
SqlUpdate *update;

virtual void Field(const char *name, Ref f) {
update->Column(name, (Value)f);
}
};

SqlUpdate::SqlUpdate(Fields f) {
UpdateFieldOperator ufo;
ufo.update = this;
f(ufo);
table = ufo.table;
}

SqlUpdate& SqlUpdate::operator()(Fields f) {
UpdateFieldOperator ufo;
ufo.update = this;
f(ufo);
return *this;
}

// ------------------------------------

SqlUpdate::operator SqlStatement() const {
StringBuffer stmt;
stmt << "update " << table.Quoted() << " set " << ~set;
if(!where.IsEmpty())
stmt << " where " << ~where;
return SqlStatement(stmt);
}

void SqlUpdate::Column(const SqlId& column, SqlVal val) {
set.Cat(SqlVal(SqlVal(column), " = ", val, SqlS::COMP));
}

void SqlUpdate::Column(const SqlSet& cols, const SqlSet& val)
{
set.Cat(SqlVal(SqlS(cols(), SqlS::HIGH), " = ", SqlS(val(), SqlS::HIGH), SqlS::COMP));
}

// ------------------------------------
// deprecated

bool SqlStatement::Execute(Sql& cursor) const {
ASSERT(text.GetCount());
return cursor.Execute(*this);
}

void SqlStatement::Force(Sql& cursor) const {
ASSERT(text.GetCount());
if(!cursor.Execute(*this))
throw SqlExc(cursor.GetSession());
}

Value SqlStatement::Fetch(Sql& cursor) const {
ASSERT(text.GetCount());
if(!Execute(cursor))
return ErrorValue(SqlExc(cursor.GetSession()));
if(!cursor.Fetch())
return Null;
return cursor[0];
}

#ifndef NOAPPSQL
bool SqlStatement::Execute() const {
return Execute(SQL);
}

void SqlStatement::Force() const {
Force(SQL);
}

Value SqlStatement::Fetch() const {
return Fetch(SQL);
}

#endif

END_UPP_NAMESPACE

Change log

r4620 by rylek on Feb 24, 2012   Diff
.Sql/SqlStatement.cpp: modified
SqlSelect::AsTable to keep extra
parentheses around aliased subselect in
all dialects except MS-SQL
Go to: 
Project members, sign in to write a code review

Older revisions

r4617 by rylek on Feb 24, 2012   Diff
.Sql/SqlStatement.cpp: modified
SqlSelect::AsTable to remove extra
parentheses only in MS-SQL dialect
r4612 by rylek on Feb 23, 2012   Diff
.Sql/SqlStatement.cpp: bug fix in
SqlSelect::AsTable to suppress extra
parentheses around aliased subselect
r4529 by cxl on Feb 3, 2012   Diff
Sql: SqlId escaping now tests whether
escaped Id is indeed valid SQL id, no
escaping otherwise (workaround for use
of SqlId('table.*') and similar)
All revisions of this file

File info

Size: 8876 bytes, 426 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting