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
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
bool SqlToBool(const String& s);
bool SqlToBool(const Value& v);
const String& BoolToSql(bool b);

class SqlSession;

class SqlExc : public Exc {
public:
#ifndef NOAPPSQL
SqlExc();
#endif
SqlExc(const SqlSession& session);
SqlExc(const Sql& sql);
SqlExc(const String& desc) : Exc(desc) {}
SqlExc(const char *desc) : Exc(desc) {}

void SetSessionError(const SqlSession& session);
};

enum { SQLRAW_V = 34 };

class SqlRaw : public String, ValueType<SqlRaw, SQLRAW_V> {
public:
operator Value() const { return RawValue<SqlRaw>(*this); }
SqlRaw(const Value& q)
: String(IsNull(q) ? String() :
IsString(q) ? String(q) :
String(RawValue<SqlRaw>::Extract(q))) {}
SqlRaw(const String& s) : String(s) {}
SqlRaw() {}
};

struct SqlColumnInfo : Moveable<SqlColumnInfo> {
String name;
int type;
int width;
int precision; //number of total digits in numeric types
int scale; //number of digits after comma in numeric types
bool nullable; //true - column can hold null values
bool binary; //column holds binary data
};

class SqlConnection {
protected:
friend class Sql;
friend class SqlSession;

virtual void SetParam(int i, const Value& r) = 0;
virtual bool Execute() = 0;
virtual int GetRowsProcessed() const;
virtual Value GetInsertedId() const;
virtual bool Fetch() = 0;
virtual void GetColumn(int i, Ref r) const = 0;
virtual void Cancel() = 0;
virtual SqlSession& GetSession() const = 0;
virtual String GetUser() const;
virtual String ToString() const = 0;

static void Attach(Sql& sql, SqlConnection *con);

SqlConnection();
virtual ~SqlConnection();

int starttime;
String statement;
Vector<SqlColumnInfo> info;
int fetchrows;
int longsize;
bool parse;
};

#define E__ColVal(I) const char *c##I, const Value& v##I
#define E__IdVal(I) SqlId c##I, const Value& v##I

class SqlSource {
protected:
virtual SqlConnection *CreateConnection() = 0;
virtual ~SqlSource() {}
friend class Sql;
SqlSource() {}

private:
void operator=(const SqlSource&);
SqlSource(const SqlSource&);
};


