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

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

E-mail: