--- sql/sqlite/parser3sqlite.C 2008/06/24 17:50:47 1.5 +++ sql/sqlite/parser3sqlite.C 2010/10/27 22:48:51 1.11 @@ -3,7 +3,7 @@ (c) Dmitry "Creator" Bobrik, 2004 */ -//static const char *RCSId="$Id: parser3sqlite.C,v 1.5 2008/06/24 17:50:47 misha Exp $"; +//static const char *RCSId="$Id: parser3sqlite.C,v 1.11 2010/10/27 22:48:51 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 @@ -25,21 +26,21 @@ #endif static char *lsplit(char *string, char delim) { - if(string) { + if(string) { char *v=strchr(string, delim); - if(v){ + if(v) { *v=0; return v+1; } - } - return 0; + } + return 0; } static char *lsplit(char **string_ref, char delim) { - char *result=*string_ref; + char *result=*string_ref; char *next=lsplit(*string_ref, delim); - *string_ref=next; - return result; + *string_ref=next; + return result; } static void toupper_str(char *out, const char *in, size_t size) { @@ -51,7 +52,8 @@ struct Connection { SQL_Driver_services* services; sqlite3* handle; - const char* cstrClientCharset; + const char* client_charset; + bool multi_statements; bool autocommit; }; @@ -77,47 +79,62 @@ public: /** connect @param url - format: @b [localhost/]dbfile? - ClientCharset=UTF-8& - autocommit=1 + format: @b db-file|:memory:|temporary:? + autocommit=1& // =0 disable autocommit. in this case 1 connect == 1 transaction. + // or you can use begin/commit|rollback explicitly + multi_statements=0& // =1 allow many statements in 1 query + ClientCharset=UTF-8 // will transcode to/from specified charset instead of UTF-8 (default for sqlite) */ void connect( - char *url, - SQL_Driver_services& services, - void **connection_ref ///< output: Connection* + char *url, + SQL_Driver_services& services, + void **connection_ref ///< output: Connection* ){ - int rc; - Connection& connection=*(Connection *)services.malloc(sizeof(Connection)); + *connection_ref=&connection; connection.services=&services; - connection.cstrClientCharset=SQLITE_DEFAULT_CHARSET; + + connection.client_charset=SQLITE_DEFAULT_CHARSET; + connection.multi_statements=false; connection.autocommit=true; - char* db = url; - char* options = lsplit(db, '?'); - - //char* document_root=(char*)services.request_document_root(); - char* document_root=0; - char *db_path=(char*)services.malloc(strlen(document_root) + strlen(db) + 2); - if(document_root){ - db_path=strncat(db_path, document_root, MAX_STRING); - db_path+='/'; + char* db_path=0; + char* db=url; + char* options=lsplit(db, '?'); + + if(strcmp(db, ":memory:")==0){ // in-memory temporary DB + db_path=db; + } else if(strcmp(db, ":temporary:")==0){ // on-disk temporary DB + // do nothing: empty path mean temporary table on disk + } else { + char* document_root=(char*)services.request_document_root(); + if(!document_root) // path to DB-file which was specified by user is path from document_root as anywhere in parser + services._throw("document_root is empty"); + + db_path=(char*)services.malloc_atomic(strlen(document_root)+1+strlen(db)+1); + strcpy(db_path, document_root); + strcat(db_path, "/"); + strcat(db_path, db); } - db_path=strncat(db_path, db, MAX_STRING); - while(options) { - if(char *key=lsplit(&options, '&')) { + //services._throw(db_path); + + while(options){ + if(char* key=lsplit(&options, '&')){ if(*key) { - if(char *value=lsplit(key, '=')) { - if(strcmp(key, "ClientCharset" )==0) { // transcoding with parser - toupper_str(value, value, strlen(value)); - connection.cstrClientCharset=value; - continue; - } else if(strcasecmp(key, "autocommit")==0) { + if(char* value=lsplit(key, '=')){ + if(strcasecmp(key, "multi_statements")==0){ + if(atoi(value)!=0) + connection.multi_statements=true; + } 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 @@ -127,114 +144,121 @@ public: } // transcode database_name from $request:charset to UTF-8 - size_t transcoded_db_path_size; - const char* sdb = db_path; - services.transcode(sdb, strlen(db_path), - sdb, transcoded_db_path_size, - services.request_charset(), - SQLITE_DEFAULT_CHARSET); - - rc=sqlite3_open(db_path, &connection.handle); + if(db_path && _transcode_required(connection, SQLITE_DEFAULT_CHARSET)){ + size_t length=strlen(db_path); + services.transcode((const char*)db_path, length, + (const char*&)db_path, length, + services.request_charset(), + SQLITE_DEFAULT_CHARSET); - if(rc!=SQLITE_OK){ - _throw(connection, sqlite3_errmsg(connection.handle)); - sqlite3_close(connection.handle); } + - *connection_ref=&connection; - - if(!connection.autocommit) - exec(connection, "SET AUTOCOMMIT=0"); - - } - - void exec(Connection& connection, const char* statement) { - char* zErr; - int rc; - rc=sqlite3_exec(connection.handle, statement, 0, 0, &zErr); + int rc=sqlite3_open(db_path, &connection.handle); if(rc!=SQLITE_OK){ - _throw(connection, zErr); - sqlite3_free(zErr); // error? can't free memory after throw + const char* error_msg=sqlite3_errmsg(connection.handle); + sqlite3_close(connection.handle); + _throw(connection, error_msg); } - + + _begin_transaction(connection); } - void disconnect(void *aconnection) { + void disconnect(void *aconnection){ Connection& connection=*static_cast(aconnection); sqlite3_close(connection.handle); connection.handle=0; } - void commit(void *aconnection) { + void commit(void *aconnection){ Connection& connection=*static_cast(aconnection); if(!connection.autocommit) - exec(connection, "COMMIT"); + _execute_cmd(connection, "COMMIT"); + + _begin_transaction(connection); } - void rollback(void *aconnection) { + void rollback(void *aconnection){ Connection& connection=*static_cast(aconnection); if(!connection.autocommit) - exec(connection, "ROLLBACK"); + _execute_cmd(connection, "ROLLBACK"); + + _begin_transaction(connection); } - bool ping(void *aconnection) { - return true; // not needed + bool ping(void *aconnection){ + 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; } void query(void *aconnection, - const char *astatement, - size_t placeholders_count, Placeholder* placeholders, - unsigned long offset, unsigned long limit, - SQL_Driver_query_event_handlers& handlers) { + const char *astatement, + size_t placeholders_count, Placeholder* placeholders, + unsigned long offset, unsigned long limit, + SQL_Driver_query_event_handlers& handlers + ){ Connection& connection=*static_cast(aconnection); SQL_Driver_services& services=*connection.services; - const char* cstrClientCharset=connection.cstrClientCharset; if(placeholders_count>0) services._throw("bind variables not supported yet"); - - // transcode from $request:charset to ClientCharset - if(cstrClientCharset) { - size_t transcoded_statement_size; - services.transcode(astatement, strlen(astatement), - astatement, transcoded_statement_size, - services.request_charset(), - cstrClientCharset); + + 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 + if(transcode_needed){ + size_t length=strlen(astatement); + services.transcode(astatement, length, + astatement, length, + request_charset, + client_charset); } const char *statement; - if(offset || limit) { + if(offset || limit!=SQL_NO_LIMIT){ size_t statement_size=strlen(astatement); char *statement_limited=(char *)services.malloc_atomic( - statement_size+MAX_NUMBER*2+8/* limit #,#*/+1); + statement_size+MAX_NUMBER*2+8/* LIMIT #,#*/+1); char *cur=statement_limited; - memcpy(cur, astatement, statement_size); cur+=statement_size; - cur+=sprintf(cur, " limit "); + memcpy(cur, astatement, statement_size); + cur+=statement_size; + cur+=sprintf(cur, " LIMIT "); if(offset) cur+=snprintf(cur, MAX_NUMBER+1, "%u,", offset); - if(limit) + if(limit!=SQL_NO_LIMIT) cur+=snprintf(cur, MAX_NUMBER, "%u", limit); statement=statement_limited; } else @@ -242,83 +266,94 @@ public: const char *pzTail; + int next_statement_length=0; sqlite3_stmt *SQL; int rc; - int i; SQL_Error sql_error; bool failed=false; do{ // cycling through SQL commands - - rc = sqlite3_prepare(connection.handle, statement, -1, &SQL, &pzTail); - + rc=sqlite3_prepare(connection.handle, statement, -1, &SQL, &pzTail); + next_statement_length=strlen(pzTail); if(rc!=SQLITE_OK){ + //sqlite3_free((char*)pzTail); _throw(connection, sqlite3_errmsg(connection.handle)); - sqlite3_free((char*)pzTail); // error? can't free memory after throw + } + if(!connection.multi_statements && next_statement_length>0){ // multi statements was not allowed but pzTail point to not empty one + //sqlite3_free((char*)pzTail); + _throw(connection, "multi statements are not allowed until opption ?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; } - - int column_count = sqlite3_column_count(SQL); + int column_count=sqlite3_column_count(SQL); - if(!column_count){ // empty result: insert|delete|update|... - rc = sqlite3_step(SQL); + 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(i=0; i 0); + statement=pzTail; + } while (next_statement_length>0); if(failed) services._throw(sql_error); } private: - void _throw(Connection& connection, const char* aerr_msg){ - size_t err_length=strlen(aerr_msg); - if(err_length && connection.cstrClientCharset) { - connection.services->transcode(aerr_msg, err_length, - aerr_msg, err_length, - connection.cstrClientCharset, - connection.services->request_charset()); - } - connection.services->_throw(aerr_msg); - } + void _begin_transaction(Connection& connection) { + if(!connection.autocommit){ + _execute_cmd(connection, "BEGIN"); + } + } + + void _execute_cmd(Connection& connection, const char* statement){ + char* zErr; + int rc=sqlite3_exec(connection.handle, statement, 0, 0, &zErr); + if(rc!=SQLITE_OK){ + size_t length=strlen(zErr); + char* err_msg=(char *)connection.services->malloc_atomic(length+1); + memcpy(err_msg, zErr, length); + + sqlite3_free(zErr); + _throw(connection, err_msg); + } + + } + + void _throw(Connection& connection, const char* aerr_msg){ + size_t length=strlen(aerr_msg); + if(length && _transcode_required(connection)){ + // transcode server error message from ?ClientCharset to $request:charset + connection.services->transcode(aerr_msg, length, + aerr_msg, length, + connection.client_charset, + connection.services->request_charset()); + } + connection.services->_throw(aerr_msg); + } + + bool _transcode_required(Connection& connection, const char* charset=0){ + return (strcmp(charset?charset:connection.client_charset, connection.services->request_charset())!=0); + } + private: // sqlite client library funcs @@ -396,6 +458,10 @@ private: // sqlite client library funcs typedef const unsigned char *(* t_sqlite3_column_text)(sqlite3_stmt*, int iCol); t_sqlite3_column_text sqlite3_column_text; + typedef const unsigned char *(* t_sqlite3_column_blob)(sqlite3_stmt*, int iCol); t_sqlite3_column_blob sqlite3_column_blob; + + typedef int (* t_sqlite3_column_bytes)(sqlite3_stmt*, int iCol); t_sqlite3_column_bytes sqlite3_column_bytes; + private: // sqlite client library funcs linking @@ -430,6 +496,8 @@ private: // sqlite client library funcs DLINK(sqlite3_step); DLINK(sqlite3_column_type); DLINK(sqlite3_column_text); + DLINK(sqlite3_column_blob); + DLINK(sqlite3_column_bytes); return 0; }