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