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

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.14    ! parser      8:        $Id: pa_sql_driver_manager.C,v 1.13 2001/05/17 09:47:17 parser Exp $
1.1       paf         9: */
                     10: 
1.14    ! parser     11: #include "pa_sql_driver_manager.h"
1.1       paf        12: #include "ltdl.h"
1.2       paf        13: #include "pa_sql_connection.h"
1.1       paf        14: #include "pa_exception.h"
                     15: #include "pa_common.h"
1.14    ! parser     16: #include "pa_threads.h"
1.1       paf        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.12      parser     55:        Pool& pool=request_url.pool(); // request pool                                                                                     
1.1       paf        56: 
                     57:        // we have table for locating protocol's library
1.7       paf        58:        if(!protocol2driver_and_client)
1.1       paf        59:                PTHROW(0, 0,
1.12      parser     60:                        &request_url,
1.6       paf        61:                        "$SQL:drivers table must be defined");
1.1       paf        62: 
1.12      parser     63:        // services associated with request
                     64:        SQL_Driver_services_impl& services=
                     65:                *new(pool) SQL_Driver_services_impl(pool, request_url);
                     66: 
1.1       paf        67:        // first trying to get cached connection
1.12      parser     68:        if(SQL_Connection *result=get_connection_from_cache(request_url))
                     69:                if(result->ping()) {
                     70:                        result->set_services(&services);
1.5       paf        71:                        return *result;
1.12      parser     72:                } else
1.5       paf        73:                        result->disconnect(); // kill unpingabe=dead connection
1.1       paf        74:        // no cached connection
                     75: 
1.12      parser     76:        int pos=request_url.pos("://", 3);
1.1       paf        77:        if(pos<0)
                     78:                PTHROW(0, 0,
1.12      parser     79:                        &request_url,
1.1       paf        80:                        "no protocol specified"); // NOTE: not THROW, but PTHROW
                     81: 
1.12      parser     82:        // make global_url C-string on global pool
                     83:        char *request_url_cstr=request_url.cstr(String::UL_AS_IS);
                     84:        char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);
                     85:        strcpy(global_url_cstr, request_url_cstr);
                     86:        // make global_url string on global pool
                     87:        String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);
1.1       paf        88:        
1.12      parser     89:        char *request_protocol_cstr=lsplit(&request_url_cstr, ':');
                     90:        // skip "//" after ':'
                     91:        while(*request_url_cstr=='/')
                     92:                request_url_cstr++;
                     93:        // make global_protocol C-string on global pool
                     94:        char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);
                     95:        strcpy(global_protocol_cstr, request_protocol_cstr);
                     96:        // make global_protocol string on global pool
                     97:        String& global_protocol=*new(this->pool()) String(this->pool(), 
                     98:                global_protocol_cstr);
1.1       paf        99: 
                    100:        SQL_Driver *driver;
1.7       paf       101:        const String *dlopen_file_spec=0;
1.1       paf       102:        // first trying to get cached driver
1.12      parser    103:        if(!(driver=get_driver_from_cache(global_protocol))) {
1.1       paf       104:                // no cached
                    105:                const String *library=0;
1.12      parser    106:                if(protocol2driver_and_client->locate(0, global_protocol)) {
1.7       paf       107:                        if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
1.1       paf       108:                                PTHROW(0, 0,
1.7       paf       109:                                        protocol2driver_and_client->origin_string(),
1.12      parser    110:                                        "driver library column for protocol '%s' is empty", 
                    111:                                                request_protocol_cstr);
1.7       paf       112:                        if(!(dlopen_file_spec=protocol2driver_and_client->item(2)) || dlopen_file_spec->size()==0)
                    113:                                PTHROW(0, 0,
                    114:                                        protocol2driver_and_client->origin_string(),
1.12      parser    115:                                        "client library column for protocol '%s' is empty", 
                    116:                                                request_protocol_cstr);
1.1       paf       117:                } else
                    118:                        PTHROW(0, 0,
1.12      parser    119:                                &request_url,
                    120:                                "undefined protocol '%s'", 
                    121:                                        request_protocol_cstr);
1.1       paf       122: 
                    123:                const char *filename=library->cstr(String::UL_FILE_NAME);
1.2       paf       124:         lt_dlhandle handle=lt_dlopen(filename);
1.1       paf       125:         if (!handle)
                    126:                        PTHROW(0, 0,
                    127:                                library,
                    128:                                "can not open the module, %s", lt_dlerror());
                    129: 
                    130:         SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, 
                    131:                        LIBRARY_CREATE_FUNC_NAME);  
                    132:         if(!create)
                    133:                        PTHROW(0, 0,
                    134:                                library,
                    135:                                "function '%s' was not found", LIBRARY_CREATE_FUNC_NAME);
                    136: 
1.2       paf       137:                // create library-driver!
1.1       paf       138:                driver=(*create)();
                    139: 
                    140:                // validate driver api version
                    141:                int driver_api_version=driver->api_version();
1.7       paf       142:                if(driver_api_version!=SQL_DRIVER_API_VERSION)
1.1       paf       143:                        PTHROW(0, 0,
                    144:                                library,
1.7       paf       145:                                "driver implements API version 0x%04X not equal to 0x%04X",
1.3       paf       146:                                        driver_api_version, SQL_DRIVER_API_VERSION);
1.1       paf       147: 
1.7       paf       148:                // initialise by connecting to sql client dynamic link library
                    149:                const char *dlopen_file_spec_cstr=dlopen_file_spec->cstr(String::UL_FILE_NAME);
                    150:                if(const char *error=driver->initialize(
                    151:                        dlopen_file_spec_cstr))
                    152:                        PTHROW(0, 0,
                    153:                                library,
                    154:                                "driver failed to initialize client library '%s', %s",
                    155:                                        dlopen_file_spec_cstr, error);
                    156: 
1.1       paf       157:                // cache it
1.12      parser    158:                put_driver_to_cache(global_protocol, *driver);
1.1       paf       159:        }
                    160:        
1.4       paf       161:        // allocate in global pool 
                    162:        // associate with services[request], deassociates at close
1.3       paf       163:        SQL_Connection& result=
1.7       paf       164:                *new(this->pool()) SQL_Connection(this->pool(), 
1.12      parser    165:                global_url, *driver, services, request_url_cstr);
1.3       paf       166:        
                    167:        return result;
1.1       paf       168: }
                    169: 
                    170: void SQL_Driver_manager::close_connection(const String& url, 
                    171:                                                                                  SQL_Connection& connection) {
                    172:        put_connection_to_cache(url, connection);
                    173: }
                    174: 
                    175: 
                    176: // driver cache
                    177: 
                    178: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
1.13      parser    179:        SYNCHRONIZED;
                    180: 
1.1       paf       181:        if(SQL_Driver *result=static_cast<SQL_Driver *>(driver_cache.get(protocol)))
                    182:                return result;
                    183: 
                    184:        return 0;
                    185: }
                    186: 
                    187: void SQL_Driver_manager::put_driver_to_cache(const String& protocol, 
                    188:                                                                                         SQL_Driver& driver) {
1.13      parser    189:        SYNCHRONIZED;
                    190: 
1.1       paf       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) { 
1.13      parser    197:        SYNCHRONIZED;
                    198: 
1.1       paf       199:        if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
                    200:                if(connections->size()) // there are cached connections to that 'url'
                    201:                        return static_cast<SQL_Connection *>(connections->pop());
                    202: 
                    203:        return 0;
                    204: }
                    205: 
1.10      paf       206: /// @todo cache expiration[use SQL_Driver::disconnect]
1.1       paf       207: void SQL_Driver_manager::put_connection_to_cache(const String& url, 
                    208:                                                                                                 SQL_Connection& connection) { 
1.13      parser    209:        SYNCHRONIZED;
                    210: 
1.1       paf       211:        Stack *connections;
                    212:        if(!(connections=static_cast<Stack *>(connection_cache.get(url)))) {
                    213:                // there are no cached connections to that 'url' yet 
                    214:                connections=NEW Stack(pool()); // NOTE: never freed up!
                    215:                connection_cache.put(url, connections);
                    216:        }       
                    217:        connections->push(&connection);
                    218: }

E-mail: