|
|
| version 1.8, 2008/06/30 09:29:55 | version 1.13, 2012/06/15 09:09:33 |
|---|---|
| Line 14 | Line 14 |
| #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 54 struct Connection { | Line 55 struct Connection { |
| const char* client_charset; | const char* client_charset; |
| bool multi_statements; | bool multi_statements; |
| bool autocommit; | bool autocommit; |
| int busy_timeout; | |
| }; | }; |
| Line 94 public: | Line 96 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 112 public: | Line 115 public: |
| services._throw("document_root is empty"); | services._throw("document_root is empty"); |
| db_path=(char*)services.malloc_atomic(strlen(document_root)+1+strlen(db)+1); | db_path=(char*)services.malloc_atomic(strlen(document_root)+1+strlen(db)+1); |
| db_path=strcpy(db_path, document_root); | strcpy(db_path, document_root); |
| db_path=strcat(db_path, "/"); | strcat(db_path, "/"); |
| db_path=strcat(db_path, db); | strcat(db_path, db); |
| } | } |
| //services._throw(db_path); | //services._throw(db_path); |
| Line 126 public: | Line 129 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 160 public: | Line 163 public: |
| _throw(connection, error_msg); | _throw(connection, error_msg); |
| } | } |
| sqlite3_busy_timeout(connection.handle, connection.busy_timeout); | |
| _begin_transaction(connection); | _begin_transaction(connection); |
| } | } |
| Line 189 public: | Line 194 public: |
| return true; // not needed | return true; // not needed |
| } | } |
| const char* quote(void *aconnection, const char *from, unsigned int length){ | // charset here is services.request_charset(), not connection.client_charset |
| // thus we can't use the sql server quoting support | |
| const char* quote(void *aconnection, const char *str, unsigned int length) | |
| { | |
| const char* from; | |
| const char* from_end=str+length; | |
| size_t quoted=0; | |
| for(from=str; from<from_end; from++){ | |
| if(*from=='\'') | |
| quoted++; | |
| } | |
| if(!quoted) | |
| return str; | |
| Connection& connection=*static_cast<Connection*>(aconnection); | Connection& connection=*static_cast<Connection*>(aconnection); |
| /* | char *result=(char*)connection.services->malloc_atomic(length + quoted + 1); |
| You must allocate the to buffer to be at least length*2+1 bytes long. | char *to = result; |
| In the worse case, each character may need to be encoded as using two bytes, | |
| and you need room for the terminating null byte. | for(from=str; from<from_end; from++){ |
| */ | if(*from=='\'') |
| char *result=(char*)connection.services->malloc_atomic(length*2+1); | *to++= '\''; // ' -> '' |
| char *to=result; | *to++=*from; |
| while(length--) { | |
| if(*from=='\'') { // ' -> '' | |
| *to++='\''; | |
| } else if(*from=='\"') { // " -> "" | |
| *to++='\"'; | |
| } | |
| *to++=*from++; | |
| } | } |
| *to=0; | *to=0; |
| return result; | return result; |
| } | } |
| Line 222 public: | Line 237 public: |
| if(placeholders_count>0) | if(placeholders_count>0) |
| services._throw("bind variables not supported yet"); | services._throw("bind variables not supported yet"); |
| const char* request_charset=services.request_charset(); | |
| const char* client_charset=connection.client_charset; | |
| bool transcode_needed=_transcode_required(connection); | bool transcode_needed=_transcode_required(connection); |
| // transcode query from $request:charset to ?ClientCharset | // transcode query from $request:charset to ?ClientCharset |
| Line 230 public: | Line 247 public: |
| size_t length=strlen(astatement); | size_t length=strlen(astatement); |
| services.transcode(astatement, length, | services.transcode(astatement, length, |
| astatement, length, | astatement, length, |
| services.request_charset(), | request_charset, |
| connection.client_charset); | client_charset); |
| } | } |
| const char *statement; | const char *statement; |
| Line 244 public: | Line 261 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 278 public: | Line 295 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 289 public: | Line 312 public: |
| if(transcode_needed){ | if(transcode_needed){ |
| services.transcode(str, length, | services.transcode(str, length, |
| str, length, | str, length, |
| connection.client_charset, | client_charset, |
| services.request_charset()); | request_charset); |
| } | } |
| CHECK(handlers.add_column(sql_error, (const char*)str, length)); | CHECK(handlers.add_column(sql_error, (const char*)str, length)); |
| } | } |
| CHECK(handlers.before_rows(sql_error)); | CHECK(handlers.before_rows(sql_error)); |
| int column_type; | |
| 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 308 public: | Line 331 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 |
| column_type=sqlite3_column_type(SQL, i); | switch(column_types[i]){ |
| bool transcode_value=false; | |
| switch(column_type){ | |
| case SQLITE_NULL: | case SQLITE_NULL: |
| length=0; | length=0; |
| str=NULL; | str=NULL; |
| Line 320 public: | Line 355 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? |
| transcode_value=transcode_needed; | |
| case SQLITE_INTEGER: | |
| case SQLITE_FLOAT: | |
| default: // what else? may be for future purposes | |
| 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 335 public: | Line 366 public: |
| memcpy(strm, str, length+1); | memcpy(strm, str, length+1); |
| str=strm; | str=strm; |
| // transcode cell value from ?ClientCharset to $request:charset | if(transcode_column[i]){ |
| if(transcode_value){ | // transcode cell value from ?ClientCharset to $request:charset |
| services.transcode(str, length, | services.transcode(str, length, |
| str, length, | str, length, |
| connection.client_charset, | client_charset, |
| services.request_charset()); | request_charset); |
| } | } |
| } else | } else |
| str=0; | str=0; |
| Line 348 public: | Line 379 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 410 private: // sqlite client library funcs | Line 442 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 438 private: // sqlite client library funcs | Line 472 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 458 private: // sqlite client library funcs | Line 496 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); |