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