--- sql/pgsql/parser3pgsql.C 2008/07/01 13:40:33 1.31 +++ sql/pgsql/parser3pgsql.C 2012/10/19 04:13:28 1.42 @@ -1,13 +1,12 @@ /** @file Parser PgSQL driver. - Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) 2007.10.25 using PgSQL 8.1.5 */ -static const char *RCSId="$Id: parser3pgsql.C,v 1.31 2008/07/01 13:40:33 misha Exp $"; #include "config_includes.h" @@ -16,6 +15,8 @@ static const char *RCSId="$Id: parser3pg #include #include +volatile const char * IDENT_PARSER3PGSQL_C="$Id: parser3pgsql.C,v 1.42 2012/10/19 04:13:28 misha Exp $" IDENT_PA_SQL_DRIVER_H; + // from catalog/pg_type.h #define BOOLOID 16 #define INT8OID 20 @@ -85,6 +86,13 @@ static void toupper_str(char *out, const *out++=(char)toupper(*in++); } +inline static const char* strdup(SQL_Driver_services& services, char* str, size_t length) { + char *strm=(char*)services.malloc_atomic(length+1); + memcpy(strm, str, length); + strm[length]=0; + return (const char*)strm; +} + struct Connection { SQL_Driver_services* services; @@ -92,6 +100,7 @@ struct Connection { const char* client_charset; bool autocommit; bool without_default_transactions; + bool standard_conforming_strings; }; /** @@ -126,7 +135,7 @@ public: 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] 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( char* url, @@ -150,6 +159,7 @@ public: connection.client_charset=0; connection.autocommit=true; connection.without_default_transactions=false; + connection.standard_conforming_strings=true; connection.conn=PQsetdbLogin( (host&&strcasecmp(host, "local")==0)?NULL/* local Unix domain socket */:host, port, @@ -184,6 +194,9 @@ public: connection.without_default_transactions=true; connection.autocommit=false; } + } else if(strcasecmp(key, "standard_conforming_strings")==0){ + if(atoi(value)==0) + connection.standard_conforming_strings=false; } else services._throw("unknown connect option" /*key*/); } else @@ -193,20 +206,20 @@ public: } if(charset){ - char statement[MAX_STRING]="SET CLIENT_ENCODING="; + char statement[MAX_STRING+1]="SET CLIENT_ENCODING="; strncat(statement, charset, MAX_STRING); _execute_cmd(connection, statement); } if(datestyle){ - char statement[MAX_STRING]="SET DATESTYLE="; + char statement[MAX_STRING+1]="SET DATESTYLE="; strncat(statement, datestyle, MAX_STRING); _execute_cmd(connection, statement); } - _begin_transaction(connection); + _transaction_begin(connection); } void disconnect(void *aconnection){ @@ -217,18 +230,14 @@ public: void commit(void *aconnection){ Connection& connection=*static_cast(aconnection); - if(!connection.without_default_transactions){ - _execute_cmd(connection, "COMMIT"); - } - _begin_transaction(connection); + _transaction_commit(connection); + _transaction_begin(connection); } void rollback(void *aconnection){ Connection& connection=*static_cast(aconnection); - if(!connection.without_default_transactions){ - _execute_cmd(connection, "ROLLBACK"); - } - _begin_transaction(connection); + _transaction_rollback(connection); + _transaction_begin(connection); } bool ping(void *aconnection) { @@ -236,15 +245,62 @@ public: return PQstatus(connection.conn)==CONNECTION_OK; } - const char* quote(void *aconnection, const char *from, unsigned int length){ + // 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) + { Connection& connection=*static_cast(aconnection); - char *result=(char*)connection.services->malloc_atomic(length*2+1); - int err=0; - PQescapeStringConn(connection.conn, result, from, length, &err); + const char* from; + const char* from_end=str+length; + + size_t quoted=0; + + if(connection.standard_conforming_strings){ + for(from=str; frommalloc_atomic(length + quoted + 1); + char *to = result; + + if(connection.standard_conforming_strings){ + for(from=str; from "''" + *to++=*from; + } + } else { + for(from=str; from "''" + *to++= '\''; + break; + case '\\': // "\" -> "\\" + *to++='\\'; + break; + } + *to++=*from; + } + } + + *to=0; return result; } - + void query(void *aconnection, const char *astatement, size_t placeholders_count, Placeholder* placeholders, @@ -266,16 +322,17 @@ public: _bind_parameters(placeholders_count, placeholders, paramValues, connection, transcode_needed); } + size_t statement_size=0; if(transcode_needed){ // transcode query from $request:charset to ?ClientCharset - size_t length=strlen(astatement); - services.transcode(astatement, length, - astatement, length, + statement_size=strlen(astatement); + services.transcode(astatement, statement_size, + astatement, statement_size, request_charset, client_charset); } - const char *statement=_preprocess_statement(connection, astatement, offset, limit); + const char *statement=_preprocess_statement(connection, astatement, statement_size, offset, limit); // error after prepare? PGresult *res; @@ -287,12 +344,17 @@ public: if(!res) throwPQerror; + bool failed=false; + SQL_Error sql_error; + switch(PQresultStatus(res)) { case PGRES_EMPTY_QUERY: PQclear_throw("no query"); break; case PGRES_COMMAND_OK: // empty result: insert|delete|update|... PQclear(res); + if(connection.autocommit) + commit(aconnection); return; case PGRES_TUPLES_OK: break; @@ -301,52 +363,29 @@ public: break; } - int column_count=PQnfields(res); - if(!column_count) - PQclear_throw("result contains no columns"); - - bool failed=false; - SQL_Error sql_error; #define CHECK(afailed) \ if(afailed) { \ failed=true; \ goto cleanup; \ } + size_t column_count=PQnfields(res); + if(!column_count) + PQclear_throw("result contains no columns"); + if(column_count>MAX_COLS) column_count=MAX_COLS; unsigned int column_types[MAX_COLS]; - bool transcode_column[MAX_COLS]; - for(int i=0; i=0){ @@ -391,33 +446,34 @@ public: PQclear_throw("lo_read can not read all bytes of object"); strm[length]=0; str=strm; + if(transcode_needed) { + // transcode cell value from ?ClientCharset to $request:charset + services.transcode(str, length, + str, length, + client_charset, + request_charset); + } } else str=0; if(lo_close(conn, fd)<0) PQclear_throwPQerror; } else PQclear_throwPQerror; + break; } default: // normal column, read it normally length=(size_t)PQgetlength(res, r, i); - if(length){ - char* strm=(char*)services.malloc(length+1); - memcpy(strm, cell, length+1); - str=strm; - } else - str=0; - } - - if(str && length && transcode_column[i]){ - //services._throw("tr"); - // transcode cell value from ?ClientCharset to $request:charset - services.transcode(str, length, - str, length, - client_charset, - request_charset); + str=length ? strdup(services, cell, length) : 0; + if(transcode_needed) { + // transcode cell value from ?ClientCharset to $request:charset + services.transcode(str, length, + str, length, + client_charset, + request_charset); + } + break; } - CHECK(handlers.add_row_cell(sql_error, str, length)); } } @@ -426,7 +482,8 @@ cleanup: if(failed) services._throw(sql_error); - commit(aconnection); + if(connection.autocommit) + commit(aconnection); } private: @@ -441,55 +498,67 @@ private: Placeholder& ph=placeholders[i]; if(transcode_needed){ size_t name_length; - size_t value_length; connection.services->transcode(ph.name, strlen(ph.name), ph.name, name_length, connection.services->request_charset(), connection.client_charset); if(ph.value) { + size_t value_length; connection.services->transcode(ph.value, strlen(ph.value), ph.value, value_length, connection.services->request_charset(), connection.client_charset); } } - int name_numner=atoi(ph.name); - if(name_numner <= 0 || name_numner > placeholders_count) + int name_number=atoi(ph.name); + if(name_number <= 0 || (size_t)name_number > placeholders_count) connection.services->_throw("bad bind parameter key"); - paramValues[name_numner-1]=ph.value; + paramValues[name_number-1]=ph.value; } } - /** - Executes a query and throw away the result. - */ + void _transaction_begin(Connection& connection){ + _execute_transactions_cmd(connection, "BEGIN"); + } + + void _transaction_commit(Connection& connection){ + _execute_transactions_cmd(connection, "COMMIT"); + } + + void _transaction_rollback(Connection& connection){ + _execute_transactions_cmd(connection, "ROLLBACK"); + } + + void _execute_transactions_cmd(const Connection& connection, const char *query){ + if(!connection.without_default_transactions) // with option ?WithoutDefaultTransaction=1 user must execute BEGIN/COMMIT/ROLLBACK by himself + _execute_cmd(connection, query); + } + + // executes a query and throw away the result. void _execute_cmd(const Connection& connection, const char *query){ if(PGresult *res=PQexec(connection.conn, query)) - PQclear(res); // throw out the result [don't need but must call] + PQclear(res); // throw away the result [don't need but must call] else throwPQerror; } - void _begin_transaction(Connection& connection){ - if(!connection.without_default_transactions) - _execute_cmd(connection, "BEGIN"); - } - const char *_preprocess_statement( Connection& connection, const char *astatement, + size_t statement_size, unsigned long offset, unsigned long limit ){ PGconn *conn=connection.conn; - size_t statement_size=strlen(astatement); + if(!statement_size) + statement_size=strlen(astatement); char *result=(char *)connection.services->malloc(statement_size - +MAX_NUMBER*2+15 // limit # offset # + +MAX_NUMBER*2+15 // " limit # offset #" +MAX_STRING // in case of short 'strings' +1); // offset & limit -> suffixes @@ -498,9 +567,9 @@ private: char *cur=result; memcpy(cur, astatement, statement_size); cur+=statement_size; if(limit!=SQL_NO_LIMIT) - cur+=snprintf(cur, 7+MAX_NUMBER, " limit %u", limit); + cur+=snprintf(cur, 7+MAX_NUMBER, " limit %lu", limit); if(offset) - cur+=snprintf(cur, 8+MAX_NUMBER, " offset %u", offset); + cur+=snprintf(cur, 8+MAX_NUMBER, " offset %lu", offset); o=result; } else o=astatement; @@ -510,7 +579,7 @@ private: while(*o) { if( o[0]=='/' && - o[1]=='*' && + o[1]=='*' && o[2]=='*') { // name start const char* saved_o=o; o+=3; @@ -641,11 +710,19 @@ private: // conn client library funcs private: // conn client library funcs linking const char *dlink(const char *dlopen_file_spec) { - if(lt_dlinit()) - return lt_dlerror(); + if(lt_dlinit()){ + if(const char* result=lt_dlerror()) + return result; + return "can not prepare to dynamic loading"; + } + lt_dlhandle handle=lt_dlopen(dlopen_file_spec); - if(!handle) + + if(!handle){ + if(const char* result=lt_dlerror()) + return result; return "can not open the dynamic link module"; + } #define DSLINK(name, action) \ name=(t_##name)lt_dlsym(handle, #name); \