--- parser3/src/sql/pgsql/Attic/parser3pgsql.C 2001/07/30 15:32:18 1.1 +++ parser3/src/sql/pgsql/Attic/parser3pgsql.C 2001/07/31 12:51:45 1.5 @@ -7,13 +7,20 @@ 2001.07.30 using PgSQL 7.1.2 */ -static const char *RCSId="$Id: parser3pgsql.C,v 1.1 2001/07/30 15:32:18 parser Exp $"; +static const char *RCSId="$Id: parser3pgsql.C,v 1.5 2001/07/31 12:51:45 parser Exp $"; #include "config_includes.h" #include "pa_sql_driver.h" #include +#include + +// #include +#define OIDOID 26 +#define LO_BUFSIZE 8192 + + #include "ltdl.h" #define MAX_STRING 0x400 @@ -21,11 +28,14 @@ static const char *RCSId="$Id: parser3pg #if _MSC_VER # define snprintf _snprintf -#endif -#ifndef strcasecmp # define strcasecmp _stricmp #endif +#ifndef max +inline int max(int a,int b) { return a>b?a:b; } +inline int min(int a,int b){ return a '' unsigned int result=length; while(length--) { - if(*from=='\'') + switch(*from) { + case '\'': // "'" -> "''" *to++='\''; + break; + case '\\': // "\" -> "\\" + *to++='\''; + break; + } *to++=*from++; } return result; @@ -117,7 +150,16 @@ public: const char *astatement, unsigned long offset, unsigned long limit, SQL_Driver_query_event_handlers& handlers) { - PGconn *pgsql=(PGconn *)connection; + PGconn *conn=(PGconn *)connection; + #define PQclear_throwPQerror { \ + PQclear(res); \ + services._throw(PQerrorMessage(conn)); \ + } + #define PQclear_throw(msg) { \ + PQclear(res); \ + services._throw(msg); \ + } + const char *statement; if(offset || limit) { @@ -134,14 +176,13 @@ public: } else statement=astatement; - PGresult *res=PQexec(pgsql, statement); + PGresult *res=PQexec(conn, statement); if(!res) - services._throw(PQerrorMessage(pgsql)); + services._throw(PQerrorMessage(conn)); switch(PQresultStatus(res)) { case PGRES_EMPTY_QUERY: - PQclear(res); - services._throw("no query"); + PQclear_throw("no query"); break; case PGRES_COMMAND_OK: // empty result: insert|delete|update|... @@ -149,16 +190,13 @@ public: case PGRES_TUPLES_OK: break; default: - PQclear(res); - services._throw("unknown PQexec error"); + PQclear_throw("unknown PQexec error"); break; } int column_count=PQnfields(res); - if(!column_count) { - PQclear(res); - services._throw("result contains no columns"); - } + if(!column_count) + PQclear_throw("result contains no columns"); for(int i=0; i=0) { + // seek to end + if(lo_lseek(conn, fd, 0, SEEK_END)<0) + PQclear_throwPQerror; + // get size + int size_tell=lo_tell(conn, fd); + if(size_tell<0) + PQclear_throwPQerror; + // seek to begin + if(lo_lseek(conn, fd, 0, SEEK_SET)<0) + PQclear_throwPQerror; + size=(size_t)size_tell; + if(size) { + // read + ptr=services.malloc(size); + char *buf=(char *)ptr; + int countdown=size_tell; + int size_read; + while(countdown && (size_read=lo_read(conn, fd, buf, min(LO_BUFSIZE, countdown)))>0) { + buf+=size_read; + countdown-=size_read; + } + if(countdown) + PQclear_throw("lo_read can not read all bytes of object"); + } else + ptr=0; + if(lo_close(conn, fd)<0) + PQclear_throwPQerror; + } else + PQclear_throwPQerror; + } else { + // normal column, read it as ASCII string + size=(size_t)PQgetlength(res, r, i); + if(size) { + ptr=services.malloc(size); + memcpy(ptr, cell, size); + } else + ptr=0; + } handlers.add_row_cell(ptr, size); } } @@ -188,7 +268,16 @@ public: PQclear(res); } -private: // pgsql client library funcs +private: // private funcs + + void begin_transaction(SQL_Driver_services& services, PGconn *conn) { + if(PGresult *res=PQexec(conn, "BEGIN")) + PQclear(res); + else + services._throw(PQerrorMessage(conn)); + } + +private: // conn client library funcs typedef PGconn* (*t_PQsetdbLogin)( const char *pghost, @@ -216,8 +305,20 @@ private: // pgsql client library funcs 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 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, char *buf, size_t len); t_lo_read lo_read; + typedef int (*t_lo_write)(PGconn *conn, int fd, 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: // pgsql client library funcs linking +private: // conn client library funcs linking const char *dlink(const char *dlopen_file_spec) { lt_dlhandle handle=lt_dlopen(dlopen_file_spec); @@ -243,6 +344,12 @@ private: // pgsql client library funcs l DLINK(PQclear); DLINK(PQresultStatus); DLINK(PQexec); + DLINK(PQftype); + 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; }