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

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

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


E-mail: