Diff for /sql/pgsql/parser3pgsql.C between versions 1.33 and 1.34

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

Removed from v.1.33  
changed lines
  Added in v.1.34


E-mail: