|
|
| version 1.11, 2010/10/27 22:48:51 | version 1.16, 2019/11/30 21:51:35 |
|---|---|
| Line 3 | Line 3 |
| (c) Dmitry "Creator" Bobrik, 2004 | (c) Dmitry "Creator" Bobrik, 2004 |
| */ | */ |
| //static const char *RCSId="$Id$"; | |
| #include "config_includes.h" | #include "config_includes.h" |
| #include "pa_sql_driver.h" | #include "pa_sql_driver.h" |
| //#include "windows.h" // for messagebox | |
| volatile const char * IDENT_PARSER3SQLITE_C="$Id$" IDENT_PA_SQL_DRIVER_H; | |
| #define NO_CLIENT_LONG_LONG | #define NO_CLIENT_LONG_LONG |
| #include "sqlite3.h" | #include "sqlite3.h" |
| #include "ltdl.h" | #include "ltdl.h" |
| #define MAX_COLS 500 | |
| #define MAX_STRING 0x400 | #define MAX_STRING 0x400 |
| #define MAX_NUMBER 20 | #define MAX_NUMBER 20 |
| Line 55 struct Connection { | Line 54 struct Connection { |
| const char* client_charset; | const char* client_charset; |
| bool multi_statements; | bool multi_statements; |
| bool autocommit; | bool autocommit; |
| int busy_timeout; | |
| }; | }; |
| Line 95 public: | Line 95 public: |
| *connection_ref=&connection; | *connection_ref=&connection; |
| connection.services=&services; | connection.services=&services; |
| connection.client_charset=SQLITE_DEFAULT_CHARSET; | connection.client_charset=SQLITE_DEFAULT_CHARSET; |
| connection.multi_statements=false; | connection.multi_statements=false; |
| connection.autocommit=true; | connection.autocommit=true; |
| connection.busy_timeout=4000; | |
| char* db_path=0; | char* db_path=0; |
| char* db=url; | char* db=url; |
| Line 127 public: | Line 128 public: |
| if(strcasecmp(key, "multi_statements")==0){ | if(strcasecmp(key, "multi_statements")==0){ |
| if(atoi(value)!=0) | if(atoi(value)!=0) |
| connection.multi_statements=true; | connection.multi_statements=true; |
| } else if(strcasecmp(key, "busy_timeout")==0){ | |
| connection.busy_timeout=atoi(value); | |
| } else if(strcasecmp(key, "autocommit")==0){ | } else if(strcasecmp(key, "autocommit")==0){ |
| if(atoi(value)==0) | if(atoi(value)==0) |
| connection.autocommit=false; | connection.autocommit=false; |
| continue; | |
| } else if(strcmp(key, "ClientCharset")==0){ | } else if(strcmp(key, "ClientCharset")==0){ |
| toupper_str(value, value, strlen(value)); | toupper_str(value, value, strlen(value)); |
| connection.client_charset=value; | connection.client_charset=value; |
| continue; | |
| } else | } else |
| services._throw("unknown connect option" /*key*/); | services._throw("unknown connect option" /*key*/); |
| } else | } else |
| Line 161 public: | Line 162 public: |
| _throw(connection, error_msg); | _throw(connection, error_msg); |
| } | } |
| sqlite3_busy_timeout(connection.handle, connection.busy_timeout); | |
| _begin_transaction(connection); | _begin_transaction(connection); |
| } | } |
| Line 257 public: | Line 260 public: |
| cur+=statement_size; | cur+=statement_size; |
| cur+=sprintf(cur, " LIMIT "); | cur+=sprintf(cur, " LIMIT "); |
| if(offset) | if(offset) |
| cur+=snprintf(cur, MAX_NUMBER+1, "%u,", offset); | cur+=snprintf(cur, MAX_NUMBER+1, "%lu,", offset); |
| if(limit!=SQL_NO_LIMIT) | if(limit!=SQL_NO_LIMIT) |
| cur+=snprintf(cur, MAX_NUMBER, "%u", limit); | cur+=snprintf(cur, MAX_NUMBER, "%lu", limit); |
| statement=statement_limited; | statement=statement_limited; |
| } else | } else |
| statement=astatement; | statement=astatement; |
| Line 281 public: | Line 284 public: |
| } | } |
| if(!connection.multi_statements && next_statement_length>0){ // multi statements was not allowed but pzTail point to not empty one | if(!connection.multi_statements && next_statement_length>0){ // multi statements was not allowed but pzTail point to not empty one |
| //sqlite3_free((char*)pzTail); | //sqlite3_free((char*)pzTail); |
| _throw(connection, "multi statements are not allowed until opption ?multi_statements=1 in connect string is specified."); | _throw(connection, "multi statements are not allowed until option ?multi_statements=1 in connect string is specified."); |
| } | } |
| #define CHECK(afailed) if(afailed){ failed=true; goto cleanup; } | #define CHECK(afailed) if(afailed){ failed=true; goto cleanup; } |
| Line 291 public: | Line 294 public: |
| if(!column_count){ // empty result: insert|delete|update|... | if(!column_count){ // empty result: insert|delete|update|... |
| rc=sqlite3_step(SQL); | rc=sqlite3_step(SQL); |
| } else { | } else { |
| if(column_count>MAX_COLS) | |
| column_count=MAX_COLS; | |
| int column_types[MAX_COLS]; | |
| bool transcode_column[MAX_COLS]; | |
| for(int i=0; i<column_count; i++){ | for(int i=0; i<column_count; i++){ |
| const char *column_name=sqlite3_column_name(SQL, i); | const char *column_name=sqlite3_column_name(SQL, i); |
| size_t length=strlen(column_name); | size_t length=strlen(column_name); |
| Line 318 public: | Line 315 public: |
| const char *str; | const char *str; |
| size_t length=0; | size_t length=0; |
| bool first_row=true; | |
| do{ | do{ |
| rc=sqlite3_step(SQL); | rc=sqlite3_step(SQL); |
| Line 327 public: | Line 323 public: |
| CHECK(handlers.add_row(sql_error)); | CHECK(handlers.add_row(sql_error)); |
| for(int i=0; i<column_count; i++){ | for(int i=0; i<column_count; i++){ |
| if(first_row){ | |
| column_types[i]=sqlite3_column_type(SQL, i); | |
| switch(column_types[i]){ | |
| case SQLITE_INTEGER: | |
| case SQLITE_FLOAT: | |
| case SQLITE_NULL: | |
| transcode_column[i]=false; | |
| break; | |
| default: | |
| transcode_column[i]=transcode_needed; | |
| break; | |
| } | |
| } | |
| // SQLite allow to get value of any type using sqlite3_column_text function | // SQLite allow to get value of any type using sqlite3_column_text function |
| switch(column_types[i]){ | bool transcode_value=false; |
| int column_type=sqlite3_column_type(SQL, i); | |
| switch(column_type){ | |
| case SQLITE_NULL: | case SQLITE_NULL: |
| length=0; | length=0; |
| str=NULL; | str=NULL; |
| Line 351 public: | Line 335 public: |
| str=(const char*)sqlite3_column_blob(SQL, i); | str=(const char*)sqlite3_column_blob(SQL, i); |
| length=(size_t)sqlite3_column_bytes(SQL, i); | length=(size_t)sqlite3_column_bytes(SQL, i); |
| break; | break; |
| case SQLITE_TEXT: // for text transcoding can be required | |
| default: // anything else? | default: // anything else? |
| transcode_value=transcode_needed; | |
| case SQLITE_INTEGER: | |
| case SQLITE_FLOAT: | |
| str=(const char*)sqlite3_column_text(SQL, i); | str=(const char*)sqlite3_column_text(SQL, i); |
| length=(size_t)sqlite3_column_bytes(SQL, i); | length=(size_t)sqlite3_column_bytes(SQL, i); |
| break; | break; |
| Line 359 public: | Line 347 public: |
| if(length){ | if(length){ |
| char* strm=(char*)services.malloc_atomic(length+1); | char* strm=(char*)services.malloc_atomic(length+1); |
| memcpy(strm, str, length+1); | memcpy(strm, str, length); |
| strm[length]=0; | |
| str=strm; | str=strm; |
| if(transcode_column[i]){ | if(transcode_value){ |
| // transcode cell value from ?ClientCharset to $request:charset | // transcode cell value from ?ClientCharset to $request:charset |
| services.transcode(str, length, | services.transcode(str, length, |
| str, length, | str, length, |
| Line 375 public: | Line 364 public: |
| CHECK(handlers.add_row_cell(sql_error, str, length)); | CHECK(handlers.add_row_cell(sql_error, str, length)); |
| } | } |
| first_row=false; | |
| } | } |
| } while(rc==SQLITE_BUSY || rc==SQLITE_ROW); | } while(rc==SQLITE_BUSY || rc==SQLITE_ROW); |
| Line 438 private: // sqlite client library funcs | Line 426 private: // sqlite client library funcs |
| typedef int (*t_sqlite3_close)(sqlite3 *); t_sqlite3_close sqlite3_close; | typedef int (*t_sqlite3_close)(sqlite3 *); t_sqlite3_close sqlite3_close; |
| typedef int (*t_sqlite3_busy_timeout)(sqlite3*, int ms); t_sqlite3_busy_timeout sqlite3_busy_timeout; | |
| typedef int (*t_sqlite3_exec)(sqlite3*, const char *sql, sqlite3_callback, void *, char **errmsg); t_sqlite3_exec sqlite3_exec; | typedef int (*t_sqlite3_exec)(sqlite3*, const char *sql, sqlite3_callback, void *, char **errmsg); t_sqlite3_exec sqlite3_exec; |
| typedef void (*t_sqlite3_free)(char *z); t_sqlite3_free sqlite3_free; | typedef void (*t_sqlite3_free)(char *z); t_sqlite3_free sqlite3_free; |
| Line 466 private: // sqlite client library funcs | Line 456 private: // sqlite client library funcs |
| private: // sqlite client library funcs linking | private: // sqlite client library funcs linking |
| const char *dlink(const char *dlopen_file_spec) { | const char *dlink(const char *dlopen_file_spec) { |
| if(lt_dlinit()) | if(lt_dlinit()){ |
| return lt_dlerror(); | if(const char* result=lt_dlerror()) |
| lt_dlhandle handle=lt_dlopen(dlopen_file_spec); | return result; |
| if (!handle) { | return "can not prepare to dynamic loading"; |
| if(const char* result=lt_dlerror()) | } |
| return result; | |
| lt_dlhandle handle=lt_dlopen(dlopen_file_spec); | |
| if(!handle){ | |
| if(const char* result=lt_dlerror()) | |
| return result; | |
| return "can not open the dynamic link module"; | return "can not open the dynamic link module"; |
| } | } |
| Line 486 private: // sqlite client library funcs | Line 480 private: // sqlite client library funcs |
| DLINK(sqlite3_open); | DLINK(sqlite3_open); |
| DLINK(sqlite3_close); | DLINK(sqlite3_close); |
| DLINK(sqlite3_busy_timeout); | |
| DLINK(sqlite3_exec); | DLINK(sqlite3_exec); |
| DLINK(sqlite3_free); | DLINK(sqlite3_free); |
| DLINK(sqlite3_errmsg); | DLINK(sqlite3_errmsg); |