class Sql {
SqlConnection *cn;
Vector<Value> param;

friend class SqlSession;
friend class SqlConnection;
friend Sql& AppCursor();
friend Sql& AppCursorR();

Value Select0(const String& what);

void SetSession(SqlSource& src);
void Attach(SqlConnection *connection);
void Detach();

protected:
Sql(SqlConnection *connection);

public:
String Compile(const SqlStatement& s);

void Clear();

void SetParam(int i, const Value& val);
void SetStatement(const String& s);
void SetStatement(const SqlStatement& s) { SetStatement(Compile(s)); }

bool Execute();
void ExecuteX();
bool Run() { return Execute(); }
void RunX() { ExecuteX(); }

bool Execute(const String& s);
void ExecuteX(const String& s);

bool Execute(const SqlStatement& s) { return Execute(Compile(s)); }
void ExecuteX(const SqlStatement& s) { ExecuteX(Compile(s)); }
//$-
#define E__Run(I) bool Run(__List##I(E__Value));
__Expand(E__Run)
//$ bool Run(const Value& v1 [, const Value& v2 ...]);

#define E__RunX(I) void RunX(__List##I(E__Value));
__Expand(E__RunX)

#define E__Execute(I) bool Execute(const String& s, __List##I(E__Value));
__Expand(E__Execute)

#define E__ExecuteX(I) void ExecuteX(const String& s, __List##I(E__Value));
__Expand(E__ExecuteX)

bool Fetch();

#define E__Fetch(I) bool Fetch(__List##I(E__Ref));
__Expand(E__Fetch)
//$+
bool Fetch(Vector<Value>& row);
bool Fetch(ValueMap& row);
bool Fetch(Fields fields);

int GetRowsProcessed() const { return cn->GetRowsProcessed(); }

int GetColumns() const;

void GetColumn(int i, Ref r) const;
void GetColumn(SqlId colid, Ref r) const;
Value operator[](int i) const;
Value operator[](SqlId colid) const;
const SqlColumnInfo& GetColumnInfo(int i) const { return cn->info[i]; }
Vector<Value> GetRow() const;
ValueMap GetRowMap() const;
operator Vector<Value>() const { return GetRow(); }
void Get(Fields fields);

void SetFetchRows(int nrows) { cn->fetchrows = nrows; }
void SetLongSize(int lsz) { cn->longsize = lsz; }

void Cancel() { cn->Cancel(); }

Value Select(const String& what);

#define E__Select(I) Value Select(const String& what, __List##I(E__Value));
__Expand(E__Select)

#define E__Insert(I) bool Insert(const char *tb, const char *c0, const Value& v0, __List##I(E__ColVal));
__Expand(E__Insert)

#define E__InsertId(I) bool Insert(SqlId tb, SqlId c0, const Value& v0, __List##I(E__IdVal));
__Expand(E__InsertId)

#define E__Update(I) bool Update(const char *tb, const char *k, const Value& kv, __List##I(E__ColVal));
__Expand(E__Update)

#define E__UpdateId(I) bool Update(SqlId tb, SqlId k, const Value& kv, __List##I(E__IdVal));
__Expand(E__UpdateId)

bool Insert(Fields nf);
bool Insert(Fields nf, const char *table);
bool Insert(Fields nf, SqlId table);

bool InsertNoKey(Fields nf, const char *table);
bool InsertNoKey(Fields nf);
bool InsertNoKey(Fields nf, SqlId table);

bool InsertNoNulls(Fields nf, const char *table);
bool InsertNoNulls(Fields nf);
bool InsertNoNulls(Fields nf, SqlId table);

bool Update(Fields nf);
bool Update(Fields nf, const char *table);
bool Update(Fields nf, SqlId table);

bool Delete(const char *table, const char *key, const Value& keyval);
bool Delete(SqlId table, SqlId key, const Value& keyval);

String ToString() const { return cn->ToString(); }

bool operator*(const SqlStatement& q) { return Execute(q); }
Sql& operator&(const SqlStatement& q) { ExecuteX(q); return *this; }
Value operator%(const SqlStatement& q);
ValueArray operator/(const SqlStatement& q);

SqlSession& GetSession() const { return cn->GetSession(); }
int GetDialect() const;

Value GetInsertedId() const { return cn->GetInsertedId(); }

String GetUser() const { return cn->GetUser(); }

enum ERRORCLASS {
ERROR_UNSPECIFIED,
CONNECTION_BROKEN,
};

void SetError(String error, String stmt, int code = 0, const char *scode = NULL, ERRORCLASS clss = ERROR_UNSPECIFIED);
String GetLastError() const;
String GetErrorStatement() const;
int GetErrorCode() const;
String GetErrorCodeString() const;
ERRORCLASS GetErrorClass() const;
void ClearError();

void Begin();
void Commit();
void Rollback();
int GetTransactionLevel();

String Savepoint();
void RollbackTo(const String& savepoint);

bool IsOpen();

bool WasError() const;

Sql(SqlSource&);
#ifndef NOAPPSQL
Sql();
Sql(const char *stmt);
Sql(const SqlStatement& s);
#endif
Sql(const char *stmt, SqlSource& session);
Sql(const SqlStatement& s, SqlSource& session);
~Sql();

void operator=(SqlSession& s); // this only works with SQL and SQLR...
#ifdef _MULTITHREADED
static void PerThread(bool b = true); // Activates thread local SQL/SQLR
#endif

private:
void operator=(const Sql&);
Sql(const Sql&);
};

struct Sql0 : Sql {
Sql0() : Sql((SqlConnection *)NULL) {}
};

#ifndef NOAPPSQL
struct SqlR : Sql {
SqlR();
SqlR(const char *stmt);
SqlR(const SqlStatement& s);
};
#endif

struct StatementExecutor {
virtual bool Execute(const String& stmt) = 0;
virtual ~StatementExecutor() {}
};

typedef bool (*RunScript)(const String& text, StatementExecutor& executor, Gate2<int, int> progress_canceled);

class AppSql;
class AppSqlR;

