Diff for /sql/odbc/parser3odbc.C between versions 1.30 and 1.35

version 1.30, 2008/07/01 08:51:16 version 1.35, 2008/07/08 09:21:08
Line 70  static void toupper_str(char *out, const Line 70  static void toupper_str(char *out, const
                 *out++=(char)toupper(*in++);                  *out++=(char)toupper(*in++);
 }  }
   
   struct modified_statement {
           const char* statement;
           bool limit;
           bool offset;
   };
   
   // todo: MySQL, SQLite, PgSQL (add LIMIT at the end of statement)
   struct SQL {
           enum SQLEnum {
                   Undefined,
                   MSSQL,
                   Pervasive,
                   FireBird
           };
   };
   
 struct Connection {  struct Connection {
         SQL_Driver_services* services;          SQL_Driver_services* services;
   
         CDatabase* db;          CDatabase* db;
         const char* client_charset;          const char* client_charset;
           SQL::SQLEnum sql_specific;
         bool autocommit;          bool autocommit;
         bool fast_offset_search;  
 };  };
   
 /**  /**
Line 110  public: Line 126  public:
                 *connection_ref=&connection;                  *connection_ref=&connection;
                 connection.services=&services;                  connection.services=&services;
                 connection.client_charset=0;                  connection.client_charset=0;
                 connection.fast_offset_search=false;                  connection.sql_specific=SQL::Undefined;
                 connection.autocommit=true;                  connection.autocommit=true;
   
                 size_t url_length=strlen(url);                  size_t url_length=strlen(url);
                 char *options=lsplit(url, '?');                  char *options=lsplit(url, '?');
   
                   // todo: analize connect string and if 'SQL Server' found, modify query and add TOP into SELECTs
   
                 while(options){                  while(options){
                         if(char *key=lsplit(&options, '&')){                          if(char *key=lsplit(&options, '&')){
                                 if(*key){                                  if(*key){
Line 126  public: Line 144  public:
                                                 } 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;
                                                 } else if(strcmp(key, "FastOffsetSearch")==0){                                                  } else if(strcmp(key, "SQL")==0){
                                                         if(atoi(value)==1)                                                          if(strcasecmp(value, "MSSQL")==0){
                                                                 connection.fast_offset_search=true;                                                                  connection.sql_specific=SQL::MSSQL;
                                                           } else if(strcasecmp(value, "Pervasive")==0){
                                                                   connection.sql_specific=SQL::Pervasive;
                                                           } else if(strcasecmp(value, "FireBird")==0){
                                                                   connection.sql_specific=SQL::FireBird;
                                                           } else {
                                                                   services._throw("unknown value of SQL option was specified" /*key*/);
                                                           }
                                                 } else                                                  } else
                                                         services._throw("unknown connect option" /*key*/);                                                          services._throw("unknown connect option" /*key*/);
                                         } else                                           } else 
Line 140  public: Line 165  public:
                 TRY {                  TRY {
                         connection.db=new CDatabase();                          connection.db=new CDatabase();
                         connection.db->OpenEx(url, CDatabase::noOdbcDialog);                          connection.db->OpenEx(url, CDatabase::noOdbcDialog);
                           connection.db->BeginTrans();
                         if(!connection.autocommit)  
                                 connection.db->BeginTrans();  
                 }                   } 
                 CATCH_ALL (e) {                  CATCH_ALL (e) {
                         _throw(services, e);                          _throw(services, e);
Line 163  public: Line 186  public:
   
         void commit(void *aconnection){          void commit(void *aconnection){
                 Connection& connection=*static_cast<Connection*>(aconnection);                  Connection& connection=*static_cast<Connection*>(aconnection);
                 if(!connection.autocommit){                  TRY
                         TRY                          connection.db->CommitTrans();
                                 connection.db->CommitTrans();                          connection.db->BeginTrans();
                                 connection.db->BeginTrans();                  CATCH_ALL (e) {
                         CATCH_ALL (e) {                          _throw(*connection.services, e);
                                 _throw(*connection.services, e);  
                         }  
                         END_CATCH_ALL  
                 }                  }
                   END_CATCH_ALL
         }          }
   
         void rollback(void *aconnection){          void rollback(void *aconnection){
                 Connection& connection=*static_cast<Connection*>(aconnection);                  Connection& connection=*static_cast<Connection*>(aconnection);
                 if(!connection.autocommit){                  TRY
                         TRY                          connection.db->Rollback();
                                 connection.db->Rollback();                          connection.db->BeginTrans();
                                 connection.db->BeginTrans();                  CATCH_ALL (e) {
                         CATCH_ALL (e) {                          _throw(*connection.services, e);
                                 _throw(*connection.services, e);  
                         }  
                         END_CATCH_ALL  
                 }                  }
                   END_CATCH_ALL
         }          }
   
         bool ping(void *connection){          bool ping(void *connection){
Line 206  public: Line 225  public:
         }          }
   
         void query(void *aconnection,           void query(void *aconnection, 
                         const char *statement,                           const char *astatement, 
                         size_t placeholders_count,                          size_t placeholders_count,
                         Placeholder* placeholders,                           Placeholder* placeholders, 
                         unsigned long offset,                          unsigned long offset,
Line 220  public: Line 239  public:
                 if(placeholders_count>0)                  if(placeholders_count>0)
                         services._throw("bind variables not supported (yet)");                          services._throw("bind variables not supported (yet)");
   
                   while(isspace((unsigned char)*astatement)) 
                           astatement++;
   
                   modified_statement mstatement=_preprocess_statement(connection, astatement, offset, limit);
                   const char* statement=mstatement.statement;
   
                 const char* client_charset=connection.client_charset;                  const char* client_charset=connection.client_charset;
                 const char* request_charset=services.request_charset();                  const char* request_charset=services.request_charset();
                 bool transcode_needed=(client_charset && strcmp(client_charset, request_charset)!=0);                  bool transcode_needed=(client_charset && strcmp(client_charset, request_charset)!=0);
   
                 // transcode query from $request:charset to ?ClientCharset  
                 if(transcode_needed){                  if(transcode_needed){
                           // transcode query from $request:charset to ?ClientCharset
                         size_t length=strlen(statement);                          size_t length=strlen(statement);
                         services.transcode(statement, length,                          services.transcode(statement, length,
                                 statement, length,                                  statement, length,
Line 233  public: Line 257  public:
                                 client_charset);                                  client_charset);
                 }                  }
   
                 while(isspace((unsigned char)*statement))   
                         statement++;  
   
                 TRY {                  TRY {
                         // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\adosql.chm::/adoprg02_4g33.htm                          // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\adosql.chm::/adoprg02_4g33.htm
                         // or http://msdn.microsoft.com/en-us/library/aa905899(SQL.80).aspx                          // or http://msdn.microsoft.com/en-us/library/aa905899(SQL.80).aspx
Line 246  public: Line 267  public:
                         // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\odbcsql.chm::/od_6_035_5dnp.htm                          // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\odbcsql.chm::/od_6_035_5dnp.htm
                         // The ODBC CALL escape sequence for calling a procedure is:                          // The ODBC CALL escape sequence for calling a procedure is:
                         // {[?=]call procedure_name[([parameter][,[parameter]]...)]}                          // {[?=]call procedure_name[([parameter][,[parameter]]...)]}
                         if(strncasecmp(statement, "SELECT", 6)==0                          if(
                                   strncasecmp(statement, "SELECT", 6)==0
                                 || strncasecmp(statement, "EXEC", 4)==0                                  || strncasecmp(statement, "EXEC", 4)==0
                                 || strncasecmp(statement, "call", 4)==0                                  || strncasecmp(statement, "call", 4)==0
                                 || strncasecmp(statement, "{", 1)==0                                  || strncasecmp(statement, "{", 1)==0
                         ){                          ){
                                 CRecordset rs(db);                                  CRecordset rs(db);
                                 DWORD options=CRecordset::executeDirect|CRecordset::readOnly;                                  DWORD options=CRecordset::executeDirect|CRecordset::readOnly;
                                 //CRecordset::skipDeletedRecords  
                                 //CRecordset::useMultiRowFetch  
                                 //CRecordset::userAllocMultiRowBuffers  
                                 //CRecordset::useExtendedFetch  
 /*  
                                 if(connection.fast_offset_search){  
                                         options+=CRecordset::useMultiRowFetch+CRecordset::userAllocMultiRowBuffers;  
                                 } else {  
                                         options+=CRecordset::skipDeletedRecords;  
                                 }  
 */  
                                 TRY {                                  TRY {
                                         rs.Open(                                          rs.Open(
                                                 (connection.fast_offset_search)?CRecordset::dynamic:CRecordset::forwardOnly,                                                  CRecordset::forwardOnly,
                                                 statement,                                                  statement,
                                                 options                                                  options
                                         );                                          );
Line 317  public: Line 328  public:
                                                         break;                                                          break;
                                                 default:                                                  default:
                                                         transcode_column[i]=transcode_needed;                                                          transcode_column[i]=transcode_needed;
                                                           break;
                                         }                                          }
                                         size_t length=fieldinfo.m_strName.GetLength();                                          size_t length=fieldinfo.m_strName.GetLength();
                                         char *str=0;                                          char *str=0;
Line 339  public: Line 350  public:
                                 CHECK(handlers.before_rows(sql_error));                                  CHECK(handlers.before_rows(sql_error));
                                                                   
                                 // skip offset rows                                  // skip offset rows
                                 if(offset){                                  if(offset && !mstatement.offset){
                                         if(connection.fast_offset_search){                                          unsigned long row=offset;
                                                 rs.Move(offset);                                          while(!rs.IsEOF() && row--)
                                         } else {                                                  rs.MoveNext();
                                                 unsigned long row=offset;  
                                                 while(!rs.IsEOF() && row>0){  
                                                         rs.MoveNext();  
                                                         row--;  
                                                 }  
                                         }  
                                 }                                  }
   
                                 unsigned long row=0;                                  unsigned long row=0;
Line 398  public: Line 403  public:
                 } CATCH_ALL (e) {                  } CATCH_ALL (e) {
                         _throw(services, e);                          _throw(services, e);
                 } END_CATCH_ALL                  } END_CATCH_ALL
   
                   if(connection.autocommit)
                           commit(aconnection);
         }          }
   
 private:  private:
Line 422  private: Line 430  private:
                         ptr=v.m_boolVal?"1":"0";                          ptr=v.m_boolVal?"1":"0";
                         length=1;                          length=1;
                         break;*/                          break;*/
 /*                                                      case DBVT_UCHAR:  /*              case DBVT_UCHAR:
                         length=strlen(ptr=v.m_chVal);                          length=strlen(ptr=v.m_chVal);
                         break;                          break;
                 case DBVT_SHORT:                  case DBVT_SHORT:
Line 482  private: Line 490  private:
                 }                  }
         }          }
   
           modified_statement _preprocess_statement(
                           Connection& connection, 
                           const char* astatement,
                           unsigned long offset,
                           unsigned long limit
           ){
                   modified_statement result={astatement, false, false};
   
                   if(limit!=SQL_NO_LIMIT && connection.sql_specific!=SQL::Undefined && strncasecmp(astatement, "select", 6)==0){
                           switch(connection.sql_specific){
                                   case SQL::MSSQL:
                                   case SQL::Pervasive: // uses TOP as well
                                           {
                                                   // add ' TOP limit+offset' after 'SELECT'
                                                   char* statement_limited=(char *)connection.services->malloc_atomic(
                                                                   strlen(astatement)
                                                                   +MAX_NUMBER
                                                                   +5/* TOP */
                                                                   +1/*terminator*/
                                                           );
   
                                                   result.limit=true;
                                                   if(!limit)
                                                           result.offset=true; // mark as we'll did it in query because of no point to skip anything if we need 0 rows
                                                   result.statement=statement_limited;
   
                                                   snprintf(statement_limited, MAX_NUMBER+11, "SELECT TOP %u", (limit)?limit+offset:0/*no point to skip anything if we need 0 rows*/);
   
                                                   astatement+=6;/*skip 'select'*/
                                                   strcat(statement_limited, astatement);
   
                                                   //connection.services->_throw(result.statement);
                                                   break;
                                           }
                                   case SQL::FireBird:
                                           {
                                                   // add ' FIRST (limit) SKIP (offset)' after 'SELECT'
                                                   char* statement_limited=(char *)connection.services->malloc_atomic(
                                                                   strlen(astatement)
                                                                   +MAX_NUMBER*2
                                                                   +9/* FIRST ()*/
                                                                   +offset?8:0/* SKIP ()*/
                                                                   +1/*terminator*/
                                                           );
   
                                                   result.limit=true;
                                                   result.offset=true;
                                                   result.statement=statement_limited;
   
                                                   statement_limited+=snprintf(statement_limited, MAX_NUMBER+15, "SELECT FIRST (%u)", limit);
                                                   if(offset && limit/*no reasons to skip something if we need 0 rows*/)
                                                           statement_limited+=snprintf(statement_limited, MAX_NUMBER+8, " SKIP (%u)", offset);
   
                                                   astatement+=6;/*skip 'select'*/
                                                   strcat((char*)result.statement, astatement);
   
                                                   //connection.services->_throw(result.statement);
                                                   break;
                                           }
                                   default:
                                           connection.services->_throw("Unknown SQL specifics");
                           }
                   }
                   return result;
           }
   
         void _throw(SQL_Driver_services& services, CException *e){          void _throw(SQL_Driver_services& services, CException *e){
                 char szCause[MAX_STRING];                  char szCause[MAX_STRING];
                 szCause[0]=0;                  szCause[0]=0;

Removed from v.1.30  
changed lines
  Added in v.1.35


E-mail: