|
|
| version 1.1, 2001/09/21 15:44:37 | version 1.8, 2002/03/22 16:19:13 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser PgSQL driver. | Parser PgSQL driver. |
| Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com) | Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| 2001.07.30 using PgSQL 7.1.2 | 2001.07.30 using PgSQL 7.1.2 |
| */ | */ |
| Line 51 static char *lsplit(char *string, char d | Line 51 static char *lsplit(char *string, char d |
| return 0; | return 0; |
| } | } |
| static char *lsplit(char **string_ref, char delim) { | |
| char *result=*string_ref; | |
| char *next=lsplit(*string_ref, delim); | |
| *string_ref=next; | |
| return result; | |
| } | |
| /** | /** |
| PgSQL server driver | PgSQL server driver |
| */ | */ |
| Line 63 public: | Line 70 public: |
| /// 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(const 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 services._throw(PQerrorMessage(conn)) | #define throwPQerror services._throw(PQerrorMessage(conn)) |
| #define PQclear_throw(msg) { \ | |
| PQclear(res); \ | |
| services._throw(msg); \ | |
| } | |
| #define PQclear_throwPQerror PQclear_throw(PQerrorMessage(conn)) | |
| /** connect | /** connect |
| @param used_only_in_connect_url | @param used_only_in_connect_url |
| Line 85 public: | Line 97 public: |
| char *pwd=lsplit(user, ':'); | char *pwd=lsplit(user, ':'); |
| char *port=lsplit(host, ':'); | char *port=lsplit(host, ':'); |
| char *options=lsplit(db, '?'); | |
| PGconn *conn=PQsetdbLogin( | PGconn *conn=PQsetdbLogin( |
| 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(!conn) | if(!conn) |
| services._throw("PQsetdbLogin failed"); | services._throw("PQsetdbLogin failed"); |
| if(PQstatus(conn)!=CONNECTION_OK) | if(PQstatus(conn)!=CONNECTION_OK) |
| throwPQerror; | throwPQerror; |
| char *charset=0; | |
| char *datestyle=0; | |
| while(options) { | |
| if(char *key=lsplit(&options, '&')) { | |
| if(*key) { | |
| if(char *value=lsplit(key, '=')) { | |
| if(strcasecmp(key, "charset")==0) { | |
| charset=value; | |
| } else if(strcasecmp(key, "datestyle")==0) { | |
| datestyle=value; | |
| } else | |
| services._throw("unknown connect option" /*key*/); | |
| } else | |
| services._throw("connect option without =value" /*key*/); | |
| } | |
| } | |
| } | |
| if(charset) { | |
| // set CLIENT_ENCODING | |
| char statement[MAX_STRING]="set CLIENT_ENCODING="; // win | |
| strncat(statement, charset, MAX_STRING); | |
| PGresult *res=PQexec(conn, statement); | |
| if(!res) | |
| throwPQerror; | |
| PQclear(res); // throw out the result [don't need but must call] | |
| } | |
| if(datestyle) { | |
| // set DATESTYLE | |
| char statement[MAX_STRING]="set DATESTYLE="; // ISO,SQL,Postgres,European,NonEuropean=US,German,DEFAULT=ISO | |
| strncat(statement, charset, MAX_STRING); | |
| PGresult *res=PQexec(conn, statement); | |
| if(!res) | |
| throwPQerror; | |
| PQclear(res); // throw out the result [don't need but must call] | |
| } | |
| *(PGconn **)connection=conn; | *(PGconn **)connection=conn; |
| begin_transaction(services, conn); | begin_transaction(services, conn); |
| } | } |
| Line 123 public: | Line 178 public: |
| unsigned int quote( | unsigned int quote( |
| SQL_Driver_services&, void *connection, | SQL_Driver_services&, void *connection, |
| char *to, const char *from, unsigned int length) { | char *to, const char *from, unsigned int length) { |
| /* | if(to) { // store mode |
| it's already UNTAINT_TIMES_BIGGER | unsigned int result=length; |
| */ | while(length--) { |
| unsigned int result=length; | switch(*from) { |
| while(length--) { | case '\'': // "'" -> "''" |
| switch(*from) { | *to++='\''; result++; |
| case '\'': // "'" -> "''" | break; |
| *to++='\''; | case '\\': // "\" -> "\\" |
| break; | *to++='\''; result++; |
| case '\\': // "\" -> "\\" | break; |
| *to++='\''; | } |
| break; | *to++=*from++; |
| } | } |
| *to++=*from++; | return result; |
| } | } else // estimate mode |
| return result; | return length*2; |
| } | } |
| void query( | void query( |
| SQL_Driver_services& services, void *connection, | SQL_Driver_services& services, void *connection, |
| Line 147 public: | Line 202 public: |
| // _asm int 3; | // _asm int 3; |
| PGconn *conn=(PGconn *)connection; | PGconn *conn=(PGconn *)connection; |
| #define PQclear_throw(msg) { \ | |
| PQclear(res); \ | |
| services._throw(msg); \ | |
| } | |
| #define PQclear_throwPQerror PQclear_throw(PQerrorMessage(conn)) | |
| const char *statement=preprocess_statement(services, conn, | const char *statement=preprocess_statement(services, conn, |
| astatement, offset, limit); | astatement, offset, limit); |
| Line 425 private: // conn client library funcs li | Line 475 private: // conn client library funcs li |
| extern "C" SQL_Driver *SQL_DRIVER_CREATE() { | extern "C" SQL_Driver *SQL_DRIVER_CREATE() { |
| return new PgSQL_Driver(); | return new PgSQL_Driver(); |
| } | |
| } |