|
|
| version 1.2, 2001/07/31 07:47:08 | version 1.10, 2001/08/01 09:49:18 |
|---|---|
| Line 14 static const char *RCSId="$Id$"; | Line 14 static const char *RCSId="$Id$"; |
| #include "pa_sql_driver.h" | #include "pa_sql_driver.h" |
| #include <libpq-fe.h> | #include <libpq-fe.h> |
| #include <libpq/libpq-fs.h> | |
| // OIDOID from #include <catalog/pg_type.h> | |
| #define OIDOID 26 | |
| // LO_BUFSIZE from interfaces\libpq\fe-lobj.c = 8192 (0x2000) | |
| // actually writing chunks of that size failed, reduced it twice | |
| #define LO_BUFSIZE 0x1000 | |
| #include "ltdl.h" | #include "ltdl.h" |
| #define MAX_STRING 0x400 | #define MAX_STRING 0x400 |
| Line 24 static const char *RCSId="$Id$"; | Line 32 static const char *RCSId="$Id$"; |
| # define strcasecmp _stricmp | # define strcasecmp _stricmp |
| #endif | #endif |
| #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; } | |
| #endif | |
| static char *lsplit(char *string, char delim) { | static char *lsplit(char *string, char delim) { |
| if(string) { | if(string) { |
| char *v=strchr(string, delim); | char *v=strchr(string, delim); |
| Line 51 public: | Line 64 public: |
| return dlopen_file_spec? | return dlopen_file_spec? |
| dlink(dlopen_file_spec):"client library column is empty"; | dlink(dlopen_file_spec):"client library column is empty"; |
| } | } |
| #define throwPQerror services._throw(PQerrorMessage(conn)) | |
| /** connect | /** connect |
| @param used_only_in_connect_url | @param used_only_in_connect_url |
| format: @b user:pass@host[:port]|[local]/database | 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( | void connect( |
| char *used_only_in_connect_url, | char *used_only_in_connect_url, |
| Line 69 public: | Line 82 public: |
| char *pwd=lsplit(user, ':'); | char *pwd=lsplit(user, ':'); |
| char *port=lsplit(host, ':'); | char *port=lsplit(host, ':'); |
| // _asm int 3; | PGconn *conn=PQsetdbLogin( |
| PGconn *pgsql=PQsetdbLogin( | |
| strcasecmp(host, "local")==0?NULL/* local Unix domain socket */:host, port, | strcasecmp(host, "local")==0?NULL/* local Unix domain socket */:host, port, |
| NULL, NULL, db, user, pwd); | NULL, NULL, db, user, pwd); |
| if(!pgsql) | if(!conn) |
| services._throw("PQsetdbLogin failed"); | services._throw("PQsetdbLogin failed"); |
| if(PQstatus(pgsql)!=CONNECTION_OK) | if(PQstatus(conn)!=CONNECTION_OK) |
| services._throw(PQerrorMessage(pgsql)); | throwPQerror; |
| *(PGconn **)connection=pgsql; | *(PGconn **)connection=conn; |
| begin_transaction(services, conn); | |
| } | } |
| void disconnect(SQL_Driver_services&, void *connection) { | void disconnect(SQL_Driver_services&, void *connection) { |
| PQfinish((PGconn *)connection); | PQfinish((PGconn *)connection); |
| } | } |
| void commit(SQL_Driver_services&, void *) {} | void commit(SQL_Driver_services& services, void *connection) { |
| void rollback(SQL_Driver_services&, void *) {} | PGconn *conn=(PGconn *)connection; |
| if(PGresult *res=PQexec(conn, "COMMIT")) | |
| PQclear(res); | |
| else | |
| throwPQerror; | |
| begin_transaction(services, conn); | |
| } | |
| void rollback(SQL_Driver_services& services, void *connection) { | |
| PGconn *conn=(PGconn *)connection; | |
| if(PGresult *res=PQexec(conn, "ROLLBACK")) | |
| PQclear(res); | |
| else | |
| throwPQerror; | |
| begin_transaction(services, conn); | |
| } | |
| bool ping(SQL_Driver_services&, void *connection) { | bool ping(SQL_Driver_services&, void *connection) { |
| return PQstatus((PGconn *)connection)==CONNECTION_OK; | return PQstatus((PGconn *)connection)==CONNECTION_OK; |
| Line 94 public: | Line 121 public: |
| SQL_Driver_services&, void *connection, | SQL_Driver_services&, void *connection, |
| char *to, const char *from, unsigned int length) { | char *to, const char *from, unsigned int length) { |
| /* | /* |
| 3.23.22b | |
| You must allocate the to buffer to be at least length*2+1 bytes long. | |
| (In the worse case, each character may need to be encoded as using two bytes, | |
| and you need room for the terminating null byte.) | |
| it's already UNTAINT_TIMES_BIGGER | it's already UNTAINT_TIMES_BIGGER |
| */ | */ |
| // ' -> '' | |
| unsigned int result=length; | unsigned int result=length; |
| while(length--) { | while(length--) { |
| if(*from=='\'') | switch(*from) { |
| case '\'': // "'" -> "''" | |
| *to++='\''; | *to++='\''; |
| break; | |
| case '\\': // "\" -> "\\" | |
| *to++='\''; | |
| break; | |
| } | |
| *to++=*from++; | *to++=*from++; |
| } | } |
| return result; | return result; |
| Line 114 public: | Line 141 public: |
| SQL_Driver_services& services, void *connection, | SQL_Driver_services& services, void *connection, |
| const char *astatement, unsigned long offset, unsigned long limit, | const char *astatement, unsigned long offset, unsigned long limit, |
| SQL_Driver_query_event_handlers& handlers) { | SQL_Driver_query_event_handlers& handlers) { |
| // _asm int 3; | |
| PGconn *pgsql=(PGconn *)connection; | PGconn *conn=(PGconn *)connection; |
| #define PQclear_throw(msg) { \ | |
| PQclear(res); \ | |
| services._throw(msg); \ | |
| } | |
| #define PQclear_throwPQerror PQclear_throw(PQerrorMessage(conn)) | |
| const char *statement; | const char *statement=preprocess_statement(services, conn, |
| if(offset || limit) { | astatement, offset, limit); |
| size_t statement_size=strlen(astatement); | |
| char *statement_limited=(char *)services.malloc( | |
| statement_size+MAX_NUMBER*2+15/* limit # offset #*/+1); | |
| char *cur=statement_limited; | |
| memcpy(cur, astatement, statement_size); cur+=statement_size; | |
| if(limit) | |
| cur+=snprintf(cur, 7+MAX_NUMBER, " limit %u", limit); | |
| if(offset) | |
| cur+=snprintf(cur, 8+MAX_NUMBER, " offset %u", offset); | |
| statement=statement_limited; | |
| } else | |
| statement=astatement; | |
| PGresult *res=PQexec(pgsql, statement); | PGresult *res=PQexec(conn, statement); |
| if(!res) | if(!res) |
| services._throw(PQerrorMessage(pgsql)); | throwPQerror; |
| switch(PQresultStatus(res)) { | switch(PQresultStatus(res)) { |
| case PGRES_EMPTY_QUERY: | case PGRES_EMPTY_QUERY: |
| PQclear(res); | PQclear_throw("no query"); |
| services._throw("no query"); | |
| break; | break; |
| case PGRES_COMMAND_OK: | case PGRES_COMMAND_OK: |
| // empty result: insert|delete|update|... | // empty result: insert|delete|update|... |
| PQclear(res); | |
| return; | return; |
| case PGRES_TUPLES_OK: | case PGRES_TUPLES_OK: |
| break; | break; |
| default: | default: |
| PQclear(res); | PQclear_throwPQerror; |
| services._throw("unknown PQexec error"); | |
| break; | break; |
| } | } |
| int column_count=PQnfields(res); | int column_count=PQnfields(res); |
| if(!column_count) { | if(!column_count) |
| PQclear(res); | PQclear_throw("result contains no columns"); |
| services._throw("result contains no columns"); | |
| } | |
| for(int i=0; i<column_count; i++){ | for(int i=0; i<column_count; i++){ |
| char *name=PQfname(res, i); | char *name=PQfname(res, i); |
| Line 167 public: | Line 185 public: |
| } | } |
| handlers.before_rows(); | handlers.before_rows(); |
| if(unsigned long row_count=(unsigned long)PQntuples(res)) | if(unsigned long row_count=(unsigned long)PQntuples(res)) |
| for(unsigned long r=0; r<row_count; r++) { | for(unsigned long r=0; r<row_count; r++) { |
| handlers.add_row(); | handlers.add_row(); |
| for(int i=0; i<column_count; i++){ | for(int i=0; i<column_count; i++){ |
| size_t size=(size_t)PQgetlength(res, r, i); | const char *cell=PQgetvalue(res, r, i); |
| size_t size; | |
| void *ptr; | void *ptr; |
| if(size) { | if(PQftype(res, i)==OIDOID) { |
| ptr=services.malloc(size); | // ObjectID column, read object bytes |
| memcpy(ptr, PQgetvalue(res, r, i), size); | |
| } else | char *error_pos=0; |
| ptr=0; | Oid oid=cell?atoi(cell):0; |
| int fd=lo_open(conn, oid, INV_READ); | |
| if(fd>=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); | |
| 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; | |
| if(lo_close(conn, fd)<0) | |
| PQclear_throwPQerror; | |
| } else | |
| PQclear_throwPQerror; | |
| } else { | |
| // normal column, read it normally | |
| 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); | handlers.add_row_cell(ptr, size); |
| } | } |
| } | } |
| Line 186 public: | Line 238 public: |
| PQclear(res); | 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 | |
| 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); | |
| char *result=(char *)services.malloc(statement_size | |
| +MAX_NUMBER*2+15 // limit # offset # | |
| +MAX_STRING // in case of short 'strings' | |
| +1); | |
| // offset & limit -> suffixes | |
| const char *o; | |
| if(offset || limit) { | |
| char *cur=result; | |
| memcpy(cur, astatement, statement_size); cur+=statement_size; | |
| if(limit) | |
| cur+=snprintf(cur, 7+MAX_NUMBER, " limit %u", limit); | |
| if(offset) | |
| cur+=snprintf(cur, 8+MAX_NUMBER, " offset %u", offset); | |
| o=result; | |
| } else | |
| o=astatement; | |
| // /**xxx**/'literal' -> oid | |
| char *n=result; | |
| while(*o) { | |
| if( | |
| o[0]=='/' && | |
| o[1]=='*' && | |
| o[2]=='*') { // name start | |
| o+=3; | |
| while(*o) | |
| if( | |
| o[0]=='*' && | |
| 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); | |
| 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**/'xxx' | |
| } else | |
| *n++=*o++; | |
| } | |
| *n=0; | |
| 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)( | typedef PGconn* (*t_PQsetdbLogin)( |
| const char *pghost, | const char *pghost, |
| Line 214 private: // pgsql client library funcs | Line 369 private: // pgsql client library funcs |
| typedef int (*t_PQnfields)(const PGresult *res); t_PQnfields PQnfields; | typedef int (*t_PQnfields)(const PGresult *res); t_PQnfields PQnfields; |
| typedef void (*t_PQclear)(PGresult *res); t_PQclear PQclear; | 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, 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: // pgsql client library funcs linking | private: // conn client library funcs linking |
| const char *dlink(const char *dlopen_file_spec) { | const char *dlink(const char *dlopen_file_spec) { |
| lt_dlhandle handle=lt_dlopen(dlopen_file_spec); | lt_dlhandle handle=lt_dlopen(dlopen_file_spec); |
| Line 241 private: // pgsql client library funcs l | Line 408 private: // pgsql client library funcs l |
| DLINK(PQclear); | DLINK(PQclear); |
| DLINK(PQresultStatus); | DLINK(PQresultStatus); |
| DLINK(PQexec); | 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; | return 0; |
| } | } |