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

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.7     ! paf         8:        $Id: pa_sql_driver_manager.C,v 1.6.2.1 2001/04/17 15:02:04 paf 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: 
                     28: /// Services_for_SQL_driver implementation
                     29: class Services_for_SQL_driver_impl : public Services_for_SQL_driver, public Pooled {
                     30: public:
                     31:        Services_for_SQL_driver_impl(Pool& apool, const String& aurl) : Pooled(apool),
                     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.1       paf        52: // url:
                     53: //   protocol://user:pass@host:port/database
                     54: //              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is driver-dependent
                     55: SQL_Connection& SQL_Driver_manager::get_connection(const String& url, 
1.7     ! paf        56:                                                                                                   Table *protocol2driver_and_client) {
1.1       paf        57:        SYNCHRONIZED(true);
                     58: 
                     59:        Pool& pool=url.pool(); // request pool                                                                                     
                     60: 
                     61:        // we have table for locating protocol's library
1.7     ! paf        62:        if(!protocol2driver_and_client)
1.1       paf        63:                PTHROW(0, 0,
                     64:                        &url,
1.6       paf        65:                        "$SQL:drivers table must be defined");
1.1       paf        66: 
                     67:        // first trying to get cached connection
                     68:        if(SQL_Connection *result=get_connection_from_cache(url))
1.5       paf        69:                if(result->ping())
                     70:                        return *result;
                     71:                else
                     72:                        result->disconnect(); // kill unpingabe=dead connection
1.1       paf        73:        // no cached connection
                     74: 
                     75:        int pos=url.pos("://", 3);
                     76:        if(pos<0)
                     77:                PTHROW(0, 0,
                     78:                        &url,
                     79:                        "no protocol specified"); // NOTE: not THROW, but PTHROW
                     80: 
                     81:        // make url string on global pool
                     82:        char *url_cstr=(char *)malloc(MAX_STRING);
                     83:        strncpy(url_cstr, url.cstr(String::UL_AS_IS), MAX_STRING);
                     84:        
                     85:        char *protocol_cstr=lsplit(&url_cstr, ':');
                     86:        String& protocol=*new(this->pool()) String(this->pool(), protocol_cstr);
                     87: 
                     88:        // skip // after :
                     89:        while(*url_cstr=='/')
                     90:                url_cstr++;
                     91: 
                     92:        SQL_Driver *driver;
1.7     ! paf        93:        const String *dlopen_file_spec=0;
1.1       paf        94:        // first trying to get cached driver
                     95:        if(!(driver=get_driver_from_cache(protocol))) {
                     96:                // no cached
                     97:                const String *library=0;
1.7     ! paf        98:                if(protocol2driver_and_client->locate(0, protocol)) {
        !            99:                        if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
1.1       paf       100:                                PTHROW(0, 0,
1.7     ! paf       101:                                        protocol2driver_and_client->origin_string(),
        !           102:                                        "driver library column for protocol '%s' is empty", protocol_cstr);
        !           103:                        if(!(dlopen_file_spec=protocol2driver_and_client->item(2)) || dlopen_file_spec->size()==0)
        !           104:                                PTHROW(0, 0,
        !           105:                                        protocol2driver_and_client->origin_string(),
        !           106:                                        "client library column for protocol '%s' is empty", protocol_cstr);
1.1       paf       107:                } else
                    108:                        PTHROW(0, 0,
1.2       paf       109:                                &url,
                    110:                                "undefined protocol '%s'", protocol_cstr);
1.1       paf       111: 
                    112:                const char *filename=library->cstr(String::UL_FILE_NAME);
1.2       paf       113:         lt_dlhandle handle=lt_dlopen(filename);
1.1       paf       114:         if (!handle)
                    115:                        PTHROW(0, 0,
                    116:                                library,
                    117:                                "can not open the module, %s", lt_dlerror());
                    118: 
                    119:         SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, 
                    120:                        LIBRARY_CREATE_FUNC_NAME);  
                    121:         if(!create)
                    122:                        PTHROW(0, 0,
                    123:                                library,
                    124:                                "function '%s' was not found", LIBRARY_CREATE_FUNC_NAME);
                    125: 
1.2       paf       126:                // create library-driver!
1.1       paf       127:                driver=(*create)();
                    128: 
                    129:                // validate driver api version
                    130:                int driver_api_version=driver->api_version();
1.7     ! paf       131:                if(driver_api_version!=SQL_DRIVER_API_VERSION)
1.1       paf       132:                        PTHROW(0, 0,
                    133:                                library,
1.7     ! paf       134:                                "driver implements API version 0x%04X not equal to 0x%04X",
1.3       paf       135:                                        driver_api_version, SQL_DRIVER_API_VERSION);
1.1       paf       136: 
1.7     ! paf       137:                // initialise by connecting to sql client dynamic link library
        !           138:                const char *dlopen_file_spec_cstr=dlopen_file_spec->cstr(String::UL_FILE_NAME);
        !           139:                if(const char *error=driver->initialize(
        !           140:                        dlopen_file_spec_cstr))
        !           141:                        PTHROW(0, 0,
        !           142:                                library,
        !           143:                                "driver failed to initialize client library '%s', %s",
        !           144:                                        dlopen_file_spec_cstr, error);
        !           145: 
1.1       paf       146:                // cache it
                    147:                put_driver_to_cache(protocol, *driver);
                    148:        }
                    149:        
1.4       paf       150:        // services associated with request
1.3       paf       151:        Services_for_SQL_driver_impl& services=
                    152:                *new(pool) Services_for_SQL_driver_impl(pool, url);
                    153: 
1.4       paf       154:        // allocate in global pool 
                    155:        // associate with services[request], deassociates at close
1.3       paf       156:        SQL_Connection& result=
1.7     ! paf       157:                *new(this->pool()) SQL_Connection(this->pool(), 
        !           158:                url, *driver, services, url_cstr);
1.3       paf       159:        
                    160:        return result;
1.1       paf       161: }
                    162: 
                    163: void SQL_Driver_manager::close_connection(const String& url, 
                    164:                                                                                  SQL_Connection& connection) {
                    165:        SYNCHRONIZED(true);
                    166: 
                    167:        put_connection_to_cache(url, connection);
                    168: }
                    169: 
                    170: 
                    171: // driver cache
                    172: 
                    173: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
                    174:        if(SQL_Driver *result=static_cast<SQL_Driver *>(driver_cache.get(protocol)))
                    175:                return result;
                    176: 
                    177:        return 0;
                    178: }
                    179: 
                    180: void SQL_Driver_manager::put_driver_to_cache(const String& protocol, 
                    181:                                                                                         SQL_Driver& driver) {
                    182:        driver_cache.put(protocol, &driver);
                    183: }
                    184: 
                    185: // connection cache
                    186: 
                    187: SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) { 
                    188:        if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
                    189:                if(connections->size()) // there are cached connections to that 'url'
                    190:                        return static_cast<SQL_Connection *>(connections->pop());
                    191: 
                    192:        return 0;
                    193: }
                    194: 
1.2       paf       195: /// @todo cache expiration[use SQL_Driver::disconnect], pinging. 
1.1       paf       196: void SQL_Driver_manager::put_connection_to_cache(const String& url, 
                    197:                                                                                                 SQL_Connection& connection) { 
                    198:        Stack *connections;
                    199:        if(!(connections=static_cast<Stack *>(connection_cache.get(url)))) {
                    200:                // there are no cached connections to that 'url' yet 
                    201:                connections=NEW Stack(pool()); // NOTE: never freed up!
                    202:                connection_cache.put(url, connections);
                    203:        }       
                    204:        connections->push(&connection);
                    205: }

E-mail: