--- parser3/src/sql/pgsql/Attic/parser3pgsql.C 2001/07/31 14:22:00 1.6 +++ parser3/src/sql/pgsql/Attic/parser3pgsql.C 2001/09/05 08:57:43 1.13 @@ -7,18 +7,22 @@ 2001.07.30 using PgSQL 7.1.2 */ -static const char *RCSId="$Id: parser3pgsql.C,v 1.6 2001/07/31 14:22:00 parser Exp $"; +static const char *RCSId="$Id: parser3pgsql.C,v 1.13 2001/09/05 08:57:43 parser Exp $"; #include "config_includes.h" #include "pa_sql_driver.h" #include -#include +#include -// #include +// OIDOID from catalog/pg_type.h #define OIDOID 26 -#define LO_BUFSIZE 8192 +// LO_BUFSIZE from interfaces\libpq\fe-lobj.c = 8192 (0x2000) +// actually writing chunks of that size failed, reduced it twice +#define LO_BUFSIZE 0x1000 +// from postgres_ext.h +#define InvalidOid ((Oid) 0) #include "ltdl.h" @@ -63,12 +67,12 @@ public: return dlopen_file_spec? dlink(dlopen_file_spec):"client library column is empty"; } + + #define throwPQerror services._throw(PQerrorMessage(conn)) + /** connect @param used_only_in_connect_url format: @b user:pass@host[:port]|[local]/database - 3.23.22b - Currently the only option for @b character_set_name is cp1251_koi8. - WARNING: must be used only to connect, for buffer doesn't live long */ void connect( char *used_only_in_connect_url, @@ -81,38 +85,34 @@ public: char *pwd=lsplit(user, ':'); char *port=lsplit(host, ':'); -// _asm int 3; PGconn *conn=PQsetdbLogin( strcasecmp(host, "local")==0?NULL/* local Unix domain socket */:host, port, NULL, NULL, db, user, pwd); if(!conn) services._throw("PQsetdbLogin failed"); if(PQstatus(conn)!=CONNECTION_OK) - services._throw(PQerrorMessage(conn)); + throwPQerror; *(PGconn **)connection=conn; begin_transaction(services, conn); } - void disconnect(SQL_Driver_services&, void *connection) { -// _asm int 3; + void disconnect(void *connection) { PQfinish((PGconn *)connection); } void commit(SQL_Driver_services& services, void *connection) { -// _asm int 3; PGconn *conn=(PGconn *)connection; if(PGresult *res=PQexec(conn, "COMMIT")) PQclear(res); else - services._throw(PQerrorMessage(conn)); + throwPQerror; begin_transaction(services, conn); } void rollback(SQL_Driver_services& services, void *connection) { -// _asm int 3; PGconn *conn=(PGconn *)connection; if(PGresult *res=PQexec(conn, "ROLLBACK")) PQclear(res); else - services._throw(PQerrorMessage(conn)); + throwPQerror; begin_transaction(services, conn); } @@ -158,7 +158,7 @@ public: PGresult *res=PQexec(conn, statement); if(!res) - services._throw(PQerrorMessage(conn)); + throwPQerror; switch(PQresultStatus(res)) { case PGRES_EMPTY_QUERY: @@ -198,7 +198,6 @@ public: void *ptr; if(PQftype(res, i)==OIDOID) { // ObjectID column, read object bytes -// _asm int 3; char *error_pos=0; Oid oid=cell?atoi(cell):0; @@ -218,14 +217,7 @@ public: 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) + if(!lo_read_ex(conn, fd, (const char *)ptr, size_tell)) PQclear_throw("lo_read can not read all bytes of object"); } else ptr=0; @@ -234,7 +226,7 @@ public: } else PQclear_throwPQerror; } else { - // normal column, read it as ASCII string + // normal column, read it normally size=(size_t)PQgetlength(res, r, i); if(size) { ptr=services.malloc(size); @@ -255,13 +247,12 @@ private: // private funcs if(PGresult *res=PQexec(conn, "BEGIN")) PQclear(res); else - services._throw(PQerrorMessage(conn)); + throwPQerror; } const char *preprocess_statement(SQL_Driver_services& services, PGconn *conn, const char *astatement, unsigned long offset, unsigned long limit) { size_t statement_size=strlen(astatement); - //_asm int 3; char *result=(char *)services.malloc(statement_size +MAX_NUMBER*2+15 // limit # offset # @@ -280,44 +271,51 @@ private: // private funcs } else o=astatement; - // /*:zzzz*/'literal' -> oid + // /**xxx**/'literal' -> oid char *n=result; while(*o) { if( o[0]=='/' && o[1]=='*' && - o[2]==':') { // name start + o[2]=='*') { // name start o+=3; while(*o) if( o[0]=='*' && - o[1]=='/' && - o[2]=='\'') { // name end - o+=3; + o[1]=='*' && + o[2]=='/' && + o[3]=='\'') { // name end + o+=4; Oid oid=lo_creat(conn, INV_READ|INV_WRITE); + if(oid==InvalidOid) + throwPQerror; int fd=lo_open(conn, oid, INV_WRITE); - const char *start=o; - bool escaped=false; - while(*o && !(o[0]=='\'' && o[1]!='\'' && !escaped)) { - escaped=*o=='\\' || (o[0]=='\'' && o[1]=='\''); - if(escaped) { - // write pending, skip "\" or "'" - if(o!=start) - lo_write(conn, fd, start, o-start); - start=++o; - } else - o++; - } - if(o!=start) - lo_write(conn, fd, start, o-start); - lo_close(conn, fd); + if(fd>=0) { + const char *start=o; + bool escaped=false; + while(*o && !(o[0]=='\'' && o[1]!='\'' && !escaped)) { + escaped=*o=='\\' || (o[0]=='\'' && o[1]=='\''); + if(escaped) { + // write pending, skip "\" or "'" + if(!lo_write_ex(conn, fd, start, o-start)) + services._throw("lo_write could not write all bytes of object (1)"); + start=++o; + } else + o++; + } + if(!lo_write_ex(conn, fd, start, o-start)) + services._throw("lo_write can not write all bytes of object (2)"); + if(lo_close(conn, fd)<0) + throwPQerror; + } else + throwPQerror; if(*o) o++; // skip "'" n+=snprintf(n, MAX_NUMBER, "%u", oid); break; } else - o++; // /*:skip*/ + o++; // /**skip**/'xxx' } else *n++=*o++; } @@ -326,6 +324,26 @@ private: // private funcs return result; } +private: // lo_read/write exchancements + + bool lo_read_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) { + int size_read; + while(len && (size_read=lo_read(conn, fd, buf, min(LO_BUFSIZE, len)))>0) { + buf+=size_read; + len-=size_read; + } + return len==0; + } + + bool lo_write_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) { + int size_written; + while(len && (size_written=lo_write(conn, fd, buf, min(LO_BUFSIZE, len)))>0) { + buf+=size_written; + len-=size_written; + } + return len==0; + } + private: // conn client library funcs typedef PGconn* (*t_PQsetdbLogin)( @@ -405,6 +423,6 @@ private: // conn client library funcs li }; -extern "C" SQL_Driver *create() { +extern "C" SQL_Driver *SQL_DRIVER_CREATE_FUNC_NAME() { return new PgSQL_Driver(); } \ No newline at end of file