|
|
1.1 misha 1: /** @file
2: Parser SQLite driver.
3:
4: (c) Dmitry "Creator" Bobrik, 2004
5: */
1.9 ! misha 6: //static const char *RCSId="$Id: parser3sqlite.C,v 1.8 2008-06-30 09:29:55 misha 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:
1.9 ! misha 17: #define MAX_COLS 500
1.1 misha 18: #define MAX_STRING 0x400
19: #define MAX_NUMBER 20
20:
1.4 misha 21: #define SQLITE_DEFAULT_CHARSET "UTF-8"
22:
1.1 misha 23: #if _MSC_VER
24: # define snprintf _snprintf
25: # define strcasecmp _stricmp
26: #endif
27:
28: static char *lsplit(char *string, char delim) {
1.6 misha 29: if(string) {
1.1 misha 30: char *v=strchr(string, delim);
1.6 misha 31: if(v) {
1.1 misha 32: *v=0;
33: return v+1;
34: }
1.6 misha 35: }
36: return 0;
1.1 misha 37: }
38:
39: static char *lsplit(char **string_ref, char delim) {
1.6 misha 40: char *result=*string_ref;
1.1 misha 41: char *next=lsplit(*string_ref, delim);
1.6 misha 42: *string_ref=next;
43: return result;
1.1 misha 44: }
45:
46: static void toupper_str(char *out, const char *in, size_t size) {
47: while(size--)
48: *out++=(char)toupper(*in++);
49: }
50:
51: struct Connection {
52: SQL_Driver_services* services;
53:
54: sqlite3* handle;
1.6 misha 55: const char* client_charset;
56: bool multi_statements;
1.1 misha 57: bool autocommit;
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:
98: connection.client_charset=SQLITE_DEFAULT_CHARSET;
99: connection.multi_statements=false;
1.4 misha 100: connection.autocommit=true;
1.1 misha 101:
1.6 misha 102: char* db_path=0;
103: char* db=url;
104: char* options=lsplit(db, '?');
105:
1.8 misha 106: if(strcmp(db, ":memory:")==0){ // in-memory temporary DB
1.6 misha 107: db_path=db;
1.8 misha 108: } else if(strcmp(db, ":temporary:")==0){ // on-disk temporary DB
1.6 misha 109: // do nothing: empty path mean temporary table on disk
1.8 misha 110: } else {
111: char* document_root=(char*)services.request_document_root();
112: if(!document_root) // path to DB-file which was specified by user is path from document_root as anywhere in parser
113: services._throw("document_root is empty");
114:
115: db_path=(char*)services.malloc_atomic(strlen(document_root)+1+strlen(db)+1);
1.9 ! misha 116: strcpy(db_path, document_root);
! 117: strcat(db_path, "/");
! 118: strcat(db_path, db);
1.5 misha 119: }
1.4 misha 120:
1.6 misha 121: //services._throw(db_path);
122:
123: while(options){
124: if(char* key=lsplit(&options, '&')){
1.4 misha 125: if(*key) {
1.6 misha 126: if(char* value=lsplit(key, '=')){
1.8 misha 127: if(strcasecmp(key, "multi_statements")==0){
1.6 misha 128: if(atoi(value)!=0)
129: connection.multi_statements=true;
130: } else if(strcasecmp(key, "autocommit")==0){
1.4 misha 131: if(atoi(value)==0)
132: connection.autocommit=false;
133: continue;
1.8 misha 134: } else if(strcmp(key, "ClientCharset")==0){
1.6 misha 135: toupper_str(value, value, strlen(value));
136: connection.client_charset=value;
137: continue;
1.4 misha 138: } else
139: services._throw("unknown connect option" /*key*/);
140: } else
141: services._throw("connect option without =value" /*key*/);
142: }
143: }
144: }
145:
146: // transcode database_name from $request:charset to UTF-8
1.6 misha 147: if(db_path && _transcode_required(connection, SQLITE_DEFAULT_CHARSET)){
148: size_t length=strlen(db_path);
149: services.transcode((const char*)db_path, length,
150: (const char*&)db_path, length,
151: services.request_charset(),
152: SQLITE_DEFAULT_CHARSET);
153:
154: }
1.4 misha 155:
1.1 misha 156:
1.6 misha 157: int rc=sqlite3_open(db_path, &connection.handle);
1.5 misha 158: if(rc!=SQLITE_OK){
1.6 misha 159: const char* error_msg=sqlite3_errmsg(connection.handle);
1.1 misha 160: sqlite3_close(connection.handle);
1.6 misha 161: _throw(connection, error_msg);
1.1 misha 162: }
1.6 misha 163:
164: _begin_transaction(connection);
1.1 misha 165: }
166:
1.6 misha 167: void disconnect(void *aconnection){
1.1 misha 168: Connection& connection=*static_cast<Connection*>(aconnection);
169: sqlite3_close(connection.handle);
170: connection.handle=0;
1.4 misha 171: }
1.1 misha 172:
1.6 misha 173: void commit(void *aconnection){
1.1 misha 174: Connection& connection=*static_cast<Connection*>(aconnection);
175: if(!connection.autocommit)
1.6 misha 176: _execute_cmd(connection, "COMMIT");
177:
178: _begin_transaction(connection);
1.1 misha 179: }
1.4 misha 180:
1.6 misha 181: void rollback(void *aconnection){
1.1 misha 182: Connection& connection=*static_cast<Connection*>(aconnection);
183: if(!connection.autocommit)
1.6 misha 184: _execute_cmd(connection, "ROLLBACK");
185:
186: _begin_transaction(connection);
1.1 misha 187: }
188:
1.6 misha 189: bool ping(void *aconnection){
190: return true; // not needed
1.1 misha 191: }
192:
1.6 misha 193: const char* quote(void *aconnection, const char *from, unsigned int length){
1.1 misha 194: Connection& connection=*static_cast<Connection*>(aconnection);
195: /*
196: You must allocate the to buffer to be at least length*2+1 bytes long.
1.4 misha 197: In the worse case, each character may need to be encoded as using two bytes,
198: and you need room for the terminating null byte.
1.1 misha 199: */
200: char *result=(char*)connection.services->malloc_atomic(length*2+1);
201: char *to=result;
202: while(length--) {
203: if(*from=='\'') { // ' -> ''
204: *to++='\'';
1.4 misha 205: } else if(*from=='\"') { // " -> ""
206: *to++='\"';
1.1 misha 207: }
208: *to++=*from++;
209: }
210: *to=0;
211: return result;
212: }
1.4 misha 213:
1.1 misha 214: void query(void *aconnection,
1.6 misha 215: const char *astatement,
216: size_t placeholders_count, Placeholder* placeholders,
217: unsigned long offset, unsigned long limit,
218: SQL_Driver_query_event_handlers& handlers
219: ){
1.1 misha 220:
221: Connection& connection=*static_cast<Connection*>(aconnection);
222: SQL_Driver_services& services=*connection.services;
223:
224: if(placeholders_count>0)
1.5 misha 225: services._throw("bind variables not supported yet");
1.9 ! misha 226:
! 227: const char* request_charset=services.request_charset();
! 228: const char* client_charset=connection.client_charset;
1.6 misha 229: bool transcode_needed=_transcode_required(connection);
230:
231: // transcode query from $request:charset to ?ClientCharset
232: if(transcode_needed){
233: size_t length=strlen(astatement);
234: services.transcode(astatement, length,
235: astatement, length,
1.9 ! misha 236: request_charset,
! 237: client_charset);
1.4 misha 238: }
239:
1.1 misha 240: const char *statement;
1.6 misha 241: if(offset || limit!=SQL_NO_LIMIT){
1.1 misha 242: size_t statement_size=strlen(astatement);
243: char *statement_limited=(char *)services.malloc_atomic(
1.6 misha 244: statement_size+MAX_NUMBER*2+8/* LIMIT #,#*/+1);
1.1 misha 245: char *cur=statement_limited;
1.6 misha 246: memcpy(cur, astatement, statement_size);
247: cur+=statement_size;
248: cur+=sprintf(cur, " LIMIT ");
1.1 misha 249: if(offset)
250: cur+=snprintf(cur, MAX_NUMBER+1, "%u,", offset);
1.6 misha 251: if(limit!=SQL_NO_LIMIT)
1.1 misha 252: cur+=snprintf(cur, MAX_NUMBER, "%u", limit);
253: statement=statement_limited;
254: } else
255: statement=astatement;
256:
257:
258: const char *pzTail;
1.6 misha 259: int next_statement_length=0;
1.1 misha 260: sqlite3_stmt *SQL;
261: int rc;
262: SQL_Error sql_error;
1.5 misha 263: bool failed=false;
1.1 misha 264:
265: do{ // cycling through SQL commands
1.6 misha 266: rc=sqlite3_prepare(connection.handle, statement, -1, &SQL, &pzTail);
267: next_statement_length=strlen(pzTail);
1.4 misha 268: if(rc!=SQLITE_OK){
1.8 misha 269: //sqlite3_free((char*)pzTail);
1.4 misha 270: _throw(connection, sqlite3_errmsg(connection.handle));
1.6 misha 271: }
272: if(!connection.multi_statements && next_statement_length>0){ // multi statements was not allowed but pzTail point to not empty one
1.8 misha 273: //sqlite3_free((char*)pzTail);
274: _throw(connection, "multi statements are not allowed until opption ?multi_statements=1 in connect string is specified.");
1.1 misha 275: }
276:
1.6 misha 277: #define CHECK(afailed) if(afailed){ failed=true; goto cleanup; }
1.1 misha 278:
1.6 misha 279: int column_count=sqlite3_column_count(SQL);
1.1 misha 280:
1.6 misha 281: if(!column_count){ // empty result: insert|delete|update|...
282: rc=sqlite3_step(SQL);
1.1 misha 283: } else {
1.9 ! misha 284: if(column_count>MAX_COLS)
! 285: column_count=MAX_COLS;
! 286:
! 287: int column_types[MAX_COLS];
! 288: bool transcode_column[MAX_COLS];
! 289:
1.6 misha 290: for(int i=0; i<column_count; i++){
291: const char *column_name=sqlite3_column_name(SQL, i);
292: size_t length=strlen(column_name);
1.1 misha 293:
294: char* strm=(char*)services.malloc_atomic(length+1);
295: memcpy(strm, column_name, length+1);
1.6 misha 296: const char* str=strm;
297: // transcode column name from ?ClientCharset to $request:charset
298: if(transcode_needed){
1.4 misha 299: services.transcode(str, length,
300: str, length,
1.9 ! misha 301: client_charset,
! 302: request_charset);
1.4 misha 303: }
304:
1.6 misha 305: CHECK(handlers.add_column(sql_error, (const char*)str, length));
1.1 misha 306: }
307: CHECK(handlers.before_rows(sql_error));
308:
1.5 misha 309: const char *str;
1.6 misha 310: size_t length=0;
1.9 ! misha 311: bool first_row=true;
1.1 misha 312:
313: do{
1.6 misha 314: rc=sqlite3_step(SQL);
315: if(rc==SQLITE_ROW){ // new line!!
1.1 misha 316:
317: CHECK(handlers.add_row(sql_error));
318:
1.6 misha 319: for(int i=0; i<column_count; i++){
1.9 ! misha 320: if(first_row){
! 321: column_types[i]=sqlite3_column_type(SQL, i);
! 322: switch(column_types[i]){
! 323: case SQLITE_INTEGER:
! 324: case SQLITE_FLOAT:
! 325: case SQLITE_NULL:
! 326: transcode_column[i]=false;
! 327: break;
! 328: default:
! 329: transcode_column[i]=transcode_needed;
! 330: break;
! 331: }
! 332: }
! 333:
1.6 misha 334: // SQLite allow to get value of any type using sqlite3_column_text function
1.9 ! misha 335: switch(column_types[i]){
1.2 misha 336: case SQLITE_NULL:
1.6 misha 337: length=0;
1.4 misha 338: str=NULL;
1.2 misha 339: break;
1.6 misha 340: case SQLITE_BLOB:
341: str=(const char*)sqlite3_column_blob(SQL, i);
342: length=(size_t)sqlite3_column_bytes(SQL, i);
343: break;
1.9 ! misha 344: default: // anything else?
1.5 misha 345: str=(const char*)sqlite3_column_text(SQL, i);
1.6 misha 346: length=(size_t)sqlite3_column_bytes(SQL, i);
1.1 misha 347: break;
348: }
349:
1.4 misha 350: if(length){
351: char* strm=(char*)services.malloc_atomic(length+1);
352: memcpy(strm, str, length+1);
1.5 misha 353: str=strm;
1.4 misha 354:
1.9 ! misha 355: if(transcode_column[i]){
! 356: // transcode cell value from ?ClientCharset to $request:charset
1.4 misha 357: services.transcode(str, length,
358: str, length,
1.9 ! misha 359: client_charset,
! 360: request_charset);
1.4 misha 361: }
362: } else
1.6 misha 363: str=0;
1.4 misha 364:
365: CHECK(handlers.add_row_cell(sql_error, str, length));
1.1 misha 366:
367: }
1.9 ! misha 368: first_row=false;
1.1 misha 369: }
1.6 misha 370: } while(rc==SQLITE_BUSY || rc==SQLITE_ROW);
1.1 misha 371:
1.6 misha 372: }
1.1 misha 373:
1.6 misha 374: if(rc==SQLITE_ERROR || rc==SQLITE_MISUSE){
1.4 misha 375: _throw(connection, sqlite3_errmsg(connection.handle));
1.1 misha 376: }
377:
378: cleanup:
379: sqlite3_finalize(SQL);
1.6 misha 380: statement=pzTail;
381: } while (next_statement_length>0);
1.1 misha 382:
383: if(failed)
1.5 misha 384: services._throw(sql_error);
1.1 misha 385: }
386:
1.4 misha 387: private:
1.6 misha 388: void _begin_transaction(Connection& connection) {
389: if(!connection.autocommit){
390: _execute_cmd(connection, "BEGIN");
391: }
392: }
393:
394: void _execute_cmd(Connection& connection, const char* statement){
395: char* zErr;
396: int rc=sqlite3_exec(connection.handle, statement, 0, 0, &zErr);
397: if(rc!=SQLITE_OK){
398: size_t length=strlen(zErr);
399: char* err_msg=(char *)connection.services->malloc_atomic(length+1);
400: memcpy(err_msg, zErr, length);
401:
402: sqlite3_free(zErr);
403: _throw(connection, err_msg);
404: }
405:
406: }
407:
408: void _throw(Connection& connection, const char* aerr_msg){
409: size_t length=strlen(aerr_msg);
410: if(length && _transcode_required(connection)){
411: // transcode server error message from ?ClientCharset to $request:charset
412: connection.services->transcode(aerr_msg, length,
413: aerr_msg, length,
414: connection.client_charset,
415: connection.services->request_charset());
416: }
417: connection.services->_throw(aerr_msg);
418: }
419:
420: bool _transcode_required(Connection& connection, const char* charset=0){
421: return (strcmp(charset?charset:connection.client_charset, connection.services->request_charset())!=0);
422: }
423:
1.4 misha 424:
1.1 misha 425: private: // sqlite client library funcs
426:
427: typedef int (*t_sqlite3_open)(const char *filename, sqlite3 **ppDb); t_sqlite3_open sqlite3_open;
428:
429: typedef int (*t_sqlite3_close)(sqlite3 *); t_sqlite3_close sqlite3_close;
430:
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) {
459: if(lt_dlinit())
460: return lt_dlerror();
461: lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
462: if (!handle) {
463: if(const char* result=lt_dlerror())
464: return result;
465:
466: return "can not open the dynamic link module";
467: }
468:
469: #define DSLINK(name, action) \
470: name=(t_##name)lt_dlsym(handle, #name); \
471: if(!name) \
472: action;
473:
474: #define DLINK(name) DSLINK(name, return "function " #name " was not found")
475: #define SLINK(name) DSLINK(name, name=subst_##name)
476:
477: DLINK(sqlite3_open);
478: DLINK(sqlite3_close);
479: DLINK(sqlite3_exec);
480: DLINK(sqlite3_free);
481: DLINK(sqlite3_errmsg);
482: DLINK(sqlite3_prepare);
483: DLINK(sqlite3_column_count);
484: DLINK(sqlite3_finalize);
485: DLINK(sqlite3_column_name);
486: DLINK(sqlite3_step);
487: DLINK(sqlite3_column_type);
488: DLINK(sqlite3_column_text);
1.6 misha 489: DLINK(sqlite3_column_blob);
490: DLINK(sqlite3_column_bytes);
1.1 misha 491: return 0;
492: }
493:
494: };
495:
496: extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
497: return new SQLite_Driver();
498: }