--- sql/pgsql/parser3pgsql.C 2008/12/25 02:33:56 1.34 +++ sql/pgsql/parser3pgsql.C 2019/11/30 22:11:11 1.46 @@ -1,13 +1,12 @@ /** @file 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 (http://paf.design.ru) 2007.10.25 using PgSQL 8.1.5 */ -static const char *RCSId="$Id: parser3pgsql.C,v 1.34 2008/12/25 02:33:56 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.46 2019/11/30 22:11:11 moko Exp $" IDENT_PA_SQL_DRIVER_H; + // from catalog/pg_type.h #define BOOLOID 16 #define INT8OID 20 @@ -85,13 +86,21 @@ 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; PGconn *conn; const char* client_charset; bool autocommit; - bool without_default_transactions; + bool with_default_transactions; + bool standard_conforming_strings; }; /** @@ -125,8 +134,9 @@ public: ClientCharset=charset& // transcode by parser 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 any BEGIN TRAN/COMMIT/ROLLBACK [can NOT be used with autocommit option] + autocommit=0& // 1 -- each statement is commited automatically, only when with_default_transaction enabled + standard_conforming_strings=1& // 0 -- escape \ char that could be needed for old servers + with_default_transaction=0 // 1 -- wrap connection into BEGIN TRAN/COMMIT/ROLLBACK */ void connect( char* url, @@ -145,11 +155,13 @@ public: char* datestyle=0; Connection& connection=*(Connection *)services.malloc(sizeof(Connection)); + *connection_ref=&connection; connection.services=&services; - connection.client_charset=0; - connection.autocommit=true; - connection.without_default_transactions=false; + connection.client_charset=0; + connection.autocommit=false; + connection.with_default_transactions=false; + connection.standard_conforming_strings=true; connection.conn=PQsetdbLogin( (host&&strcasecmp(host, "local")==0)?NULL/* local Unix domain socket */:host, port, @@ -173,17 +185,20 @@ public: } else if(strcasecmp(key, "datestyle")==0){ datestyle=value; } else if(strcasecmp(key, "autocommit")==0){ - if(connection.without_default_transactions) - services._throw("options WithoutDefaultTransaction and autocommit can't be used together"); - if(atoi(value)==0) - 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.without_default_transactions=true; - connection.autocommit=false; + if(!connection.with_default_transactions) + services._throw("autocommit can be used only with_default_transaction enabled"); + connection.autocommit=true; } + } else if(strcmp(key, "with_default_transaction")==0){ + if(atoi(value)==1) + connection.with_default_transactions=true; + } else if(strcmp(key, "WithoutDefaultTransaction")==0){ + if(atoi(value)==0) + connection.with_default_transactions=true; + } 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,14 +208,14 @@ 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); @@ -232,15 +247,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, @@ -262,16 +324,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; @@ -308,7 +371,7 @@ public: goto cleanup; \ } - int column_count=PQnfields(res); + size_t column_count=PQnfields(res); if(!column_count) PQclear_throw("result contains no columns"); @@ -316,36 +379,16 @@ public: column_count=MAX_COLS; unsigned int column_types[MAX_COLS]; - bool transcode_column[MAX_COLS]; - for(int i=0; i=0){ @@ -389,32 +448,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]){ - // 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)); } } @@ -452,11 +513,11 @@ private: connection.client_charset); } } - int name_numner=atoi(ph.name); - if(name_numner <= 0 || (size_t)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; } } @@ -474,7 +535,7 @@ private: } 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 + if(connection.with_default_transactions) // without ?with_default_transaction=1 user must execute BEGIN/COMMIT/ROLLBACK by himself _execute_cmd(connection, query); } @@ -489,15 +550,17 @@ private: 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 @@ -506,9 +569,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; @@ -518,7 +581,7 @@ private: while(*o) { if( o[0]=='/' && - o[1]=='*' && + o[1]=='*' && o[2]=='*') { // name start const char* saved_o=o; o+=3; @@ -649,11 +712,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); \