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

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: 
        !             8:        $Id: pa_exception.C,v 1.9 2001/03/19 17:42:16 paf Exp $
        !             9: */
        !            10: 
        !            11: #include "pa_config_includes.h"
        !            12: #include "ltdl.h"
        !            13: #include "pa_sql_driver_manager.h"
        !            14: #include "pa_exception.h"
        !            15: #include "pa_common.h"
        !            16: 
        !            17: // globals
        !            18: 
        !            19: SQL_Driver_manager *SQL_driver_manager;
        !            20: 
        !            21: // consts
        !            22: 
        !            23: const int MAX_PROTOCOL=20;
        !            24: const char *LIBRARY_CREATE_FUNC_NAME="create";
        !            25: 
        !            26: // url:
        !            27: //   protocol://user:pass@host:port/database
        !            28: //              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is driver-dependent
        !            29: SQL_Connection& SQL_Driver_manager::get_connection(const String& url, 
        !            30:                                                                                                   Table *protocol2library) {
        !            31:        SYNCHRONIZED(true);
        !            32: 
        !            33:        Pool& pool=url.pool(); // request pool                                                                                     
        !            34: 
        !            35:        // we have table for locating protocol's library
        !            36:        if(!protocol2library)
        !            37:                PTHROW(0, 0,
        !            38:                        &url,
        !            39:                        "SQL:drivers table must be defined");
        !            40: 
        !            41:        // first trying to get cached connection
        !            42:        if(SQL_Connection *result=get_connection_from_cache(url))
        !            43:                return *result;
        !            44:        // no cached connection
        !            45: 
        !            46:        int pos=url.pos("://", 3);
        !            47:        if(pos<0)
        !            48:                PTHROW(0, 0,
        !            49:                        &url,
        !            50:                        "no protocol specified"); // NOTE: not THROW, but PTHROW
        !            51: 
        !            52:        // make url string on global pool
        !            53:        char *url_cstr=(char *)malloc(MAX_STRING);
        !            54:        strncpy(url_cstr, url.cstr(String::UL_AS_IS), MAX_STRING);
        !            55:        
        !            56:        char *protocol_cstr=lsplit(&url_cstr, ':');
        !            57:        String& protocol=*new(this->pool()) String(this->pool(), protocol_cstr);
        !            58: 
        !            59:        // skip // after :
        !            60:        while(*url_cstr=='/')
        !            61:                url_cstr++;
        !            62: 
        !            63:        SQL_Driver *driver;
        !            64:        // first trying to get cached driver
        !            65:        if(!(driver=get_driver_from_cache(protocol))) {
        !            66:                // no cached
        !            67:                const String *library=0;
        !            68:                if(protocol2library->locate(0, protocol)) {
        !            69:                        if(!(library=protocol2library->item(1)))
        !            70:                                PTHROW(0, 0,
        !            71:                                        protocol2library->origin_string(),
        !            72:                                        "library column for protocol '%s' is empty", protocol_cstr);
        !            73:                } else
        !            74:                        PTHROW(0, 0,
        !            75:                                protocol2library->origin_string(),
        !            76:                                "protocol '%s' not found", protocol_cstr);
        !            77: 
        !            78:                const char *filename=library->cstr(String::UL_FILE_NAME);
        !            79:         lt_dlhandle handle = lt_dlopen(filename);
        !            80:         if (!handle)
        !            81:                        PTHROW(0, 0,
        !            82:                                library,
        !            83:                                "can not open the module, %s", lt_dlerror());
        !            84: 
        !            85:         SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, 
        !            86:                        LIBRARY_CREATE_FUNC_NAME);  
        !            87:         if(!create)
        !            88:                        PTHROW(0, 0,
        !            89:                                library,
        !            90:                                "function '%s' was not found", LIBRARY_CREATE_FUNC_NAME);
        !            91: 
        !            92:                // create library-driver
        !            93:                driver=(*create)();
        !            94: 
        !            95:                // validate driver api version
        !            96:                int driver_api_version=driver->api_version();
        !            97:                if(driver_api_version < SQL_API_VERSION)
        !            98:                        PTHROW(0, 0,
        !            99:                                library,
        !           100:                                "driver API version is 0x%04X while current minimum is 0x%04X",
        !           101:                                        driver_api_version, SQL_API_VERSION);
        !           102: 
        !           103:                // cache it
        !           104:                put_driver_to_cache(protocol, *driver);
        !           105:        }
        !           106:        
        !           107:        void *info;
        !           108:        char *error;
        !           109:        if(!driver->connect(url_cstr, &info, &error))
        !           110:                PTHROW(0, 0,
        !           111:                        &url,
        !           112:                        "can not connect - %s", error);
        !           113: 
        !           114:        return *new(this->pool()) SQL_Connection(url.pool(), //< associate with request
        !           115:                *driver, info, url);
        !           116: }
        !           117: 
        !           118: void SQL_Driver_manager::close_connection(const String& url, 
        !           119:                                                                                  SQL_Connection& connection) {
        !           120:        SYNCHRONIZED(true);
        !           121: 
        !           122:        connection.set_pool(0); // deassociate from request
        !           123:        put_connection_to_cache(url, connection);
        !           124: }
        !           125: 
        !           126: 
        !           127: // driver cache
        !           128: 
        !           129: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
        !           130:        if(SQL_Driver *result=static_cast<SQL_Driver *>(driver_cache.get(protocol)))
        !           131:                return result;
        !           132: 
        !           133:        return 0;
        !           134: }
        !           135: 
        !           136: void SQL_Driver_manager::put_driver_to_cache(const String& protocol, 
        !           137:                                                                                         SQL_Driver& driver) {
        !           138:        driver_cache.put(protocol, &driver);
        !           139: }
        !           140: 
        !           141: // connection cache
        !           142: 
        !           143: SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) { 
        !           144:        if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
        !           145:                if(connections->size()) // there are cached connections to that 'url'
        !           146:                        return static_cast<SQL_Connection *>(connections->pop());
        !           147: 
        !           148:        return 0;
        !           149: }
        !           150: 
        !           151: void SQL_Driver_manager::put_connection_to_cache(const String& url, 
        !           152:                                                                                                 SQL_Connection& connection) { 
        !           153:        Stack *connections;
        !           154:        if(!(connections=static_cast<Stack *>(connection_cache.get(url)))) {
        !           155:                // there are no cached connections to that 'url' yet 
        !           156:                connections=NEW Stack(pool()); // NOTE: never freed up!
        !           157:                connection_cache.put(url, connections);
        !           158:        }       
        !           159:        connections->push(&connection);
        !           160: }

E-mail: