--- sql/sqlite/parser3sqlite.C 2008/06/30 09:29:55 1.8 +++ sql/sqlite/parser3sqlite.C 2012/04/19 21:33:08 1.12 @@ -3,7 +3,7 @@ (c) Dmitry "Creator" Bobrik, 2004 */ -//static const char *RCSId="$Id: parser3sqlite.C,v 1.8 2008/06/30 09:29:55 misha Exp $"; +//static const char *RCSId="$Id: parser3sqlite.C,v 1.12 2012/04/19 21:33:08 moko Exp $"; #include "config_includes.h" @@ -14,6 +14,7 @@ #include "sqlite3.h" #include "ltdl.h" +#define MAX_COLS 500 #define MAX_STRING 0x400 #define MAX_NUMBER 20 @@ -54,6 +55,7 @@ struct Connection { const char* client_charset; bool multi_statements; bool autocommit; + int busy_timeout; }; @@ -94,9 +96,10 @@ public: *connection_ref=&connection; connection.services=&services; - connection.client_charset=SQLITE_DEFAULT_CHARSET; + connection.client_charset=SQLITE_DEFAULT_CHARSET; connection.multi_statements=false; connection.autocommit=true; + connection.busy_timeout=4000; char* db_path=0; char* db=url; @@ -112,9 +115,9 @@ public: services._throw("document_root is empty"); db_path=(char*)services.malloc_atomic(strlen(document_root)+1+strlen(db)+1); - db_path=strcpy(db_path, document_root); - db_path=strcat(db_path, "/"); - db_path=strcat(db_path, db); + strcpy(db_path, document_root); + strcat(db_path, "/"); + strcat(db_path, db); } //services._throw(db_path); @@ -126,14 +129,14 @@ public: if(strcasecmp(key, "multi_statements")==0){ if(atoi(value)!=0) connection.multi_statements=true; + } else if(strcasecmp(key, "busy_timeout")==0){ + connection.busy_timeout=atoi(value); } else if(strcasecmp(key, "autocommit")==0){ if(atoi(value)==0) connection.autocommit=false; - continue; } else if(strcmp(key, "ClientCharset")==0){ toupper_str(value, value, strlen(value)); connection.client_charset=value; - continue; } else services._throw("unknown connect option" /*key*/); } else @@ -160,6 +163,8 @@ public: _throw(connection, error_msg); } + sqlite3_busy_timeout(connection.handle, connection.busy_timeout); + _begin_transaction(connection); } @@ -189,23 +194,33 @@ public: 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(aconnection); - /* - You must allocate the to buffer to be at least length*2+1 bytes long. - In the worse case, each character may need to be encoded as using two bytes, - and you need room for the terminating null byte. - */ - char *result=(char*)connection.services->malloc_atomic(length*2+1); - char *to=result; - while(length--) { - if(*from=='\'') { // ' -> '' - *to++='\''; - } else if(*from=='\"') { // " -> "" - *to++='\"'; - } - *to++=*from++; + char *result=(char*)connection.services->malloc_atomic(length + quoted + 1); + char *to = result; + + for(from=str; from '' + *to++=*from; } + *to=0; return result; } @@ -222,7 +237,9 @@ public: if(placeholders_count>0) 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); // transcode query from $request:charset to ?ClientCharset @@ -230,8 +247,8 @@ public: size_t length=strlen(astatement); services.transcode(astatement, length, astatement, length, - services.request_charset(), - connection.client_charset); + request_charset, + client_charset); } const char *statement; @@ -244,9 +261,9 @@ public: cur+=statement_size; cur+=sprintf(cur, " LIMIT "); if(offset) - cur+=snprintf(cur, MAX_NUMBER+1, "%u,", offset); + cur+=snprintf(cur, MAX_NUMBER+1, "%lu,", offset); if(limit!=SQL_NO_LIMIT) - cur+=snprintf(cur, MAX_NUMBER, "%u", limit); + cur+=snprintf(cur, MAX_NUMBER, "%lu", limit); statement=statement_limited; } else statement=astatement; @@ -278,6 +295,12 @@ public: if(!column_count){ // empty result: insert|delete|update|... rc=sqlite3_step(SQL); } 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