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

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.50    ! moko       18: volatile const char * IDENT_PARSER3PGSQL_C="$Id: parser3pgsql.C,v 1.49 2021/11/03 16:27:15 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: 
                    144:                char *options=lsplit(db, '?');
                    145: 
                    146:                char* charset=0;
                    147:                char* datestyle=0;
                    148: 
1.50    ! moko      149:                char* pq_options=0;
        !           150:                size_t  pq_options_len=options ? strlen(options) : 0;
        !           151: 
1.34      misha     152:                Connection& connection=*(Connection *)services.malloc(sizeof(Connection));
1.45      moko      153: 
1.34      misha     154:                *connection_ref=&connection;
                    155:                connection.services=&services;
1.45      moko      156:                connection.client_charset=0;
1.47      moko      157:                connection.autocommit=true;
1.42      misha     158:                connection.standard_conforming_strings=true;
1.34      misha     159: 
                    160:                while(options){
                    161:                        if(char *key=lsplit(&options, '&')){
                    162:                                if(*key){
                    163:                                        if(char *value=lsplit(key, '=')){
                    164:                                                if(strcmp(key, "ClientCharset")==0){
                    165:                                                        toupper_str(value, value, strlen(value));
                    166:                                                        connection.client_charset=value;
                    167:                                                } else if(strcasecmp(key, "charset")==0){
                    168:                                                        charset=value;
                    169:                                                } else if(strcasecmp(key, "datestyle")==0){
                    170:                                                        datestyle=value;
                    171:                                                } else if(strcasecmp(key, "autocommit")==0){
1.45      moko      172:                                                        if(atoi(value)==0)
1.47      moko      173:                                                                connection.autocommit=false;
1.42      misha     174:                                                } else if(strcasecmp(key, "standard_conforming_strings")==0){
                    175:                                                        if(atoi(value)==0)
                    176:                                                                connection.standard_conforming_strings=false;
1.50    ! moko      177:                                                } else {
        !           178:                                                        if(!pq_options) {
        !           179:                                                                pq_options=(char*)services.malloc_atomic(pq_options_len+2);
        !           180:                                                                strcpy(pq_options, "?");
        !           181:                                                        } else {
        !           182:                                                                strcat(pq_options, "&");
        !           183:                                                        }
        !           184:                                                        strcat(pq_options, key);
        !           185:                                                        strcat(pq_options, "=");
        !           186:                                                        strcat(pq_options, value);
        !           187:                                                }
1.34      misha     188:                                        } else 
                    189:                                                services._throw("connect option without =value" /*key*/);
                    190:                                }
                    191:                        }
                    192:                }
                    193: 
1.50    ! moko      194:                if(host && (strchr(host, ',') || pq_options)){ // pq_options can exist only if host and db are not null
        !           195:                        char pq_url[MAX_STRING+1];
        !           196:                        snprintf(pq_url, MAX_STRING, "postgresql://%s/%s%s", host, db ? db : "", pq_options ? pq_options : "");
        !           197:                        connection.conn=PQsetdbLogin(NULL, NULL, NULL, NULL, pq_url, user, pwd);
        !           198:                } else {
        !           199:                        char* port=lsplit(host, ':');
        !           200:                        connection.conn=PQsetdbLogin( (host && strcasecmp(host, "local") == 0) ? NULL /* local Unix domain socket */ : host, port, NULL, NULL, db, user, pwd);
        !           201:                }
1.49      moko      202: 
                    203:                if(!connection.conn)
                    204:                        services._throw("PQsetdbLogin failed");
                    205: 
                    206:                if(PQstatus(connection.conn)!=CONNECTION_OK)
                    207:                        throwPQerror;
                    208: 
1.34      misha     209:                if(charset){
1.39      moko      210:                        char statement[MAX_STRING+1]="SET CLIENT_ENCODING=";
1.34      misha     211:                        strncat(statement, charset, MAX_STRING);
                    212: 
                    213:                        _execute_cmd(connection, statement);
                    214:                }
                    215: 
                    216:                if(datestyle){
1.40      moko      217:                        char statement[MAX_STRING+1]="SET DATESTYLE=";
1.34      misha     218:                        strncat(statement, datestyle, MAX_STRING);
                    219: 
                    220:                        _execute_cmd(connection, statement);
                    221:                }
                    222: 
1.47      moko      223:                if(!connection.autocommit)
1.50    ! moko      224:                        _execute_cmd(connection, "BEGIN");
1.34      misha     225:        }
                    226: 
                    227:        void disconnect(void *aconnection){
                    228:                Connection& connection=*static_cast<Connection*>(aconnection);
                    229:                PQfinish(connection.conn);
                    230:                connection.conn=0;
                    231:        }
                    232: 
                    233:        void commit(void *aconnection){
                    234:                Connection& connection=*static_cast<Connection*>(aconnection);
1.47      moko      235:                if(!connection.autocommit)
                    236:                        _execute_cmd(connection, "COMMIT");
1.34      misha     237:        }
                    238: 
                    239:        void rollback(void *aconnection){
                    240:                Connection& connection=*static_cast<Connection*>(aconnection);
1.47      moko      241:                if(!connection.autocommit)
                    242:                        _execute_cmd(connection, "ROLLBACK");
1.34      misha     243:        }
                    244: 
                    245:        bool ping(void *aconnection) {
                    246:                Connection& connection=*static_cast<Connection*>(aconnection);
                    247:                return PQstatus(connection.conn)==CONNECTION_OK;
                    248:        }
                    249: 
1.35      moko      250:        // charset here is services.request_charset(), not connection.client_charset
                    251:        // thus we can't use the sql server quoting support
1.48      moko      252:        const char* quote(void *aconnection, const char *str, unsigned int length){
1.42      misha     253:                Connection& connection=*static_cast<Connection*>(aconnection);
                    254: 
1.35      moko      255:                const char* from;
                    256:                const char* from_end=str+length;
                    257: 
                    258:                size_t quoted=0;
                    259: 
1.42      misha     260:                if(connection.standard_conforming_strings){
                    261:                        for(from=str; from<from_end; from++){
                    262:                                if(*from=='\'')
                    263:                                        quoted++;
                    264:                        }
                    265:                } else {
                    266:                        for(from=str; from<from_end; from++){
                    267:                                switch (*from) {
                    268:                                case '\'':
                    269:                                case '\\':
                    270:                                        quoted++;
                    271:                                }
1.35      moko      272:                        }
                    273:                }
                    274: 
                    275:                if(!quoted)
                    276:                        return str;
                    277: 
                    278:                char *result=(char*)connection.services->malloc_atomic(length + quoted + 1);
                    279:                char *to = result;
1.34      misha     280: 
1.42      misha     281:                if(connection.standard_conforming_strings){
                    282:                        for(from=str; from<from_end; from++){
                    283:                                if(*from=='\'')
                    284:                                        *to++= '\''; // "'" -> "''"
                    285:                                *to++=*from;
                    286:                        }
                    287:                } else {
                    288:                        for(from=str; from<from_end; from++){
                    289:                                switch (*from) {
                    290:                                case '\'': // "'" -> "''"
                    291:                                        *to++= '\'';
                    292:                                        break;
                    293:                                case '\\': // "\" -> "\\"
                    294:                                        *to++='\\';
                    295:                                        break;
                    296:                                }
                    297:                                *to++=*from;
1.35      moko      298:                        }
                    299:                }
                    300:                
                    301:                *to=0;
1.34      misha     302:                return result;
                    303:        }
1.35      moko      304: 
1.48      moko      305:        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     306:                Connection& connection=*static_cast<Connection*>(aconnection);
                    307:                SQL_Driver_services& services=*connection.services;
                    308:                PGconn *conn=connection.conn;
                    309: 
                    310:                const char* client_charset=connection.client_charset;
                    311:                const char* request_charset=services.request_charset();
                    312:                bool transcode_needed=client_charset && strcmp(client_charset, request_charset)!=0;
                    313: 
                    314:                const char** paramValues;
                    315:                if(placeholders_count>0){
                    316:                        int binds_size=sizeof(char)*placeholders_count;
                    317:                        paramValues = static_cast<const char**>(services.malloc_atomic(binds_size));
                    318:                        _bind_parameters(placeholders_count, placeholders, paramValues, connection, transcode_needed);
                    319:                }
                    320: 
1.36      misha     321:                size_t statement_size=0;
1.34      misha     322:                if(transcode_needed){
                    323:                        // transcode query from $request:charset to ?ClientCharset
1.36      misha     324:                        statement_size=strlen(astatement);
1.48      moko      325:                        services.transcode(astatement, statement_size, astatement, statement_size, request_charset, client_charset);
1.34      misha     326:                }
                    327: 
1.36      misha     328:                const char *statement=_preprocess_statement(connection, astatement, statement_size, offset, limit);
1.34      misha     329:                // error after prepare?
                    330: 
                    331:                PGresult *res;
                    332:                if(placeholders_count>0){
                    333:                        res=PQexecParams(conn, statement, placeholders_count, NULL, paramValues, NULL, NULL, 0);
                    334:                } else {
                    335:                        res=PQexec(conn, statement);
                    336:                }
                    337:                if(!res) 
                    338:                        throwPQerror;
                    339: 
                    340:                bool failed=false;
                    341:                SQL_Error sql_error;
                    342: 
                    343:                switch(PQresultStatus(res)) {
1.48      moko      344:                        case PGRES_EMPTY_QUERY:
1.34      misha     345:                                PQclear_throw("no query");
                    346:                                break;
                    347:                        case PGRES_COMMAND_OK: // empty result: insert|delete|update|...
                    348:                                PQclear(res);
                    349:                                return;
                    350:                        case PGRES_TUPLES_OK: 
1.47      moko      351:                                break;
1.34      misha     352:                        default:
                    353:                                PQclear_throwPQerror;
                    354:                                break;
                    355:                }
                    356:                
                    357: #define CHECK(afailed) \
                    358:                if(afailed) { \
                    359:                        failed=true; \
                    360:                        goto cleanup; \
                    361:                }
                    362: 
1.36      misha     363:                size_t column_count=PQnfields(res);
1.34      misha     364:                if(!column_count)
                    365:                        PQclear_throw("result contains no columns");
                    366: 
                    367:                if(column_count>MAX_COLS)
                    368:                        column_count=MAX_COLS;
                    369: 
                    370:                unsigned int column_types[MAX_COLS];
                    371: 
1.36      misha     372:                for(size_t i=0; i<column_count; i++){
1.34      misha     373:                        column_types[i]=PQftype(res, i);
1.36      misha     374: 
1.34      misha     375:                        char *name=PQfname(res, i);
                    376:                        size_t length=strlen(name);
1.36      misha     377:                        const char* str=strdup(services, name, length);
1.34      misha     378: 
1.36      misha     379:                        if(transcode_needed)
                    380:                                // transcode column name from ?ClientCharset to $request:charset
1.48      moko      381:                                services.transcode(str, length, str, length, client_charset, request_charset);
1.34      misha     382: 
                    383:                        CHECK(handlers.add_column(sql_error, str, length));
                    384:                }
                    385: 
                    386:                CHECK(handlers.before_rows(sql_error));
                    387: 
                    388:                if(unsigned long row_count=(unsigned long)PQntuples(res))
                    389:                        for(unsigned long r=0; r<row_count; r++) {
                    390:                                CHECK(handlers.add_row(sql_error));
1.36      misha     391:                                for(size_t i=0; i<column_count; i++){
                    392:                                        char *cell=PQgetvalue(res, r, i);
                    393: 
                    394:                                        size_t length=0;
1.34      misha     395:                                        const char* str;
                    396: 
                    397:                                        switch(column_types[i]){
1.36      misha     398:                                                case BOOLOID:
                    399:                                                case INT8OID:
                    400:                                                case INT2OID:
                    401:                                                case INT4OID:
                    402:                                                case FLOAT4OID:
                    403:                                                case FLOAT8OID:
                    404:                                                case DATEOID:
                    405:                                                case TIMEOID:
                    406:                                                case TIMESTAMPOID:
                    407:                                                case TIMESTAMPTZOID:
                    408:                                                case TIMETZOID:
                    409:                                                case NUMERICOID:
                    410:                                                        length=(size_t)PQgetlength(res, r, i);
                    411:                                                        str=length ? strdup(services, cell, length) : 0;
                    412:                                                        // transcode is never required for these types
                    413:                                                        break;
1.34      misha     414:                                                case OIDOID:
                    415:                                                        {
                    416:                                                                Oid oid=cell?atoi(cell):0;
                    417:                                                                int fd=lo_open(conn, oid, INV_READ);
                    418:                                                                if(fd>=0){
                    419:                                                                        // seek to end
                    420:                                                                        if(lo_lseek(conn, fd, 0, SEEK_END)<0)
                    421:                                                                                PQclear_throwPQerror;
                    422:                                                                        // get length
                    423:                                                                        int size_tell=lo_tell(conn, fd);
                    424:                                                                        if(size_tell<0)
                    425:                                                                                PQclear_throwPQerror;
                    426:                                                                        // seek to begin
                    427:                                                                        if(lo_lseek(conn, fd, 0, SEEK_SET)<0)
                    428:                                                                                PQclear_throwPQerror;
                    429:                                                                        length=(size_t)size_tell;
                    430:                                                                        if(length){
                    431:                                                                                // read 
                    432:                                                                                char* strm=(char*)services.malloc(length+1);
                    433:                                                                                if(!lo_read_ex(conn, fd, strm, size_tell))
                    434:                                                                                        PQclear_throw("lo_read can not read all bytes of object");
                    435:                                                                                strm[length]=0;
                    436:                                                                                str=strm;
1.36      misha     437:                                                                                if(transcode_needed) {
                    438:                                                                                        // transcode cell value from ?ClientCharset to $request:charset
                    439:                                                                                        services.transcode(str, length,
                    440:                                                                                                str, length,
                    441:                                                                                                client_charset,
                    442:                                                                                                request_charset);
                    443:                                                                                }
1.34      misha     444:                                                                        } else
                    445:                                                                                str=0;
                    446:                                                                        if(lo_close(conn, fd)<0)
                    447:                                                                                PQclear_throwPQerror;
                    448:                                                                } else
                    449:                                                                        PQclear_throwPQerror;
1.36      misha     450:                                                                break;
1.34      misha     451:                                                        }
                    452:                                                default:
                    453:                                                        // normal column, read it normally
                    454:                                                        length=(size_t)PQgetlength(res, r, i);
1.36      misha     455:                                                        str=length ? strdup(services, cell, length) : 0;
                    456:                                                        if(transcode_needed) {
                    457:                                                                // transcode cell value from ?ClientCharset to $request:charset
                    458:                                                                services.transcode(str, length,
                    459:                                                                        str, length,
                    460:                                                                        client_charset,
                    461:                                                                        request_charset);
                    462:                                                        }
                    463:                                                        break;
1.34      misha     464:                                        }
                    465:                                        CHECK(handlers.add_row_cell(sql_error, str, length));
                    466:                                }
                    467:                        }
                    468: cleanup:
                    469:                PQclear(res);
                    470:                if(failed)
                    471:                        services._throw(sql_error);
                    472:        }
                    473: 
                    474: private:
1.48      moko      475:        void _bind_parameters( size_t placeholders_count, Placeholder* placeholders, const char** paramValues, Connection& connection, bool transcode_needed){
1.34      misha     476:                for(size_t i=0; i<placeholders_count; i++){
                    477:                        Placeholder& ph=placeholders[i];
                    478:                        if(transcode_needed){
                    479:                                size_t name_length;
1.48      moko      480:                                connection.services->transcode(ph.name, strlen(ph.name), ph.name, name_length, connection.services->request_charset(), connection.client_charset);
1.34      misha     481: 
                    482:                                if(ph.value) {
                    483:                                        size_t value_length;
1.48      moko      484:                                        connection.services->transcode(ph.value, strlen(ph.value), ph.value, value_length, connection.services->request_charset(), connection.client_charset);
1.34      misha     485:                                }
                    486:                        }
1.36      misha     487:                        int name_number=atoi(ph.name);
                    488:                        if(name_number <= 0 || (size_t)name_number > placeholders_count)
1.34      misha     489:                                connection.services->_throw("bad bind parameter key");
                    490: 
1.36      misha     491:                        paramValues[name_number-1]=ph.value;
1.34      misha     492:                }
                    493:        }
                    494:        
                    495:        // executes a query and throw away the result.
                    496:        void _execute_cmd(const Connection& connection, const char *query){
                    497:                if(PGresult *res=PQexec(connection.conn, query))
                    498:                        PQclear(res); // throw away the result [don't need but must call]
                    499:                else
                    500:                        throwPQerror;
                    501:        }
                    502: 
1.48      moko      503:        const char *_preprocess_statement(Connection& connection, const char *astatement, size_t statement_size, unsigned long offset, unsigned long limit){
1.34      misha     504:                PGconn *conn=connection.conn;
                    505: 
1.36      misha     506:                if(!statement_size)
                    507:                        statement_size=strlen(astatement);
1.34      misha     508: 
                    509:                char *result=(char *)connection.services->malloc(statement_size
1.36      misha     510:                        +MAX_NUMBER*2+15 // " limit # offset #"
1.34      misha     511:                        +MAX_STRING // in case of short 'strings'
                    512:                        +1);
                    513:                // offset & limit -> suffixes
                    514:                const char *o;
                    515:                if(offset || limit!=SQL_NO_LIMIT){
                    516:                        char *cur=result;
                    517:                        memcpy(cur, astatement, statement_size); cur+=statement_size;
                    518:                        if(limit!=SQL_NO_LIMIT)
1.39      moko      519:                                cur+=snprintf(cur, 7+MAX_NUMBER, " limit %lu", limit);
1.34      misha     520:                        if(offset)
1.39      moko      521:                                cur+=snprintf(cur, 8+MAX_NUMBER, " offset %lu", offset);
1.34      misha     522:                        o=result;
                    523:                } else 
                    524:                        o=astatement;
                    525: 
                    526:                // /**xxx**/'literal' -> oid
                    527:                char *n=result;
                    528:                while(*o) {
                    529:                        if(
                    530:                                o[0]=='/' &&
1.36      misha     531:                                o[1]=='*' &&
1.34      misha     532:                                o[2]=='*') { // name start
                    533:                                const char* saved_o=o;
                    534:                                o+=3;
                    535:                                while(*o)
1.48      moko      536:                                        if(o[0]=='*' &&         o[1]=='*' && o[2]=='/' && o[3]=='\'') { // name end
1.34      misha     537:                                                saved_o=0; // found, marking that
                    538:                                                o+=4;
                    539:                                                Oid oid=lo_creat(conn, INV_READ|INV_WRITE);
                    540:                                                if(oid==InvalidOid)
                    541:                                                        throwPQerror;
                    542:                                                int fd=lo_open(conn, oid, INV_WRITE);
                    543:                                                if(fd>=0) {
                    544:                                                        const char *start=o;
                    545:                                                        bool escaped=false;
                    546:                                                        while(*o && !(o[0]=='\'' && o[1]!='\'' && !escaped)) {
                    547:                                                                escaped=*o=='\\' || (o[0]=='\'' && o[1]=='\'');
                    548:                                                                if(escaped) {
                    549:                                                                        // write pending, skip "\" or "'"
                    550:                                                                        if(!lo_write_ex(conn, fd, start, o-start))
                    551:                                                                                connection.services->_throw("lo_write could not write all bytes of object (1)");
                    552:                                                                        start=++o;
                    553:                                                                } else
                    554:                                                                        o++;
                    555:                                                        }
                    556:                                                        if(!lo_write_ex(conn, fd, start, o-start))
                    557:                                                                connection.services->_throw("lo_write can not write all bytes of object (2)");
                    558:                                                        if(lo_close(conn, fd)<0)
                    559:                                                                throwPQerror;
                    560:                                                } else
                    561:                                                        throwPQerror;
                    562:                                                if(*o)
                    563:                                                        o++; // skip "'"
                    564: 
                    565:                                                n+=snprintf(n, MAX_NUMBER, "%u", oid);
                    566:                                                break;
                    567:                                        } else
                    568:                                                o++; // /**skip**/'xxx'
                    569:                                if(saved_o) {
                    570:                                        o=saved_o;
                    571:                                        *n++=*o++;
                    572:                                }
                    573:                        } else
                    574:                                *n++=*o++;
                    575:                }
                    576:                *n=0;
                    577: 
                    578:                return result;
                    579:        }
                    580: 
                    581: private: // lo_read/write exchancements
                    582: 
                    583:        bool lo_read_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) {
                    584:                return lo_rw_method (conn, fd, buf, len, lo_read);
                    585:        }
                    586: 
                    587:        bool lo_write_ex(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len) {
                    588:                return lo_rw_method (conn, fd, buf, len, lo_write);
                    589:        }
                    590: 
                    591:        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)) {
                    592:                int size_op;
                    593:                while(len && (size_op=lo_func(conn, fd, buf, min(LO_BUFSIZE, len)))>0) {
                    594:                        buf+=size_op;
                    595:                        len-=size_op;
                    596:                }
                    597:                return len==0;
                    598:        }
                    599: 
                    600: private: // conn client library funcs
                    601: 
                    602:        typedef PGconn* (*t_PQsetdbLogin)(
                    603:                const char *pghost,
                    604:                const char *pgport,
                    605:                const char *pgoptions,
                    606:                const char *pgtty,
                    607:                const char *dbName,
                    608:                const char *login,
                    609:                const char *pwd); t_PQsetdbLogin PQsetdbLogin;
                    610:        typedef void (*t_PQfinish)(PGconn *conn);  t_PQfinish PQfinish;
                    611:        typedef char *(*t_PQerrorMessage)(const PGconn* conn); t_PQerrorMessage PQerrorMessage;
                    612:        typedef ConnStatusType (*t_PQstatus)(const PGconn *conn); t_PQstatus PQstatus;
                    613:        typedef PGresult *(*t_PQexec)(PGconn *conn,
                    614:                                                const char *query); t_PQexec PQexec;
                    615:        typedef PGresult *(*t_PQexecParams)(
                    616:                                                PGconn *conn,
                    617:                                                const char *query, 
                    618:                                                int nParams,
                    619:                                                const Oid *paramTypes,
                    620:                                                const char * const *paramValues,
                    621:                                                const int *paramLengths,
                    622:                                                const int *paramFormats,
                    623:                                                int resultFormat); t_PQexecParams PQexecParams;
                    624: 
                    625:        typedef ExecStatusType (*t_PQresultStatus)(const PGresult *res); t_PQresultStatus PQresultStatus;
                    626:        typedef int (*t_PQgetlength)(const PGresult *res,
                    627:                                                int tup_num,
                    628:                                                int field_num); t_PQgetlength PQgetlength;
                    629:        typedef char* (*t_PQgetvalue)(const PGresult *res,
                    630:                                                int tup_num,
                    631:                                                int field_num); t_PQgetvalue PQgetvalue;
                    632:        typedef int     (*t_PQntuples)(const PGresult *res); t_PQntuples PQntuples;
                    633:        typedef char *(*t_PQfname)(const PGresult *res,
                    634:                                                int field_index); t_PQfname PQfname;
                    635:        typedef int (*t_PQnfields)(const PGresult *res); t_PQnfields PQnfields;
                    636:        typedef void (*t_PQclear)(PGresult *res); t_PQclear PQclear;
                    637: 
                    638:        typedef Oid     (*t_PQftype)(const PGresult *res, int field_num); t_PQftype PQftype;
                    639: 
                    640:        typedef size_t (*t_PQescapeStringConn)(PGconn *conn,
                    641:                                                char *to, const char *from, size_t length,
                    642:                                                int *error); t_PQescapeStringConn PQescapeStringConn;
                    643: 
                    644:        typedef int     (*t_lo_open)(PGconn *conn, Oid lobjId, int mode); t_lo_open lo_open;
                    645:        typedef int     (*t_lo_close)(PGconn *conn, int fd); t_lo_close lo_close;
                    646:        typedef int     (*t_lo_read)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len); t_lo_read lo_read;
                    647:        typedef int     (*t_lo_write)(PGconn *conn, int fd, const/*paf*/ char *buf, size_t len); t_lo_write lo_write;
                    648:        typedef int     (*t_lo_lseek)(PGconn *conn, int fd, int offset, int whence); t_lo_lseek lo_lseek;
                    649:        typedef Oid     (*t_lo_creat)(PGconn *conn, int mode); t_lo_creat lo_creat;
                    650:        typedef int     (*t_lo_tell)(PGconn *conn, int fd); t_lo_tell lo_tell;
                    651:        typedef int     (*t_lo_unlink)(PGconn *conn, Oid lobjId); t_lo_unlink lo_unlink;
                    652:        typedef Oid     (*t_lo_import)(PGconn *conn, const char *filename); t_lo_import lo_import;
                    653:        typedef int     (*t_lo_export)(PGconn *conn, Oid lobjId, const char *filename); t_lo_export lo_export;
                    654: 
                    655: private: // conn client library funcs linking
                    656: 
                    657:        const char *dlink(const char *dlopen_file_spec) {
1.41      moko      658:                if(lt_dlinit()){
                    659:                        if(const char* result=lt_dlerror())
                    660:                                return result;
                    661:                        return "can not prepare to dynamic loading";
                    662:                }
                    663: 
1.34      misha     664:                lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
1.41      moko      665: 
                    666:                if(!handle){
                    667:                        if(const char* result=lt_dlerror())
                    668:                                return result;
1.34      misha     669:                        return "can not open the dynamic link module";
1.41      moko      670:                }
1.34      misha     671: 
                    672:                #define DSLINK(name, action) \
                    673:                        name=(t_##name)lt_dlsym(handle, #name); \
                    674:                                if(!name) \
                    675:                                        action;
                    676: 
                    677:                #define DLINK(name) DSLINK(name, return "function " #name " was not found")
                    678:                
                    679:                DLINK(PQsetdbLogin);
                    680:                DLINK(PQerrorMessage);
                    681:                DLINK(PQstatus);
                    682:                DLINK(PQfinish);
                    683:                DLINK(PQgetvalue);
                    684:                DLINK(PQgetlength);
                    685:                DLINK(PQntuples);
                    686:                DLINK(PQfname);
                    687:                DLINK(PQnfields);
                    688:                DLINK(PQclear);
                    689:                DLINK(PQresultStatus);
                    690:                DLINK(PQexec);
                    691:                DLINK(PQexecParams);
                    692:                DLINK(PQftype);
                    693:                DLINK(PQescapeStringConn);
1.48      moko      694:                DLINK(lo_open);   DLINK(lo_close);
                    695:                DLINK(lo_read);   DLINK(lo_write);
                    696:                DLINK(lo_lseek);  DLINK(lo_creat);
                    697:                DLINK(lo_tell);   DLINK(lo_unlink);
                    698:                DLINK(lo_import); DLINK(lo_export);
1.34      misha     699: 
                    700:                return 0;
                    701:        }
                    702: };
                    703: 
                    704: extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
                    705:        return new PgSQL_Driver();
                    706: }

E-mail: