|
|
1.1 misha 1: /** @file
2: Parser SQLite driver.
3:
4: (c) Dmitry "Creator" Bobrik, 2004
5: */
1.14 ! moko 6: //static const char *RCSId="$Id: parser3sqlite.C,v 1.13 2012/06/15 09:09:33 moko Exp $";
1.1 misha 7:
8: #include "config_includes.h"
9:
10: #include "pa_sql_driver.h"
11: //#include "windows.h" // for messagebox
12:
13: #define NO_CLIENT_LONG_LONG
14: #include "sqlite3.h"
15: #include "ltdl.h"
16:
17: #define MAX_STRING 0x400
18: #define MAX_NUMBER 20
19:
1.4 misha 20: #define SQLITE_DEFAULT_CHARSET "UTF-8"
21:
1.1 misha 22: #if _MSC_VER
23: # define snprintf _snprintf
24: # define strcasecmp _stricmp
25: #endif
26:
27: static char *lsplit(char *string, char delim) {
1.6 misha 28: if(string) {
1.1 misha 29: char *v=strchr(string, delim);
1.6 misha 30: if(v) {
1.1 misha 31: *v=0;
32: return v+1;
33: }
1.6 misha 34: }
35: return 0;
1.1 misha 36: }
37:
38: static char *lsplit(char **string_ref, char delim) {
1.6 misha 39: char *result=*string_ref;
1.1 misha 40: char *next=lsplit(*string_ref, delim);
1.6 misha 41: *string_ref=next;
42: return result;
1.1 misha 43: }
44:
45: static void toupper_str(char *out, const char *in, size_t size) {
46: while(size--)
47: *out++=(char)toupper(*in++);
48: }
49:
50: struct Connection {
51: SQL_Driver_services* services;
52:
53: sqlite3* handle;
1.6 misha 54: const char* client_charset;
55: bool multi_statements;
1.1 misha 56: bool autocommit;
1.12 moko 57: int busy_timeout;
1.1 misha 58: };
59:
60:
61:
62: /**
63: SQLite server driver
64: */
65: class SQLite_Driver : public SQL_Driver {
66: public:
67:
68: SQLite_Driver() : SQL_Driver() {
69: }
70:
71: /// get api version
72: int api_version() { return SQL_DRIVER_API_VERSION; }
1.4 misha 73:
1.1 misha 74: /// initialize driver by loading sql dynamic link library
75: const char *initialize(char *dlopen_file_spec) {
76: return dlopen_file_spec?
77: dlink(dlopen_file_spec):"client library column is empty";
78: }
1.4 misha 79:
1.1 misha 80: /** connect
81: @param url
1.8 misha 82: format: @b db-file|:memory:|temporary:?
83: autocommit=1& // =0 disable autocommit. in this case 1 connect == 1 transaction.
84: // or you can use begin/commit|rollback explicitly
85: multi_statements=0& // =1 allow many statements in 1 query
86: ClientCharset=UTF-8 // will transcode to/from specified charset instead of UTF-8 (default for sqlite)
1.1 misha 87: */
88: void connect(
1.6 misha 89: char *url,
90: SQL_Driver_services& services,
91: void **connection_ref ///< output: Connection*
1.4 misha 92: ){
1.1 misha 93:
1.4 misha 94: Connection& connection=*(Connection *)services.malloc(sizeof(Connection));
1.6 misha 95: *connection_ref=&connection;
1.4 misha 96: connection.services=&services;
1.6 misha 97:
1.12 moko 98: connection.client_charset=SQLITE_DEFAULT_CHARSET;
1.6 misha 99: connection.multi_statements=false;
1.4 misha 100: connection.autocommit=true;
1.12 moko 101: connection.busy_timeout=4000;
1.1 misha 102:
1.6 misha 103: char* db_path=0;
104: char* db=url;
105: char* options=lsplit(db, '?');
106:
1.8 misha 107: if(strcmp(db, ":memory:")==0){ // in-memory temporary DB
1.6 misha 108: db_path=db;
1.8 misha 109: } else if(strcmp(db, ":temporary:")==0){ // on-disk temporary DB
1.6 misha 110: // do nothing: empty path mean temporary table on disk
1.8 misha 111: } else {
112: char* document_root=(char*)services.request_document_root();
113: if(!document_root) // path to DB-file which was specified by user is path from document_root as anywhere in parser
114: services._throw("document_root is empty");
115:
116: db_path=(char*)services.malloc_atomic(strlen(document_root)+1+strlen(db)+1);
1.9 misha 117: strcpy(db_path, document_root);
118: strcat(db_path, "/");
119: strcat(db_path, db);
1.5 misha 120: }
1.4 misha 121:
1.6 misha 122: //services._throw(db_path);
123:
124: while(options){
125: if(char* key=lsplit(&options, '&')){
1.4 misha 126: if(*key) {
1.6 misha 127: if(char* value=lsplit(key, '=')){
1.8 misha 128: if(strcasecmp(key, "multi_statements")==0){
1.6 misha 129: if(atoi(value)!=0)
130: connection.multi_statements=true;
1.12 moko 131: } else if(strcasecmp(key, "busy_timeout")==0){
132: connection.busy_timeout=atoi(value);
1.6 misha 133: } else if(strcasecmp(key, "autocommit")==0){
1.4 misha 134: if(atoi(value)==0)
135: connection.autocommit=false;
1.8 misha 136: } else if(strcmp(key, "ClientCharset")==0){
1.6 misha 137: toupper_str(value, value, strlen(value));
138: connection.client_charset=value;
1.4 misha 139: } else
140: services._throw("unknown connect option" /*key*/);
141: } else
142: services._throw("connect option without =value" /*key*/);
143: }
144: }
145: }
146:
147: // transcode database_name from $request:charset to UTF-8
1.6 misha 148: if(db_path && _transcode_required(connection, SQLITE_DEFAULT_CHARSET)){
149: size_t length=strlen(db_path);
150: services.transcode((const char*)db_path, length,
151: (const char*&)db_path, length,
152: services.request_charset(),
153: SQLITE_DEFAULT_CHARSET);
154:
155: }
1.4 misha 156:
1.1 misha 157:
1.6 misha 158: int rc=sqlite3_open(db_path, &connection.handle);
1.5 misha 159: if(rc!=SQLITE_OK){
1.6 misha 160: const char* error_msg=sqlite3_errmsg(connection.handle);
1.1 misha 161: sqlite3_close(connection.handle);
1.6 misha 162: _throw(connection, error_msg);
1.1 misha 163: }
1.6 misha 164:
1.12 moko 165: sqlite3_busy_timeout(connection.handle, connection.busy_timeout);
166:
1.6 misha 167: _begin_transaction(connection);
1.1 misha 168: }
169:
1.6 misha 170: void disconnect(void *aconnection){
1.1 misha 171: Connection& connection=*static_cast<Connection*>(aconnection);
172: sqlite3_close(connection.handle);
173: connection.handle=0;
1.4 misha 174: }
1.1 misha 175:
1.6 misha 176: void commit(void *aconnection){
1.1 misha 177: Connection& connection=*static_cast<Connection*>(aconnection);
178: if(!connection.autocommit)
1.6 misha 179: _execute_cmd(connection, "COMMIT");
180:
181: _begin_transaction(connection);
1.1 misha 182: }
1.4 misha 183:
1.6 misha 184: void rollback(void *aconnection){
1.1 misha 185: Connection& connection=*static_cast<Connection*>(aconnection);
186: if(!connection.autocommit)
1.6 misha 187: _execute_cmd(connection, "ROLLBACK");
188:
189: _begin_transaction(connection);
1.1 misha 190: }
191:
1.6 misha 192: bool ping(void *aconnection){
193: return true; // not needed
1.1 misha 194: }
195:
1.11 moko 196: // charset here is services.request_charset(), not connection.client_charset
197: // thus we can't use the sql server quoting support
198: const char* quote(void *aconnection, const char *str, unsigned int length)
199: {
200: const char* from;
201: const char* from_end=str+length;
202:
203: size_t quoted=0;
204:
205: for(from=str; from<from_end; from++){
206: if(*from=='\'')
207: quoted++;
208: }
209:
210: if(!quoted)
211: return str;
212:
1.1 misha 213: Connection& connection=*static_cast<Connection*>(aconnection);
1.11 moko 214: char *result=(char*)connection.services->malloc_atomic(length + quoted + 1);
215: char *to = result;
216:
217: for(from=str; from<from_end; from++){
218: if(*from=='\'')
219: *to++= '\''; // ' -> ''
220: *to++=*from;
1.1 misha 221: }
1.11 moko 222:
1.1 misha 223: *to=0;
224: return result;
225: }
1.4 misha 226:
1.1 misha 227: void query(void *aconnection,
1.6 misha 228: const char *astatement,
229: size_t placeholders_count, Placeholder* placeholders,
230: unsigned long offset, unsigned long limit,
231: SQL_Driver_query_event_handlers& handlers
232: ){
1.1 misha 233:
234: Connection& connection=*static_cast<Connection*>(aconnection);
235: SQL_Driver_services& services=*connection.services;
236:
237: if(placeholders_count>0)
1.5 misha 238: services._throw("bind variables not supported yet");
1.9 misha 239:
240: const char* request_charset=services.request_charset();
241: const char* client_charset=connection.client_charset;
1.6 misha 242: bool transcode_needed=_transcode_required(connection);
243:
244: // transcode query from $request:charset to ?ClientCharset
245: if(transcode_needed){
246: size_t length=strlen(astatement);
247: services.transcode(astatement, length,
248: astatement, length,
1.9 misha 249: request_charset,
250: client_charset);
1.4 misha 251: }
252:
1.1 misha 253: const char *statement;
1.6 misha 254: if(offset || limit!=SQL_NO_LIMIT){
1.1 misha 255: size_t statement_size=strlen(astatement);
256: char *statement_limited=(char *)services.malloc_atomic(
1.6 misha 257: statement_size+MAX_NUMBER*2+8/* LIMIT #,#*/+1);
1.1 misha 258: char *cur=statement_limited;
1.6 misha 259: memcpy(cur, astatement, statement_size);
260: cur+=statement_size;
261: cur+=sprintf(cur, " LIMIT ");
1.1 misha 262: if(offset)
1.12 moko 263: cur+=snprintf(cur, MAX_NUMBER+1, "%lu,", offset);
1.6 misha 264: if(limit!=SQL_NO_LIMIT)
1.12 moko 265: cur+=snprintf(cur, MAX_NUMBER, "%lu", limit);
1.1 misha 266: statement=statement_limited;
267: } else
268: statement=astatement;
269:
270:
271: const char *pzTail;
1.6 misha 272: int next_statement_length=0;
1.1 misha 273: sqlite3_stmt *SQL;
274: int rc;
275: SQL_Error sql_error;
1.5 misha 276: bool failed=false;
1.1 misha 277:
278: do{ // cycling through SQL commands
1.6 misha 279: rc=sqlite3_prepare(connection.handle, statement, -1, &SQL, &pzTail);
280: next_statement_length=strlen(pzTail);
1.4 misha 281: if(rc!=SQLITE_OK){
1.8 misha 282: //sqlite3_free((char*)pzTail);
1.4 misha 283: _throw(connection, sqlite3_errmsg(connection.handle));
1.6 misha 284: }
285: if(!connection.multi_statements && next_statement_length>0){ // multi statements was not allowed but pzTail point to not empty one
1.8 misha 286: //sqlite3_free((char*)pzTail);
287: _throw(connection, "multi statements are not allowed until opption ?multi_statements=1 in connect string is specified.");
1.1 misha 288: }
289:
1.6 misha 290: #define CHECK(afailed) if(afailed){ failed=true; goto cleanup; }
1.1 misha 291:
1.6 misha 292: int column_count=sqlite3_column_count(SQL);
1.1 misha 293:
1.6 misha 294: if(!column_count){ // empty result: insert|delete|update|...
295: rc=sqlite3_step(SQL);
1.1 misha 296: } else {
1.6 misha 297: for(int i=0; i<column_count; i++){
298: const char *column_name=sqlite3_column_name(SQL, i);
299: size_t length=strlen(column_name);
1.1 misha 300:
301: char* strm=(char*)services.malloc_atomic(length+1);
302: memcpy(strm, column_name, length+1);
1.6 misha 303: const char* str=strm;
304: // transcode column name from ?ClientCharset to $request:charset
305: if(transcode_needed){
1.4 misha 306: services.transcode(str, length,
307: str, length,
1.9 misha 308: client_charset,
309: request_charset);
1.4 misha 310: }
311:
1.6 misha 312: CHECK(handlers.add_column(sql_error, (const char*)str, length));
1.1 misha 313: }
314: CHECK(handlers.before_rows(sql_error));
315:
1.5 misha 316: const char *str;
1.6 misha 317: size_t length=0;
1.1 misha 318:
319: do{
1.6 misha 320: rc=sqlite3_step(SQL);
321: if(rc==SQLITE_ROW){ // new line!!
1.1 misha 322:
323: CHECK(handlers.add_row(sql_error));
324:
1.6 misha 325: for(int i=0; i<column_count; i++){
326: // SQLite allow to get value of any type using sqlite3_column_text function
1.14 ! moko 327: bool transcode_value=false;
! 328: int column_type=sqlite3_column_type(SQL, i);
! 329: switch(column_type){
1.2 misha 330: case SQLITE_NULL:
1.6 misha 331: length=0;
1.4 misha 332: str=NULL;
1.2 misha 333: break;
1.6 misha 334: case SQLITE_BLOB:
335: str=(const char*)sqlite3_column_blob(SQL, i);
336: length=(size_t)sqlite3_column_bytes(SQL, i);
337: break;
1.14 ! moko 338: case SQLITE_TEXT: // for text transcoding can be required
1.9 misha 339: default: // anything else?
1.14 ! moko 340: transcode_value=transcode_needed;
! 341: case SQLITE_INTEGER:
! 342: case SQLITE_FLOAT:
1.5 misha 343: str=(const char*)sqlite3_column_text(SQL, i);
1.6 misha 344: length=(size_t)sqlite3_column_bytes(SQL, i);
1.1 misha 345: break;
346: }
347:
1.4 misha 348: if(length){
349: char* strm=(char*)services.malloc_atomic(length+1);
1.14 ! moko 350: memcpy(strm, str, length);
! 351: strm[length]=0;
1.5 misha 352: str=strm;
1.4 misha 353:
1.14 ! moko 354: if(transcode_value){
1.9 misha 355: // transcode cell value from ?ClientCharset to $request:charset
1.4 misha 356: services.transcode(str, length,
357: str, length,
1.9 misha 358: client_charset,
359: request_charset);
1.4 misha 360: }
361: } else
1.6 misha 362: str=0;
1.4 misha 363:
364: CHECK(handlers.add_row_cell(sql_error, str, length));
1.1 misha 365:
366: }
367: }
1.6 misha 368: } while(rc==SQLITE_BUSY || rc==SQLITE_ROW);
1.1 misha 369:
1.6 misha 370: }
1.1 misha 371:
1.6 misha 372: if(rc==SQLITE_ERROR || rc==SQLITE_MISUSE){
1.4 misha 373: _throw(connection, sqlite3_errmsg(connection.handle));
1.1 misha 374: }
375:
376: cleanup:
377: sqlite3_finalize(SQL);
1.6 misha 378: statement=pzTail;
379: } while (next_statement_length>0);
1.1 misha 380:
381: if(failed)
1.5 misha 382: services._throw(sql_error);
1.1 misha 383: }
384:
1.4 misha 385: private:
1.6 misha 386: void _begin_transaction(Connection& connection) {
387: if(!connection.autocommit){
388: _execute_cmd(connection, "BEGIN");
389: }
390: }
391:
392: void _execute_cmd(Connection& connection, const char* statement){
393: char* zErr;
394: int rc=sqlite3_exec(connection.handle, statement, 0, 0, &zErr);
395: if(rc!=SQLITE_OK){
396: size_t length=strlen(zErr);
397: char* err_msg=(char *)connection.services->malloc_atomic(length+1);
398: memcpy(err_msg, zErr, length);
399:
400: sqlite3_free(zErr);
401: _throw(connection, err_msg);
402: }
403:
404: }
405:
406: void _throw(Connection& connection, const char* aerr_msg){
407: size_t length=strlen(aerr_msg);
408: if(length && _transcode_required(connection)){
409: // transcode server error message from ?ClientCharset to $request:charset
410: connection.services->transcode(aerr_msg, length,
411: aerr_msg, length,
412: connection.client_charset,
413: connection.services->request_charset());
414: }
415: connection.services->_throw(aerr_msg);
416: }
417:
418: bool _transcode_required(Connection& connection, const char* charset=0){
419: return (strcmp(charset?charset:connection.client_charset, connection.services->request_charset())!=0);
420: }
421:
1.4 misha 422:
1.1 misha 423: private: // sqlite client library funcs
424:
425: typedef int (*t_sqlite3_open)(const char *filename, sqlite3 **ppDb); t_sqlite3_open sqlite3_open;
426:
427: typedef int (*t_sqlite3_close)(sqlite3 *); t_sqlite3_close sqlite3_close;
428:
1.12 moko 429: typedef int (*t_sqlite3_busy_timeout)(sqlite3*, int ms); t_sqlite3_busy_timeout sqlite3_busy_timeout;
430:
1.1 misha 431: typedef int (*t_sqlite3_exec)(sqlite3*, const char *sql, sqlite3_callback, void *, char **errmsg); t_sqlite3_exec sqlite3_exec;
432:
433: typedef void (*t_sqlite3_free)(char *z); t_sqlite3_free sqlite3_free;
434:
435: typedef const char *(* t_sqlite3_errmsg)(sqlite3*); t_sqlite3_errmsg sqlite3_errmsg;
436:
437: typedef int (* t_sqlite3_prepare)(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail); t_sqlite3_prepare sqlite3_prepare;
438:
439: typedef int (* t_sqlite3_column_count)(sqlite3_stmt *pStmt); t_sqlite3_column_count sqlite3_column_count;
440:
441: typedef int (* t_sqlite3_finalize)(sqlite3_stmt *pStmt); t_sqlite3_finalize sqlite3_finalize;
442:
443: typedef const char *(* t_sqlite3_column_name)(sqlite3_stmt*,int); t_sqlite3_column_name sqlite3_column_name;
444:
445: typedef int (* t_sqlite3_step)(sqlite3_stmt*); t_sqlite3_step sqlite3_step;
446:
447: typedef int (* t_sqlite3_column_type)(sqlite3_stmt*, int iCol); t_sqlite3_column_type sqlite3_column_type;
448:
449: typedef const unsigned char *(* t_sqlite3_column_text)(sqlite3_stmt*, int iCol); t_sqlite3_column_text sqlite3_column_text;
450:
1.6 misha 451: typedef const unsigned char *(* t_sqlite3_column_blob)(sqlite3_stmt*, int iCol); t_sqlite3_column_blob sqlite3_column_blob;
452:
453: typedef int (* t_sqlite3_column_bytes)(sqlite3_stmt*, int iCol); t_sqlite3_column_bytes sqlite3_column_bytes;
454:
1.5 misha 455:
1.1 misha 456: private: // sqlite client library funcs linking
457:
458: const char *dlink(const char *dlopen_file_spec) {
1.13 moko 459: if(lt_dlinit()){
460: if(const char* result=lt_dlerror())
461: return result;
462: return "can not prepare to dynamic loading";
463: }
464:
465: lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
1.1 misha 466:
1.13 moko 467: if(!handle){
468: if(const char* result=lt_dlerror())
469: return result;
1.1 misha 470: return "can not open the dynamic link module";
471: }
472:
473: #define DSLINK(name, action) \
474: name=(t_##name)lt_dlsym(handle, #name); \
475: if(!name) \
476: action;
477:
478: #define DLINK(name) DSLINK(name, return "function " #name " was not found")
479: #define SLINK(name) DSLINK(name, name=subst_##name)
480:
481: DLINK(sqlite3_open);
482: DLINK(sqlite3_close);
1.12 moko 483: DLINK(sqlite3_busy_timeout);
1.1 misha 484: DLINK(sqlite3_exec);
485: DLINK(sqlite3_free);
486: DLINK(sqlite3_errmsg);
487: DLINK(sqlite3_prepare);
488: DLINK(sqlite3_column_count);
489: DLINK(sqlite3_finalize);
490: DLINK(sqlite3_column_name);
491: DLINK(sqlite3_step);
492: DLINK(sqlite3_column_type);
493: DLINK(sqlite3_column_text);
1.6 misha 494: DLINK(sqlite3_column_blob);
495: DLINK(sqlite3_column_bytes);
1.1 misha 496: return 0;
497: }
498:
499: };
500:
501: extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
502: return new SQLite_Driver();
503: }