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

version 1.31, 2008/07/01 13:40:33 version 1.33, 2008/12/21 00:14:25
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 auto commit, 'BEGIN TRAN' at connection start and COMMIT/ROLLBACK at the end [can't be used together 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);
                 }                  }
   
                 _begin_transaction(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);
                 if(!connection.without_default_transactions){                  _transaction_commit(connection);
                         _execute_cmd(connection, "COMMIT");                  _transaction_begin(connection);
                 }          }
                 _begin_transaction(connection);  
         }          void rollback(void *aconnection){
                   Connection& connection=*static_cast<Connection*>(aconnection);
         void rollback(void *aconnection){                  _transaction_rollback(connection);
                 Connection& connection=*static_cast<Connection*>(aconnection);                  _transaction_begin(connection);
                 if(!connection.without_default_transactions){          }
                         _execute_cmd(connection, "ROLLBACK");  
                 }          bool ping(void *aconnection) {
                 _begin_transaction(connection);                  Connection& connection=*static_cast<Connection*>(aconnection);
         }                  return PQstatus(connection.conn)==CONNECTION_OK;
           }
         bool ping(void *aconnection) {  
                 Connection& connection=*static_cast<Connection*>(aconnection);          const char* quote(void *aconnection, const char *from, unsigned int length){
                 return PQstatus(connection.conn)==CONNECTION_OK;                  Connection& connection=*static_cast<Connection*>(aconnection);
         }  
                   char *result=(char*)connection.services->malloc_atomic(length*2+1);
         const char* quote(void *aconnection, const char *from, unsigned int length){                  int err=0;
                 Connection& connection=*static_cast<Connection*>(aconnection);                  PQescapeStringConn(connection.conn, result, from, length, &err);
                   return result;
                 char *result=(char*)connection.services->malloc_atomic(length*2+1);          }
                 int err=0;         
                 PQescapeStringConn(connection.conn, result, from, length, &err);          void query(void *aconnection,
                 return result;                                  const char *astatement,
         }                                  size_t placeholders_count, Placeholder* placeholders,
                                           unsigned long offset, unsigned long limit,
         void query(void *aconnection,                                   SQL_Driver_query_event_handlers& handlers
                                 const char *astatement,           ){
                                 size_t placeholders_count, Placeholder* placeholders,                   Connection& connection=*static_cast<Connection*>(aconnection);
                                 unsigned long offset, unsigned long limit,                  SQL_Driver_services& services=*connection.services;
                                 SQL_Driver_query_event_handlers& handlers                  PGconn *conn=connection.conn;
         ){  
                 Connection& connection=*static_cast<Connection*>(aconnection);                  const char* client_charset=connection.client_charset;
                 SQL_Driver_services& services=*connection.services;                  const char* request_charset=services.request_charset();
                 PGconn *conn=connection.conn;                  bool transcode_needed=client_charset && strcmp(client_charset, request_charset)!=0;
   
                 const char* client_charset=connection.client_charset;                  const char** paramValues;
                 const char* request_charset=services.request_charset();                  if(placeholders_count>0){
                 bool transcode_needed=client_charset && strcmp(client_charset, request_charset)!=0;                          int binds_size=sizeof(char)*placeholders_count;
                           paramValues = static_cast<const char**>(services.malloc_atomic(binds_size));
                 const char** paramValues;                          _bind_parameters(placeholders_count, placeholders, paramValues, connection, transcode_needed);
                 if(placeholders_count>0){                  }
                         int binds_size=sizeof(char)*placeholders_count;  
                         paramValues = static_cast<const char**>(services.malloc_atomic(binds_size));                  if(transcode_needed){
                         _bind_parameters(placeholders_count, placeholders, paramValues, connection, transcode_needed);                          // transcode query from $request:charset to ?ClientCharset
                 }                          size_t length=strlen(astatement);
                           services.transcode(astatement, length,
                 if(transcode_needed){                                  astatement, length,
                         // transcode query from $request:charset to ?ClientCharset                                  request_charset,
                         size_t length=strlen(astatement);                                  client_charset);
                         services.transcode(astatement, length,                  }
                                 astatement, length,  
                                 request_charset,                  const char *statement=_preprocess_statement(connection, astatement, offset, limit);
                                 client_charset);                  // error after prepare?
                 }  
                   PGresult *res;
                 const char *statement=_preprocess_statement(connection, astatement, offset, limit);                  if(placeholders_count>0){
                 // error after prepare?                          res=PQexecParams(conn, statement, placeholders_count, NULL, paramValues, NULL, NULL, 0);
                   } else {
                 PGresult *res;                          res=PQexec(conn, statement);
                 if(placeholders_count>0){                  }
                         res=PQexecParams(conn, statement, placeholders_count, NULL, paramValues, NULL, NULL, 0);                  if(!res)
                 } else {                          throwPQerror;
                         res=PQexec(conn, statement);  
                 }                  bool failed=false;
                 if(!res)                   SQL_Error sql_error;
                         throwPQerror;  
                   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;
                                 return;                          case PGRES_TUPLES_OK:
                         case PGRES_TUPLES_OK:                                   break;  
                                 break;                            default:
                         default:                                  PQclear_throwPQerror;
                                 PQclear_throwPQerror;                                  break;
                                 break;                  }
                 }                 
                   #define CHECK(afailed) \
                 int column_count=PQnfields(res);                  if(afailed) { \
                 if(!column_count)                          failed=true; \
                         PQclear_throw("result contains no columns");                          goto cleanup; \
                   }
                 bool failed=false;  
                 SQL_Error sql_error;                  int column_count=PQnfields(res);
 #define CHECK(afailed) \                  if(!column_count)
                 if(afailed) { \                          PQclear_throw("result contains no columns");
                         failed=true; \  
                         goto cleanup; \                  if(column_count>MAX_COLS)
                 }                          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:
                         char *name=PQfname(res, i);                                  case INT8OID:
                         size_t length=strlen(name);                                  case INT2OID:
                         column_types[i]=PQftype(res, i);                                  case INT4OID:
                         switch(column_types[i]){                                  case FLOAT4OID:
                                 case BOOLOID:                                  case FLOAT8OID:
                                 case INT8OID:                                  case DATEOID:
                                 case INT2OID:                                  case TIMEOID:
                                 case INT4OID:                                  case TIMESTAMPOID:
                                 case FLOAT4OID:                                  case TIMESTAMPTZOID:
                                 case FLOAT8OID:                                  case TIMETZOID:
                                 case DATEOID:                                  case NUMERICOID:
                                 case TIMEOID:                                          transcode_column[i]=false;
                                 case TIMESTAMPOID:                                          break;
                                 case TIMESTAMPTZOID:                                  default:
                                 case TIMETZOID:                                          transcode_column[i]=transcode_needed;
                                 case NUMERICOID:                                          break;
                                         transcode_column[i]=false;                          }
                                         break;                          char *name=PQfname(res, i);
                                 default:                          size_t length=strlen(name);
                                         transcode_column[i]=transcode_needed;                          char* strm=(char*)services.malloc(length+1);
                                         break;                          memcpy(strm, name, length+1);
                         }                          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)                                           client_charset,
                                 // transcode column name from ?ClientCharset to $request:charset                                          request_charset);
                                 services.transcode(str, length,  
                                         str, length,                          CHECK(handlers.add_column(sql_error, str, length));
                                         client_charset,                  }
                                         request_charset);  
                   CHECK(handlers.before_rows(sql_error));
                         CHECK(handlers.add_column(sql_error, str, length));  
                 }                  if(unsigned long row_count=(unsigned long)PQntuples(res))
                           for(unsigned long r=0; r<row_count; r++) {
                 CHECK(handlers.before_rows(sql_error));                                  CHECK(handlers.add_row(sql_error));
                                   for(int i=0; i<column_count; i++){
                 if(unsigned long row_count=(unsigned long)PQntuples(res))                                          const char *cell=PQgetvalue(res, r, i);
                         for(unsigned long r=0; r<row_count; r++) {                                          size_t length;
                                 CHECK(handlers.add_row(sql_error));                                          const char* str;
                                 for(int i=0; i<column_count; i++){  
                                         const char *cell=PQgetvalue(res, r, i);                                          switch(column_types[i]){
                                         size_t length;                                                  case OIDOID:
                                         const char* str;                                                          {
                                                                   char *error_pos=0;
                                         switch(column_types[i]){                                                                  Oid oid=cell?atoi(cell):0;
                                                 case OIDOID:                                                                  int fd=lo_open(conn, oid, INV_READ);
                                                         {                                                                  if(fd>=0){
                                                                 char *error_pos=0;                                                                          // seek to end
                                                                 Oid oid=cell?atoi(cell):0;                                                                          if(lo_lseek(conn, fd, 0, SEEK_END)<0)
                                                                 int fd=lo_open(conn, oid, INV_READ);                                                                                  PQclear_throwPQerror;
                                                                 if(fd>=0){                                                                          // get length
                                                                         // seek to end                                                                          int size_tell=lo_tell(conn, fd);
                                                                         if(lo_lseek(conn, fd, 0, SEEK_END)<0)                                                                          if(size_tell<0)
                                                                                 PQclear_throwPQerror;                                                                                  PQclear_throwPQerror;
                                                                         // get length                                                                          // seek to begin
                                                                         int size_tell=lo_tell(conn, fd);                                                                          if(lo_lseek(conn, fd, 0, SEEK_SET)<0)
                                                                         if(size_tell<0)                                                                                  PQclear_throwPQerror;
                                                                                 PQclear_throwPQerror;                                                                          length=(size_t)size_tell;
                                                                         // seek to begin                                                                          if(length){
                                                                         if(lo_lseek(conn, fd, 0, SEEK_SET)<0)                                                                                  // read
                                                                                 PQclear_throwPQerror;                                                                                  char* strm=(char*)services.malloc(length+1);
                                                                         length=(size_t)size_tell;                                                                                  if(!lo_read_ex(conn, fd, strm, size_tell))
                                                                         if(length){                                                                                          PQclear_throw("lo_read can not read all bytes of object");
                                                                                 // read                                                                                   strm[length]=0;
                                                                                 char* strm=(char*)services.malloc(length+1);                                                                                  str=strm;
                                                                                 if(!lo_read_ex(conn, fd, strm, size_tell))                                                                          } else
                                                                                         PQclear_throw("lo_read can not read all bytes of object");                                                                                  str=0;
                                                                                 strm[length]=0;                                                                          if(lo_close(conn, fd)<0)
                                                                                 str=strm;                                                                                  PQclear_throwPQerror;
                                                                         } else                                                                  } else
                                                                                 str=0;                                                                          PQclear_throwPQerror;
                                                                         if(lo_close(conn, fd)<0)                                                          }
                                                                                 PQclear_throwPQerror;                                                  default:
                                                                 } else                                                          // normal column, read it normally
                                                                         PQclear_throwPQerror;                                                          length=(size_t)PQgetlength(res, r, i);
                                                         }                                                          if(length){
                                                 default:                                                                  char* strm=(char*)services.malloc(length+1);
                                                         // normal column, read it normally                                                                  memcpy(strm, cell, length+1);
                                                         length=(size_t)PQgetlength(res, r, i);                                                                  str=strm;
                                                         if(length){                                                          } else
                                                                 char* strm=(char*)services.malloc(length+1);                                                                  str=0;
                                                                 memcpy(strm, cell, length+1);                                          }
                                                                 str=strm;  
                                                         } else                                          if(str && length && transcode_column[i]){
                                                                 str=0;                                                  // transcode cell value from ?ClientCharset to $request:charset
                                         }                                                  services.transcode(str, length,
                                                           str, length,
                                         if(str && length && transcode_column[i]){                                                          client_charset,
                                                 //services._throw("tr");                                                          request_charset);
                                                 // transcode cell value from ?ClientCharset to $request:charset                                          }
                                                 services.transcode(str, length,  
                                                         str, length,                                          CHECK(handlers.add_row_cell(sql_error, str, length));
                                                         client_charset,                                  }
                                                         request_charset);                          }
                                         }  cleanup:
                   PQclear(res);
                                         CHECK(handlers.add_row_cell(sql_error, str, length));                  if(failed)
                                 }                          services._throw(sql_error);
                         }  
 cleanup:                  if(connection.autocommit)
                 PQclear(res);                          commit(aconnection);
                 if(failed)          }
                         services._throw(sql_error);  
   private:
                 commit(aconnection);          void _bind_parameters(
         }                                  size_t placeholders_count,
                                   Placeholder* placeholders,
 private:                                  const char** paramValues,
         void _bind_parameters(                                  Connection& connection,
                                 size_t placeholders_count,                                   bool transcode_needed
                                 Placeholder* placeholders,           ){
                                 const char** paramValues,                  for(size_t i=0; i<placeholders_count; i++){
                                 Connection& connection,                          Placeholder& ph=placeholders[i];
                                 bool transcode_needed                          if(transcode_needed){
         ){                                  size_t name_length;
                 for(size_t i=0; i<placeholders_count; i++){                                  size_t value_length;
                         Placeholder& ph=placeholders[i];                                  connection.services->transcode(ph.name, strlen(ph.name),
                         if(transcode_needed){                                          ph.name, name_length,
                                 size_t name_length;                                          connection.services->request_charset(),
                                 size_t value_length;                                          connection.client_charset);
                                 connection.services->transcode(ph.name, strlen(ph.name),  
                                         ph.name, name_length,                                  if(ph.value) {
                                         connection.services->request_charset(),                                          connection.services->transcode(ph.value, strlen(ph.value),
                                         connection.client_charset);                                                  ph.value, value_length,
                                                   connection.services->request_charset(),
                                 if(ph.value) {                                                  connection.client_charset);
                                         connection.services->transcode(ph.value, strlen(ph.value),                                  }
                                                 ph.value, value_length,                          }
                                                 connection.services->request_charset(),                          int name_numner=atoi(ph.name);
                                                 connection.client_charset);                          if(name_numner <= 0 || name_numner > placeholders_count)
                                 }                                  connection.services->_throw("bad bind parameter key");
                         }  
                         int name_numner=atoi(ph.name);                          paramValues[name_numner-1]=ph.value;
                         if(name_numner <= 0 || name_numner > placeholders_count)                  }
                                 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_commit(Connection& connection){
                 Executes a query and throw away the result.                  _execute_transactions_cmd(connection, "COMMIT");
         */          }
         void _execute_cmd(const Connection& connection, const char *query){  
                 if(PGresult *res=PQexec(connection.conn, query))          void _transaction_rollback(Connection& connection){
                         PQclear(res); // throw out the result [don't need but must call]                  _execute_transactions_cmd(connection, "ROLLBACK");
                 else          }
                         throwPQerror;  
         }          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
         void _begin_transaction(Connection& connection){                          _execute_cmd(connection, query);
                 if(!connection.without_default_transactions)          }
                         _execute_cmd(connection, "BEGIN");  
         }          // executes a query and throw away the result.
           void _execute_cmd(const Connection& connection, const char *query){
         const char *_preprocess_statement(                  if(PGresult *res=PQexec(connection.conn, query))
                                         Connection& connection,                          PQclear(res); // throw away the result [don't need but must call]
                                         const char *astatement,                  else
                                         unsigned long offset,                          throwPQerror;
                                         unsigned long limit          }
         ){  
                 PGconn *conn=connection.conn;          const char *_preprocess_statement(
                                           Connection& connection,
                 size_t statement_size=strlen(astatement);                                          const char *astatement,
                                           unsigned long offset,
                 char *result=(char *)connection.services->malloc(statement_size                                          unsigned long limit
                         +MAX_NUMBER*2+15 // limit # offset #          ){
                         +MAX_STRING // in case of short 'strings'                  PGconn *conn=connection.conn;
                         +1);  
                 // offset & limit -> suffixes                  size_t statement_size=strlen(astatement);
                 const char *o;  
                 if(offset || limit!=SQL_NO_LIMIT){                  char *result=(char *)connection.services->malloc(statement_size
                         char *cur=result;                          +MAX_NUMBER*2+15 // limit # offset #
                         memcpy(cur, astatement, statement_size); cur+=statement_size;                          +MAX_STRING // in case of short 'strings'
                         if(limit!=SQL_NO_LIMIT)                          +1);
                                 cur+=snprintf(cur, 7+MAX_NUMBER, " limit %u", limit);                  // offset & limit -> suffixes
                         if(offset)                  const char *o;
                                 cur+=snprintf(cur, 8+MAX_NUMBER, " offset %u", offset);                  if(offset || limit!=SQL_NO_LIMIT){
                         o=result;                          char *cur=result;
                 } else                           memcpy(cur, astatement, statement_size); cur+=statement_size;
                         o=astatement;                          if(limit!=SQL_NO_LIMIT)
                                   cur+=snprintf(cur, 7+MAX_NUMBER, " limit %u", limit);
                 // /**xxx**/'literal' -> oid                          if(offset)
                 char *n=result;                                  cur+=snprintf(cur, 8+MAX_NUMBER, " offset %u", offset);
                 while(*o) {                          o=result;
                         if(                  } else
                                 o[0]=='/' &&                          o=astatement;
                                 o[1]=='*' &&   
                                 o[2]=='*') { // name start                  // /**xxx**/'literal' -> oid
                                 const char* saved_o=o;                  char *n=result;
                                 o+=3;                  while(*o) {
                                 while(*o)                          if(
                                         if(                                  o[0]=='/' &&
                                                 o[0]=='*' &&                                  o[1]=='*' &&
                                                 o[1]=='*' &&                                  o[2]=='*') { // name start
                                                 o[2]=='/' &&                                  const char* saved_o=o;
                                                 o[3]=='\'') { // name end                                  o+=3;
                                                 saved_o=0; // found, marking that                                  while(*o)
                                                 o+=4;                                          if(
                                                 Oid oid=lo_creat(conn, INV_READ|INV_WRITE);                                                  o[0]=='*' &&
                                                 if(oid==InvalidOid)                                                  o[1]=='*' &&
                                                         throwPQerror;                                                  o[2]=='/' &&
                                                 int fd=lo_open(conn, oid, INV_WRITE);                                                  o[3]=='\'') { // name end
                                                 if(fd>=0) {                                                  saved_o=0; // found, marking that
                                                         const char *start=o;                                                  o+=4;
                                                         bool escaped=false;                                                  Oid oid=lo_creat(conn, INV_READ|INV_WRITE);
                                                         while(*o && !(o[0]=='\'' && o[1]!='\'' && !escaped)) {                                                  if(oid==InvalidOid)
                                                                 escaped=*o=='\\' || (o[0]=='\'' && o[1]=='\'');                                                          throwPQerror;
                                                                 if(escaped) {                                                  int fd=lo_open(conn, oid, INV_WRITE);
                                                                         // write pending, skip "\" or "'"                                                  if(fd>=0) {
                                                                         if(!lo_write_ex(conn, fd, start, o-start))                                                          const char *start=o;
                                                                                 connection.services->_throw("lo_write could not write all bytes of object (1)");                                                          bool escaped=false;
                                                                         start=++o;                                                          while(*o && !(o[0]=='\'' && o[1]!='\'' && !escaped)) {
                                                                 } else                                                                  escaped=*o=='\\' || (o[0]=='\'' && o[1]=='\'');
                                                                         o++;                                                                  if(escaped) {
                                                         }                                                                          // write pending, skip "\" or "'"
                                                         if(!lo_write_ex(conn, fd, start, o-start))                                                                          if(!lo_write_ex(conn, fd, start, o-start))
                                                                 connection.services->_throw("lo_write can not write all bytes of object (2)");                                                                                  connection.services->_throw("lo_write could not write all bytes of object (1)");
                                                         if(lo_close(conn, fd)<0)                                                                          start=++o;
                                                                 throwPQerror;                                                                  } else
                                                 } else                                                                          o++;
                                                         throwPQerror;                                                          }
                                                 if(*o)                                                          if(!lo_write_ex(conn, fd, start, o-start))
                                                         o++; // skip "'"                                                                  connection.services->_throw("lo_write can not write all bytes of object (2)");
                                                           if(lo_close(conn, fd)<0)
                                                 n+=snprintf(n, MAX_NUMBER, "%u", oid);                                                                  throwPQerror;
                                                 break;                                                  } else
                                         } else                                                          throwPQerror;
                                                 o++; // /**skip**/'xxx'                                                  if(*o)
                                 if(saved_o) {                                                          o++; // skip "'"
                                         o=saved_o;  
                                         *n++=*o++;                                                  n+=snprintf(n, MAX_NUMBER, "%u", oid);
                                 }                                                  break;
                         } else                                          } else
                                 *n++=*o++;                                                  o++; // /**skip**/'xxx'
                 }                                  if(saved_o) {
                 *n=0;                                          o=saved_o;
                                           *n++=*o++;
                 return result;                                  }
         }                          } else
                                   *n++=*o++;
 private: // lo_read/write exchancements                  }
                   *n=0;
         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);                  return result;
         }          }
   
         bool lo_write_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_write);  
         }          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_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_write_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) {
                         buf+=size_op;                  return lo_rw_method (conn, fd, buf, len, lo_write);
                         len-=size_op;          }
                 }  
                 return 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)) {
         }                  int size_op;
                   while(len && (size_op=lo_func(conn, fd, buf, min(LO_BUFSIZE, len)))>0) {
 private: // conn client library funcs                          buf+=size_op;
                           len-=size_op;
         typedef PGconn* (*t_PQsetdbLogin)(                  }
                 const char *pghost,                  return len==0;
                 const char *pgport,          }
                 const char *pgoptions,  
                 const char *pgtty,  private: // conn client library funcs
                 const char *dbName,  
                 const char *login,          typedef PGconn* (*t_PQsetdbLogin)(
                 const char *pwd); t_PQsetdbLogin PQsetdbLogin;                  const char *pghost,
         typedef void (*t_PQfinish)(PGconn *conn);  t_PQfinish PQfinish;                  const char *pgport,
         typedef char *(*t_PQerrorMessage)(const PGconn* conn); t_PQerrorMessage PQerrorMessage;                  const char *pgoptions,
         typedef ConnStatusType (*t_PQstatus)(const PGconn *conn); t_PQstatus PQstatus;                  const char *pgtty,
         typedef PGresult *(*t_PQexec)(PGconn *conn,                  const char *dbName,
                                                 const char *query); t_PQexec PQexec;                  const char *login,
         typedef PGresult *(*t_PQexecParams)(                  const char *pwd); t_PQsetdbLogin PQsetdbLogin;
                                                 PGconn *conn,          typedef void (*t_PQfinish)(PGconn *conn);  t_PQfinish PQfinish;
                                                 const char *query,           typedef char *(*t_PQerrorMessage)(const PGconn* conn); t_PQerrorMessage PQerrorMessage;
                                                 int nParams,          typedef ConnStatusType (*t_PQstatus)(const PGconn *conn); t_PQstatus PQstatus;
                                                 const Oid *paramTypes,          typedef PGresult *(*t_PQexec)(PGconn *conn,
                                                 const char * const *paramValues,                                                  const char *query); t_PQexec PQexec;
                                                 const int *paramLengths,          typedef PGresult *(*t_PQexecParams)(
                                                 const int *paramFormats,                                                  PGconn *conn,
                                                 int resultFormat); t_PQexecParams PQexecParams;                                                  const char *query,
                                                   int nParams,
         typedef ExecStatusType (*t_PQresultStatus)(const PGresult *res); t_PQresultStatus PQresultStatus;                                                  const Oid *paramTypes,
         typedef int (*t_PQgetlength)(const PGresult *res,                                                  const char * const *paramValues,
                                                 int tup_num,                                                  const int *paramLengths,
                                                 int field_num); t_PQgetlength PQgetlength;                                                  const int *paramFormats,
         typedef char* (*t_PQgetvalue)(const PGresult *res,                                                  int resultFormat); t_PQexecParams PQexecParams;
                                                 int tup_num,  
                                                 int field_num); t_PQgetvalue PQgetvalue;          typedef ExecStatusType (*t_PQresultStatus)(const PGresult *res); t_PQresultStatus PQresultStatus;
         typedef int     (*t_PQntuples)(const PGresult *res); t_PQntuples PQntuples;          typedef int (*t_PQgetlength)(const PGresult *res,
         typedef char *(*t_PQfname)(const PGresult *res,                                                  int tup_num,
                                                 int field_index); t_PQfname PQfname;                                                  int field_num); t_PQgetlength PQgetlength;
         typedef int (*t_PQnfields)(const PGresult *res); t_PQnfields PQnfields;          typedef char* (*t_PQgetvalue)(const PGresult *res,
         typedef void (*t_PQclear)(PGresult *res); t_PQclear PQclear;                                                  int tup_num,
                                                   int field_num); t_PQgetvalue PQgetvalue;
         typedef Oid     (*t_PQftype)(const PGresult *res, int field_num); t_PQftype PQftype;          typedef int     (*t_PQntuples)(const PGresult *res); t_PQntuples PQntuples;
           typedef char *(*t_PQfname)(const PGresult *res,
         typedef size_t (*t_PQescapeStringConn)(PGconn *conn,                                                  int field_index); t_PQfname PQfname;
                                                 char *to, const char *from, size_t length,          typedef int (*t_PQnfields)(const PGresult *res); t_PQnfields PQnfields;
                                                 int *error); t_PQescapeStringConn PQescapeStringConn;          typedef void (*t_PQclear)(PGresult *res); t_PQclear PQclear;
   
         typedef int     (*t_lo_open)(PGconn *conn, Oid lobjId, int mode); t_lo_open lo_open;          typedef Oid     (*t_PQftype)(const PGresult *res, int field_num); t_PQftype PQftype;
         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 size_t (*t_PQescapeStringConn)(PGconn *conn,
         typedef int     (*t_lo_write)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len); t_lo_write lo_write;                                                  char *to, const char *from, size_t length,
         typedef int     (*t_lo_lseek)(PGconn *conn, int fd, int offset, int whence); t_lo_lseek lo_lseek;                                                  int *error); t_PQescapeStringConn PQescapeStringConn;
         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_open)(PGconn *conn, Oid lobjId, int mode); t_lo_open lo_open;
         typedef int     (*t_lo_unlink)(PGconn *conn, Oid lobjId); t_lo_unlink lo_unlink;          typedef int     (*t_lo_close)(PGconn *conn, int fd); t_lo_close lo_close;
         typedef Oid     (*t_lo_import)(PGconn *conn, const char *filename); t_lo_import lo_import;          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_export)(PGconn *conn, Oid lobjId, const char *filename); t_lo_export lo_export;          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;
 private: // conn client library funcs linking          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;
         const char *dlink(const char *dlopen_file_spec) {          typedef int     (*t_lo_unlink)(PGconn *conn, Oid lobjId); t_lo_unlink lo_unlink;
                 if(lt_dlinit())          typedef Oid     (*t_lo_import)(PGconn *conn, const char *filename); t_lo_import lo_import;
                         return lt_dlerror();          typedef int     (*t_lo_export)(PGconn *conn, Oid lobjId, const char *filename); t_lo_export lo_export;
                 lt_dlhandle handle=lt_dlopen(dlopen_file_spec);  
                 if(!handle)  private: // conn client library funcs linking
                         return "can not open the dynamic link module";  
           const char *dlink(const char *dlopen_file_spec) {
                 #define DSLINK(name, action) \                  if(lt_dlinit())
                         name=(t_##name)lt_dlsym(handle, #name); \                          return lt_dlerror();
                                 if(!name) \                  lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
                                         action;                  if(!handle)
                           return "can not open the dynamic link module";
                 #define DLINK(name) DSLINK(name, return "function " #name " was not found")  
                                   #define DSLINK(name, action) \
                 DLINK(PQsetdbLogin);                          name=(t_##name)lt_dlsym(handle, #name); \
                 DLINK(PQerrorMessage);                                  if(!name) \
                 DLINK(PQstatus);                                          action;
                 DLINK(PQfinish);  
                 DLINK(PQgetvalue);                  #define DLINK(name) DSLINK(name, return "function " #name " was not found")
                 DLINK(PQgetlength);                 
                 DLINK(PQntuples);                  DLINK(PQsetdbLogin);
                 DLINK(PQfname);                  DLINK(PQerrorMessage);
                 DLINK(PQnfields);                  DLINK(PQstatus);
                 DLINK(PQclear);                  DLINK(PQfinish);
                 DLINK(PQresultStatus);                  DLINK(PQgetvalue);
                 DLINK(PQexec);                  DLINK(PQgetlength);
                 DLINK(PQexecParams);                  DLINK(PQntuples);
                 DLINK(PQftype);                  DLINK(PQfname);
                 DLINK(PQescapeStringConn);                  DLINK(PQnfields);
                 DLINK(lo_open);         DLINK(lo_close);                  DLINK(PQclear);
                 DLINK(lo_read);         DLINK(lo_write);                  DLINK(PQresultStatus);
                 DLINK(lo_lseek);                DLINK(lo_creat);                  DLINK(PQexec);
                 DLINK(lo_tell);         DLINK(lo_unlink);                  DLINK(PQexecParams);
                 DLINK(lo_import);               DLINK(lo_export);                  DLINK(PQftype);
                   DLINK(PQescapeStringConn);
                 return 0;                  DLINK(lo_open);         DLINK(lo_close);
         }                  DLINK(lo_read);         DLINK(lo_write);
 };                  DLINK(lo_lseek);                DLINK(lo_creat);
                   DLINK(lo_tell);         DLINK(lo_unlink);
 extern "C" SQL_Driver *SQL_DRIVER_CREATE() {                  DLINK(lo_import);               DLINK(lo_export);
         return new PgSQL_Driver();  
 }                  return 0;
           }
   };
   
   extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
           return new PgSQL_Driver();
   }

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


E-mail: