|
|
| version 1.8, 2002/03/22 16:19:13 | version 1.13, 2003/07/24 10:09:40 |
|---|---|
| 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: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| Line 175 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++='\''; result++; |
| break; | break; |
| case '\\': // "\" -> "\\" | case '\\': // "\" -> "\\" |
| *to++='\''; result++; | *to++='\\'; result++; |
| 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 229 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 266 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 279 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 438 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"; |