|
|
| version 1.5, 2001/07/31 12:51:45 | version 1.6, 2001/07/31 14:22:00 |
|---|---|
| Line 124 public: | Line 124 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; |
| Line 149 public: | Line 144 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 *conn=(PGconn *)connection; | PGconn *conn=(PGconn *)connection; |
| #define PQclear_throwPQerror { \ | |
| PQclear(res); \ | |
| services._throw(PQerrorMessage(conn)); \ | |
| } | |
| #define PQclear_throw(msg) { \ | #define PQclear_throw(msg) { \ |
| PQclear(res); \ | PQclear(res); \ |
| services._throw(msg); \ | 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(conn, statement); | PGresult *res=PQexec(conn, statement); |
| if(!res) | if(!res) |
| Line 186 public: | Line 166 public: |
| 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_throw("unknown PQexec error"); | PQclear_throwPQerror; |
| break; | break; |
| } | } |
| Line 277 private: // private funcs | Line 258 private: // private funcs |
| services._throw(PQerrorMessage(conn)); | services._throw(PQerrorMessage(conn)); |
| } | } |
| 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 # | |
| +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; | |
| // /*:zzzz*/'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]=='\'') { // name end | |
| o+=3; | |
| Oid oid=lo_creat(conn, INV_READ|INV_WRITE); | |
| 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(*o) | |
| o++; // skip "'" | |
| n+=snprintf(n, MAX_NUMBER, "%u", oid); | |
| break; | |
| } else | |
| o++; // /*:skip*/ | |
| } else | |
| *n++=*o++; | |
| } | |
| *n=0; | |
| return result; | |
| } | |
| private: // conn client library funcs | private: // conn client library funcs |
| typedef PGconn* (*t_PQsetdbLogin)( | typedef PGconn* (*t_PQsetdbLogin)( |
| Line 309 private: // conn client library funcs | Line 358 private: // conn client library funcs |
| typedef int (*t_lo_open)(PGconn *conn, Oid lobjId, int mode); t_lo_open lo_open; | 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_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_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, char *buf, size_t len); t_lo_write lo_write; | 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 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 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_tell)(PGconn *conn, int fd); t_lo_tell lo_tell; |