|
|
1.1 misha 1: /** @file
2: Parser SQLite driver.
3:
4: (c) Dmitry "Creator" Bobrik, 2004
5: */
1.11 ! moko 6: //static const char *RCSId="$Id: parser3sqlite.C,v 1.10 2008-07-04 11:56:40 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.11 ! moko 193: // charset here is services.request_charset(), not connection.client_charset
! 194: // thus we can't use the sql server quoting support
! 195: const char* quote(void *aconnection, const char *str, unsigned int length)
! 196: {
! 197: const char* from;
! 198: const char* from_end=str+length;
! 199:
! 200: size_t quoted=0;
! 201:
! 202: for(from=str; from<from_end; from++){
! 203: if(*from=='\'')
! 204: quoted++;
! 205: }
! 206:
! 207: if(!quoted)
! 208: return str;
! 209:
1.1 misha 210: Connection& connection=*static_cast<Connection*>(aconnection);
1.11 ! moko 211: char *result=(char*)connection.services->malloc_atomic(length + quoted + 1);
! 212: char *to = result;
! 213:
! 214: for(from=str; from<from_end; from++){
! 215: if(*from=='\'')
! 216: *to++= '\''; // ' -> ''
! 217: *to++=*from;
1.1 misha 218: }
1.11 ! moko 219:
1.1 misha 220: *to=0;
221: return result;
222: }
1.4 misha 223:
1.1 misha 224: void query(void *aconnection,
1.6 misha 225: const char *astatement,
226: size_t placeholders_count, Placeholder* placeholders,
227: unsigned long offset, unsigned long limit,
228: SQL_Driver_query_event_handlers& handlers
229: ){
1.1 misha 230:
231: Connection& connection=*static_cast<Connection*>(aconnection);
232: SQL_Driver_services& services=*connection.services;
233:
234: if(placeholders_count>0)
1.5 misha 235: services._throw("bind variables not supported yet");
1.9 misha 236:
237: const char* request_charset=services.request_charset();
238: const char* client_charset=connection.client_charset;
1.6 misha 239: bool transcode_needed=_transcode_required(connection);
240:
241: // transcode query from $request:charset to ?ClientCharset
242: if(transcode_needed){
243: size_t length=strlen(astatement);
244: services.transcode(astatement, length,
245: astatement, length,
1.9 misha 246: request_charset,
247: client_charset);
1.4 misha 248: }
249:
1.1 misha 250: const char *statement;
1.6 misha 251: if(offset || limit!=SQL_NO_LIMIT){
1.1 misha 252: size_t statement_size=strlen(astatement);
253: char *statement_limited=(char *)services.malloc_atomic(
1.6 misha 254: statement_size+MAX_NUMBER*2+8/* LIMIT #,#*/+1);
1.1 misha 255: char *cur=statement_limited;
1.6 misha 256: memcpy(cur, astatement, statement_size);
257: cur+=statement_size;
258: cur+=sprintf(cur, " LIMIT ");
1.1 misha 259: if(offset)
260: cur+=snprintf(cur, MAX_NUMBER+1, "%u,", offset);
1.6 misha 261: if(limit!=SQL_NO_LIMIT)
1.1 misha 262: cur+=snprintf(cur, MAX_NUMBER, "%u", limit);
263: statement=statement_limited;
264: } else
265: statement=astatement;
266:
267:
268: const char *pzTail;
1.6 misha 269: int next_statement_length=0;
1.1 misha 270: sqlite3_stmt *SQL;
271: int rc;
272: SQL_Error sql_error;
1.5 misha 273: bool failed=false;
1.1 misha 274:
275: do{ // cycling through SQL commands
1.6 misha 276: rc=sqlite3_prepare(connection.handle, statement, -1, &SQL, &pzTail);
277: next_statement_length=strlen(pzTail);
1.4 misha 278: if(rc!=SQLITE_OK){
1.8 misha 279: //sqlite3_free((char*)pzTail);
1.4 misha 280: _throw(connection, sqlite3_errmsg(connection.handle));
1.6 misha 281: }
282: if(!connection.multi_statements && next_statement_length>0){ // multi statements was not allowed but pzTail point to not empty one
1.8 misha 283: //sqlite3_free((char*)pzTail);
284: _throw(connection, "multi statements are not allowed until opption ?multi_statements=1 in connect string is specified.");
1.1 misha 285: }
286:
1.6 misha 287: #define CHECK(afailed) if(afailed){ failed=true; goto cleanup; }
1.1 misha 288:
1.6 misha 289: int column_count=sqlite3_column_count(SQL);
1.1 misha 290:
1.6 misha 291: if(!column_count){ // empty result: insert|delete|update|...
292: rc=sqlite3_step(SQL);
1.1 misha 293: } else {
1.9 misha 294: if(column_count>MAX_COLS)
295: column_count=MAX_COLS;
296:
297: int column_types[MAX_COLS];
298: bool transcode_column[MAX_COLS];
299:
1.6 misha 300: for(int i=0; i<column_count; i++){
301: const char *column_name=sqlite3_column_name(SQL, i);
302: size_t length=strlen(column_name);
1.1 misha 303:
304: char* strm=(char*)services.malloc_atomic(length+1);
305: memcpy(strm, column_name, length+1);
1.6 misha 306: const char* str=strm;
307: // transcode column name from ?ClientCharset to $request:charset
308: if(transcode_needed){
1.4 misha 309: services.transcode(str, length,
310: str, length,
1.9 misha 311: client_charset,
312: request_charset);
1.4 misha 313: }
314:
1.6 misha 315: CHECK(handlers.add_column(sql_error, (const char*)str, length));
1.1 misha 316: }
317: CHECK(handlers.before_rows(sql_error));
318:
1.5 misha 319: const char *str;
1.6 misha 320: size_t length=0;
1.9 misha 321: bool first_row=true;
1.1 misha 322:
323: do{
1.6 misha 324: rc=sqlite3_step(SQL);
325: if(rc==SQLITE_ROW){ // new line!!
1.1 misha 326:
327: CHECK(handlers.add_row(sql_error));
328:
1.6 misha 329: for(int i=0; i<column_count; i++){
1.9 misha 330: if(first_row){
331: column_types[i]=sqlite3_column_type(SQL, i);
332: switch(column_types[i]){
333: case SQLITE_INTEGER:
334: case SQLITE_FLOAT:
335: case SQLITE_NULL:
336: transcode_column[i]=false;
337: break;
338: default:
339: transcode_column[i]=transcode_needed;
340: break;
341: }
342: }
343:
1.6 misha 344: // SQLite allow to get value of any type using sqlite3_column_text function
1.9 misha 345: switch(column_types[i]){
1.2 misha 346: case SQLITE_NULL:
1.6 misha 347: length=0;
1.4 misha 348: str=NULL;
1.2 misha 349: break;
1.6 misha 350: case SQLITE_BLOB:
351: str=(const char*)sqlite3_column_blob(SQL, i);
352: length=(size_t)sqlite3_column_bytes(SQL, i);
353: break;
1.9 misha 354: default: // anything else?
1.5 misha 355: str=(const char*)sqlite3_column_text(SQL, i);
1.6 misha 356: length=(size_t)sqlite3_column_bytes(SQL, i);
1.1 misha 357: break;
358: }
359:
1.4 misha 360: if(length){
361: char* strm=(char*)services.malloc_atomic(length+1);
362: memcpy(strm, str, length+1);
1.5 misha 363: str=strm;
1.4 misha 364:
1.9 misha 365: if(transcode_column[i]){
366: // transcode cell value from ?ClientCharset to $request:charset
1.4 misha 367: services.transcode(str, length,
368: str, length,
1.9 misha 369: client_charset,
370: request_charset);
1.4 misha 371: }
372: } else
1.6 misha 373: str=0;
1.4 misha 374:
375: CHECK(handlers.add_row_cell(sql_error, str, length));
1.1 misha 376:
377: }
1.9 misha 378: first_row=false;
1.1 misha 379: }
1.6 misha 380: } while(rc==SQLITE_BUSY || rc==SQLITE_ROW);
1.1 misha 381:
1.6 misha 382: }
1.1 misha 383:
1.6 misha 384: if(rc==SQLITE_ERROR || rc==SQLITE_MISUSE){
1.4 misha 385: _throw(connection, sqlite3_errmsg(connection.handle));
1.1 misha 386: }
387:
388: cleanup:
389: sqlite3_finalize(SQL);
1.6 misha 390: statement=pzTail;
391: } while (next_statement_length>0);
1.1 misha 392:
393: if(failed)
1.5 misha 394: services._throw(sql_error);
1.1 misha 395: }
396:
1.4 misha 397: private:
1.6 misha 398: void _begin_transaction(Connection& connection) {
399: if(!connection.autocommit){
400: _execute_cmd(connection, "BEGIN");
401: }
402: }
403:
404: void _execute_cmd(Connection& connection, const char* statement){
405: char* zErr;
406: int rc=sqlite3_exec(connection.handle, statement, 0, 0, &zErr);
407: if(rc!=SQLITE_OK){
408: size_t length=strlen(zErr);
409: char* err_msg=(char *)connection.services->malloc_atomic(length+1);
410: memcpy(err_msg, zErr, length);
411:
412: sqlite3_free(zErr);
413: _throw(connection, err_msg);
414: }
415:
416: }
417:
418: void _throw(Connection& connection, const char* aerr_msg){
419: size_t length=strlen(aerr_msg);
420: if(length && _transcode_required(connection)){
421: // transcode server error message from ?ClientCharset to $request:charset
422: connection.services->transcode(aerr_msg, length,
423: aerr_msg, length,
424: connection.client_charset,
425: connection.services->request_charset());
426: }
427: connection.services->_throw(aerr_msg);
428: }
429:
430: bool _transcode_required(Connection& connection, const char* charset=0){
431: return (strcmp(charset?charset:connection.client_charset, connection.services->request_charset())!=0);
432: }
433:
1.4 misha 434:
1.1 misha 435: private: // sqlite client library funcs
436:
437: typedef int (*t_sqlite3_open)(const char *filename, sqlite3 **ppDb); t_sqlite3_open sqlite3_open;
438:
439: typedef int (*t_sqlite3_close)(sqlite3 *); t_sqlite3_close sqlite3_close;
440:
441: typedef int (*t_sqlite3_exec)(sqlite3*, const char *sql, sqlite3_callback, void *, char **errmsg); t_sqlite3_exec sqlite3_exec;
442:
443: typedef void (*t_sqlite3_free)(char *z); t_sqlite3_free sqlite3_free;
444:
445: typedef const char *(* t_sqlite3_errmsg)(sqlite3*); t_sqlite3_errmsg sqlite3_errmsg;
446:
447: typedef int (* t_sqlite3_prepare)(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail); t_sqlite3_prepare sqlite3_prepare;
448:
449: typedef int (* t_sqlite3_column_count)(sqlite3_stmt *pStmt); t_sqlite3_column_count sqlite3_column_count;
450:
451: typedef int (* t_sqlite3_finalize)(sqlite3_stmt *pStmt); t_sqlite3_finalize sqlite3_finalize;
452:
453: typedef const char *(* t_sqlite3_column_name)(sqlite3_stmt*,int); t_sqlite3_column_name sqlite3_column_name;
454:
455: typedef int (* t_sqlite3_step)(sqlite3_stmt*); t_sqlite3_step sqlite3_step;
456:
457: typedef int (* t_sqlite3_column_type)(sqlite3_stmt*, int iCol); t_sqlite3_column_type sqlite3_column_type;
458:
459: typedef const unsigned char *(* t_sqlite3_column_text)(sqlite3_stmt*, int iCol); t_sqlite3_column_text sqlite3_column_text;
460:
1.6 misha 461: typedef const unsigned char *(* t_sqlite3_column_blob)(sqlite3_stmt*, int iCol); t_sqlite3_column_blob sqlite3_column_blob;
462:
463: typedef int (* t_sqlite3_column_bytes)(sqlite3_stmt*, int iCol); t_sqlite3_column_bytes sqlite3_column_bytes;
464:
1.5 misha 465:
1.1 misha 466: private: // sqlite client library funcs linking
467:
468: const char *dlink(const char *dlopen_file_spec) {
469: if(lt_dlinit())
470: return lt_dlerror();
471: lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
472: if (!handle) {
473: if(const char* result=lt_dlerror())
474: return result;
475:
476: return "can not open the dynamic link module";
477: }
478:
479: #define DSLINK(name, action) \
480: name=(t_##name)lt_dlsym(handle, #name); \
481: if(!name) \
482: action;
483:
484: #define DLINK(name) DSLINK(name, return "function " #name " was not found")
485: #define SLINK(name) DSLINK(name, name=subst_##name)
486:
487: DLINK(sqlite3_open);
488: DLINK(sqlite3_close);
489: DLINK(sqlite3_exec);
490: DLINK(sqlite3_free);
491: DLINK(sqlite3_errmsg);
492: DLINK(sqlite3_prepare);
493: DLINK(sqlite3_column_count);
494: DLINK(sqlite3_finalize);
495: DLINK(sqlite3_column_name);
496: DLINK(sqlite3_step);
497: DLINK(sqlite3_column_type);
498: DLINK(sqlite3_column_text);
1.6 misha 499: DLINK(sqlite3_column_blob);
500: DLINK(sqlite3_column_bytes);
1.1 misha 501: return 0;
502: }
503:
504: };
505:
506: extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
507: return new SQLite_Driver();
508: }