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

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

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


E-mail: