Diff for /sql/sqlite/parser3sqlite.C between versions 1.5 and 1.7

version 1.5, 2008/06/24 17:50:47 version 1.7, 2008/06/27 17:36:30
Line 25 Line 25
 #endif  #endif
   
 static char *lsplit(char *string, char delim) {  static char *lsplit(char *string, char delim) {
     if(string) {          if(string) {
                 char *v=strchr(string, delim);                  char *v=strchr(string, delim);
                 if(v){                  if(v) {
                         *v=0;                          *v=0;
                         return v+1;                          return v+1;
                 }                  }
     }          }
     return 0;          return 0;
 }  }
   
 static char *lsplit(char **string_ref, char delim) {  static char *lsplit(char **string_ref, char delim) {
     char *result=*string_ref;          char *result=*string_ref;
         char *next=lsplit(*string_ref, delim);          char *next=lsplit(*string_ref, delim);
     *string_ref=next;          *string_ref=next;
     return result;          return result;
 }  }
   
 static void toupper_str(char *out, const char *in, size_t size) {  static void toupper_str(char *out, const char *in, size_t size) {
Line 51  struct Connection { Line 51  struct Connection {
         SQL_Driver_services* services;          SQL_Driver_services* services;
   
         sqlite3* handle;          sqlite3* handle;
         const char* cstrClientCharset;          const char* client_charset;
           bool multi_statements;
         bool autocommit;          bool autocommit;
 };  };
   
Line 78  public: Line 79  public:
         /**     connect          /**     connect
                 @param url                  @param url
                         format: @b [localhost/]dbfile?                          format: @b [localhost/]dbfile?
                         ClientCharset=UTF-8&                          autocommit=1&
                         autocommit=1                          multi_statements=0&
                           ClientCharset=UTF-8
         */          */
         void connect(          void connect(
                         char *url,                                   char *url, 
                         SQL_Driver_services& services,                                   SQL_Driver_services& services, 
                         void **connection_ref ///< output: Connection*                                  void **connection_ref ///< output: Connection*
                 ){                  ){
   
                 int rc;  
   
                 Connection& connection=*(Connection *)services.malloc(sizeof(Connection));                  Connection& connection=*(Connection *)services.malloc(sizeof(Connection));
                   *connection_ref=&connection;
                 connection.services=&services;                  connection.services=&services;
                 connection.cstrClientCharset=SQLITE_DEFAULT_CHARSET;      
                   connection.client_charset=SQLITE_DEFAULT_CHARSET;       
                   connection.multi_statements=false;
                 connection.autocommit=true;                  connection.autocommit=true;
   
                 char* db = url;                  char* db_path=0;
                 char* options = lsplit(db, '?');                  char* db=url;
                                   char* options=lsplit(db, '?');
                 //char* document_root=(char*)services.request_document_root();  
                 char* document_root=0;                  if(strcmp(db, ":memory:")==0){ // in-memory table
                 char *db_path=(char*)services.malloc(strlen(document_root) + strlen(db) + 2);                           db_path=db;
                 if(document_root){                  } else if(strcmp(db, ":temporary:")==0){ // on disk temporary table (in :memory: style)
                         db_path=strncat(db_path, document_root, MAX_STRING);                          // do nothing: empty path mean temporary table on disk
                         db_path+='/';                  } else { // build path to DB-file from document_root (as anywhere in parser)
                           if(char* document_root=(char*)services.request_document_root()){
                                   db_path=(char*)services.malloc_atomic(strlen(document_root)+strlen(db)+1+1);
                                   db_path=strncpy(db_path, document_root, MAX_STRING);
                                   db_path=strncat(db_path, "/", MAX_STRING);
                           } else {
                                   // if document root empty -- build path from executable file
                                   db_path=(char*)services.malloc_atomic(strlen(db)+2+1);
                                   db_path=strncpy(db_path, "./", MAX_STRING);
                           }
                           db_path=strncat(db_path, db, strlen(db));
                 }                  }
                 db_path=strncat(db_path, db, MAX_STRING);  
   
                 while(options) {                  //services._throw(db_path);
                         if(char *key=lsplit(&options, '&')) {  
                   while(options){
                           if(char* key=lsplit(&options, '&')){
                                 if(*key) {                                  if(*key) {
                                         if(char *value=lsplit(key, '=')) {                                          if(char* value=lsplit(key, '=')){
                                                 if(strcmp(key, "ClientCharset" )==0) { // transcoding with parser                                                  if(strcasecmp(key, "multi_statements")==0) {
                                                         toupper_str(value, value, strlen(value));                                                          if(atoi(value)!=0)
                                                         connection.cstrClientCharset=value;                                                                  connection.multi_statements=true;
                                                         continue;                                                  } 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;                                                          continue;
                                                   } else if(strcmp(key, "ClientCharset")==0){     // transcoding with parser. 
                                                                                                   // by default we always transcode to UTF-8 (sqlite default)
                                                                                                   // so use this option only if you already stored data in your sqlite DB
                                                                                                   // in wrong encoding (1251 for ex.)
                                                           toupper_str(value, value, strlen(value));
                                                           connection.client_charset=value;
                                                           continue;
                                                 } else                                                  } else
                                                         services._throw("unknown connect option" /*key*/);                                                          services._throw("unknown connect option" /*key*/);
                                         } else                                           } else 
Line 127  public: Line 147  public:
                 }                  }
   
                 // transcode database_name from $request:charset to UTF-8                  // transcode database_name from $request:charset to UTF-8
                 size_t transcoded_db_path_size;                  if(db_path && _transcode_required(connection, SQLITE_DEFAULT_CHARSET)){
                 const char* sdb = db_path;                          size_t length=strlen(db_path);
                 services.transcode(sdb, strlen(db_path),                          services.transcode((const char*)db_path, length,
                         sdb, transcoded_db_path_size,                                  (const char*&)db_path, length,
                         services.request_charset(),                                  services.request_charset(),
                         SQLITE_DEFAULT_CHARSET);                                  SQLITE_DEFAULT_CHARSET);
                   
                 rc=sqlite3_open(db_path, &connection.handle);  
   
                 if(rc!=SQLITE_OK){  
                         _throw(connection, sqlite3_errmsg(connection.handle));  
                         sqlite3_close(connection.handle);  
                 }                  }
                   
   
                 *connection_ref=&connection;                  int rc=sqlite3_open(db_path, &connection.handle);
   
                 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);  
                 if(rc!=SQLITE_OK){                  if(rc!=SQLITE_OK){
                         _throw(connection, zErr);                          const char* error_msg=sqlite3_errmsg(connection.handle);
                         sqlite3_free(zErr); // error? can't free memory after throw                          sqlite3_close(connection.handle);
                           _throw(connection, error_msg);
                 }                  }
                   
                   _begin_transaction(connection);
         }          }
   
         void disconnect(void *aconnection) {          void disconnect(void *aconnection){
                 Connection& connection=*static_cast<Connection*>(aconnection);                  Connection& connection=*static_cast<Connection*>(aconnection);
                 sqlite3_close(connection.handle);                  sqlite3_close(connection.handle);
                 connection.handle=0;                  connection.handle=0;
         }          }
   
         void commit(void *aconnection) {          void commit(void *aconnection){
                 Connection& connection=*static_cast<Connection*>(aconnection);                  Connection& connection=*static_cast<Connection*>(aconnection);
                 if(!connection.autocommit)                  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<Connection*>(aconnection);                  Connection& connection=*static_cast<Connection*>(aconnection);
                 if(!connection.autocommit)                  if(!connection.autocommit)
                         exec(connection, "ROLLBACK");                          _execute_cmd(connection, "ROLLBACK");
   
                   _begin_transaction(connection);
         }          }
   
         bool ping(void *aconnection) {          bool ping(void *aconnection){
                 return true;  // not needed                  return true; // not needed
         }          }
   
         const char* quote(void *aconnection, const char *from, unsigned int length) {          const char* quote(void *aconnection, const char *from, unsigned int length){
                 Connection& connection=*static_cast<Connection*>(aconnection);                  Connection& connection=*static_cast<Connection*>(aconnection);
                 /*                  /*
                         You must allocate the to buffer to be at least length*2+1 bytes long.                           You must allocate the to buffer to be at least length*2+1 bytes long. 
Line 203  public: Line 215  public:
         }          }
   
         void query(void *aconnection,           void query(void *aconnection, 
                 const char *astatement,                           const char *astatement, 
                 size_t placeholders_count, Placeholder* placeholders,                           size_t placeholders_count, Placeholder* placeholders, 
                 unsigned long offset, unsigned long limit,                          unsigned long offset, unsigned long limit,
                 SQL_Driver_query_event_handlers& handlers) {                          SQL_Driver_query_event_handlers& handlers
                   ){
   
                 Connection& connection=*static_cast<Connection*>(aconnection);                  Connection& connection=*static_cast<Connection*>(aconnection);
                 SQL_Driver_services& services=*connection.services;                  SQL_Driver_services& services=*connection.services;
                 const char* cstrClientCharset=connection.cstrClientCharset;  
   
                 if(placeholders_count>0)                  if(placeholders_count>0)
                         services._throw("bind variables not supported yet");                          services._throw("bind variables not supported yet");
         
                 // transcode from $request:charset to ClientCharset                  bool transcode_needed=_transcode_required(connection);
                 if(cstrClientCharset) {  
                         size_t transcoded_statement_size;                  // transcode query from $request:charset to ?ClientCharset
                         services.transcode(astatement, strlen(astatement),                  if(transcode_needed){
                                 astatement, transcoded_statement_size,                          size_t length=strlen(astatement);
                           services.transcode(astatement, length,
                                   astatement, length,
                                 services.request_charset(),                                  services.request_charset(),
                                 cstrClientCharset);                                  connection.client_charset);
                 }                  }
                                   
                 const char *statement;                  const char *statement;
                 if(offset || limit) {                  if(offset || limit!=SQL_NO_LIMIT){
                         size_t statement_size=strlen(astatement);                          size_t statement_size=strlen(astatement);
                         char *statement_limited=(char *)services.malloc_atomic(                          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;                          char *cur=statement_limited;
                         memcpy(cur, astatement, statement_size); cur+=statement_size;                          memcpy(cur, astatement, statement_size);
                         cur+=sprintf(cur, " limit ");                          cur+=statement_size;
                           cur+=sprintf(cur, " LIMIT ");
                         if(offset)                          if(offset)
                                 cur+=snprintf(cur, MAX_NUMBER+1, "%u,", offset);                                  cur+=snprintf(cur, MAX_NUMBER+1, "%u,", offset);
                         if(limit)                          if(limit!=SQL_NO_LIMIT)
                                 cur+=snprintf(cur, MAX_NUMBER, "%u", limit);                                  cur+=snprintf(cur, MAX_NUMBER, "%u", limit);
                         statement=statement_limited;                          statement=statement_limited;
                 } else                  } else
Line 242  public: Line 257  public:
   
   
                 const char *pzTail;                  const char *pzTail;
                   int next_statement_length=0;
                 sqlite3_stmt *SQL;                  sqlite3_stmt *SQL;
                 int rc;                  int rc;
                 int i;  
                 SQL_Error sql_error;                  SQL_Error sql_error;
                 bool failed=false;                  bool failed=false;
   
                 do{ // cycling through SQL commands                  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){                          if(rc!=SQLITE_OK){
                                   sqlite3_free((char*)pzTail);
                                 _throw(connection, sqlite3_errmsg(connection.handle));                                  _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 in this connection");
                         }                          }
                                                   
                           #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|...                          if(!column_count){ // empty result: insert|delete|update|...
                                 rc = sqlite3_step(SQL);                                  rc=sqlite3_step(SQL);
                         } else {                          } else {
                                   for(int i=0; i<column_count; i++){
                                 for(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);  
   
                                         char* strm=(char*)services.malloc_atomic(length+1);                                          char* strm=(char*)services.malloc_atomic(length+1);
                                         memcpy(strm, column_name, length+1);                                          memcpy(strm, column_name, length+1);
                                         const char* str = strm;                                          const char* str=strm;
                                         // transcode to $request:charset from connect-string?ClientCharset                                          // transcode column name from ?ClientCharset to $request:charset 
                                         if(cstrClientCharset) {                                          if(transcode_needed){
                                                 services.transcode(str, length,                                                  services.transcode(str, length,
                                                         str, length,                                                          str, length,
                                                         cstrClientCharset,                                                          connection.client_charset,
                                                         services.request_charset());                                                          services.request_charset());
                                         }                                          }
                                                                                                                   
                                         CHECK(handlers.add_column(sql_error, (const char*)strm, 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;                                  int column_type;
                                 const char *str;                                  const char *str;
                                 size_t length = 0;                                  size_t length=0;
   
                                 do{                                  do{
                                         rc = sqlite3_step(SQL);                                          rc=sqlite3_step(SQL);
                                         if( rc == SQLITE_ROW ){   // новая строка!!                                          if(rc==SQLITE_ROW){ // new line!!
   
                                                 CHECK(handlers.add_row(sql_error));                                                  CHECK(handlers.add_row(sql_error));
   
                                                 for(i=0; i<column_count; i++){                                                  for(int i=0; i<column_count; i++){
                                                           // SQLite allow to get value of any type using sqlite3_column_text function
                                                         column_type = sqlite3_column_type(SQL, i);                                                          column_type=sqlite3_column_type(SQL, i);
                                                                           bool transcode_value=false;
                                                         // SQLite позволяет поле любого типа получить в виде строки через sqlite3_column_text                                                          switch(column_type){
                                                         // просто перекодирует если требуется  
                                                         // а парсер только строковые значения получает  
                                                         // но switch я всё-таки сделал - так, на будущее  
                                                         switch(column_type) {  
                                                                 case SQLITE_TEXT:  
                                                                         str=(const char*)sqlite3_column_text(SQL, i);  
                                                                         length=strlen((const char*)str);  
                                                                         break;  
                                                                 case SQLITE_INTEGER:  
                                                                         str=(const char*)sqlite3_column_text(SQL, i);  
                                                                         length=strlen((const char*)str);  
                                                                         break;  
                                                                 case SQLITE_NULL:                                                                  case SQLITE_NULL:
                                                                         str=NULL;  
                                                                         length=0;                                                                          length=0;
                                                                           str=NULL;
                                                                         break;                                                                          break;
                                                                 default:                                                                  case SQLITE_BLOB:
                                                                           str=(const char*)sqlite3_column_blob(SQL, i);
                                                                           length=(size_t)sqlite3_column_bytes(SQL, i);
                                                                           break;
                                                                   case SQLITE_TEXT: // for text transcoding can be required
                                                                           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=strlen((const char*)str);                                                                          length=(size_t)sqlite3_column_bytes(SQL, i);
                                                                         break;                                                                          break;
                                                         }                                                          }
   
Line 327  public: Line 339  public:
                                                                 memcpy(strm, str, length+1);                                                                  memcpy(strm, str, length+1);
                                                                 str=strm;                                                                  str=strm;
   
                                                                 // transcode to $request:charset from connect-string?ClientCharset                                                                  // transcode cell value from ?ClientCharset to $request:charset 
                                                                 if(cstrClientCharset) {                                                                  if(transcode_value){
                                                                         services.transcode(str, length,                                                                          services.transcode(str, length,
                                                                                 str, length,                                                                                  str, length,
                                                                                 cstrClientCharset,                                                                                  connection.client_charset,
                                                                                 services.request_charset());                                                                                  services.request_charset());
                                                                 }                                                                  }
                                                         } else                                                          } else
                                                                 str = 0;                                                                  str=0;
                                                                                                                   
                                                         CHECK(handlers.add_row_cell(sql_error, str, length));                                                          CHECK(handlers.add_row_cell(sql_error, str, length));
   
                                                 }                                                  }
                                         }                                          }
                                 } while(rc == SQLITE_BUSY || rc == SQLITE_ROW);                                  } while(rc==SQLITE_BUSY || rc==SQLITE_ROW);
   
                         }  // if column                          }
   
                         if(rc == SQLITE_ERROR || rc == SQLITE_MISUSE){                          if(rc==SQLITE_ERROR || rc==SQLITE_MISUSE){
                                 _throw(connection, sqlite3_errmsg(connection.handle));                                  _throw(connection, sqlite3_errmsg(connection.handle));
                         }                          }
   
         cleanup:          cleanup:
                         sqlite3_finalize(SQL);                          sqlite3_finalize(SQL);
                         statement = pzTail;                          statement=pzTail;
                 } while (strlen(pzTail) > 0);                  } while (next_statement_length>0);
   
                 if(failed)                  if(failed)
                         services._throw(sql_error);                          services._throw(sql_error);
         }          }
   
 private:  private:
         void _throw(Connection& connection, const char* aerr_msg){          void _begin_transaction(Connection& connection) {
                         size_t err_length=strlen(aerr_msg);                  if(!connection.autocommit){
                         if(err_length && connection.cstrClientCharset) {                          _execute_cmd(connection, "BEGIN");
                                 connection.services->transcode(aerr_msg, err_length,                  }
                                         aerr_msg, err_length,          }
                                         connection.cstrClientCharset,  
                                         connection.services->request_charset());          void _execute_cmd(Connection& connection, const char* statement){
                         }                  char* zErr;
                         connection.services->_throw(aerr_msg);                  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  private: // sqlite client library funcs
   
Line 396  private: // sqlite client library funcs Line 434  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_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  private: // sqlite client library funcs linking
   
Line 430  private: // sqlite client library funcs Line 472  private: // sqlite client library funcs
                 DLINK(sqlite3_step);                  DLINK(sqlite3_step);
                 DLINK(sqlite3_column_type);                  DLINK(sqlite3_column_type);
                 DLINK(sqlite3_column_text);                  DLINK(sqlite3_column_text);
                   DLINK(sqlite3_column_blob);
                   DLINK(sqlite3_column_bytes);
                 return 0;                  return 0;
         }          }
   

Removed from v.1.5  
changed lines
  Added in v.1.7


E-mail: