|
|
| version 1.4, 2001/11/16 12:39:15 | version 1.33, 2008/12/21 00:14:25 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser PgSQL driver. | Parser PgSQL driver. |
| Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com) | Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf) | 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; } | |
| /** | #endif |
| PgSQL server driver | |
| */ | static char *lsplit(char *string, char delim){ |
| class PgSQL_Driver : public SQL_Driver { | if(string){ |
| public: | if(char *v=strchr(string, delim)){ |
| *v=0; | |
| PgSQL_Driver() : SQL_Driver() { | return v+1; |
| } | } |
| } | |
| /// get api version | return 0; |
| int api_version() { return SQL_DRIVER_API_VERSION; } | } |
| /// initialize driver by loading sql dynamic link library | |
| const char *initialize(char *dlopen_file_spec) { | static char *lsplit(char **string_ref, char delim){ |
| return dlopen_file_spec? | char *result=*string_ref; |
| dlink(dlopen_file_spec):"client library column is empty"; | char *next=lsplit(*string_ref, delim); |
| } | *string_ref=next; |
| return result; | |
| #define throwPQerror services._throw(PQerrorMessage(conn)) | } |
| /** connect | static char* rsplit(char* string, char delim){ |
| @param used_only_in_connect_url | if(string){ |
| format: @b user:pass@host[:port]|[local]/database | if(char* v=strrchr(string, delim)){ |
| */ | *v=0; |
| void connect( | return v+1; |
| char *used_only_in_connect_url, | } |
| SQL_Driver_services& services, | } |
| void **connection ///< output: PGconn * | return NULL; |
| ) { | } |
| char *user=used_only_in_connect_url; | |
| char *host=lsplit(user, '@'); | static void toupper_str(char *out, const char *in, size_t size){ |
| char *db=lsplit(host, '/'); | while(size--) |
| char *pwd=lsplit(user, ':'); | *out++=(char)toupper(*in++); |
| char *port=lsplit(host, ':'); | } |
| PGconn *conn=PQsetdbLogin( | struct Connection { |
| strcasecmp(host, "local")==0?NULL/* local Unix domain socket */:host, port, | SQL_Driver_services* services; |
| NULL, NULL, db, user, pwd); | |
| if(!conn) | PGconn *conn; |
| services._throw("PQsetdbLogin failed"); | const char* client_charset; |
| if(PQstatus(conn)!=CONNECTION_OK) | bool autocommit; |
| throwPQerror; | bool without_default_transactions; |
| }; | |
| *(PGconn **)connection=conn; | |
| begin_transaction(services, conn); | /** |
| } | PgSQL server driver |
| void disconnect(void *connection) { | */ |
| PQfinish((PGconn *)connection); | class PgSQL_Driver : public SQL_Driver { |
| } | public: |
| void commit(SQL_Driver_services& services, void *connection) { | |
| PGconn *conn=(PGconn *)connection; | PgSQL_Driver() : SQL_Driver() { |
| if(PGresult *res=PQexec(conn, "COMMIT")) | } |
| PQclear(res); | |
| else | /// get api version |
| throwPQerror; | int api_version(){ return SQL_DRIVER_API_VERSION; } |
| begin_transaction(services, conn); | |
| } | /// initialize driver by loading sql dynamic link library |
| void rollback(SQL_Driver_services& services, void *connection) { | const char *initialize(char *dlopen_file_spec){ |
| PGconn *conn=(PGconn *)connection; | return dlopen_file_spec? |
| if(PGresult *res=PQexec(conn, "ROLLBACK")) | dlink(dlopen_file_spec):"client library column is empty"; |
| PQclear(res); | } |
| else | |
| throwPQerror; | #define throwPQerror connection.services->_throw(PQerrorMessage(connection.conn)) |
| begin_transaction(services, conn); | #define PQclear_throw(msg) { \ |
| } | PQclear(res); \ |
| connection.services->_throw(msg); \ | |
| bool ping(SQL_Driver_services&, void *connection) { | } |
| return PQstatus((PGconn *)connection)==CONNECTION_OK; | #define PQclear_throwPQerror PQclear_throw(PQerrorMessage(connection.conn)) |
| } | |
| /** connect | |
| unsigned int quote( | @param url |
| SQL_Driver_services&, void *connection, | format: @b user:pass@host[:port]|[local]/database? |
| char *to, const char *from, unsigned int length) { | ClientCharset=charset& // transcode by parser |
| if(to) { // store mode | charset=value& // transcode by server with 'SET CLIENT_ENCODING=value' |
| unsigned int result=length; | datestyle=value& // 'SET DATESTYLE=value' available values are: ISO|SQL|Postgres|European|US|German [default=ISO] |
| while(length--) { | autocommit=1& // each transaction is commited automatically (default) |
| switch(*from) { | WithoutDefaultTransaction=0 // 1 -- disable any BEGIN TRAN/COMMIT/ROLLBACK [can NOT be used with autocommit option] |
| case '\'': // "'" -> "''" | */ |
| *to++='\''; result++; | void connect( |
| break; | char* url, |
| case '\\': // "\" -> "\\" | SQL_Driver_services& services, |
| *to++='\''; result++; | void** connection_ref ///< output: Connection* |
| break; | ){ |
| } | char* user=url; |
| *to++=*from++; | char* host=rsplit(user, '@'); |
| } | char* db=lsplit(host, '/'); |
| return result; | char* pwd=lsplit(user, ':'); |
| } else // estimate mode | char* port=lsplit(host, ':'); |
| return length*2; | |
| } | char *options=lsplit(db, '?'); |
| void query( | |
| SQL_Driver_services& services, void *connection, | char* charset=0; |
| const char *astatement, unsigned long offset, unsigned long limit, | char* datestyle=0; |
| SQL_Driver_query_event_handlers& handlers) { | |
| // _asm int 3; | Connection& connection=*(Connection *)services.malloc(sizeof(Connection)); |
| *connection_ref=&connection; | |
| PGconn *conn=(PGconn *)connection; | connection.services=&services; |
| #define PQclear_throw(msg) { \ | connection.client_charset=0; |
| PQclear(res); \ | connection.autocommit=true; |
| services._throw(msg); \ | connection.without_default_transactions=false; |
| } | |
| #define PQclear_throwPQerror PQclear_throw(PQerrorMessage(conn)) | connection.conn=PQsetdbLogin( |
| (host&&strcasecmp(host, "local")==0)?NULL/* local Unix domain socket */:host, port, | |
| const char *statement=preprocess_statement(services, conn, | NULL, NULL, db, user, pwd); |
| astatement, offset, limit); | |
| if(!connection.conn) | |
| PGresult *res=PQexec(conn, statement); | services._throw("PQsetdbLogin failed"); |
| if(!res) | |
| throwPQerror; | if(PQstatus(connection.conn)!=CONNECTION_OK) |
| throwPQerror; | |
| switch(PQresultStatus(res)) { | |
| case PGRES_EMPTY_QUERY: | while(options){ |
| PQclear_throw("no query"); | if(char *key=lsplit(&options, '&')){ |
| break; | if(*key){ |
| case PGRES_COMMAND_OK: | if(char *value=lsplit(key, '=')){ |
| // empty result: insert|delete|update|... | if(strcmp(key, "ClientCharset")==0){ |
| PQclear(res); | toupper_str(value, value, strlen(value)); |
| return; | connection.client_charset=value; |
| case PGRES_TUPLES_OK: | } else if(strcasecmp(key, "charset")==0){ |
| break; | charset=value; |
| default: | } else if(strcasecmp(key, "datestyle")==0){ |
| PQclear_throwPQerror; | datestyle=value; |
| break; | } else if(strcasecmp(key, "autocommit")==0){ |
| } | if(connection.without_default_transactions) |
| services._throw("options WithoutDefaultTransaction and autocommit can't be used together"); | |
| int column_count=PQnfields(res); | if(atoi(value)==0) |
| if(!column_count) | connection.autocommit=false; |
| PQclear_throw("result contains no columns"); | } else if(strcmp(key, "WithoutDefaultTransaction")==0){ |
| if(!connection.autocommit) | |
| for(int i=0; i<column_count; i++){ | services._throw("options WithoutDefaultTransaction and autocommit can't be used together"); |
| char *name=PQfname(res, i); | if(atoi(value)==1){ |
| size_t size=strlen(name); | connection.without_default_transactions=true; |
| void *ptr=services.malloc(size); | connection.autocommit=false; |
| memcpy(ptr, name, size); | } |
| handlers.add_column(ptr, size); | } else |
| } | services._throw("unknown connect option" /*key*/); |
| } else | |
| handlers.before_rows(); | services._throw("connect option without =value" /*key*/); |
| } | |
| if(unsigned long row_count=(unsigned long)PQntuples(res)) | } |
| for(unsigned long r=0; r<row_count; r++) { | } |
| handlers.add_row(); | |
| for(int i=0; i<column_count; i++){ | if(charset){ |
| const char *cell=PQgetvalue(res, r, i); | char statement[MAX_STRING]="SET CLIENT_ENCODING="; |
| size_t size; | strncat(statement, charset, MAX_STRING); |
| void *ptr; | |
| if(PQftype(res, i)==OIDOID) { | _execute_cmd(connection, statement); |
| // ObjectID column, read object bytes | } |
| char *error_pos=0; | if(datestyle){ |
| Oid oid=cell?atoi(cell):0; | char statement[MAX_STRING]="SET DATESTYLE="; |
| int fd=lo_open(conn, oid, INV_READ); | strncat(statement, datestyle, MAX_STRING); |
| if(fd>=0) { | |
| // seek to end | _execute_cmd(connection, statement); |
| if(lo_lseek(conn, fd, 0, SEEK_END)<0) | } |
| PQclear_throwPQerror; | |
| // get size | _transaction_begin(connection); |
| int size_tell=lo_tell(conn, fd); | } |
| if(size_tell<0) | |
| PQclear_throwPQerror; | void disconnect(void *aconnection){ |
| // seek to begin | Connection& connection=*static_cast<Connection*>(aconnection); |
| if(lo_lseek(conn, fd, 0, SEEK_SET)<0) | PQfinish(connection.conn); |
| PQclear_throwPQerror; | connection.conn=0; |
| size=(size_t)size_tell; | } |
| if(size) { | |
| // read | void commit(void *aconnection){ |
| ptr=services.malloc(size); | Connection& connection=*static_cast<Connection*>(aconnection); |
| if(!lo_read_ex(conn, fd, (const char *)ptr, size_tell)) | _transaction_commit(connection); |
| PQclear_throw("lo_read can not read all bytes of object"); | _transaction_begin(connection); |
| } else | } |
| ptr=0; | |
| if(lo_close(conn, fd)<0) | void rollback(void *aconnection){ |
| PQclear_throwPQerror; | Connection& connection=*static_cast<Connection*>(aconnection); |
| } else | _transaction_rollback(connection); |
| PQclear_throwPQerror; | _transaction_begin(connection); |
| } else { | } |
| // normal column, read it normally | |
| size=(size_t)PQgetlength(res, r, i); | bool ping(void *aconnection) { |
| if(size) { | Connection& connection=*static_cast<Connection*>(aconnection); |
| ptr=services.malloc(size); | return PQstatus(connection.conn)==CONNECTION_OK; |
| memcpy(ptr, cell, size); | } |
| } else | |
| ptr=0; | const char* quote(void *aconnection, const char *from, unsigned int length){ |
| } | Connection& connection=*static_cast<Connection*>(aconnection); |
| handlers.add_row_cell(ptr, size); | |
| } | char *result=(char*)connection.services->malloc_atomic(length*2+1); |
| } | int err=0; |
| PQescapeStringConn(connection.conn, result, from, length, &err); | |
| PQclear(res); | return result; |
| } | } |
| private: // private funcs | void query(void *aconnection, |
| const char *astatement, | |
| void begin_transaction(SQL_Driver_services& services, PGconn *conn) { | size_t placeholders_count, Placeholder* placeholders, |
| if(PGresult *res=PQexec(conn, "BEGIN")) | unsigned long offset, unsigned long limit, |
| PQclear(res); | SQL_Driver_query_event_handlers& handlers |
| else | ){ |
| throwPQerror; | Connection& connection=*static_cast<Connection*>(aconnection); |
| } | SQL_Driver_services& services=*connection.services; |
| PGconn *conn=connection.conn; | |
| const char *preprocess_statement(SQL_Driver_services& services, PGconn *conn, | |
| const char *astatement, unsigned long offset, unsigned long limit) { | const char* client_charset=connection.client_charset; |
| size_t statement_size=strlen(astatement); | const char* request_charset=services.request_charset(); |
| bool transcode_needed=client_charset && strcmp(client_charset, request_charset)!=0; | |
| char *result=(char *)services.malloc(statement_size | |
| +MAX_NUMBER*2+15 // limit # offset # | const char** paramValues; |
| +MAX_STRING // in case of short 'strings' | if(placeholders_count>0){ |
| +1); | int binds_size=sizeof(char)*placeholders_count; |
| // offset & limit -> suffixes | paramValues = static_cast<const char**>(services.malloc_atomic(binds_size)); |
| const char *o; | _bind_parameters(placeholders_count, placeholders, paramValues, connection, transcode_needed); |
| if(offset || limit) { | } |
| char *cur=result; | |
| memcpy(cur, astatement, statement_size); cur+=statement_size; | if(transcode_needed){ |
| if(limit) | // transcode query from $request:charset to ?ClientCharset |
| cur+=snprintf(cur, 7+MAX_NUMBER, " limit %u", limit); | size_t length=strlen(astatement); |
| if(offset) | services.transcode(astatement, length, |
| cur+=snprintf(cur, 8+MAX_NUMBER, " offset %u", offset); | astatement, length, |
| o=result; | request_charset, |
| } else | client_charset); |
| o=astatement; | } |
| // /**xxx**/'literal' -> oid | const char *statement=_preprocess_statement(connection, astatement, offset, limit); |
| char *n=result; | // error after prepare? |
| while(*o) { | |
| if( | PGresult *res; |
| o[0]=='/' && | if(placeholders_count>0){ |
| o[1]=='*' && | res=PQexecParams(conn, statement, placeholders_count, NULL, paramValues, NULL, NULL, 0); |
| o[2]=='*') { // name start | } else { |
| o+=3; | res=PQexec(conn, statement); |
| while(*o) | } |
| if( | if(!res) |
| o[0]=='*' && | throwPQerror; |
| o[1]=='*' && | |
| o[2]=='/' && | bool failed=false; |
| o[3]=='\'') { // name end | SQL_Error sql_error; |
| o+=4; | |
| Oid oid=lo_creat(conn, INV_READ|INV_WRITE); | switch(PQresultStatus(res)) { |
| if(oid==InvalidOid) | case PGRES_EMPTY_QUERY: |
| throwPQerror; | PQclear_throw("no query"); |
| int fd=lo_open(conn, oid, INV_WRITE); | break; |
| if(fd>=0) { | case PGRES_COMMAND_OK: // empty result: insert|delete|update|... |
| const char *start=o; | goto cleanup; |
| bool escaped=false; | break; |
| while(*o && !(o[0]=='\'' && o[1]!='\'' && !escaped)) { | case PGRES_TUPLES_OK: |
| escaped=*o=='\\' || (o[0]=='\'' && o[1]=='\''); | break; |
| if(escaped) { | default: |
| // write pending, skip "\" or "'" | PQclear_throwPQerror; |
| if(!lo_write_ex(conn, fd, start, o-start)) | break; |
| services._throw("lo_write could not write all bytes of object (1)"); | } |
| start=++o; | |
| } else | #define CHECK(afailed) \ |
| o++; | if(afailed) { \ |
| } | failed=true; \ |
| if(!lo_write_ex(conn, fd, start, o-start)) | goto cleanup; \ |
| services._throw("lo_write can not write all bytes of object (2)"); | } |
| if(lo_close(conn, fd)<0) | |
| throwPQerror; | int column_count=PQnfields(res); |
| } else | if(!column_count) |
| throwPQerror; | PQclear_throw("result contains no columns"); |
| if(*o) | |
| o++; // skip "'" | if(column_count>MAX_COLS) |
| column_count=MAX_COLS; | |
| n+=snprintf(n, MAX_NUMBER, "%u", oid); | |
| break; | unsigned int column_types[MAX_COLS]; |
| } else | bool transcode_column[MAX_COLS]; |
| o++; // /**skip**/'xxx' | |
| } else | for(int i=0; i<column_count; i++){ |
| *n++=*o++; | column_types[i]=PQftype(res, i); |
| } | switch(column_types[i]){ |
| *n=0; | case BOOLOID: |
| case INT8OID: | |
| return result; | case INT2OID: |
| } | case INT4OID: |
| case FLOAT4OID: | |
| private: // lo_read/write exchancements | case FLOAT8OID: |
| case DATEOID: | |
| bool lo_read_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) { | case TIMEOID: |
| int size_read; | case TIMESTAMPOID: |
| while(len && (size_read=lo_read(conn, fd, buf, min(LO_BUFSIZE, len)))>0) { | case TIMESTAMPTZOID: |
| buf+=size_read; | case TIMETZOID: |
| len-=size_read; | case NUMERICOID: |
| } | transcode_column[i]=false; |
| return len==0; | break; |
| } | default: |
| transcode_column[i]=transcode_needed; | |
| bool lo_write_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) { | break; |
| int size_written; | } |
| while(len && (size_written=lo_write(conn, fd, buf, min(LO_BUFSIZE, len)))>0) { | char *name=PQfname(res, i); |
| buf+=size_written; | size_t length=strlen(name); |
| len-=size_written; | char* strm=(char*)services.malloc(length+1); |
| } | memcpy(strm, name, length+1); |
| return len==0; | const char* str=strm; |
| } | |
| if(transcode_needed) // transcode column name from ?ClientCharset to $request:charset | |
| private: // conn client library funcs | services.transcode(str, length, |
| str, length, | |
| typedef PGconn* (*t_PQsetdbLogin)( | client_charset, |
| const char *pghost, | request_charset); |
| const char *pgport, | |
| const char *pgoptions, | CHECK(handlers.add_column(sql_error, str, length)); |
| const char *pgtty, | } |
| const char *dbName, | |
| const char *login, | CHECK(handlers.before_rows(sql_error)); |
| const char *pwd); t_PQsetdbLogin PQsetdbLogin; | |
| typedef void (*t_PQfinish)(PGconn *conn); t_PQfinish PQfinish; | if(unsigned long row_count=(unsigned long)PQntuples(res)) |
| typedef char *(*t_PQerrorMessage)(const PGconn* conn); t_PQerrorMessage PQerrorMessage; | for(unsigned long r=0; r<row_count; r++) { |
| typedef ConnStatusType (*t_PQstatus)(const PGconn *conn); t_PQstatus PQstatus; | CHECK(handlers.add_row(sql_error)); |
| typedef PGresult *(*t_PQexec)(PGconn *conn, | for(int i=0; i<column_count; i++){ |
| const char *query); t_PQexec PQexec; | const char *cell=PQgetvalue(res, r, i); |
| typedef ExecStatusType (*t_PQresultStatus)(const PGresult *res); t_PQresultStatus PQresultStatus; | size_t length; |
| typedef int (*t_PQgetlength)(const PGresult *res, | const char* str; |
| int tup_num, | |
| int field_num); t_PQgetlength PQgetlength; | switch(column_types[i]){ |
| typedef char* (*t_PQgetvalue)(const PGresult *res, | case OIDOID: |
| int tup_num, | { |
| int field_num); t_PQgetvalue PQgetvalue; | char *error_pos=0; |
| typedef int (*t_PQntuples)(const PGresult *res); t_PQntuples PQntuples; | Oid oid=cell?atoi(cell):0; |
| typedef char *(*t_PQfname)(const PGresult *res, | int fd=lo_open(conn, oid, INV_READ); |
| int field_index); t_PQfname PQfname; | if(fd>=0){ |
| typedef int (*t_PQnfields)(const PGresult *res); t_PQnfields PQnfields; | // seek to end |
| typedef void (*t_PQclear)(PGresult *res); t_PQclear PQclear; | if(lo_lseek(conn, fd, 0, SEEK_END)<0) |
| PQclear_throwPQerror; | |
| typedef Oid (*t_PQftype)(const PGresult *res, int field_num); t_PQftype PQftype; | // get length |
| int size_tell=lo_tell(conn, fd); | |
| typedef int (*t_lo_open)(PGconn *conn, Oid lobjId, int mode); t_lo_open lo_open; | if(size_tell<0) |
| typedef int (*t_lo_close)(PGconn *conn, int fd); t_lo_close lo_close; | PQclear_throwPQerror; |
| typedef int (*t_lo_read)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len); t_lo_read lo_read; | // seek to begin |
| typedef int (*t_lo_write)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len); t_lo_write lo_write; | if(lo_lseek(conn, fd, 0, SEEK_SET)<0) |
| typedef int (*t_lo_lseek)(PGconn *conn, int fd, int offset, int whence); t_lo_lseek lo_lseek; | PQclear_throwPQerror; |
| typedef Oid (*t_lo_creat)(PGconn *conn, int mode); t_lo_creat lo_creat; | length=(size_t)size_tell; |
| typedef int (*t_lo_tell)(PGconn *conn, int fd); t_lo_tell lo_tell; | if(length){ |
| typedef int (*t_lo_unlink)(PGconn *conn, Oid lobjId); t_lo_unlink lo_unlink; | // read |
| typedef Oid (*t_lo_import)(PGconn *conn, const char *filename); t_lo_import lo_import; | char* strm=(char*)services.malloc(length+1); |
| typedef int (*t_lo_export)(PGconn *conn, Oid lobjId, const char *filename); t_lo_export lo_export; | if(!lo_read_ex(conn, fd, strm, size_tell)) |
| PQclear_throw("lo_read can not read all bytes of object"); | |
| private: // conn client library funcs linking | strm[length]=0; |
| str=strm; | |
| const char *dlink(const char *dlopen_file_spec) { | } else |
| lt_dlhandle handle=lt_dlopen(dlopen_file_spec); | str=0; |
| if(!handle) | if(lo_close(conn, fd)<0) |
| return "can not open the dynamic link module"; | PQclear_throwPQerror; |
| } else | |
| #define DSLINK(name, action) \ | PQclear_throwPQerror; |
| name=(t_##name)lt_dlsym(handle, #name); \ | } |
| if(!name) \ | default: |
| action; | // normal column, read it normally |
| length=(size_t)PQgetlength(res, r, i); | |
| #define DLINK(name) DSLINK(name, return "function " #name " was not found") | if(length){ |
| char* strm=(char*)services.malloc(length+1); | |
| DLINK(PQsetdbLogin); | memcpy(strm, cell, length+1); |
| DLINK(PQerrorMessage); | str=strm; |
| DLINK(PQstatus); | } else |
| DLINK(PQfinish); | str=0; |
| DLINK(PQgetvalue); | } |
| DLINK(PQgetlength); | |
| DLINK(PQntuples); | if(str && length && transcode_column[i]){ |
| DLINK(PQfname); | // transcode cell value from ?ClientCharset to $request:charset |
| DLINK(PQnfields); | services.transcode(str, length, |
| DLINK(PQclear); | str, length, |
| DLINK(PQresultStatus); | client_charset, |
| DLINK(PQexec); | request_charset); |
| DLINK(PQftype); | } |
| DLINK(lo_open); DLINK(lo_close); | |
| DLINK(lo_read); DLINK(lo_write); | CHECK(handlers.add_row_cell(sql_error, str, length)); |
| DLINK(lo_lseek); DLINK(lo_creat); | } |
| DLINK(lo_tell); DLINK(lo_unlink); | } |
| DLINK(lo_import); DLINK(lo_export); | cleanup: |
| PQclear(res); | |
| return 0; | if(failed) |
| } | services._throw(sql_error); |
| }; | if(connection.autocommit) |
| commit(aconnection); | |
| extern "C" SQL_Driver *SQL_DRIVER_CREATE() { | } |
| return new PgSQL_Driver(); | |
| } | private: |
| void _bind_parameters( | |
| size_t placeholders_count, | |
| Placeholder* placeholders, | |
| const char** paramValues, | |
| Connection& connection, | |
| bool transcode_needed | |
| ){ | |
| for(size_t i=0; i<placeholders_count; i++){ | |
| Placeholder& ph=placeholders[i]; | |
| if(transcode_needed){ | |
| size_t name_length; | |
| size_t value_length; | |
| connection.services->transcode(ph.name, strlen(ph.name), | |
| ph.name, name_length, | |
| connection.services->request_charset(), | |
| connection.client_charset); | |
| if(ph.value) { | |
| connection.services->transcode(ph.value, strlen(ph.value), | |
| ph.value, value_length, | |
| connection.services->request_charset(), | |
| connection.client_charset); | |
| } | |
| } | |
| int name_numner=atoi(ph.name); | |
| if(name_numner <= 0 || name_numner > placeholders_count) | |
| connection.services->_throw("bad bind parameter key"); | |
| paramValues[name_numner-1]=ph.value; | |
| } | |
| } | |
| void _transaction_begin(Connection& connection){ | |
| _execute_transactions_cmd(connection, "BEGIN"); | |
| } | |
| void _transaction_commit(Connection& connection){ | |
| _execute_transactions_cmd(connection, "COMMIT"); | |
| } | |
| void _transaction_rollback(Connection& connection){ | |
| _execute_transactions_cmd(connection, "ROLLBACK"); | |
| } | |
| void _execute_transactions_cmd(const Connection& connection, const char *query){ | |
| if(!connection.without_default_transactions) // with option ?WithoutDefaultTransaction=1 user must execute BEGIN/COMMIT/ROLLBACK by himself | |
| _execute_cmd(connection, query); | |
| } | |
| // executes a query and throw away the result. | |
| void _execute_cmd(const Connection& connection, const char *query){ | |
| if(PGresult *res=PQexec(connection.conn, query)) | |
| PQclear(res); // throw away the result [don't need but must call] | |
| else | |
| throwPQerror; | |
| } | |
| const char *_preprocess_statement( | |
| Connection& connection, | |
| const char *astatement, | |
| unsigned long offset, | |
| unsigned long limit | |
| ){ | |
| PGconn *conn=connection.conn; | |
| size_t statement_size=strlen(astatement); | |
| char *result=(char *)connection.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!=SQL_NO_LIMIT){ | |
| char *cur=result; | |
| memcpy(cur, astatement, statement_size); cur+=statement_size; | |
| if(limit!=SQL_NO_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 | |
| const char* saved_o=o; | |
| o+=3; | |
| while(*o) | |
| if( | |
| o[0]=='*' && | |
| o[1]=='*' && | |
| o[2]=='/' && | |
| o[3]=='\'') { // name end | |
| saved_o=0; // found, marking that | |
| 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)) | |
| connection.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)) | |
| connection.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' | |
| if(saved_o) { | |
| o=saved_o; | |
| *n++=*o++; | |
| } | |
| } 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) { | |
| return lo_rw_method (conn, fd, buf, len, lo_read); | |
| } | |
| bool lo_write_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) { | |
| return lo_rw_method (conn, fd, buf, len, lo_write); | |
| } | |
| 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)) { | |
| int size_op; | |
| 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(); | |
| } |