Diff for /sql/odbc/parser3odbc.C between versions 1.2 and 1.8

version 1.2, 2001/10/29 08:36:51 version 1.8, 2002/05/15 10:21:46
Line 1 Line 1
 /** @file  /** @file
         Parser ODBC driver.          Parser ODBC driver.
   
         Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)          Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
   
         Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)          Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
 */  */
 static const char *RCSId="$Id$";   static const char *RCSId="$Id$"; 
   
Line 21  static const char *RCSId="$Id$"; Line 21  static const char *RCSId="$Id$";
 #include <AFXDB.H>  #include <AFXDB.H>
   
 #define MAX_STRING 0x400  #define MAX_STRING 0x400
   #define MAX_NUMBER 40
   
   // MSSQL2000
   #ifndef SQL_NVARCHAR
   #define SQL_NVARCHAR (-9)
   #endif
   #ifndef SQL_NTEXT 
   #define SQL_NTEXT (-10)
   #endif
   #ifndef SQL_SMALLDATETIME
   #define SQL_SMALLDATETIME 11
   #endif
   // create table test (id int, a smalldatetime, b ntext, c nvarchar(100))
   
 #define snprintf _snprintf  #define snprintf _snprintf
 #ifndef strncasecmp  #ifndef strncasecmp
Line 49  public: Line 62  public:
   
         /// get api version          /// get api version
         int api_version() { return SQL_DRIVER_API_VERSION; }          int api_version() { return SQL_DRIVER_API_VERSION; }
         const char *initialize(const char *dlopen_file_spec) { return 0; }          const char *initialize(char *dlopen_file_spec) { return 0; }
         /**     connect          /**     connect
                 @param used_only_in_connect_url                  @param used_only_in_connect_url
                         format: @b DSN=dsn;UID=user;PWD=password (ODBC connect string)                          format: @b DSN=dsn;UID=user;PWD=password (ODBC connect string)
Line 112  public: Line 125  public:
         unsigned int quote(          unsigned int quote(
                 SQL_Driver_services&, void *connection,                  SQL_Driver_services&, void *connection,
                 char *to, const char *from, unsigned int length) {                  char *to, const char *from, unsigned int length) {
                 /*                  if(to) { // store mode
                         You must allocate the to buffer to be at least length*2+1 bytes long.                           unsigned int result=length;
                         (In the worse case, each character may need to be encoded as using two bytes,                           while(length--) {
                         and you need room for the terminating null byte.)                                  if(*from=='\'') { // ' -> ''
                                           *to++='\''; result++;
                         it's already UNTAINT_TIMES_BIGGER                                  }
                 */                                  *to++=*from++;
                 // ' -> ''                          }
                 unsigned int result=length;                          return result;
                 while(length--) {                  } else // estimate mode
                         if(*from=='\'')                          return length*2;
                                 *to++='\'';  
                         *to++=*from++;  
                 }  
                 return result;  
         }          }
         void query(          void query(
                 SQL_Driver_services& services, void *connection,                   SQL_Driver_services& services, void *connection, 
Line 139  public: Line 148  public:
                         statement++;                          statement++;
                                   
                 TRY {                  TRY {
                         if(strncasecmp(statement, "select", 6)==0) {                          // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\adosql.chm::/adoprg02_4g33.htm
                           // Server cursors are created only for statements that begin with: 
                           // SELECT
                           // EXEC[ute] procedure_name
                           // call procedure_name
                           // 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:
                           // {[?=]call procedure_name[([parameter][,[parameter]]...)]}
                           if(strncasecmp(statement, "select", 6)==0
                                   || strncasecmp(statement, "EXEC", 4)==0
                                   || strncasecmp(statement, "call", 4)==0
                                   || strncasecmp(statement, "{", 1)==0) {
                                 CRecordset rs(db);                                   CRecordset rs(db); 
                                 rs.Open(                                  rs.Open(
                                         CRecordset::forwardOnly,                                           CRecordset::forwardOnly, 
Line 171  public: Line 191  public:
                                         if(row>=offset) {                                          if(row>=offset) {
                                                 handlers.add_row();                                                  handlers.add_row();
                                                 for(int i=0; i<column_count; i++)                                               {                                                  for(int i=0; i<column_count; i++)                                               {
                                                         CString string;                                                          CODBCFieldInfo fieldinfo;
                                                         rs.GetFieldValue(i, string);                                                          rs.GetODBCFieldInfo(i, fieldinfo);
                                                         size_t size=string.GetLength();                                                          size_t size;
                                                         void *ptr=0;                                                          void *ptr;
                                                         if(size) {                                                          switch(fieldinfo.m_nSQLType) {
                                                                 ptr=services.malloc(size);                                                          //case xBOOL:
                                                                 memcpy(ptr, (char *)LPCTSTR(string), size);  //                                                      case SQL_INTEGER: // serg@design.ru did that in parser2. test first!
                                                           //case SQL_DATETIME:
                                                           case SQL_SMALLDATETIME:
                                                                   {
                                                                           CDBVariant v;
                                                                           rs.GetFieldValue(i, v);
                                                                           getFromDBVariant(services, v, ptr, size);
                                                                           break;
                                                                   }
                                                           default:
                                                                   {
                                                                           CString s;
                                                                           rs.GetFieldValue(i, s);
                                                                           getFromString(services, s, ptr, size);
                                                                           break;
                                                                   }
                                                         }                                                          }
                                                         handlers.add_row_cell(ptr, size);                                                          handlers.add_row_cell(ptr, size);
                                                 }                                                  }
Line 196  public: Line 231  public:
                 END_CATCH_ALL                  END_CATCH_ALL
         }          }
   
           void getFromDBVariant(SQL_Driver_services& services, CDBVariant& v, void *& ptr, size_t& size) {
                   switch(v.m_dwType) {
                   case DBVT_NULL: // No union member is valid for access. 
                           ptr=0;
                           size=0;
                           break;
   /*              case DBVT_BOOL:
                           ptr=v.m_boolVal?"1":"0";
                           size=1;
                           break;*/
   /*                                                      case DBVT_UCHAR:
                           size=strlen(ptr=v.m_chVal);
                           break;
                   case DBVT_SHORT:
                           char buf[MAX_NUMBER];
                           size=snprintf(HEAPIZE buf, "%d", v.m_iVal);
                           break;*/
   /*              case DBVT_LONG: 
                           {
                                   char local_buf[MAX_NUMBER];
                                   size=snprintf(local_buf, MAX_NUMBER, "%ld", v.m_lVal);
                                   ptr=services.malloc(size);
                                   memcpy(ptr, local_buf, size);
                                   break;
                           }*/
                   /*case DBVT_SINGLE:
                           m_fltVal 
                           break;
           case DBVT_DOUBLE m_dblVal 
           case DBVT_STRING m_pstring 
           case DBVT_BINARY m_pbinary */
                   case DBVT_DATE:
                           {
                                   char local_buf[MAX_STRING];
                                   size=snprintf(local_buf, MAX_STRING, 
                                           "%04d-%02d-%02d %02d:%02d:%02d.%03d",
                                           v.m_pdate->year, 
                                           v.m_pdate->month,
                                           v.m_pdate->day,
                                           v.m_pdate->hour,
                                           v.m_pdate->minute,
                                           v.m_pdate->second,
                                           v.m_pdate->fraction);
                                   ptr=services.malloc(size);
                                   memcpy(ptr, local_buf, size);
                                   break;
                           }
                   default:
                           char msg[MAX_STRING];
                           snprintf(msg, MAX_STRING, "unknown column return variant type (%d)",
                                   v.m_dwType);
                           services._throw(msg);
                   }
           }
   
           void getFromString(SQL_Driver_services& services, CString& s, void *& ptr, size_t& size) {
                   if(s.IsEmpty()) {
                           ptr=0;
                           size=0;
                   } else {
                           const char *cstr=LPCTSTR(s);
                           size=strlen(cstr); //string.GetLength() works wrong with non-string types: 
                           ptr=services.malloc(size);
                           memcpy(ptr, cstr, size);
                   }
           }
   
         void _throw(SQL_Driver_services& services, CException *e) {          void _throw(SQL_Driver_services& services, CException *e) {
                 char szCause[MAX_STRING]; szCause[0]=0;                  char szCause[MAX_STRING]; szCause[0]=0;
                 e->GetErrorMessage(szCause, MAX_STRING);                  e->GetErrorMessage(szCause, MAX_STRING);

Removed from v.1.2  
changed lines
  Added in v.1.8


E-mail: