Annotation of parser3/src/main/pa_sql_driver_manager.C, revision 1.12

1.1       paf         1: /** @file
                      2:        Parser: sql driver manager implementation.
                      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      5: 
                      6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: 
1.12    ! parser      8:        $Id: pa_sql_driver_manager.C,v 1.11 2001/05/16 08:10:22 parser Exp $
1.1       paf         9: */
                     10: 
                     11: #include "pa_config_includes.h"
                     12: #include "ltdl.h"
                     13: #include "pa_sql_driver_manager.h"
1.2       paf        14: #include "pa_sql_connection.h"
1.1       paf        15: #include "pa_exception.h"
                     16: #include "pa_common.h"
                     17: 
                     18: // globals
                     19: 
                     20: SQL_Driver_manager *SQL_driver_manager;
                     21: 
                     22: // consts
                     23: 
                     24: const int MAX_PROTOCOL=20;
                     25: const char *LIBRARY_CREATE_FUNC_NAME="create";
                     26: 
1.3       paf        27: 
1.8       paf        28: /// SQL_Driver_services Pooled implementation
                     29: class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled {
1.3       paf        30: public:
1.8       paf        31:        SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool),
1.3       paf        32:                furl(aurl) {
                     33:        }
                     34: 
                     35:        /// allocates some bytes on pool
                     36:        void *malloc(size_t size) { return Pooled::malloc(size); }
                     37:        /// allocates some bytes clearing them with zeros
                     38:        void *calloc(size_t size) { return Pooled::calloc(size); }
                     39:        /// throw exception
                     40:        void _throw(const char *comment) { 
                     41:                THROW(0, 0, 
                     42:                        &furl, 
                     43:                        comment); 
                     44:        }
                     45: 
                     46: private:
                     47:        const String& furl;
                     48: };
                     49: 
                     50: // SQL_Driver_manager
                     51: 
1.12    ! parser     52: /// @param request_url protocol://[driver-dependent]
        !            53: SQL_Connection& SQL_Driver_manager::get_connection(const String& request_url, 
1.7       paf        54:                                                                                                   Table *protocol2driver_and_client) {
1.1       paf        55:        SYNCHRONIZED(true);
                     56: 
1.12    ! parser     57:        Pool& pool=request_url.pool(); // request pool                                                                                     
1.1       paf        58: 
                     59:        // we have table for locating protocol's library
1.7       paf        60:        if(!protocol2driver_and_client)
1.1       paf        61:                PTHROW(0, 0,
1.12    ! parser     62:                        &request_url,
1.6       paf        63:                        "$SQL:drivers table must be defined");
1.1       paf        64: 
1.12    ! parser     65:        // services associated with request
        !            66:        SQL_Driver_services_impl& services=
        !            67:                *new(pool) SQL_Driver_services_impl(pool, request_url);
        !            68: 
1.1       paf        69:        // first trying to get cached connection
1.12    ! parser     70:        if(SQL_Connection *result=get_connection_from_cache(request_url))
        !            71:                if(result->ping()) {
        !            72:                        result->set_services(&services);
1.5       paf        73:                        return *result;
1.12    ! parser     74:                } else
1.5       paf        75:                        result->disconnect(); // kill unpingabe=dead connection
1.1       paf        76:        // no cached connection
                     77: 
1.12    ! parser     78:        int pos=request_url.pos("://", 3);
1.1       paf        79:        if(pos<0)
                     80:                PTHROW(0, 0,
1.12    ! parser     81:                        &request_url,
1.1       paf        82:                        "no protocol specified"); // NOTE: not THROW, but PTHROW
                     83: 
1.12    ! parser     84:        // make global_url C-string on global pool
        !            85:        char *request_url_cstr=request_url.cstr(String::UL_AS_IS);
        !            86:        char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);
        !            87:        strcpy(global_url_cstr, request_url_cstr);
        !            88:        // make global_url string on global pool
        !            89:        String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);
1.1       paf        90:        
1.12    ! parser     91:        char *request_protocol_cstr=lsplit(&request_url_cstr, ':');
        !            92:        // skip "//" after ':'
        !            93:        while(*request_url_cstr=='/')
        !            94:                request_url_cstr++;
        !            95:        // make global_protocol C-string on global pool
        !            96:        char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);
        !            97:        strcpy(global_protocol_cstr, request_protocol_cstr);
        !            98:        // make global_protocol string on global pool
        !            99:        String& global_protocol=*new(this->pool()) String(this->pool(), 
        !           100:                global_protocol_cstr);
1.1       paf       101: 
                    102:        SQL_Driver *driver;
1.7       paf       103:        const String *dlopen_file_spec=0;
1.1       paf       104:        // first trying to get cached driver
1.12    ! parser    105:        if(!(driver=get_driver_from_cache(global_protocol))) {
1.1       paf       106:                // no cached
                    107:                const String *library=0;
1.12    ! parser    108:                if(protocol2driver_and_client->locate(0, global_protocol)) {
1.7       paf       109:                        if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
1.1       paf       110:                                PTHROW(0, 0,
1.7       paf       111:                                        protocol2driver_and_client->origin_string(),
1.12    ! parser    112:                                        "driver library column for protocol '%s' is empty", 
        !           113:                                                request_protocol_cstr);
1.7       paf       114:                        if(!(dlopen_file_spec=protocol2driver_and_client->item(2)) || dlopen_file_spec->size()==0)
                    115:                                PTHROW(0, 0,
                    116:                                        protocol2driver_and_client->origin_string(),
1.12    ! parser    117:                                        "client library column for protocol '%s' is empty", 
        !           118:                                                request_protocol_cstr);
1.1       paf       119:                } else
                    120:                        PTHROW(0, 0,
1.12    ! parser    121:                                &request_url,
        !           122:                                "undefined protocol '%s'", 
        !           123:                                        request_protocol_cstr);
1.1       paf       124: 
                    125:                const char *filename=library->cstr(String::UL_FILE_NAME);
1.2       paf       126:         lt_dlhandle handle=lt_dlopen(filename);
1.1       paf       127:         if (!handle)
                    128:                        PTHROW(0, 0,
                    129:                                library,
                    130:                                "can not open the module, %s", lt_dlerror());
                    131: 
                    132:         SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, 
                    133:                        LIBRARY_CREATE_FUNC_NAME);  
                    134:         if(!create)
                    135:                        PTHROW(0, 0,
                    136:                                library,
                    137:                                "function '%s' was not found", LIBRARY_CREATE_FUNC_NAME);
                    138: 
1.2       paf       139:                // create library-driver!
1.1       paf       140:                driver=(*create)();
                    141: 
                    142:                // validate driver api version
                    143:                int driver_api_version=driver->api_version();
1.7       paf       144:                if(driver_api_version!=SQL_DRIVER_API_VERSION)
1.1       paf       145:                        PTHROW(0, 0,
                    146:                                library,
1.7       paf       147:                                "driver implements API version 0x%04X not equal to 0x%04X",
1.3       paf       148:                                        driver_api_version, SQL_DRIVER_API_VERSION);
1.1       paf       149: 
1.7       paf       150:                // initialise by connecting to sql client dynamic link library
                    151:                const char *dlopen_file_spec_cstr=dlopen_file_spec->cstr(String::UL_FILE_NAME);
                    152:                if(const char *error=driver->initialize(
                    153:                        dlopen_file_spec_cstr))
                    154:                        PTHROW(0, 0,
                    155:                                library,
                    156:                                "driver failed to initialize client library '%s', %s",
                    157:                                        dlopen_file_spec_cstr, error);
                    158: 
1.1       paf       159:                // cache it
1.12    ! parser    160:                put_driver_to_cache(global_protocol, *driver);
1.1       paf       161:        }
                    162:        
1.4       paf       163:        // allocate in global pool 
                    164:        // associate with services[request], deassociates at close
1.3       paf       165:        SQL_Connection& result=
1.7       paf       166:                *new(this->pool()) SQL_Connection(this->pool(), 
1.12    ! parser    167:                global_url, *driver, services, request_url_cstr);
1.3       paf       168:        
                    169:        return result;
1.1       paf       170: }
                    171: 
                    172: void SQL_Driver_manager::close_connection(const String& url, 
                    173:                                                                                  SQL_Connection& connection) {
                    174:        SYNCHRONIZED(true);
                    175: 
                    176:        put_connection_to_cache(url, connection);
                    177: }
                    178: 
                    179: 
                    180: // driver cache
                    181: 
                    182: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
                    183:        if(SQL_Driver *result=static_cast<SQL_Driver *>(driver_cache.get(protocol)))
                    184:                return result;
                    185: 
                    186:        return 0;
                    187: }
                    188: 
                    189: void SQL_Driver_manager::put_driver_to_cache(const String& protocol, 
                    190:                                                                                         SQL_Driver& driver) {
                    191:        driver_cache.put(protocol, &driver);
                    192: }
                    193: 
                    194: // connection cache
                    195: 
                    196: SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) { 
                    197:        if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
                    198:                if(connections->size()) // there are cached connections to that 'url'
                    199:                        return static_cast<SQL_Connection *>(connections->pop());
                    200: 
                    201:        return 0;
                    202: }
                    203: 
1.10      paf       204: /// @todo cache expiration[use SQL_Driver::disconnect]
1.1       paf       205: void SQL_Driver_manager::put_connection_to_cache(const String& url, 
                    206:                                                                                                 SQL_Connection& connection) { 
                    207:        Stack *connections;
                    208:        if(!(connections=static_cast<Stack *>(connection_cache.get(url)))) {
                    209:                // there are no cached connections to that 'url' yet 
                    210:                connections=NEW Stack(pool()); // NOTE: never freed up!
                    211:                connection_cache.put(url, connections);
                    212:        }       
                    213:        connections->push(&connection);
                    214: }

E-mail: