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