class SqlSession : public SqlSource {
public:
enum {
START_FETCHING,
END_FETCHING,
END_FETCHING_MANY,
START_EXECUTING,
END_EXECUTING,
EXECUTING_ERROR,
CONNECTION_ERROR,
BEFORE_EXECUTING,
AFTER_EXECUTING
};

protected:
virtual SqlConnection *CreateConnection();

friend class Sql;

Stream *trace;
bool logerrors;
bool tracetime;
bool usrlog;
int traceslow;
int dialect;
int exectime;

String statement;

String lasterror;
String errorstatement;
int errorcode_number;
String errorcode_string;
Sql::ERRORCLASS errorclass;
bool (*error_handler)(String error, String stmt, int code, const char *scode, Sql::ERRORCLASS clss);
bool throwonerror;

int status;

One<Sql> sql;
One<Sql> sqlr;

void SessionClose();

static void Attach(Sql& sql, SqlConnection *con);

public:
virtual void Begin();
virtual void Commit();
virtual void Rollback();
virtual int GetTransactionLevel() const;

virtual String Savepoint();
virtual void RollbackTo(const String& savepoint);

virtual bool IsOpen() const;

virtual RunScript GetRunScript() const;

virtual Vector<String> EnumUsers();
virtual Vector<String> EnumDatabases();
virtual Vector<String> EnumTables(String database);
virtual Vector<String> EnumViews(String database);
virtual Vector<String> EnumSequences(String database);
virtual Vector<SqlColumnInfo> EnumColumns(String database, String table);
virtual Vector<String> EnumPrimaryKey(String database, String table);
virtual String EnumRowID(String database, String table);
virtual Vector<String> EnumReservedWords();

SqlSession& Dialect(int q) { dialect = q; return *this; }
int GetDialect() const { ASSERT(dialect != 255); return dialect; }

void SetTrace(Stream& s = VppLog()) { trace = &s; }
Stream *GetTrace() const { return trace; }
void KillTrace() { trace = NULL; }

void TraceTime(bool b = true) { tracetime = b; }
bool IsTraceTime() const { return tracetime; }

SqlSession& LogErrors(bool b = true) { logerrors = true; return *this; }
SqlSession& UsrLog(bool b = true) { usrlog = true; return *this; }
SqlSession& TraceSlow(int ms = 5000) { traceslow = ms; return *this; }

SqlSession& ThrowOnError(bool b = true) { throwonerror = b; return *this; }

bool WasError() const { return !GetLastError().IsEmpty(); }

void SetError(String error, String stmt, int code = 0, const char * scode = NULL, Sql::ERRORCLASS clss = Sql::ERROR_UNSPECIFIED);
String GetLastError() const { return lasterror; }
String GetErrorStatement() const { return errorstatement; }
int GetErrorCode() const { return errorcode_number; }
String GetErrorCodeString() const { return errorcode_string; }
Sql::ERRORCLASS GetErrorClass() const { return errorclass; }
void ClearError();
void InstallErrorHandler(bool (*handler)(String error, String stmt, int code, const char *scode, Sql::ERRORCLASS clss));

String GetStatement() const { return statement; }
void SetStatement(const String& s) { statement = s; }

void SetTime(int t) { exectime = t; }
int GetTime() const { return exectime; }

String GetUser() { return Sql(*this).GetUser(); }

Sql& GetSessionSql();
Sql& GetSessionSqlR();

operator bool() const { return IsOpen(); }

int GetStatus() { return status; }
void SetStatus(int s) { status = s; WhenDatabaseActivity(*this); }
bool operator == (int s) const { return status == s; }
bool operator != (int s) const { return status != s; }

Callback1<const SqlSession&> WhenDatabaseActivity;

#ifdef _MULTITHREADED
static void PerThread(bool b = true); // Activates thread local SQL/SQLR
#endif

SqlSession();
virtual ~SqlSession();
};


#ifndef NOAPPSQL

Sql& AppCursor();
Sql& AppCursorR();

//$-
#define SQL AppCursor()
#define SQLR AppCursorR()
//$+
// Assist++ cheat:
//$ Sql SQL;

#endif

class OciConnection;

bool SqlPerformScript(SqlSession& session, Stream& script,
Gate2<int, int> progress_canceled = false, bool stoponerror = false);
bool SqlPerformScript(Stream& script,
Gate2<int, int> progress_canceled = false, bool stoponerror = false);
bool SqlPerformScript(SqlSession& session, const String& script,
Gate2<int, int> progress_canceled = false, bool stoponerror = false);
bool SqlPerformScript(const String& script,
Gate2<int, int> progress_canceled = false, bool stoponerror = false);

class SqlMassInsert {
struct Row : Moveable<Row> {
dword nulls;
Vector <Value> value;
SqlBool remove;
};

Sql& sql;
SqlId table;
Vector<String> column;
Vector<Row> cache;
int pos;
bool error;

void NewRow();

public:
SqlMassInsert& operator()(SqlId col, const Value& val);
SqlMassInsert& EndRow(SqlBool remove = SqlBool());
void Flush();
bool IsError() const { return error; }

SqlMassInsert(Sql& sql, SqlId table) : sql(sql), table(table) { pos = 0; error = false; }
#ifndef NOAPPSQL
SqlMassInsert(SqlId table) : sql(SQL), table(table) { pos = 0; error = false; }
#endif
~SqlMassInsert();
};

// Deprecated, use SqlPerformScript instead
struct StdStatementExecutor : StatementExecutor {
StdStatementExecutor(SqlSession& session) : cursor(session) {}
virtual bool Execute(const String& stmt);
Sql cursor;
};

#ifndef NOAPPSQL
StatementExecutor& SQLStatementExecutor();
#endif

#ifdef BackwardCompatibility
typedef Sql QSql;
typedef SqlSession QSession;
#endif

Change log

r4950 by cxl on May 14, 2012   Diff
Draw: Font, Drawing, Painting, Image now
support Xmlize and Jsonize
Go to: 
Project members, sign in to write a code review

Older revisions

r4631 by cxl on Feb 28, 2012   Diff
Sql: Sql/SqlSession::PerThread
r4526 by cxl on Feb 3, 2012   Diff
Sql: SqlIds now quoted
r4443 by cxl on Jan 19, 2012   Diff
Sql: MassInsert now has remove option
All revisions of this file

File info

Size: 15422 bytes, 478 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting