Annotation of sql/pgsql/parser3pgsql.C, revision 1.30

1.1       parser      1: /** @file
                      2:        Parser PgSQL driver.
                      3: 
1.12      paf         4:        Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.1       parser      5: 
1.6       paf         6:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       parser      7: 
1.28      misha       8:        2007.10.25 using PgSQL 8.1.5
1.1       parser      9: */
1.29      misha      10: static const char *RCSId="$Id: parser3pgsql.C,v 1.28 2007/10/25 17:08:41 misha Exp $"; 
1.1       parser     11: 
                     12: #include "config_includes.h"
                     13: 
                     14: #include "pa_sql_driver.h"
                     15: 
                     16: #include <libpq-fe.h>
                     17: #include <libpq/libpq-fs.h>
                     18: 
                     19: // OIDOID from catalog/pg_type.h
                     20: #define OIDOID                 26
                     21: // LO_BUFSIZE from interfaces\libpq\fe-lobj.c = 8192 (0x2000)
                     22: // actually writing chunks of that size failed, reduced it twice
                     23: #define LO_BUFSIZE               0x1000
                     24: // from postgres_ext.h
1.26      misha      25: //#define InvalidOid           ((Oid) 0)
1.1       parser     26: 
                     27: 
                     28: #include "ltdl.h"
                     29: 
                     30: #define MAX_STRING 0x400
                     31: #define MAX_NUMBER 20
                     32: 
                     33: #if _MSC_VER
                     34: #      define snprintf _snprintf
                     35: #      define strcasecmp _stricmp
                     36: #endif
                     37: 
                     38: #ifndef max
1.30    ! misha      39: inline int max(int a,int b){ return a>b?a:b; }
1.1       parser     40: inline int min(int a,int b){ return a<b?a:b; }
                     41: #endif
                     42: 
1.30    ! misha      43: static char *lsplit(char *string, char delim){
        !            44:        if(string){
        !            45:                if(char *v=strchr(string, delim)){
1.1       parser     46:                        *v=0;
                     47:                        return v+1;
                     48:                }
1.29      misha      49:        }
                     50:        return 0;
1.1       parser     51: }
                     52: 
1.30    ! misha      53: static char *lsplit(char **string_ref, char delim){
1.29      misha      54:        char *result=*string_ref;
1.8       paf        55:        char *next=lsplit(*string_ref, delim);
1.29      misha      56:        *string_ref=next;
                     57:        return result;
1.8       paf        58: }
                     59: 
1.30    ! misha      60: static char* rsplit(char* string, char delim){
        !            61:        if(string){
        !            62:                if(char* v=strrchr(string, delim)){
1.25      paf        63:                        *v=0;
                     64:                        return v+1;
                     65:                }
1.29      misha      66:        }
                     67:        return NULL;    
1.25      paf        68: }
                     69: 
1.30    ! misha      70: static void toupper_str(char *out, const char *in, size_t size){
1.18      paf        71:        while(size--)
                     72:                *out++=(char)toupper(*in++);
                     73: }
                     74: 
1.16      paf        75: struct Connection {
                     76:        SQL_Driver_services* services;
                     77: 
                     78:        PGconn *conn;
1.30    ! misha      79:        const char* client_charset;
1.29      misha      80:        bool autocommit;
1.16      paf        81: };
                     82: 
1.1       parser     83: /**
                     84:        PgSQL server driver
                     85: */
                     86: class PgSQL_Driver : public SQL_Driver {
                     87: public:
                     88: 
                     89:        PgSQL_Driver() : SQL_Driver() {
                     90:        }
                     91: 
                     92:        /// get api version
1.30    ! misha      93:        int api_version(){ return SQL_DRIVER_API_VERSION; }
1.29      misha      94: 
1.1       parser     95:        /// initialize driver by loading sql dynamic link library
1.30    ! misha      96:        const char *initialize(char *dlopen_file_spec){
1.1       parser     97:                return dlopen_file_spec?
                     98:                        dlink(dlopen_file_spec):"client library column is empty";
                     99:        }
                    100: 
1.16      paf       101:        #define throwPQerror connection.services->_throw(PQerrorMessage(connection.conn))
1.8       paf       102:        #define PQclear_throw(msg) { \
                    103:                        PQclear(res); \
1.16      paf       104:                        connection.services->_throw(msg); \
1.8       paf       105:                }                                               
1.16      paf       106:        #define PQclear_throwPQerror PQclear_throw(PQerrorMessage(connection.conn))
1.1       parser    107: 
                    108:        /**     connect
1.21      paf       109:                @param url
1.29      misha     110:                        format: @b user:pass@host[:port]|[local]/database?
1.30    ! misha     111:                        ClientCharset=charset&  // transcode by parser
        !           112:                        charset=value&                  // transcode by server with 'SET CLIENT_ENCODING=value'
        !           113:                        datestyle=value&                // 'SET DATESTYLE=value' available values are: ISO|SQL|Postgres|European|US|German [default=ISO]
1.29      misha     114:                        autocommit=1&
                    115:                        WithoutDefaultTransaction=1     // == autocommit=0
1.1       parser    116:        */
                    117:        void connect(
1.30    ! misha     118:                                char* url, 
        !           119:                                SQL_Driver_services& services, 
        !           120:                                void** connection_ref ///< output: Connection*
        !           121:        ){
        !           122:                char* user=url;
        !           123:                char* host=rsplit(user, '@');
        !           124:                char* db=lsplit(host, '/');
        !           125:                char* pwd=lsplit(user, ':');
        !           126:                char* port=lsplit(host, ':');
1.1       parser    127: 
1.8       paf       128:                char *options=lsplit(db, '?');
                    129: 
1.30    ! misha     130:                char* charset=0;
1.29      misha     131:                char* datestyle=0;
1.18      paf       132: 
1.30    ! misha     133:                Connection& connection=*(Connection *)services.malloc(sizeof(Connection));
1.16      paf       134:                *connection_ref=&connection;
                    135:                connection.services=&services;
1.30    ! misha     136:                connection.client_charset=0;    
1.29      misha     137:                connection.autocommit=true;
1.16      paf       138:                connection.conn=PQsetdbLogin(
1.7       paf       139:                        (host&&strcasecmp(host, "local")==0)?NULL/* local Unix domain socket */:host, port, 
1.1       parser    140:                        NULL, NULL, db, user, pwd);
1.29      misha     141: 
1.16      paf       142:                if(!connection.conn)
1.1       parser    143:                        services._throw("PQsetdbLogin failed");
1.29      misha     144: 
                    145:                if(PQstatus(connection.conn)!=CONNECTION_OK)
1.1       parser    146:                        throwPQerror;
                    147: 
1.29      misha     148:                while(options){
                    149:                        if(char *key=lsplit(&options, '&')){
                    150:                                if(*key){
                    151:                                        if(char *value=lsplit(key, '=')){
1.30    ! misha     152:                                                if(strcmp(key, "ClientCharset")==0){ // transcoding with parser
1.20      paf       153:                                                        toupper_str(value, value, strlen(value));
1.30    ! misha     154:                                                        connection.client_charset=value;
1.29      misha     155:                                                } else if(strcasecmp(key, "charset")==0){ // transcoding with server
1.30    ! misha     156:                                                        charset=value;
        !           157:                                                } else if(strcasecmp(key, "datestyle")==0){
1.8       paf       158:                                                        datestyle=value;
1.29      misha     159:                                                } else if(strcasecmp(key, "autocommit")==0){
                    160:                                                        if(atoi(value)==0)
                    161:                                                                connection.autocommit=false;
                    162:                                                } else if(strcmp(key, "WithoutDefaultTransaction")==0){ // backward, use autocommit=0
                    163:                                                        if(atoi(value)==1)
                    164:                                                                connection.autocommit=false;
1.8       paf       165:                                                } else
                    166:                                                        services._throw("unknown connect option" /*key*/);
                    167:                                        } else 
                    168:                                                services._throw("connect option without =value" /*key*/);
                    169:                                }
                    170:                        }
                    171:                }
                    172: 
1.30    ! misha     173:                if(charset){
        !           174:                        char statement[MAX_STRING]="SET CLIENT_ENCODING=";
        !           175:                        strncat(statement, charset, MAX_STRING);
1.26      misha     176: 
1.30    ! misha     177:                        _execute_cmd(connection, statement);
1.8       paf       178:                }
                    179: 
1.29      misha     180:                if(datestyle){
1.30    ! misha     181:                        char statement[MAX_STRING]="SET DATESTYLE=";
1.29      misha     182:                        strncat(statement, datestyle, MAX_STRING);
1.26      misha     183: 
1.30    ! misha     184:                        _execute_cmd(connection, statement);
1.8       paf       185:                }
                    186: 
1.30    ! misha     187:                _begin_transaction(connection);
1.1       parser    188:        }
1.29      misha     189: 
1.30    ! misha     190:        void disconnect(void *aconnection){
1.16      paf       191:                Connection& connection=*static_cast<Connection*>(aconnection);
1.29      misha     192:                PQfinish(connection.conn);
1.16      paf       193:                connection.conn=0;
1.1       parser    194:        }
1.29      misha     195: 
1.30    ! misha     196:        void commit(void *aconnection){
        !           197:                Connection& connection=*static_cast<Connection*>(aconnection);
        !           198:                if(connection.autocommit){
        !           199:                        _execute_cmd(connection, "COMMIT");
        !           200:                }
        !           201:                _begin_transaction(connection);
1.1       parser    202:        }
1.29      misha     203: 
1.30    ! misha     204:        void rollback(void *aconnection){
        !           205:                Connection& connection=*static_cast<Connection*>(aconnection);
        !           206:                if(connection.autocommit){
        !           207:                        _execute_cmd(connection, "ROLLBACK");
        !           208:                }
        !           209:                _begin_transaction(connection);
1.1       parser    210:        }
                    211: 
1.16      paf       212:        bool ping(void *aconnection) {
                    213:                Connection& connection=*static_cast<Connection*>(aconnection);
                    214:                return PQstatus(connection.conn)==CONNECTION_OK;
1.1       parser    215:        }
                    216: 
1.30    ! misha     217:        const char* quote(void *aconnection, const char *from, unsigned int length){
1.16      paf       218:                Connection& connection=*static_cast<Connection*>(aconnection);
                    219: 
                    220:                char *result=(char*)connection.services->malloc_atomic(length*2+1);
1.29      misha     221:                int err=0;
1.30    ! misha     222:                PQescapeStringConn(connection.conn, result, from, length, &err);
1.13      paf       223:                return result;
1.26      misha     224:        }
                    225:        
1.16      paf       226:        void query(void *aconnection, 
1.30    ! misha     227:                                const char *astatement, 
        !           228:                                size_t placeholders_count, Placeholder* placeholders, 
        !           229:                                unsigned long offset, unsigned long limit,
        !           230:                                SQL_Driver_query_event_handlers& handlers
        !           231:        ){
1.16      paf       232:                Connection& connection=*static_cast<Connection*>(aconnection);
1.30    ! misha     233:                const char* client_charset=connection.client_charset;
1.16      paf       234:                SQL_Driver_services& services=*connection.services;
                    235:                PGconn *conn=connection.conn;
1.22      paf       236: 
1.30    ! misha     237:                bool transcode_needed=_transcode_required(connection);
        !           238: 
1.26      misha     239:                const char** paramValues;
                    240:                if(placeholders_count>0){
1.30    ! misha     241:                        int binds_size=sizeof(char)*placeholders_count;
1.26      misha     242:                        paramValues = static_cast<const char**>(services.malloc_atomic(binds_size));
1.30    ! misha     243:                        _bind_parameters(placeholders_count, placeholders, paramValues, connection, transcode_needed);
1.26      misha     244:                }
1.1       parser    245: 
1.30    ! misha     246:                // transcode query from $request:charset to ?ClientCharset
        !           247:                if(transcode_needed){
        !           248:                        size_t length=strlen(astatement);
        !           249:                        services.transcode(astatement, length,
        !           250:                                astatement, length,
1.18      paf       251:                                services.request_charset(),
1.30    ! misha     252:                                connection.client_charset);
1.18      paf       253:                }
                    254: 
1.30    ! misha     255:                const char *statement=_preprocess_statement(connection, astatement, offset, limit);
        !           256:                // error after prepare?
1.1       parser    257: 
1.26      misha     258:                PGresult *res;
                    259:                if(placeholders_count>0){
                    260:                        res=PQexecParams(conn, statement, placeholders_count, NULL, paramValues, NULL, NULL, 0);
                    261:                } else {
                    262:                        res=PQexec(conn, statement);
                    263:                }
1.1       parser    264:                if(!res) 
                    265:                        throwPQerror;
                    266: 
                    267:                switch(PQresultStatus(res)) {
1.30    ! misha     268:                        case PGRES_EMPTY_QUERY: 
        !           269:                                PQclear_throw("no query");
        !           270:                                break;
        !           271:                        case PGRES_COMMAND_OK: // empty result: insert|delete|update|...
        !           272:                                PQclear(res);
        !           273:                                return;
        !           274:                        case PGRES_TUPLES_OK: 
        !           275:                                break;  
        !           276:                        default:
        !           277:                                PQclear_throwPQerror;
        !           278:                                break;
1.1       parser    279:                }
                    280:                
                    281:                int column_count=PQnfields(res);
                    282:                if(!column_count)
                    283:                        PQclear_throw("result contains no columns");
                    284: 
1.9       paf       285:                bool failed=false;
                    286:                SQL_Error sql_error;
                    287: #define CHECK(afailed) \
                    288:                if(afailed) { \
                    289:                        failed=true; \
                    290:                        goto cleanup; \
                    291:                }
                    292: 
1.1       parser    293:                for(int i=0; i<column_count; i++){
                    294:                        char *name=PQfname(res, i);
1.18      paf       295:                        size_t length=strlen(name);
1.19      paf       296:                        char* strm=(char*)services.malloc(length+1);
                    297:                        memcpy(strm, name, length+1);
                    298:                        const char* str=strm;
1.18      paf       299: 
1.30    ! misha     300:                        // transcode column name from ?ClientCharset to $request:charset
        !           301:                        if(transcode_needed) 
1.18      paf       302:                                services.transcode(str, length,
                    303:                                        str, length,
1.30    ! misha     304:                                        connection.client_charset,
1.18      paf       305:                                        services.request_charset());
                    306: 
                    307:                        CHECK(handlers.add_column(sql_error, str, length));
1.1       parser    308:                }
                    309: 
1.9       paf       310:                CHECK(handlers.before_rows(sql_error));
1.1       parser    311: 
                    312:                if(unsigned long row_count=(unsigned long)PQntuples(res))
                    313:                        for(unsigned long r=0; r<row_count; r++) {
1.9       paf       314:                                CHECK(handlers.add_row(sql_error));
1.1       parser    315:                                for(int i=0; i<column_count; i++){
                    316:                                        const char *cell=PQgetvalue(res, r, i);
1.18      paf       317:                                        size_t length;
1.19      paf       318:                                        const char* str;
1.1       parser    319:                                        if(PQftype(res, i)==OIDOID) {
                    320:                                                // ObjectID column, read object bytes
                    321: 
                    322:                                                char *error_pos=0;
                    323:                                                Oid oid=cell?atoi(cell):0;
                    324:                                                int fd=lo_open(conn, oid, INV_READ);
                    325:                                                if(fd>=0) {
                    326:                                                        // seek to end
                    327:                                                        if(lo_lseek(conn, fd, 0, SEEK_END)<0)
                    328:                                                                PQclear_throwPQerror;
1.18      paf       329:                                                        // get length
1.1       parser    330:                                                        int size_tell=lo_tell(conn, fd);
                    331:                                                        if(size_tell<0)
                    332:                                                                PQclear_throwPQerror;
                    333:                                                        // seek to begin
                    334:                                                        if(lo_lseek(conn, fd, 0, SEEK_SET)<0)
                    335:                                                                PQclear_throwPQerror;
1.18      paf       336:                                                        length=(size_t)size_tell;
                    337:                                                        if(length) {
1.1       parser    338:                                                                // read 
1.19      paf       339:                                                                char* strm=(char*)services.malloc(length+1);
                    340:                                                                if(!lo_read_ex(conn, fd, strm, size_tell))
1.1       parser    341:                                                                        PQclear_throw("lo_read can not read all bytes of object");
1.19      paf       342:                                                                strm[length]=0;
                    343:                                                                str=strm;
1.1       parser    344:                                                        } else
1.13      paf       345:                                                                str=0;
1.1       parser    346:                                                        if(lo_close(conn, fd)<0)
                    347:                                                                PQclear_throwPQerror;
                    348:                                                } else
                    349:                                                        PQclear_throwPQerror;
                    350:                                        } else {
                    351:                                                // normal column, read it normally
1.18      paf       352:                                                length=(size_t)PQgetlength(res, r, i);
                    353:                                                if(length) {
1.19      paf       354:                                                        char* strm=(char*)services.malloc(length+1);
                    355:                                                        memcpy(strm, cell, length+1);
                    356:                                                        str=strm;
1.1       parser    357:                                                } else
1.13      paf       358:                                                        str=0;
1.1       parser    359:                                        }
1.18      paf       360: 
1.30    ! misha     361:                                        if(transcode_needed && str && length){
        !           362:                                                // transcode cell value from ?ClientCharset to $request:charset
        !           363:                                                services.transcode(str, length,
        !           364:                                                        str, length,
        !           365:                                                        connection.client_charset,
        !           366:                                                        services.request_charset());
1.18      paf       367:                                        }
                    368: 
                    369:                                        CHECK(handlers.add_row_cell(sql_error, str, length));
1.1       parser    370:                                }
                    371:                        }
1.9       paf       372: cleanup:
1.1       parser    373:                PQclear(res);
1.9       paf       374:                if(failed)
                    375:                        services._throw(sql_error);
1.1       parser    376:        }
                    377: 
1.30    ! misha     378: private:
        !           379:        void _bind_parameters(
        !           380:                                size_t placeholders_count, 
        !           381:                                Placeholder* placeholders, 
        !           382:                                const char** paramValues,
        !           383:                                Connection& connection,
        !           384:                                bool transcode_needed
        !           385:        ){
        !           386:                for(size_t i=0; i<placeholders_count; i++){
1.27      misha     387:                        Placeholder& ph=placeholders[i];
1.30    ! misha     388:                        if(transcode_needed){
1.27      misha     389:                                size_t name_length;
1.30    ! misha     390:                                size_t value_length;
1.27      misha     391:                                connection.services->transcode(ph.name, strlen(ph.name),
                    392:                                        ph.name, name_length,
                    393:                                        connection.services->request_charset(),
1.30    ! misha     394:                                        connection.client_charset);
1.27      misha     395: 
                    396:                                if(ph.value) {
                    397:                                        connection.services->transcode(ph.value, strlen(ph.value),
                    398:                                                ph.value, value_length,
                    399:                                                connection.services->request_charset(),
1.30    ! misha     400:                                                connection.client_charset);
1.27      misha     401:                                }
                    402:                        }
1.30    ! misha     403:                        int name_numner=atoi(ph.name);
        !           404:                        if(name_numner <= 0 || name_numner > placeholders_count)
1.27      misha     405:                                connection.services->_throw("bad bind parameter key");
1.30    ! misha     406: 
        !           407:                        paramValues[name_numner-1]=ph.value;
1.27      misha     408:                }
                    409:        }
                    410:        
                    411:        
1.26      misha     412:        /**
1.30    ! misha     413:                Executes a query and throw away the result.
1.26      misha     414:        */
1.30    ! misha     415:        void _execute_cmd(const Connection& connection, const char *query){
1.26      misha     416:                if(PGresult *res=PQexec(connection.conn, query))
                    417:                        PQclear(res); // throw out the result [don't need but must call]
                    418:                else
                    419:                        throwPQerror;
                    420:        }
                    421: 
1.30    ! misha     422:        void _begin_transaction(Connection& connection){
        !           423:                if(connection.autocommit)
        !           424:                        _execute_cmd(connection, "BEGIN");
1.1       parser    425:        }
                    426: 
1.30    ! misha     427:        const char *_preprocess_statement(
        !           428:                                        Connection& connection,
        !           429:                                        const char *astatement,
        !           430:                                        unsigned long offset,
        !           431:                                        unsigned long limit
        !           432:        ){
1.16      paf       433:                PGconn *conn=connection.conn;
                    434: 
1.1       parser    435:                size_t statement_size=strlen(astatement);
                    436: 
1.16      paf       437:                char *result=(char *)connection.services->malloc(statement_size
1.1       parser    438:                        +MAX_NUMBER*2+15 // limit # offset #
                    439:                        +MAX_STRING // in case of short 'strings'
                    440:                        +1);
                    441:                // offset & limit -> suffixes
                    442:                const char *o;
1.30    ! misha     443:                if(offset || limit!=SQL_NO_LIMIT){
1.1       parser    444:                        char *cur=result;
                    445:                        memcpy(cur, astatement, statement_size); cur+=statement_size;
1.30    ! misha     446:                        if(limit!=SQL_NO_LIMIT)
1.1       parser    447:                                cur+=snprintf(cur, 7+MAX_NUMBER, " limit %u", limit);
                    448:                        if(offset)
                    449:                                cur+=snprintf(cur, 8+MAX_NUMBER, " offset %u", offset);
                    450:                        o=result;
                    451:                } else 
                    452:                        o=astatement;
                    453: 
                    454:                // /**xxx**/'literal' -> oid
                    455:                char *n=result;
                    456:                while(*o) {
                    457:                        if(
                    458:                                o[0]=='/' &&
                    459:                                o[1]=='*' && 
                    460:                                o[2]=='*') { // name start
1.15      paf       461:                                const char* saved_o=o;
1.1       parser    462:                                o+=3;
                    463:                                while(*o)
                    464:                                        if(
                    465:                                                o[0]=='*' &&
                    466:                                                o[1]=='*' &&
                    467:                                                o[2]=='/' &&
                    468:                                                o[3]=='\'') { // name end
1.15      paf       469:                                                saved_o=0; // found, marking that
1.1       parser    470:                                                o+=4;
                    471:                                                Oid oid=lo_creat(conn, INV_READ|INV_WRITE);
                    472:                                                if(oid==InvalidOid)
                    473:                                                        throwPQerror;
                    474:                                                int fd=lo_open(conn, oid, INV_WRITE);
                    475:                                                if(fd>=0) {
                    476:                                                        const char *start=o;
                    477:                                                        bool escaped=false;
                    478:                                                        while(*o && !(o[0]=='\'' && o[1]!='\'' && !escaped)) {
                    479:                                                                escaped=*o=='\\' || (o[0]=='\'' && o[1]=='\'');
                    480:                                                                if(escaped) {
                    481:                                                                        // write pending, skip "\" or "'"
                    482:                                                                        if(!lo_write_ex(conn, fd, start, o-start))
1.16      paf       483:                                                                                connection.services->_throw("lo_write could not write all bytes of object (1)");
1.1       parser    484:                                                                        start=++o;
                    485:                                                                } else
                    486:                                                                        o++;
                    487:                                                        }
                    488:                                                        if(!lo_write_ex(conn, fd, start, o-start))
1.16      paf       489:                                                                connection.services->_throw("lo_write can not write all bytes of object (2)");
1.1       parser    490:                                                        if(lo_close(conn, fd)<0)
                    491:                                                                throwPQerror;
                    492:                                                } else
                    493:                                                        throwPQerror;
                    494:                                                if(*o)
                    495:                                                        o++; // skip "'"
                    496: 
                    497:                                                n+=snprintf(n, MAX_NUMBER, "%u", oid);
                    498:                                                break;
                    499:                                        } else
                    500:                                                o++; // /**skip**/'xxx'
1.15      paf       501:                                if(saved_o) {
                    502:                                        o=saved_o;
                    503:                                        *n++=*o++;
                    504:                                }
1.1       parser    505:                        } else
                    506:                                *n++=*o++;
                    507:                }
                    508:                *n=0;
                    509: 
                    510:                return result;
                    511:        }
                    512: 
