Diff for /sql/pgsql/parser3pgsql.C between versions 1.30 and 1.32

version 1.30, 2008/06/26 15:49:40 version 1.32, 2008/12/18 01:45:03
Line 1 Line 1
 /** @file  /** @file
         Parser PgSQL driver.          Parser PgSQL driver.
   
         Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)          Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
   
         Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)          Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
   
         2007.10.25 using PgSQL 8.1.5          2007.10.25 using PgSQL 8.1.5
 */  */
 static const char *RCSId="$Id$";   static const char *RCSId="$Id$";
   
 #include "config_includes.h"  #include "config_includes.h"
   
 #include "pa_sql_driver.h"  #include "pa_sql_driver.h"
   
 #include <libpq-fe.h>  #include <libpq-fe.h>
 #include <libpq/libpq-fs.h>  #include <libpq/libpq-fs.h>
   
 // OIDOID from catalog/pg_type.h  // from catalog/pg_type.h
 #define OIDOID                  26  #define BOOLOID                 16
 // LO_BUFSIZE from interfaces\libpq\fe-lobj.c = 8192 (0x2000)  #define INT8OID                 20
 // actually writing chunks of that size failed, reduced it twice  #define INT2OID                 21
 #define LO_BUFSIZE                0x1000  #define INT4OID                 23
 // from postgres_ext.h  #define OIDOID                  26
 //#define InvalidOid            ((Oid) 0)  #define FLOAT4OID               700
   #define FLOAT8OID               701
   #define DATEOID                 1082
 #include "ltdl.h"  #define TIMEOID                 1083
   #define TIMESTAMPOID    1114
 #define MAX_STRING 0x400  #define TIMESTAMPTZOID  1184
 #define MAX_NUMBER 20  #define TIMETZOID               1266
   #define NUMERICOID              1700
 #if _MSC_VER  
 #       define snprintf _snprintf  // LO_BUFSIZE from interfaces\libpq\fe-lobj.c = 8192 (0x2000)
 #       define strcasecmp _stricmp  // actually writing chunks of that size failed, reduced it twice
 #endif  #define LO_BUFSIZE                0x1000
   
 #ifndef max  
 inline int max(int a,int b){ return a>b?a:b; }  #include "ltdl.h"
 inline int min(int a,int b){ return a<b?a:b; }  
 #endif  #define MAX_COLS 500
   
 static char *lsplit(char *string, char delim){  #define MAX_STRING 0x400
         if(string){  #define MAX_NUMBER 20
                 if(char *v=strchr(string, delim)){  
                         *v=0;  #if _MSC_VER
                         return v+1;  #       define snprintf _snprintf
                 }  #       define strcasecmp _stricmp
         }  #endif
         return 0;  
 }  #ifndef max
   inline int max(int a,int b){ return a>b?a:b; }
 static char *lsplit(char **string_ref, char delim){  inline int min(int a,int b){ return a<b?a:b; }
         char *result=*string_ref;  #endif
         char *next=lsplit(*string_ref, delim);  
         *string_ref=next;  static char *lsplit(char *string, char delim){
         return result;          if(string){
 }                  if(char *v=strchr(string, delim)){
                           *v=0;
 static char* rsplit(char* string, char delim){                          return v+1;
         if(string){                  }
                 if(char* v=strrchr(string, delim)){          }
                         *v=0;          return 0;
                         return v+1;  }
                 }  
         }  static char *lsplit(char **string_ref, char delim){
         return NULL;              char *result=*string_ref;
 }          char *next=lsplit(*string_ref, delim);
           *string_ref=next;
 static void toupper_str(char *out, const char *in, size_t size){          return result;
         while(size--)  }
                 *out++=(char)toupper(*in++);  
 }  static char* rsplit(char* string, char delim){
           if(string){
 struct Connection {                  if(char* v=strrchr(string, delim)){
         SQL_Driver_services* services;                          *v=0;
                           return v+1;
         PGconn *conn;                  }
         const char* client_charset;          }
         bool autocommit;          return NULL;    
 };  }
   
 /**  static void toupper_str(char *out, const char *in, size_t size){
         PgSQL server driver          while(size--)
 */                  *out++=(char)toupper(*in++);
 class PgSQL_Driver : public SQL_Driver {  }
 public:  
   struct Connection {
         PgSQL_Driver() : SQL_Driver() {          SQL_Driver_services* services;
         }  
           PGconn *conn;
         /// get api version          const char* client_charset;
         int api_version(){ return SQL_DRIVER_API_VERSION; }          bool autocommit;
           bool without_default_transactions;
         /// initialize driver by loading sql dynamic link library  };
         const char *initialize(char *dlopen_file_spec){  
                 return dlopen_file_spec?  /**
                         dlink(dlopen_file_spec):"client library column is empty";          PgSQL server driver
         }  */
   class PgSQL_Driver : public SQL_Driver {
         #define throwPQerror connection.services->_throw(PQerrorMessage(connection.conn))  public:
         #define PQclear_throw(msg) { \  
                         PQclear(res); \          PgSQL_Driver() : SQL_Driver() {
                         connection.services->_throw(msg); \          }
                 }                                                 
         #define PQclear_throwPQerror PQclear_throw(PQerrorMessage(connection.conn))          /// get api version
           int api_version(){ return SQL_DRIVER_API_VERSION; }
         /**     connect  
                 @param url          /// initialize driver by loading sql dynamic link library
                         format: @b user:pass@host[:port]|[local]/database?          const char *initialize(char *dlopen_file_spec){
                         ClientCharset=charset&  // transcode by parser                  return dlopen_file_spec?
                         charset=value&                  // transcode by server with 'SET CLIENT_ENCODING=value'                          dlink(dlopen_file_spec):"client library column is empty";
                         datestyle=value&                // 'SET DATESTYLE=value' available values are: ISO|SQL|Postgres|European|US|German [default=ISO]          }
                         autocommit=1&  
                         WithoutDefaultTransaction=1     // == autocommit=0          #define throwPQerror connection.services->_throw(PQerrorMessage(connection.conn))
         */          #define PQclear_throw(msg) { \
         void connect(                          PQclear(res); \
                                 char* url,                           connection.services->_throw(msg); \
                                 SQL_Driver_services& services,                   }                                              
                                 void** connection_ref ///< output: Connection*          #define PQclear_throwPQerror PQclear_throw(PQerrorMessage(connection.conn))
         ){  
                 char* user=url;          /**     connect
                 char* host=rsplit(user, '@');                  @param url
                 char* db=lsplit(host, '/');                          format: @b user:pass@host[:port]|[local]/database?
                 char* pwd=lsplit(user, ':');                          ClientCharset=charset&  // transcode by parser
                 char* port=lsplit(host, ':');                          charset=value&                  // transcode by server with 'SET CLIENT_ENCODING=value'
                           datestyle=value&                // 'SET DATESTYLE=value' available values are: ISO|SQL|Postgres|European|US|German [default=ISO]
                 char *options=lsplit(db, '?');                          autocommit=1&                   // each transaction is commited automatically (default)
                           WithoutDefaultTransaction=0     // 1 -- disable auto commit, 'BEGIN TRAN' at connection start and COMMIT/ROLLBACK at the end [can't be used together with autocommit option]
                 char* charset=0;          */
                 char* datestyle=0;          void connect(
                                   char* url,
                 Connection& connection=*(Connection *)services.malloc(sizeof(Connection));                                  SQL_Driver_services& services,
                 *connection_ref=&connection;                                  void** connection_ref ///< output: Connection*
                 connection.services=&services;          ){
                 connection.client_charset=0;                      char* user=url;
                 connection.autocommit=true;                  char* host=rsplit(user, '@');
                 connection.conn=PQsetdbLogin(                  char* db=lsplit(host, '/');
                         (host&&strcasecmp(host, "local")==0)?NULL/* local Unix domain socket */:host, port,                   char* pwd=lsplit(user, ':');
                         NULL, NULL, db, user, pwd);                  char* port=lsplit(host, ':');
   
                 if(!connection.conn)                  char *options=lsplit(db, '?');
                         services._throw("PQsetdbLogin failed");  
                   char* charset=0;
                 if(PQstatus(connection.conn)!=CONNECTION_OK)                  char* datestyle=0;
                         throwPQerror;  
                   Connection& connection=*(Connection *)services.malloc(sizeof(Connection));
                 while(options){                  *connection_ref=&connection;
                         if(char *key=lsplit(&options, '&')){                  connection.services=&services;
                                 if(*key){                  connection.client_charset=0;    
                                         if(char *value=lsplit(key, '=')){                  connection.autocommit=true;
                                                 if(strcmp(key, "ClientCharset")==0){ // transcoding with parser                  connection.without_default_transactions=false;
                                                         toupper_str(value, value, strlen(value));  
                                                         connection.client_charset=value;                  connection.conn=PQsetdbLogin(
                                                 } else if(strcasecmp(key, "charset")==0){ // transcoding with server                          (host&&strcasecmp(host, "local")==0)?NULL/* local Unix domain socket */:host, port,
                                                         charset=value;                          NULL, NULL, db, user, pwd);
                                                 } else if(strcasecmp(key, "datestyle")==0){  
                                                         datestyle=value;                  if(!connection.conn)
                                                 } else if(strcasecmp(key, "autocommit")==0){                          services._throw("PQsetdbLogin failed");
                                                         if(atoi(value)==0)  
                                                                 connection.autocommit=false;                  if(PQstatus(connection.conn)!=CONNECTION_OK)
                                                 } else if(strcmp(key, "WithoutDefaultTransaction")==0){ // backward, use autocommit=0                          throwPQerror;
                                                         if(atoi(value)==1)  
                                                                 connection.autocommit=false;                  while(options){
                                                 } else                          if(char *key=lsplit(&options, '&')){
                                                         services._throw("unknown connect option" /*key*/);                                  if(*key){
                                         } else                                           if(char *value=lsplit(key, '=')){
                                                 services._throw("connect option without =value" /*key*/);                                                  if(strcmp(key, "ClientCharset")==0){
                                 }                                                          toupper_str(value, value, strlen(value));
                         }                                                          connection.client_charset=value;
                 }                                                  } else if(strcasecmp(key, "charset")==0){
                                                           charset=value;
                 if(charset){                                                  } else if(strcasecmp(key, "datestyle")==0){
                         char statement[MAX_STRING]="SET CLIENT_ENCODING=";                                                          datestyle=value;
                         strncat(statement, charset, MAX_STRING);                                                  } else if(strcasecmp(key, "autocommit")==0){
                                                           if(connection.without_default_transactions)
                         _execute_cmd(connection, statement);                                                                  services._throw("options WithoutDefaultTransaction and autocommit can't be used together");
                 }                                                          if(atoi(value)==0)
                                                                   connection.autocommit=false;
                 if(datestyle){                                                  } else if(strcmp(key, "WithoutDefaultTransaction")==0){
                         char statement[MAX_STRING]="SET DATESTYLE=";                                                          if(!connection.autocommit)
                         strncat(statement, datestyle, MAX_STRING);                                                                  services._throw("options WithoutDefaultTransaction and autocommit can't be used together");
                                                           if(atoi(value)==1){
                         _execute_cmd(connection, statement);                                                                  connection.without_default_transactions=true;
                 }                                                                  connection.autocommit=false;
                                                           }
                 _begin_transaction(connection);                                                  } else
         }                                                          services._throw("unknown connect option" /*key*/);
                                           } else
         void disconnect(void *aconnection){                                                  services._throw("connect option without =value" /*key*/);
                 Connection& connection=*static_cast<Connection*>(aconnection);                                  }
                 PQfinish(connection.conn);                          }
                 connection.conn=0;                  }
         }  
                   if(charset){
         void commit(void *aconnection){                          char statement[MAX_STRING]="SET CLIENT_ENCODING=";
                 Connection& connection=*static_cast<Connection*>(aconnection);                          strncat(statement, charset, MAX_STRING);
                 if(connection.autocommit){  
                         _execute_cmd(connection, "COMMIT");                          _execute_cmd(connection, statement);
                 }                  }
                 _begin_transaction(connection);  
         }                  if(datestyle){
                           char statement[MAX_STRING]="SET DATESTYLE=";
         void rollback(void *aconnection){                          strncat(statement, datestyle, MAX_STRING);
                 Connection& connection=*static_cast<Connection*>(aconnection);  
                 if(connection.autocommit){                          _execute_cmd(connection, statement);
                         _execute_cmd(connection, "ROLLBACK");                  }
                 }  
                 _begin_transaction(connection);                  _begin_transaction(connection);
         }          }
   
         bool ping(void *aconnection) {          void disconnect(void *aconnection){
                 Connection& connection=*static_cast<Connection*>(aconnection);                  Connection& connection=*static_cast<Connection*>(aconnection);
                 return PQstatus(connection.conn)==CONNECTION_OK;                  PQfinish(connection.conn);
         }                  connection.conn=0;
           }
         const char* quote(void *aconnection, const char *from, unsigned int length){  
                 Connection& connection=*static_cast<Connection*>(aconnection);          void commit(void *aconnection){
                   Connection& connection=*static_cast<Connection*>(aconnection);
                 char *result=(char*)connection.services->malloc_atomic(length*2+1);                  if(!connection.without_default_transactions){
                 int err=0;                          _execute_cmd(connection, "COMMIT");
                 PQescapeStringConn(connection.conn, result, from, length, &err);                  }
                 return result;                  _begin_transaction(connection);
         }          }
           
         void query(void *aconnection,           void rollback(void *aconnection){
                                 const char *astatement,                   Connection& connection=*static_cast<Connection*>(aconnection);
                                 size_t placeholders_count, Placeholder* placeholders,                   if(!connection.without_default_transactions){
                                 unsigned long offset, unsigned long limit,                          _execute_cmd(connection, "ROLLBACK");
                                 SQL_Driver_query_event_handlers& handlers                  }
         ){                  _begin_transaction(connection);
                 Connection& connection=*static_cast<Connection*>(aconnection);          }
                 const char* client_charset=connection.client_charset;  
                 SQL_Driver_services& services=*connection.services;          bool ping(void *aconnection) {
                 PGconn *conn=connection.conn;                  Connection& connection=*static_cast<Connection*>(aconnection);
                   return PQstatus(connection.conn)==CONNECTION_OK;
                 bool transcode_needed=_transcode_required(connection);          }
   
                 const char** paramValues;          const char* quote(void *aconnection, const char *from, unsigned int length){
                 if(placeholders_count>0){                  Connection& connection=*static_cast<Connection*>(aconnection);
                         int binds_size=sizeof(char)*placeholders_count;  
                         paramValues = static_cast<const char**>(services.malloc_atomic(binds_size));                  char *result=(char*)connection.services->malloc_atomic(length*2+1);
                         _bind_parameters(placeholders_count, placeholders, paramValues, connection, transcode_needed);                  int err=0;
                 }                  PQescapeStringConn(connection.conn, result, from, length, &err);
                   return result;
                 // transcode query from $request:charset to ?ClientCharset          }
                 if(transcode_needed){         
                         size_t length=strlen(astatement);          void query(void *aconnection,
                         services.transcode(astatement, length,                                  const char *astatement,
                                 astatement, length,                                  size_t placeholders_count, Placeholder* placeholders,
                                 services.request_charset(),                                  unsigned long offset, unsigned long limit,
                                 connection.client_charset);                                  SQL_Driver_query_event_handlers& handlers
                 }          ){
                   Connection& connection=*static_cast<Connection*>(aconnection);
                 const char *statement=_preprocess_statement(connection, astatement, offset, limit);                  SQL_Driver_services& services=*connection.services;
                 // error after prepare?                  PGconn *conn=connection.conn;
   
                 PGresult *res;                  const char* client_charset=connection.client_charset;
                 if(placeholders_count>0){                  const char* request_charset=services.request_charset();
                         res=PQexecParams(conn, statement, placeholders_count, NULL, paramValues, NULL, NULL, 0);                  bool transcode_needed=client_charset && strcmp(client_charset, request_charset)!=0;
                 } else {  
                         res=PQexec(conn, statement);                  const char** paramValues;
                 }                  if(placeholders_count>0){
                 if(!res)                           int binds_size=sizeof(char)*placeholders_count;
                         throwPQerror;                          paramValues = static_cast<const char**>(services.malloc_atomic(binds_size));
                           _bind_parameters(placeholders_count, placeholders, paramValues, connection, transcode_needed);
                 switch(PQresultStatus(res)) {                  }
                         case PGRES_EMPTY_QUERY:   
                                 PQclear_throw("no query");                  if(transcode_needed){
                                 break;                          // transcode query from $request:charset to ?ClientCharset
                         case PGRES_COMMAND_OK: // empty result: insert|delete|update|...                          size_t length=strlen(astatement);
                                 PQclear(res);                          services.transcode(astatement, length,
                                 return;                                  astatement, length,
                         case PGRES_TUPLES_OK:                                   request_charset,
                                 break;                                    client_charset);
                         default:                  }
                                 PQclear_throwPQerror;  
                                 break;                  const char *statement=_preprocess_statement(connection, astatement, offset, limit);
                 }                  // error after prepare?
                   
                 int column_count=PQnfields(res);                  PGresult *res;
                 if(!column_count)                  if(placeholders_count>0){
                         PQclear_throw("result contains no columns");                          res=PQexecParams(conn, statement, placeholders_count, NULL, paramValues, NULL, NULL, 0);
                   } else {
                 bool failed=false;                          res=PQexec(conn, statement);
                 SQL_Error sql_error;                  }
 #define CHECK(afailed) \                  if(!res)
                 if(afailed) { \                          throwPQerror;
                         failed=true; \  
                         goto cleanup; \                  switch(PQresultStatus(res)) {
                 }                          case PGRES_EMPTY_QUERY:
                                   PQclear_throw("no query");
                 for(int i=0; i<column_count; i++){                                  break;
                         char *name=PQfname(res, i);                          case PGRES_COMMAND_OK: // empty result: insert|delete|update|...
                         size_t length=strlen(name);                                  PQclear(res);
                         char* strm=(char*)services.malloc(length+1);                                  return;
                         memcpy(strm, name, length+1);                          case PGRES_TUPLES_OK:
                         const char* str=strm;                                  break;  
                           default:
                         // transcode column name from ?ClientCharset to $request:charset                                  PQclear_throwPQerror;
                         if(transcode_needed)                                   break;
                                 services.transcode(str, length,                  }
                                         str, length,                 
                                         connection.client_charset,                  int column_count=PQnfields(res);
                                         services.request_charset());                  if(!column_count)
                           PQclear_throw("result contains no columns");
                         CHECK(handlers.add_column(sql_error, str, length));  
                 }                  bool failed=false;
                   SQL_Error sql_error;
                 CHECK(handlers.before_rows(sql_error));  #define CHECK(afailed) \
                   if(afailed) { \
                 if(unsigned long row_count=(unsigned long)PQntuples(res))                          failed=true; \
                         for(unsigned long r=0; r<row_count; r++) {                          goto cleanup; \
                                 CHECK(handlers.add_row(sql_error));                  }
                                 for(int i=0; i<column_count; i++){  
                                         const char *cell=PQgetvalue(res, r, i);                  if(column_count>MAX_COLS)
                                         size_t length;                          column_count=MAX_COLS;
                                         const char* str;  
                                         if(PQftype(res, i)==OIDOID) {                  unsigned int column_types[MAX_COLS];
                                                 // ObjectID column, read object bytes                  bool transcode_column[MAX_COLS];
   
                                                 char *error_pos=0;                  for(int i=0; i<column_count; i++){
                                                 Oid oid=cell?atoi(cell):0;                          char *name=PQfname(res, i);
                                                 int fd=lo_open(conn, oid, INV_READ);                          size_t length=strlen(name);
                                                 if(fd>=0) {                          column_types[i]=PQftype(res, i);
                                                         // seek to end                          switch(column_types[i]){
                                                         if(lo_lseek(conn, fd, 0, SEEK_END)<0)                                  case BOOLOID:
                                                                 PQclear_throwPQerror;                                  case INT8OID:
                                                         // get length                                  case INT2OID:
                                                         int size_tell=lo_tell(conn, fd);                                  case INT4OID:
                                                         if(size_tell<0)                                  case FLOAT4OID:
                                                                 PQclear_throwPQerror;                                  case FLOAT8OID:
                                                         // seek to begin                                  case DATEOID:
                                                         if(lo_lseek(conn, fd, 0, SEEK_SET)<0)                                  case TIMEOID:
                                                                 PQclear_throwPQerror;                                  case TIMESTAMPOID:
                                                         length=(size_t)size_tell;                                  case TIMESTAMPTZOID:
                                                         if(length) {                                  case TIMETZOID:
                                                                 // read                                   case NUMERICOID:
                                                                 char* strm=(char*)services.malloc(length+1);                                          transcode_column[i]=false;
                                                                 if(!lo_read_ex(conn, fd, strm, size_tell))                                          break;
                                                                         PQclear_throw("lo_read can not read all bytes of object");                                  default:
                                                                 strm[length]=0;                                          transcode_column[i]=transcode_needed;
                                                                 str=strm;                                          break;
                                                         } else                          }
                                                                 str=0;                          char* strm=(char*)services.malloc(length+1);
                                                         if(lo_close(conn, fd)<0)                          memcpy(strm, name, length+1);
                                                                 PQclear_throwPQerror;                          const char* str=strm;
                                                 } else  
                                                         PQclear_throwPQerror;                          if(transcode_needed)
                                         } else {                                  // transcode column name from ?ClientCharset to $request:charset
                                                 // normal column, read it normally                                  services.transcode(str, length,
                                                 length=(size_t)PQgetlength(res, r, i);                                          str, length,
                                                 if(length) {                                          client_charset,
                                                         char* strm=(char*)services.malloc(length+1);                                          request_charset);
                                                         memcpy(strm, cell, length+1);  
                                                         str=strm;                          CHECK(handlers.add_column(sql_error, str, length));
                                                 } else                  }
                                                         str=0;  
                                         }                  CHECK(handlers.before_rows(sql_error));
   
                                         if(transcode_needed && str && length){                  if(unsigned long row_count=(unsigned long)PQntuples(res))
                                                 // transcode cell value from ?ClientCharset to $request:charset                          for(unsigned long r=0; r<row_count; r++) {
                                                 services.transcode(str, length,                                  CHECK(handlers.add_row(sql_error));
                                                         str, length,                                  for(int i=0; i<column_count; i++){
                                                         connection.client_charset,                                          const char *cell=PQgetvalue(res, r, i);
                                                         services.request_charset());                                          size_t length;
                                         }                                          const char* str;
   
                                         CHECK(handlers.add_row_cell(sql_error, str, length));                                          switch(column_types[i]){
                                 }                                                  case OIDOID:
                         }                                                          {
 cleanup:                                                                  char *error_pos=0;
                 PQclear(res);                                                                  Oid oid=cell?atoi(cell):0;
                 if(failed)                                                                  int fd=lo_open(conn, oid, INV_READ);
                         services._throw(sql_error);                                                                  if(fd>=0){
         }                                                                          // seek to end
                                                                           if(lo_lseek(conn, fd, 0, SEEK_END)<0)
 private:                                                                                  PQclear_throwPQerror;
         void _bind_parameters(                                                                          // get length
                                 size_t placeholders_count,                                                                           int size_tell=lo_tell(conn, fd);
                                 Placeholder* placeholders,                                                                           if(size_tell<0)
                                 const char** paramValues,                                                                                  PQclear_throwPQerror;
                                 Connection& connection,                                                                          // seek to begin
                                 bool transcode_needed                                                                          if(lo_lseek(conn, fd, 0, SEEK_SET)<0)
         ){                                                                                  PQclear_throwPQerror;
                 for(size_t i=0; i<placeholders_count; i++){                                                                          length=(size_t)size_tell;
                         Placeholder& ph=placeholders[i];                                                                          if(length){
                         if(transcode_needed){                                                                                  // read
                                 size_t name_length;                                                                                  char* strm=(char*)services.malloc(length+1);
                                 size_t value_length;                                                                                  if(!lo_read_ex(conn, fd, strm, size_tell))
                                 connection.services->transcode(ph.name, strlen(ph.name),                                                                                          PQclear_throw("lo_read can not read all bytes of object");
                                         ph.name, name_length,                                                                                  strm[length]=0;
                                         connection.services->request_charset(),                                                                                  str=strm;
                                         connection.client_charset);                                                                          } else
                                                                                   str=0;
                                 if(ph.value) {                                                                          if(lo_close(conn, fd)<0)
                                         connection.services->transcode(ph.value, strlen(ph.value),                                                                                  PQclear_throwPQerror;
                                                 ph.value, value_length,                                                                  } else
                                                 connection.services->request_charset(),                                                                          PQclear_throwPQerror;
                                                 connection.client_charset);                                                          }
                                 }                                                  default:
                         }                                                          // normal column, read it normally
                         int name_numner=atoi(ph.name);                                                          length=(size_t)PQgetlength(res, r, i);
                         if(name_numner <= 0 || name_numner > placeholders_count)                                                          if(length){
                                 connection.services->_throw("bad bind parameter key");                                                                  char* strm=(char*)services.malloc(length+1);
                                                                   memcpy(strm, cell, length+1);
                         paramValues[name_numner-1]=ph.value;                                                                  str=strm;
                 }                                                          } else
         }                                                                  str=0;
                                                   }
           
         /**                                          if(str && length && transcode_column[i]){
                 Executes a query and throw away the result.                                                  //services._throw("tr");
         */                                                  // transcode cell value from ?ClientCharset to $request:charset
         void _execute_cmd(const Connection& connection, const char *query){                                                  services.transcode(str, length,
                 if(PGresult *res=PQexec(connection.conn, query))                                                          str, length,
                         PQclear(res); // throw out the result [don't need but must call]                                                          client_charset,
                 else                                                          request_charset);
                         throwPQerror;                                          }
         }  
                                           CHECK(handlers.add_row_cell(sql_error, str, length));
         void _begin_transaction(Connection& connection){                                  }
                 if(connection.autocommit)                          }
                         _execute_cmd(connection, "BEGIN");  cleanup:
         }                  PQclear(res);
                   if(failed)
         const char *_preprocess_statement(                          services._throw(sql_error);
                                         Connection& connection,  
                                         const char *astatement,                  if(connection.autocommit)
                                         unsigned long offset,                          commit(aconnection);
                                         unsigned long limit          }
         ){  
                 PGconn *conn=connection.conn;  private:
           void _bind_parameters(
                 size_t statement_size=strlen(astatement);                                  size_t placeholders_count,
                                   Placeholder* placeholders,
                 char *result=(char *)connection.services->malloc(statement_size                                  const char** paramValues,
                         +MAX_NUMBER*2+15 // limit # offset #                                  Connection& connection,
                         +MAX_STRING // in case of short 'strings'                                  bool transcode_needed
                         +1);          ){
                 // offset & limit -> suffixes                  for(size_t i=0; i<placeholders_count; i++){
                 const char *o;                          Placeholder& ph=placeholders[i];
                 if(offset || limit!=SQL_NO_LIMIT){                          if(transcode_needed){
                         char *cur=result;                                  size_t name_length;
                         memcpy(cur, astatement, statement_size); cur+=statement_size;                                  size_t value_length;
                         if(limit!=SQL_NO_LIMIT)                                  connection.services->transcode(ph.name, strlen(ph.name),
                                 cur+=snprintf(cur, 7+MAX_NUMBER, " limit %u", limit);                                          ph.name, name_length,
                         if(offset)                                          connection.services->request_charset(),
                                 cur+=snprintf(cur, 8+MAX_NUMBER, " offset %u", offset);                                          connection.client_charset);
                         o=result;  
                 } else                                   if(ph.value) {
                         o=astatement;                                          connection.services->transcode(ph.value, strlen(ph.value),
                                                   ph.value, value_length,
                 // /**xxx**/'literal' -> oid                                                  connection.services->request_charset(),
                 char *n=result;                                                  connection.client_charset);
                 while(*o) {                                  }
                         if(                          }
                                 o[0]=='/' &&                          int name_numner=atoi(ph.name);
                                 o[1]=='*' &&                           if(name_numner <= 0 || name_numner > placeholders_count)
                                 o[2]=='*') { // name start                                  connection.services->_throw("bad bind parameter key");
                                 const char* saved_o=o;  
                                 o+=3;                          paramValues[name_numner-1]=ph.value;
                                 while(*o)                  }
                                         if(          }
                                                 o[0]=='*' &&         
                                                 o[1]=='*' &&         
                                                 o[2]=='/' &&          /**
                                                 o[3]=='\'') { // name end                  Executes a query and throw away the result.
                                                 saved_o=0; // found, marking that          */
                                                 o+=4;          void _execute_cmd(const Connection& connection, const char *query){
                                                 Oid oid=lo_creat(conn, INV_READ|INV_WRITE);                  if(PGresult *res=PQexec(connection.conn, query))
                                                 if(oid==InvalidOid)                          PQclear(res); // throw out the result [don't need but must call]
                                                         throwPQerror;                  else
                                                 int fd=lo_open(conn, oid, INV_WRITE);                          throwPQerror;
                                                 if(fd>=0) {          }
                                                         const char *start=o;  
                                                         bool escaped=false;          void _begin_transaction(Connection& connection){
                                                         while(*o && !(o[0]=='\'' && o[1]!='\'' && !escaped)) {                  if(!connection.without_default_transactions)
                                                                 escaped=*o=='\\' || (o[0]=='\'' && o[1]=='\'');                          _execute_cmd(connection, "BEGIN");
                                                                 if(escaped) {          }
                                                                         // write pending, skip "\" or "'"  
                                                                         if(!lo_write_ex(conn, fd, start, o-start))          const char *_preprocess_statement(
                                                                                 connection.services->_throw("lo_write could not write all bytes of object (1)");                                          Connection& connection,
                                                                         start=++o;                                          const char *astatement,
                                                                 } else                                          unsigned long offset,
                                                                         o++;                                          unsigned long limit
                                                         }          ){
                                                         if(!lo_write_ex(conn, fd, start, o-start))                  PGconn *conn=connection.conn;
                                                                 connection.services->_throw("lo_write can not write all bytes of object (2)");  
                                                         if(lo_close(conn, fd)<0)                  size_t statement_size=strlen(astatement);
                                                                 throwPQerror;  
                                                 } else                  char *result=(char *)connection.services->malloc(statement_size
                                                         throwPQerror;                          +MAX_NUMBER*2+15 // limit # offset #
                                                 if(*o)                          +MAX_STRING // in case of short 'strings'
                                                         o++; // skip "'"                          +1);
                   // offset & limit -> suffixes
                                                 n+=snprintf(n, MAX_NUMBER, "%u", oid);                  const char *o;
                                                 break;                  if(offset || limit!=SQL_NO_LIMIT){
                                         } else                          char *cur=result;
                                                 o++; // /**skip**/'xxx'                          memcpy(cur, astatement, statement_size); cur+=statement_size;
                                 if(saved_o) {                          if(limit!=SQL_NO_LIMIT)
                                         o=saved_o;                                  cur+=snprintf(cur, 7+MAX_NUMBER, " limit %u", limit);
                                         *n++=*o++;                          if(offset)
                                 }                                  cur+=snprintf(cur, 8+MAX_NUMBER, " offset %u", offset);
                         } else                          o=result;
                                 *n++=*o++;                  } else
                 }                          o=astatement;
                 *n=0;  
                   // /**xxx**/'literal' -> oid
                 return result;                  char *n=result;
         }                  while(*o) {
                           if(
         bool _transcode_required(Connection& connection){                                  o[0]=='/' &&
                 return (connection.client_charset && strcmp(connection.client_charset, connection.services->request_charset())!=0);                                  o[1]=='*' &&
         }                                  o[2]=='*') { // name start
                                   const char* saved_o=o;
 private: // lo_read/write exchancements                                  o+=3;
                                   while(*o)
         bool lo_read_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) {                                          if(
                 return lo_rw_method (conn, fd, buf, len, lo_read);                                                  o[0]=='*' &&
         }                                                  o[1]=='*' &&
                                                   o[2]=='/' &&
         bool lo_write_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) {                                                  o[3]=='\'') { // name end
                 return lo_rw_method (conn, fd, buf, len, lo_write);                                                  saved_o=0; // found, marking that
         }                                                  o+=4;
                                                   Oid oid=lo_creat(conn, INV_READ|INV_WRITE);
         bool lo_rw_method(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len, int (*lo_func)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len)) {                                                  if(oid==InvalidOid)
                 int size_op;                                                          throwPQerror;
                 while(len && (size_op=lo_func(conn, fd, buf, min(LO_BUFSIZE, len)))>0) {                                                  int fd=lo_open(conn, oid, INV_WRITE);
                         buf+=size_op;                                                  if(fd>=0) {
                         len-=size_op;                                                          const char *start=o;
                 }                                                          bool escaped=false;
                 return len==0;                                                          while(*o && !(o[0]=='\'' && o[1]!='\'' && !escaped)) {
         }                                                                  escaped=*o=='\\' || (o[0]=='\'' && o[1]=='\'');
                                                                   if(escaped) {
 private: // conn client library funcs                                                                          // write pending, skip "\" or "'"
                                                                           if(!lo_write_ex(conn, fd, start, o-start))
         typedef PGconn* (*t_PQsetdbLogin)(                                                                                  connection.services->_throw("lo_write could not write all bytes of object (1)");
                 const char *pghost,                                                                          start=++o;
                 const char *pgport,                                                                  } else
                 const char *pgoptions,                                                                          o++;
                 const char *pgtty,                                                          }
                 const char *dbName,                                                          if(!lo_write_ex(conn, fd, start, o-start))
                 const char *login,                                                                  connection.services->_throw("lo_write can not write all bytes of object (2)");
                 const char *pwd); t_PQsetdbLogin PQsetdbLogin;                                                          if(lo_close(conn, fd)<0)
         typedef void (*t_PQfinish)(PGconn *conn);  t_PQfinish PQfinish;                                                                  throwPQerror;
         typedef char *(*t_PQerrorMessage)(const PGconn* conn); t_PQerrorMessage PQerrorMessage;                                                  } else
         typedef ConnStatusType (*t_PQstatus)(const PGconn *conn); t_PQstatus PQstatus;                                                          throwPQerror;
         typedef PGresult *(*t_PQexec)(PGconn *conn,                                                  if(*o)
                                                 const char *query); t_PQexec PQexec;                                                          o++; // skip "'"
         typedef PGresult *(*t_PQexecParams)(  
                                                 PGconn *conn,                                                  n+=snprintf(n, MAX_NUMBER, "%u", oid);
                                                 const char *query,                                                   break;
                                                 int nParams,                                          } else
                                                 const Oid *paramTypes,                                                  o++; // /**skip**/'xxx'
                                                 const char * const *paramValues,                                  if(saved_o) {
                                                 const int *paramLengths,                                          o=saved_o;
                                                 const int *paramFormats,                                          *n++=*o++;
                                                 int resultFormat); t_PQexecParams PQexecParams;                                  }
                           } else
         typedef ExecStatusType (*t_PQresultStatus)(const PGresult *res); t_PQresultStatus PQresultStatus;                                  *n++=*o++;
         typedef int (*t_PQgetlength)(const PGresult *res,                  }
                                                 int tup_num,                  *n=0;
                                                 int field_num); t_PQgetlength PQgetlength;  
         typedef char* (*t_PQgetvalue)(const PGresult *res,                  return result;
                                                 int tup_num,          }
                                                 int field_num); t_PQgetvalue PQgetvalue;  
         typedef int     (*t_PQntuples)(const PGresult *res); t_PQntuples PQntuples;  private: // lo_read/write exchancements
         typedef char *(*t_PQfname)(const PGresult *res,  
                                                 int field_index); t_PQfname PQfname;          bool lo_read_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) {
         typedef int (*t_PQnfields)(const PGresult *res); t_PQnfields PQnfields;                  return lo_rw_method (conn, fd, buf, len, lo_read);
         typedef void (*t_PQclear)(PGresult *res); t_PQclear PQclear;          }
   
         typedef Oid     (*t_PQftype)(const PGresult *res, int field_num); t_PQftype PQftype;          bool lo_write_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) {
                   return lo_rw_method (conn, fd, buf, len, lo_write);
         typedef size_t (*t_PQescapeStringConn)(PGconn *conn,          }
                                                 char *to, const char *from, size_t length,  
                                                 int *error); t_PQescapeStringConn PQescapeStringConn;          bool lo_rw_method(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len, int (*lo_func)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len)) {
                   int size_op;
         typedef int     (*t_lo_open)(PGconn *conn, Oid lobjId, int mode); t_lo_open lo_open;                  while(len && (size_op=lo_func(conn, fd, buf, min(LO_BUFSIZE, len)))>0) {
         typedef int     (*t_lo_close)(PGconn *conn, int fd); t_lo_close lo_close;                          buf+=size_op;
         typedef int     (*t_lo_read)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len); t_lo_read lo_read;                          len-=size_op;
         typedef int     (*t_lo_write)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len); t_lo_write lo_write;                  }
         typedef int     (*t_lo_lseek)(PGconn *conn, int fd, int offset, int whence); t_lo_lseek lo_lseek;                  return len==0;
         typedef Oid     (*t_lo_creat)(PGconn *conn, int mode); t_lo_creat lo_creat;          }
         typedef int     (*t_lo_tell)(PGconn *conn, int fd); t_lo_tell lo_tell;  
         typedef int     (*t_lo_unlink)(PGconn *conn, Oid lobjId); t_lo_unlink lo_unlink;  private: // conn client library funcs
         typedef Oid     (*t_lo_import)(PGconn *conn, const char *filename); t_lo_import lo_import;  
         typedef int     (*t_lo_export)(PGconn *conn, Oid lobjId, const char *filename); t_lo_export lo_export;          typedef PGconn* (*t_PQsetdbLogin)(
                   const char *pghost,
 private: // conn client library funcs linking                  const char *pgport,
                   const char *pgoptions,
         const char *dlink(const char *dlopen_file_spec) {                  const char *pgtty,
                 if(lt_dlinit())                  const char *dbName,
                         return lt_dlerror();                  const char *login,
                 lt_dlhandle handle=lt_dlopen(dlopen_file_spec);                  const char *pwd); t_PQsetdbLogin PQsetdbLogin;
                 if(!handle)          typedef void (*t_PQfinish)(PGconn *conn);  t_PQfinish PQfinish;
                         return "can not open the dynamic link module";          typedef char *(*t_PQerrorMessage)(const PGconn* conn); t_PQerrorMessage PQerrorMessage;
           typedef ConnStatusType (*t_PQstatus)(const PGconn *conn); t_PQstatus PQstatus;
                 #define DSLINK(name, action) \          typedef PGresult *(*t_PQexec)(PGconn *conn,
                         name=(t_##name)lt_dlsym(handle, #name); \                                                  const char *query); t_PQexec PQexec;
                                 if(!name) \          typedef PGresult *(*t_PQexecParams)(
                                         action;                                                  PGconn *conn,
                                                   const char *query,
                 #define DLINK(name) DSLINK(name, return "function " #name " was not found")                                                  int nParams,
                                                                   const Oid *paramTypes,
                 DLINK(PQsetdbLogin);                                                  const char * const *paramValues,
                 DLINK(PQerrorMessage);                                                  const int *paramLengths,
                 DLINK(PQstatus);                                                  const int *paramFormats,
                 DLINK(PQfinish);                                                  int resultFormat); t_PQexecParams PQexecParams;
                 DLINK(PQgetvalue);  
                 DLINK(PQgetlength);          typedef ExecStatusType (*t_PQresultStatus)(const PGresult *res); t_PQresultStatus PQresultStatus;
                 DLINK(PQntuples);          typedef int (*t_PQgetlength)(const PGresult *res,
                 DLINK(PQfname);                                                  int tup_num,
                 DLINK(PQnfields);                                                  int field_num); t_PQgetlength PQgetlength;
                 DLINK(PQclear);          typedef char* (*t_PQgetvalue)(const PGresult *res,
                 DLINK(PQresultStatus);                                                  int tup_num,
                 DLINK(PQexec);                                                  int field_num); t_PQgetvalue PQgetvalue;
                 DLINK(PQexecParams);          typedef int     (*t_PQntuples)(const PGresult *res); t_PQntuples PQntuples;
                 DLINK(PQftype);          typedef char *(*t_PQfname)(const PGresult *res,
                 DLINK(PQescapeStringConn);                                                  int field_index); t_PQfname PQfname;
                 DLINK(lo_open);         DLINK(lo_close);          typedef int (*t_PQnfields)(const PGresult *res); t_PQnfields PQnfields;
                 DLINK(lo_read);         DLINK(lo_write);          typedef void (*t_PQclear)(PGresult *res); t_PQclear PQclear;
                 DLINK(lo_lseek);                DLINK(lo_creat);  
                 DLINK(lo_tell);         DLINK(lo_unlink);          typedef Oid     (*t_PQftype)(const PGresult *res, int field_num); t_PQftype PQftype;
                 DLINK(lo_import);               DLINK(lo_export);  
           typedef size_t (*t_PQescapeStringConn)(PGconn *conn,
                 return 0;                                                  char *to, const char *from, size_t length,
         }                                                  int *error); t_PQescapeStringConn PQescapeStringConn;
 };  
           typedef int     (*t_lo_open)(PGconn *conn, Oid lobjId, int mode); t_lo_open lo_open;
 extern "C" SQL_Driver *SQL_DRIVER_CREATE() {          typedef int     (*t_lo_close)(PGconn *conn, int fd); t_lo_close lo_close;
         return new PgSQL_Driver();          typedef int     (*t_lo_read)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len); t_lo_read lo_read;
 }          typedef int     (*t_lo_write)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len); t_lo_write lo_write;
           typedef int     (*t_lo_lseek)(PGconn *conn, int fd, int offset, int whence); t_lo_lseek lo_lseek;
           typedef Oid     (*t_lo_creat)(PGconn *conn, int mode); t_lo_creat lo_creat;
           typedef int     (*t_lo_tell)(PGconn *conn, int fd); t_lo_tell lo_tell;
           typedef int     (*t_lo_unlink)(PGconn *conn, Oid lobjId); t_lo_unlink lo_unlink;
           typedef Oid     (*t_lo_import)(PGconn *conn, const char *filename); t_lo_import lo_import;
           typedef int     (*t_lo_export)(PGconn *conn, Oid lobjId, const char *filename); t_lo_export lo_export;
   
   private: // conn client library funcs linking
   
           const char *dlink(const char *dlopen_file_spec) {
                   if(lt_dlinit())
                           return lt_dlerror();
                   lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
                   if(!handle)
                           return "can not open the dynamic link module";
   
                   #define DSLINK(name, action) \
                           name=(t_##name)lt_dlsym(handle, #name); \
                                   if(!name) \
                                           action;
   
                   #define DLINK(name) DSLINK(name, return "function " #name " was not found")
                  
                   DLINK(PQsetdbLogin);
                   DLINK(PQerrorMessage);
                   DLINK(PQstatus);
                   DLINK(PQfinish);
                   DLINK(PQgetvalue);
                   DLINK(PQgetlength);
                   DLINK(PQntuples);
                   DLINK(PQfname);
                   DLINK(PQnfields);
                   DLINK(PQclear);
                   DLINK(PQresultStatus);
                   DLINK(PQexec);
                   DLINK(PQexecParams);
                   DLINK(PQftype);
                   DLINK(PQescapeStringConn);
                   DLINK(lo_open);         DLINK(lo_close);
                   DLINK(lo_read);         DLINK(lo_write);
                   DLINK(lo_lseek);                DLINK(lo_creat);
                   DLINK(lo_tell);         DLINK(lo_unlink);
                   DLINK(lo_import);               DLINK(lo_export);
   
                   return 0;
           }
   };
   
   extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
           return new PgSQL_Driver();
   }

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


E-mail: