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