1.30    ! misha     513:        bool _transcode_required(Connection& connection){
        !           514:                return (connection.client_charset && strcmp(connection.client_charset, connection.services->request_charset())!=0);
        !           515:        }
        !           516: 
1.1       parser    517: private: // lo_read/write exchancements
                    518: 
                    519:        bool lo_read_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) {
1.26      misha     520:                return lo_rw_method (conn, fd, buf, len, lo_read);
1.1       parser    521:        }
                    522: 
                    523:        bool lo_write_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) {
1.26      misha     524:                return lo_rw_method (conn, fd, buf, len, lo_write);
                    525:        }
                    526: 
                    527:        bool lo_rw_method(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len, int (*lo_func)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len)) {
                    528:                int size_op;
                    529:                while(len && (size_op=lo_func(conn, fd, buf, min(LO_BUFSIZE, len)))>0) {
                    530:                        buf+=size_op;
1.29      misha     531:                        len-=size_op;
1.1       parser    532:                }
                    533:                return len==0;
                    534:        }
                    535: 
                    536: private: // conn client library funcs
                    537: 
                    538:        typedef PGconn* (*t_PQsetdbLogin)(
                    539:                const char *pghost,
                    540:                const char *pgport,
                    541:                const char *pgoptions,
                    542:                const char *pgtty,
                    543:                const char *dbName,
                    544:                const char *login,
                    545:                const char *pwd); t_PQsetdbLogin PQsetdbLogin;
                    546:        typedef void (*t_PQfinish)(PGconn *conn);  t_PQfinish PQfinish;
                    547:        typedef char *(*t_PQerrorMessage)(const PGconn* conn); t_PQerrorMessage PQerrorMessage;
                    548:        typedef ConnStatusType (*t_PQstatus)(const PGconn *conn); t_PQstatus PQstatus;
                    549:        typedef PGresult *(*t_PQexec)(PGconn *conn,
1.29      misha     550:                                                const char *query); t_PQexec PQexec;
1.26      misha     551:        typedef PGresult *(*t_PQexecParams)(
1.29      misha     552:                                                PGconn *conn,
                    553:                                                const char *query, 
                    554:                                                int nParams,
                    555:                                                const Oid *paramTypes,
                    556:                                                const char * const *paramValues,
                    557:                                                const int *paramLengths,
                    558:                                                const int *paramFormats,
                    559:                                                int resultFormat); t_PQexecParams PQexecParams;
1.26      misha     560: 
1.1       parser    561:        typedef ExecStatusType (*t_PQresultStatus)(const PGresult *res); t_PQresultStatus PQresultStatus;
                    562:        typedef int (*t_PQgetlength)(const PGresult *res,
1.29      misha     563:                                                int tup_num,
                    564:                                                int field_num); t_PQgetlength PQgetlength;
1.1       parser    565:        typedef char* (*t_PQgetvalue)(const PGresult *res,
1.29      misha     566:                                                int tup_num,
                    567:                                                int field_num); t_PQgetvalue PQgetvalue;
1.30    ! misha     568:        typedef int     (*t_PQntuples)(const PGresult *res); t_PQntuples PQntuples;
1.1       parser    569:        typedef char *(*t_PQfname)(const PGresult *res,
                    570:                                                int field_index); t_PQfname PQfname;
                    571:        typedef int (*t_PQnfields)(const PGresult *res); t_PQnfields PQnfields;
                    572:        typedef void (*t_PQclear)(PGresult *res); t_PQclear PQclear;
                    573: 
                    574:        typedef Oid     (*t_PQftype)(const PGresult *res, int field_num); t_PQftype PQftype;
                    575: 
1.26      misha     576:        typedef size_t (*t_PQescapeStringConn)(PGconn *conn,
1.29      misha     577:                                                char *to, const char *from, size_t length,
                    578:                                                int *error); t_PQescapeStringConn PQescapeStringConn;
1.26      misha     579: 
1.1       parser    580:        typedef int     (*t_lo_open)(PGconn *conn, Oid lobjId, int mode); t_lo_open lo_open;
                    581:        typedef int     (*t_lo_close)(PGconn *conn, int fd); t_lo_close lo_close;
                    582:        typedef int     (*t_lo_read)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len); t_lo_read lo_read;
                    583:        typedef int     (*t_lo_write)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len); t_lo_write lo_write;
                    584:        typedef int     (*t_lo_lseek)(PGconn *conn, int fd, int offset, int whence); t_lo_lseek lo_lseek;
                    585:        typedef Oid     (*t_lo_creat)(PGconn *conn, int mode); t_lo_creat lo_creat;
                    586:        typedef int     (*t_lo_tell)(PGconn *conn, int fd); t_lo_tell lo_tell;
                    587:        typedef int     (*t_lo_unlink)(PGconn *conn, Oid lobjId); t_lo_unlink lo_unlink;
                    588:        typedef Oid     (*t_lo_import)(PGconn *conn, const char *filename); t_lo_import lo_import;
                    589:        typedef int     (*t_lo_export)(PGconn *conn, Oid lobjId, const char *filename); t_lo_export lo_export;
                    590: 
                    591: private: // conn client library funcs linking
                    592: 
                    593:        const char *dlink(const char *dlopen_file_spec) {
1.11      paf       594:                if(lt_dlinit())
                    595:                        return lt_dlerror();
1.29      misha     596:                lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
                    597:                if(!handle)
1.1       parser    598:                        return "can not open the dynamic link module";
                    599: 
                    600:                #define DSLINK(name, action) \
                    601:                        name=(t_##name)lt_dlsym(handle, #name); \
                    602:                                if(!name) \
                    603:                                        action;
                    604: 
                    605:                #define DLINK(name) DSLINK(name, return "function " #name " was not found")
                    606:                
                    607:                DLINK(PQsetdbLogin);
                    608:                DLINK(PQerrorMessage);
                    609:                DLINK(PQstatus);
                    610:                DLINK(PQfinish);
                    611:                DLINK(PQgetvalue);
                    612:                DLINK(PQgetlength);
                    613:                DLINK(PQntuples);
                    614:                DLINK(PQfname);
                    615:                DLINK(PQnfields);
                    616:                DLINK(PQclear);
                    617:                DLINK(PQresultStatus);
                    618:                DLINK(PQexec);
1.26      misha     619:                DLINK(PQexecParams);
1.1       parser    620:                DLINK(PQftype);
1.26      misha     621:                DLINK(PQescapeStringConn);
1.1       parser    622:                DLINK(lo_open);         DLINK(lo_close);
                    623:                DLINK(lo_read);         DLINK(lo_write);
                    624:                DLINK(lo_lseek);                DLINK(lo_creat);
                    625:                DLINK(lo_tell);         DLINK(lo_unlink);
                    626:                DLINK(lo_import);               DLINK(lo_export);
                    627: 
                    628:                return 0;
                    629:        }
                    630: };
                    631: 
                    632: extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
                    633:        return new PgSQL_Driver();
1.2       paf       634: }

E-mail: