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

version 1.7, 2008/06/27 17:36:30 version 1.11, 2010/10/27 22:48:51
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 78  public: Line 79  public:
   
         /**     connect          /**     connect
                 @param url                  @param url
                         format: @b [localhost/]dbfile?                          format: @b db-file|:memory:|temporary:?
                         autocommit=1&                          autocommit=1&                   // =0 disable autocommit. in this case 1 connect == 1 transaction.
                         multi_statements=0&                                                                          //      or you can use begin/commit|rollback explicitly
                         ClientCharset=UTF-8                          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(          void connect(
                                 char *url,                                   char *url, 
Line 101  public: Line 103  public:
                 char* db=url;                  char* db=url;
                 char* options=lsplit(db, '?');                  char* options=lsplit(db, '?');
   
                 if(strcmp(db, ":memory:")==0){ // in-memory table                  if(strcmp(db, ":memory:")==0){ // in-memory temporary DB
                         db_path=db;                          db_path=db;
                 } else if(strcmp(db, ":temporary:")==0){ // on disk temporary table (in :memory: style)                  } else if(strcmp(db, ":temporary:")==0){ // on-disk temporary DB
                         // do nothing: empty path mean temporary table on disk                          // do nothing: empty path mean temporary table on disk
                 } else { // build path to DB-file from document_root (as anywhere in parser)                  } else {
                         if(char* document_root=(char*)services.request_document_root()){                          char* document_root=(char*)services.request_document_root();
                                 db_path=(char*)services.malloc_atomic(strlen(document_root)+strlen(db)+1+1);                          if(!document_root) // path to DB-file which was specified by user is path from document_root as anywhere in parser
                                 db_path=strncpy(db_path, document_root, MAX_STRING);                                  services._throw("document_root is empty");
                                 db_path=strncat(db_path, "/", MAX_STRING);  
                         } else {                          db_path=(char*)services.malloc_atomic(strlen(document_root)+1+strlen(db)+1);
                                 // if document root empty -- build path from executable file                          strcpy(db_path, document_root);
                                 db_path=(char*)services.malloc_atomic(strlen(db)+2+1);                          strcat(db_path, "/");
                                 db_path=strncpy(db_path, "./", MAX_STRING);                          strcat(db_path, db);
                         }  
                         db_path=strncat(db_path, db, strlen(db));  
                 }                  }
   
                 //services._throw(db_path);                  //services._throw(db_path);
Line 124  public: Line 124  public:
                         if(char* key=lsplit(&options, '&')){                          if(char* key=lsplit(&options, '&')){
                                 if(*key) {                                  if(*key) {
                                         if(char* value=lsplit(key, '=')){                                          if(char* value=lsplit(key, '=')){
                                                 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, "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.                                                   } else if(strcmp(key, "ClientCharset")==0){
                                                                                                 // 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));                                                          toupper_str(value, value, strlen(value));
                                                         connection.client_charset=value;                                                          connection.client_charset=value;
                                                         continue;                                                          continue;
Line 193  public: Line 190  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 226  public: Line 233  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 234  public: Line 243  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 267  public: Line 276  public:
                         rc=sqlite3_prepare(connection.handle, statement, -1, &SQL, &pzTail);                          rc=sqlite3_prepare(connection.handle, statement, -1, &SQL, &pzTail);
                         next_statement_length=strlen(pzTail);                          next_statement_length=strlen(pzTail);
                         if(rc!=SQLITE_OK){                          if(rc!=SQLITE_OK){
                                 sqlite3_free((char*)pzTail);                                  //sqlite3_free((char*)pzTail);
                                 _throw(connection, sqlite3_errmsg(connection.handle));                                  _throw(connection, sqlite3_errmsg(connection.handle));
                         }                          }
                         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 in this connection");                                  _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; }
Line 282  public: Line 291  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 293  public: Line 308  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 312  public: Line 327  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 324  public: Line 351  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 339  public: Line 362  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 352  public: Line 375  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);
   

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


E-mail: