Diff for /sql/pgsql/parser3pgsql.C between versions 1.1 and 1.13

version 1.1, 2001/09/21 15:44:37 version 1.13, 2003/07/24 10:09:40
Line 1 Line 1
 /** @file  /** @file
         Parser PgSQL driver.          Parser PgSQL driver.
   
         Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)          Copyright(c) 2001, 2003 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)
   
         2001.07.30 using PgSQL 7.1.2          2001.07.30 using PgSQL 7.1.2
 */  */
Line 51  static char *lsplit(char *string, char d Line 51  static char *lsplit(char *string, char d
     return 0;      return 0;
 }  }
   
   static char *lsplit(char **string_ref, char delim) {
       char *result=*string_ref;
           char *next=lsplit(*string_ref, delim);
       *string_ref=next;
       return result;
   }
   
 /**  /**
         PgSQL server driver          PgSQL server driver
 */  */
Line 63  public: Line 70  public:
         /// get api version          /// get api version
         int api_version() { return SQL_DRIVER_API_VERSION; }          int api_version() { return SQL_DRIVER_API_VERSION; }
         /// initialize driver by loading sql dynamic link library          /// initialize driver by loading sql dynamic link library
         const char *initialize(const char *dlopen_file_spec) {          const char *initialize(char *dlopen_file_spec) {
                 return dlopen_file_spec?                  return dlopen_file_spec?
                         dlink(dlopen_file_spec):"client library column is empty";                          dlink(dlopen_file_spec):"client library column is empty";
         }          }
   
         #define throwPQerror services._throw(PQerrorMessage(conn))          #define throwPQerror services._throw(PQerrorMessage(conn))
           #define PQclear_throw(msg) { \
                           PQclear(res); \
                           services._throw(msg); \
                   }                                               
           #define PQclear_throwPQerror PQclear_throw(PQerrorMessage(conn))
   
         /**     connect          /**     connect
                 @param used_only_in_connect_url                  @param used_only_in_connect_url
Line 85  public: Line 97  public:
                 char *pwd=lsplit(user, ':');                  char *pwd=lsplit(user, ':');
                 char *port=lsplit(host, ':');                  char *port=lsplit(host, ':');
   
                   char *options=lsplit(db, '?');
   
                 PGconn *conn=PQsetdbLogin(                  PGconn *conn=PQsetdbLogin(
                         strcasecmp(host, "local")==0?NULL/* local Unix domain socket */:host, port,                           (host&&strcasecmp(host, "local")==0)?NULL/* local Unix domain socket */:host, port, 
                         NULL, NULL, db, user, pwd);                          NULL, NULL, db, user, pwd);
                 if(!conn)                  if(!conn)
                         services._throw("PQsetdbLogin failed");                          services._throw("PQsetdbLogin failed");
                 if(PQstatus(conn)!=CONNECTION_OK)                    if(PQstatus(conn)!=CONNECTION_OK)  
                         throwPQerror;                          throwPQerror;
   
                   char *charset=0;
                   char *datestyle=0;
   
                   while(options) {
                           if(char *key=lsplit(&options, '&')) {
                                   if(*key) {
                                           if(char *value=lsplit(key, '=')) {
                                                   if(strcasecmp(key, "charset")==0) {
                                                           charset=value;
                                                   } else if(strcasecmp(key, "datestyle")==0) {
                                                           datestyle=value;
                                                   } else
                                                           services._throw("unknown connect option" /*key*/);
                                           } else 
                                                   services._throw("connect option without =value" /*key*/);
                                   }
                           }
                   }
   
                   if(charset) {
                           // set CLIENT_ENCODING
                           char statement[MAX_STRING]="set CLIENT_ENCODING="; // win
                           strncat(statement, charset, MAX_STRING);
                           
                           PGresult *res=PQexec(conn, statement);
                           if(!res) 
                                   throwPQerror;
                           PQclear(res); // throw out the result [don't need but must call]
                   }
   
                   if(datestyle) {
                           // set DATESTYLE
                           char statement[MAX_STRING]="set DATESTYLE="; // ISO,SQL,Postgres,European,NonEuropean=US,German,DEFAULT=ISO
                           strncat(statement, charset, MAX_STRING);
                           
                           PGresult *res=PQexec(conn, statement);
                           if(!res) 
                                   throwPQerror;
                           PQclear(res); // throw out the result [don't need but must call]
                   }
   
                 *(PGconn **)connection=conn;                  *(PGconn **)connection=conn;
                 begin_transaction(services, conn);                  begin_transaction(services, conn);
         }          }
Line 120  public: Line 175  public:
                 return PQstatus((PGconn *)connection)==CONNECTION_OK;                  return PQstatus((PGconn *)connection)==CONNECTION_OK;
         }          }
   
         unsigned int quote(          const char* quote(
                 SQL_Driver_services&, void *connection,                  SQL_Driver_services& services, void *connection,
                 char *to, const char *from, unsigned int length) {                  const char *from, unsigned int length) {
                 /*                  char *result=(char*)services.malloc_atomic(length*2+1);
                         it's already UNTAINT_TIMES_BIGGER                  char *to=result;
                 */  
                 unsigned int result=length;  
                 while(length--) {                  while(length--) {
                         switch(*from) {                          switch(*from) {
                         case '\'': // "'" -> "''"                          case '\'': // "'" -> "''"
                                 *to++='\'';                                  *to++='\''; result++;
                                 break;                                  break;
                         case '\\': // "\" -> "\\"                          case '\\': // "\" -> "\\"
                                 *to++='\'';                                  *to++='\\'; result++;
                                 break;                                  break;
                         }                          }
                         *to++=*from++;                          *to++=*from++;
                 }                  }
                   *to=0;
                 return result;                  return result;
         }                  }
         void query(          void query(
                 SQL_Driver_services& services, void *connection,                   SQL_Driver_services& services, void *connection, 
                 const char *astatement, unsigned long offset, unsigned long limit,                  const char *astatement, unsigned long offset, unsigned long limit,
Line 147  public: Line 201  public:
 //              _asm int 3;  //              _asm int 3;
   
                 PGconn *conn=(PGconn *)connection;                  PGconn *conn=(PGconn *)connection;
                 #define PQclear_throw(msg) { \  
                                 PQclear(res); \  
                                 services._throw(msg); \  
                         }                                                 
                 #define PQclear_throwPQerror PQclear_throw(PQerrorMessage(conn))  
   
                 const char *statement=preprocess_statement(services, conn,                  const char *statement=preprocess_statement(services, conn,
                         astatement, offset, limit);                          astatement, offset, limit);
Line 179  public: Line 228  public:
                 if(!column_count)                  if(!column_count)
                         PQclear_throw("result contains no columns");                          PQclear_throw("result contains no columns");
   
                   bool failed=false;
                   SQL_Error sql_error;
   #define CHECK(afailed) \
                   if(afailed) { \
                           failed=true; \
                           goto cleanup; \
                   }
   
                 for(int i=0; i<column_count; i++){                  for(int i=0; i<column_count; i++){
                         char *name=PQfname(res, i);                          char *name=PQfname(res, i);
                         size_t size=strlen(name);                          size_t size=strlen(name);
                         void *ptr=services.malloc(size);                          char* str=(char*)services.malloc(size+1);
                         memcpy(ptr, name, size);                          memcpy(str, name, size+1);
                         handlers.add_column(ptr, size);                          CHECK(handlers.add_column(sql_error, str, size));
                 }                  }
   
                 handlers.before_rows();                  CHECK(handlers.before_rows(sql_error));
   
                 if(unsigned long row_count=(unsigned long)PQntuples(res))                  if(unsigned long row_count=(unsigned long)PQntuples(res))
                         for(unsigned long r=0; r<row_count; r++) {                          for(unsigned long r=0; r<row_count; r++) {
                                 handlers.add_row();                                  CHECK(handlers.add_row(sql_error));
                                 for(int i=0; i<column_count; i++){                                  for(int i=0; i<column_count; i++){
                                         const char *cell=PQgetvalue(res, r, i);                                          const char *cell=PQgetvalue(res, r, i);
                                         size_t size;                                          size_t size;
                                         void *ptr;                                          char* str;
                                         if(PQftype(res, i)==OIDOID) {                                          if(PQftype(res, i)==OIDOID) {
                                                 // ObjectID column, read object bytes                                                  // ObjectID column, read object bytes
   
Line 216  public: Line 273  public:
                                                         size=(size_t)size_tell;                                                          size=(size_t)size_tell;
                                                         if(size) {                                                          if(size) {
                                                                 // read                                                                   // read 
                                                                 ptr=services.malloc(size);                                                                  str=(char*)services.malloc(size+1);
                                                                 if(!lo_read_ex(conn, fd, (const char *)ptr, size_tell))                                                                  if(!lo_read_ex(conn, fd, str, size_tell))
                                                                         PQclear_throw("lo_read can not read all bytes of object");                                                                          PQclear_throw("lo_read can not read all bytes of object");
                                                                   str[size]=0;
                                                         } else                                                          } else
                                                                 ptr=0;                                                                  str=0;
                                                         if(lo_close(conn, fd)<0)                                                          if(lo_close(conn, fd)<0)
                                                                 PQclear_throwPQerror;                                                                  PQclear_throwPQerror;
                                                 } else                                                  } else
Line 229  public: Line 287  public:
                                                 // normal column, read it normally                                                  // normal column, read it normally
                                                 size=(size_t)PQgetlength(res, r, i);                                                  size=(size_t)PQgetlength(res, r, i);
                                                 if(size) {                                                  if(size) {
                                                         ptr=services.malloc(size);                                                          str=(char*)services.malloc(size+1);
                                                         memcpy(ptr, cell, size);                                                          memcpy(str, cell, size+1);
                                                 } else                                                  } else
                                                         ptr=0;                                                          str=0;
                                         }                                          }
                                         handlers.add_row_cell(ptr, size);                                          CHECK(handlers.add_row_cell(sql_error, str, size));
                                 }                                  }
                         }                          }
   cleanup:
                 PQclear(res);                  PQclear(res);
                   if(failed)
                           services._throw(sql_error);
         }          }
   
 private: // private funcs  private: // private funcs
Line 388  private: // conn client library funcs Line 448  private: // conn client library funcs
 private: // conn client library funcs linking  private: // conn client library funcs linking
   
         const char *dlink(const char *dlopen_file_spec) {          const char *dlink(const char *dlopen_file_spec) {
                   if(lt_dlinit())
                           return lt_dlerror();
         lt_dlhandle handle=lt_dlopen(dlopen_file_spec);          lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
         if(!handle)          if(!handle)
                         return "can not open the dynamic link module";                          return "can not open the dynamic link module";
Line 425  private: // conn client library funcs li Line 487  private: // conn client library funcs li
   
 extern "C" SQL_Driver *SQL_DRIVER_CREATE() {  extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
         return new PgSQL_Driver();          return new PgSQL_Driver();
 }  
   
   }

Removed from v.1.1  
changed lines
  Added in v.1.13


E-mail: