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

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

E-mail: