|
|
| version 1.4, 2001/11/16 12:39:15 | version 1.15, 2003/09/29 06:09:57 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser PgSQL driver. | Parser PgSQL driver. |
| Copyright(c) 2001 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 120 public: | Line 175 public: |
| return PQstatus((PGconn *)connection)==CONNECTION_OK; | return PQstatus((PGconn *)connection)==CONNECTION_OK; |
| } | } |
| unsigned int quote( | const char* quote( |
| SQL_Driver_services&, void *connection, | SQL_Driver_services& services, void *connection, |
| char *to, const char *from, unsigned int length) { | const char *from, unsigned int length) { |
| if(to) { // store mode | char *result=(char*)services.malloc_atomic(length*2+1); |
| unsigned int result=length; | char *to=result; |
| while(length--) { | while(length--) { |
| switch(*from) { | switch(*from) { |
| case '\'': // "'" -> "''" | case '\'': // "'" -> "''" |
| *to++='\''; result++; | *to++='\''; |
| break; | break; |
| case '\\': // "\" -> "\\" | case '\\': // "\" -> "\\" |
| *to++='\''; result++; | *to++='\\'; |
| break; | break; |
| } | |
| *to++=*from++; | |
| } | } |
| return result; | *to++=*from++; |
| } else // estimate mode | } |
| return length*2; | *to=0; |
| } | return result; |
| } | |
| void query( | void query( |
| SQL_Driver_services& services, void *connection, | SQL_Driver_services& services, void *connection, |
| const char *astatement, unsigned long offset, unsigned long limit, | const char *astatement, unsigned long offset, unsigned long limit, |
| Line 147 public: | Line 201 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 228 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); | char* str=(char*)services.malloc(size+1); |
| memcpy(ptr, name, size); | memcpy(str, name, size+1); |
| handlers.add_column(ptr, size); | CHECK(handlers.add_column(sql_error, str, 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; |
| void *ptr; | char* str; |
| if(PQftype(res, i)==OIDOID) { | if(PQftype(res, i)==OIDOID) { |
| // ObjectID column, read object bytes | // ObjectID column, read object bytes |
| Line 216 public: | Line 273 public: |
| size=(size_t)size_tell; | size=(size_t)size_tell; |
| if(size) { | if(size) { |
| // read | // read |
| ptr=services.malloc(size); | str=(char*)services.malloc(size+1); |
| if(!lo_read_ex(conn, fd, (const char *)ptr, size_tell)) | if(!lo_read_ex(conn, fd, str, size_tell)) |
| PQclear_throw("lo_read can not read all bytes of object"); | PQclear_throw("lo_read can not read all bytes of object"); |
| str[size]=0; | |
| } else | } else |
| ptr=0; | str=0; |
| if(lo_close(conn, fd)<0) | if(lo_close(conn, fd)<0) |
| PQclear_throwPQerror; | PQclear_throwPQerror; |
| } else | } else |
| Line 229 public: | Line 287 public: |
| // normal column, read it normally | // normal column, read it normally |
| size=(size_t)PQgetlength(res, r, i); | size=(size_t)PQgetlength(res, r, i); |
| if(size) { | if(size) { |
| ptr=services.malloc(size); | str=(char*)services.malloc(size+1); |
| memcpy(ptr, cell, size); | memcpy(str, cell, size+1); |
| } else | } else |
| ptr=0; | str=0; |
| } | } |
| handlers.add_row_cell(ptr, size); | CHECK(handlers.add_row_cell(sql_error, str, size)); |
| } | } |
| } | } |
| cleanup: | |
| PQclear(res); | PQclear(res); |
| if(failed) | |
| services._throw(sql_error); | |
| } | } |
| private: // private funcs | private: // private funcs |
| Line 278 private: // private funcs | Line 338 private: // private funcs |
| o[0]=='/' && | o[0]=='/' && |
| o[1]=='*' && | o[1]=='*' && |
| o[2]=='*') { // name start | o[2]=='*') { // name start |
| const char* saved_o=o; | |
| o+=3; | o+=3; |
| while(*o) | while(*o) |
| if( | if( |
| Line 285 private: // private funcs | Line 346 private: // private funcs |
| o[1]=='*' && | o[1]=='*' && |
| o[2]=='/' && | o[2]=='/' && |
| o[3]=='\'') { // name end | o[3]=='\'') { // name end |
| saved_o=0; // found, marking that | |
| o+=4; | o+=4; |
| Oid oid=lo_creat(conn, INV_READ|INV_WRITE); | Oid oid=lo_creat(conn, INV_READ|INV_WRITE); |
| if(oid==InvalidOid) | if(oid==InvalidOid) |
| Line 316 private: // private funcs | Line 378 private: // private funcs |
| break; | break; |
| } else | } else |
| o++; // /**skip**/'xxx' | o++; // /**skip**/'xxx' |
| if(saved_o) { | |
| o=saved_o; | |
| *n++=*o++; | |
| } | |
| } else | } else |
| *n++=*o++; | *n++=*o++; |
| } | } |
| Line 388 private: // conn client library funcs | Line 454 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"; |