|
|
| version 1.5, 2002/02/08 07:28:25 | version 1.12, 2003/01/21 15:51:30 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser PgSQL driver. | Parser PgSQL driver. |
| Copyright(c) 2001, 2002 ArtLebedev Group(http://www.artlebedev.com) | Copyright(c) 2001, 2003 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 69 public: | Line 76 public: |
| } | } |
| #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 131 public: | Line 186 public: |
| *to++='\''; result++; | *to++='\''; result++; |
| break; | break; |
| case '\\': // "\" -> "\\" | case '\\': // "\" -> "\\" |
| *to++='\''; result++; | *to++='\\'; result++; |
| break; | break; |
| } | } |
| *to++=*from++; | *to++=*from++; |
| 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 179 public: | Line 229 public: |
| if(!column_count) | if(!column_count) |
| PQclear_throw("result contains no columns"); | PQclear_throw("result contains no columns"); |
| bool failed=false; | |
| SQL_Error sql_error; | |
| #define CHECK(afailed) \ | |
| if(afailed) { \ | |
| failed=true; \ | |
| goto cleanup; \ | |
| } | |
| for(int i=0; i<column_count; i++){ | for(int i=0; i<column_count; i++){ |
| char *name=PQfname(res, i); | char *name=PQfname(res, i); |
| size_t size=strlen(name); | size_t size=strlen(name); |
| void *ptr=services.malloc(size); | void *ptr=services.malloc(size); |
| memcpy(ptr, name, size); | memcpy(ptr, name, size); |
| handlers.add_column(ptr, size); | CHECK(handlers.add_column(sql_error, ptr, size)); |
| } | } |
| handlers.before_rows(); | CHECK(handlers.before_rows(sql_error)); |
| if(unsigned long row_count=(unsigned long)PQntuples(res)) | if(unsigned long row_count=(unsigned long)PQntuples(res)) |
| for(unsigned long r=0; r<row_count; r++) { | for(unsigned long r=0; r<row_count; r++) { |
| handlers.add_row(); | CHECK(handlers.add_row(sql_error)); |
| for(int i=0; i<column_count; i++){ | for(int i=0; i<column_count; i++){ |
| const char *cell=PQgetvalue(res, r, i); | const char *cell=PQgetvalue(res, r, i); |
| size_t size; | size_t size; |
| Line 234 public: | Line 292 public: |
| } else | } else |
| ptr=0; | ptr=0; |
| } | } |
| handlers.add_row_cell(ptr, size); | CHECK(handlers.add_row_cell(sql_error, ptr, size)); |
| } | } |
| } | } |
| cleanup: | |
| PQclear(res); | PQclear(res); |
| if(failed) | |
| services._throw(sql_error); | |
| } | } |
| private: // private funcs | private: // private funcs |
| Line 388 private: // conn client library funcs | Line 448 private: // conn client library funcs |
| private: // conn client library funcs linking | private: // conn client library funcs linking |
| const char *dlink(const char *dlopen_file_spec) { | const char *dlink(const char *dlopen_file_spec) { |
| if(lt_dlinit()) | |
| return lt_dlerror(); | |
| lt_dlhandle handle=lt_dlopen(dlopen_file_spec); | lt_dlhandle handle=lt_dlopen(dlopen_file_spec); |
| if(!handle) | if(!handle) |
| return "can not open the dynamic link module"; | return "can not open the dynamic link module"; |