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

1.54      paf         1: /** @file
                      2:        Parser: sql driver manager implementation.
                      3: 
1.69.2.9! paf         4:        Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.61      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.63      paf         6: */
1.54      paf         7: 
1.69.2.9! paf         8: static const char* IDENT_SQL_DRIVER_MANAGER_C="$Date: 2003/01/31 12:10:47 $";
1.54      paf         9: 
                     10: #include "pa_sql_driver_manager.h"
                     11: #include "ltdl.h"
1.69.2.7  paf        12: #include "pa_threads.h"
1.54      paf        13: #include "pa_sql_connection.h"
                     14: #include "pa_exception.h"
                     15: #include "pa_common.h"
1.69.2.7  paf        16: #include "pa_value_includes.h"
1.54      paf        17: #include "pa_vhash.h"
                     18: #include "pa_vtable.h"
                     19: 
                     20: // globals
                     21: 
1.69.2.1  paf        22: SQL_Driver_manager SQL_driver_manager;
1.54      paf        23: 
                     24: // consts
                     25: 
1.69.2.4  paf        26: const time_t EXPIRE_UNUSED_CONNECTION_SECONDS=60;
                     27: const time_t CHECK_EXPIRED_CONNECTIONS_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
1.54      paf        28: 
                     29: // helpers
                     30: 
1.69.2.8  paf        31: StringPtr SQL_Driver_services_impl::url_without_login() const {
1.69.2.3  paf        32:        StringPtr result(new String);
1.69.2.4  paf        33:        *result << furl->mid(0, furl->pos(":")) << "://****";
1.54      paf        34: 
1.69.2.4  paf        35:        int at_pos=furl->pos("@");
1.54      paf        36:        if(at_pos>0)
1.69.2.4  paf        37:                *result << furl->mid(at_pos, furl->size());
1.54      paf        38: 
                     39:        return result;
                     40: }
                     41: 
                     42: // helpers
                     43: 
1.69.2.3  paf        44: static void expire_connection(SQL_Connection& connection, time_t older_dies) {
1.54      paf        45:        if(connection.connected() && connection.expired(older_dies))
                     46:                connection.disconnect();
                     47: }
1.69.2.3  paf        48: static void expire_connections(
                     49:                                                           SQL_Driver_manager::connection_cache_type::key_type /*key*/, 
                     50:                                                           SQL_Driver_manager::connection_cache_type::value_type stack, 
                     51:                                                           time_t older_dies) {
                     52:        for(int i=0; i<=stack->top_index(); i++)
                     53:                expire_connection(*stack->get(i), older_dies);
1.54      paf        54: }
                     55: 
                     56: // SQL_Driver_manager
                     57: 
1.69.2.1  paf        58: SQL_Driver_manager::SQL_Driver_manager(): 
                     59:        prev_expiration_pass_time(0) {
1.69.2.6  paf        60: 
1.69.2.8  paf        61:        cache_managers.put(StringPtr(new String("sql")), this);
1.54      paf        62: }
                     63: 
                     64: SQL_Driver_manager::~SQL_Driver_manager() {
1.69.2.3  paf        65:        connection_cache.for_each(expire_connections, time(0)+(time_t)1/*=in future=expire all*/);
1.54      paf        66: }
                     67: 
1.69.2.4  paf        68: /// @param aurl protocol://[driver-dependent]
1.69.2.8  paf        69: SQL_ConnectionPtr SQL_Driver_manager::get_connection(Pool& apool, StringPtr arequest_url,
                     70:                                                                                                   StringPtr aorigin,
1.54      paf        71:                                                                                                   Table *protocol2driver_and_client) {
                     72:        // we have table for locating protocol's library
                     73:        if(!protocol2driver_and_client)
1.62      paf        74:                throw Exception("parser.runtime",
1.69.2.4  paf        75:                        arequest_url,
1.54      paf        76:                        "$"MAIN_SQL_NAME":"MAIN_SQL_DRIVERS_NAME" table must be defined");
                     77: 
                     78:        // first trying to get cached connection
1.69.2.4  paf        79:        SQL_ConnectionPtr connection=get_connection_from_cache(arequest_url);
1.58      paf        80:        if(connection) {
1.69.2.4  paf        81:                connection->set_pool(&apool);
1.58      paf        82:                if(!connection->ping()) { // we have some cached connection, is it pingable?
                     83:                        connection->disconnect(); // kill unpingabe=dead connection
1.69.2.4  paf        84:                        connection=SQL_ConnectionPtr(0);
1.54      paf        85:                }
                     86:        }
                     87: 
                     88:        char *request_url_cstr;
1.58      paf        89:        if(connection)
1.54      paf        90:                request_url_cstr=0; // calm, compiler
                     91:        else { // no cached connection or it were unpingabe: connect/reconnect
1.69.2.4  paf        92:                if(arequest_url->pos("://")<0)
1.62      paf        93:                        throw Exception("parser.runtime",
1.69.2.4  paf        94:                                arequest_url->size()?arequest_url:aorigin,
1.62      paf        95:                                "connection string must start with protocol://");
1.54      paf        96: 
1.69.2.5  paf        97:                // make global_url C-string on driver_cache_pool pool
                     98:                StringPtr global_url(new String(arequest_url->cstr(driver_cache_pool)));
1.54      paf        99:                
1.69.2.4  paf       100:                request_url_cstr=arequest_url->cstr();
1.54      paf       101:                char *request_protocol_cstr=lsplit(&request_url_cstr, ':');
                    102:                // skip "//" after ':'
                    103:                while(*request_url_cstr=='/')
                    104:                        request_url_cstr++;
1.69.2.5  paf       105:                // make global_protocol C-string on driver_cache_pool pool
                    106:                StringPtr global_protocol(new String(driver_cache_pool.copy(request_protocol_cstr)));
1.54      paf       107: 
                    108:                SQL_Driver *driver;
                    109:                // first trying to get cached driver
                    110:                if(!(driver=get_driver_from_cache(global_protocol))) {
                    111:                        // no cached
1.69.2.8  paf       112:                        StringPtr library(0);
                    113:                        StringPtr dlopen_file_spec(0);
1.54      paf       114:                        if(protocol2driver_and_client->locate(0, global_protocol)) {
                    115:                                if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
1.62      paf       116:                                        throw Exception("parser.runtime",
1.54      paf       117:                                                protocol2driver_and_client->origin_string(),
                    118:                                                "driver library column for protocol '%s' is empty", 
                    119:                                                        request_protocol_cstr);
                    120:                                dlopen_file_spec=protocol2driver_and_client->item(2);
                    121:                        } else
1.62      paf       122:                                throw Exception("parser.runtime",
1.69.2.4  paf       123:                                        arequest_url,
1.54      paf       124:                                        "undefined protocol '%s'", 
                    125:                                                request_protocol_cstr);
                    126: 
                    127:                        if(lt_dlinit())
1.62      paf       128:                                throw Exception(0,
1.54      paf       129:                                        library,
                    130:                                        "prepare to dynamic loading failed, %s", lt_dlerror());
                    131: 
1.69.2.9! paf       132:                        const char* filename=library->cstr(String::UL_FILE_SPEC);
1.54      paf       133:                        lt_dlhandle handle=lt_dlopen(filename);
                    134:                        if (!handle)
1.62      paf       135:                                throw Exception(0,
1.54      paf       136:                                        library,
                    137:                                        "can not open the module, %s", lt_dlerror());
                    138: 
                    139:                        SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, 
                    140:                                SQL_DRIVER_CREATE_NAME);
                    141:                        if(!create)
1.62      paf       142:                                throw Exception(0,
1.54      paf       143:                                        library,
                    144:                                        "function '"SQL_DRIVER_CREATE_NAME"' was not found");
                    145: 
                    146:                        // create library-driver!
                    147:                        driver=(*create)();
                    148: 
                    149:                        // validate driver api version
                    150:                        int driver_api_version=driver->api_version();
                    151:                        if(driver_api_version!=SQL_DRIVER_API_VERSION)
1.62      paf       152:                                throw Exception(0,
1.54      paf       153:                                        library,
                    154:                                        "driver implements API version 0x%04X not equal to 0x%04X",
                    155:                                                driver_api_version, SQL_DRIVER_API_VERSION);
                    156: 
                    157:                        // initialise by connecting to sql client dynamic link library
1.69.2.4  paf       158:                        CharPtr dlopen_file_spec_cstr=
1.54      paf       159:                                dlopen_file_spec && dlopen_file_spec->size()?
1.69.2.4  paf       160:                                dlopen_file_spec->cstr(String::UL_AS_IS):CharPtrZero;
1.69.2.9! paf       161:                        if(const char* error=driver->initialize(
1.54      paf       162:                                dlopen_file_spec_cstr))
1.62      paf       163:                                throw Exception(0,
1.54      paf       164:                                        library,
                    165:                                        "driver failed to initialize client library '%s', %s",
                    166:                                                dlopen_file_spec_cstr?dlopen_file_spec_cstr:"unspecifed", 
                    167:                                                error);
                    168: 
                    169:                        // cache it
1.69.2.4  paf       170:                        put_driver_to_cache(global_protocol, driver);
1.54      paf       171:                }
                    172:        
1.69.2.4  paf       173:                connection=SQL_ConnectionPtr(new SQL_Connection(global_url, *driver));
                    174:                // associate with pool[request]  (deassociates at close)
                    175:                connection->set_pool(&apool); 
1.54      paf       176:        }
                    177: 
1.58      paf       178:        // if not connected yet, do that now, when connection has services
                    179:        if(!connection->connected())
                    180:                connection->connect(request_url_cstr);
                    181:        // return autoclosing object for it
1.69.2.1  paf       182:        return SQL_ConnectionPtr(connection);
1.54      paf       183: }
                    184: 
1.69.2.4  paf       185: void SQL_Driver_manager::close_connection(connection_cache_type::key_type url, 
                    186:                                                                                  SQL_ConnectionPtr connection) {
                    187:        // deassociate from pool[request]
                    188:        connection->set_pool(0);
1.54      paf       189:        put_connection_to_cache(url, connection);
                    190: }
                    191: 
                    192: 
                    193: // driver cache
                    194: 
1.69.2.4  paf       195: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(driver_cache_type::key_type protocol) {
1.54      paf       196:        SYNCHRONIZED;
                    197: 
1.69.2.4  paf       198:        return driver_cache.get(protocol);
1.54      paf       199: }
                    200: 
1.69.2.4  paf       201: void SQL_Driver_manager::put_driver_to_cache(
                    202:                                                                                         driver_cache_type::key_type protocol, 
                    203:                                                                                         driver_cache_type::value_type driver) {
1.54      paf       204:        SYNCHRONIZED;
                    205: 
1.69.2.4  paf       206:        driver_cache.put(protocol, driver);
1.54      paf       207: }
                    208: 
                    209: // connection cache
1.69.2.4  paf       210: SQL_ConnectionPtr SQL_Driver_manager::get_connection_from_cache(
                    211:                                                                                                                                connection_cache_type::key_type url) { 
1.54      paf       212:        SYNCHRONIZED;
                    213: 
1.69.2.4  paf       214:        if(connection_cache_type::value_type connections=connection_cache.get(url))
1.54      paf       215:                while(connections->top_index()>=0) { // there are cached connections to that 'url'
1.69.2.4  paf       216:                        SQL_ConnectionPtr result=connections->pop();
1.54      paf       217:                        if(result->connected()) // not expired?
                    218:                                return result;
                    219:                }
                    220: 
                    221:        return 0;
                    222: }
                    223: 
1.69.2.4  paf       224: void SQL_Driver_manager::put_connection_to_cache(
                    225:                                                                                                 connection_cache_type::key_type url, 
                    226:                                                                                                 SQL_ConnectionPtr connection) { 
1.54      paf       227:        SYNCHRONIZED;
                    228: 
1.69.2.4  paf       229:        connection_cache_type::value_type connections=connection_cache.get(url);
1.54      paf       230:        if(!connections) { // there are no cached connections to that 'url' yet?
1.69.2.4  paf       231:                connections=connection_cache_type::value_type(new connection_cache_type::value_type::element_type);
1.54      paf       232:                connection_cache.put(url, connections);
                    233:        }       
1.69.2.4  paf       234:        connections->push(connection);
1.54      paf       235: }
                    236: 
                    237: void SQL_Driver_manager::maybe_expire_cache() {
                    238:        time_t now=time(0);
                    239: 
                    240:        if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
1.69.2.4  paf       241:                connection_cache.for_each(expire_connections, time_t(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
1.54      paf       242: 
                    243:                prev_expiration_pass_time=now;
                    244:        }
                    245: }
1.69.2.4  paf       246: /*
1.54      paf       247: static void add_connection_to_status_cache_table(Array::Item *value, void *info) {
                    248:        SQL_Connection& connection=*static_cast<SQL_Connection *>(value);
                    249:        Table& table=*static_cast<Table *>(info);
                    250: 
                    251:        if(connection.connected()) {
                    252:                Pool& pool=table.pool();
                    253:                Array& row=*new(pool) Array(pool);
                    254: 
                    255:                // url
                    256:                row+=&url_without_login(pool, connection.get_url());
                    257:                // time
1.58      paf       258:                time_t time_used=connection.get_time_used();
1.69.2.9! paf       259:                const char* unsafe_time_cstr=ctime(&time_used);
1.54      paf       260:                int time_buf_size=strlen(unsafe_time_cstr);
                    261:                char *safe_time_buf=(char *)pool.malloc(time_buf_size);
                    262:                memcpy(safe_time_buf, unsafe_time_cstr, time_buf_size);
                    263:                row+=new(pool) String(pool, safe_time_buf, time_buf_size);
                    264: 
                    265:                table+=&row;
                    266:        }
                    267: }
                    268: static void add_connections_to_status_cache_table(const Hash::Key& key, Hash::Val *value, void *info) {
                    269:        Stack& stack=*static_cast<Stack *>(value);
                    270:        Array_iter iter(stack);
                    271:        for(int countdown=stack.top_index(); countdown-->=0; )
                    272:                add_connection_to_status_cache_table(iter.next(), info);
1.69.2.4  paf       273: }*/
                    274: /// @todo convert to object_ptr
1.69.2.8  paf       275: ValuePtr SQL_Driver_manager::get_status(Pool& pool, StringPtr source) {
1.69.2.4  paf       276:        ValuePtr result(new VHash);
                    277:        /*
1.54      paf       278:        // cache
                    279:        {
                    280:                Array& columns=*new(pool) Array(pool);
                    281:                columns+=new(pool) String(pool, "url");
                    282:                columns+=new(pool) String(pool, "time");
                    283:                Table& table=*new(pool) Table(pool, 0, &columns, connection_cache.size());
                    284: 
                    285:                connection_cache.for_each(add_connections_to_status_cache_table, &table);
                    286: 
                    287:                result.hash(source).put(*new(pool) String(pool, "cache"), new(pool) VTable(pool, &table));
1.69.2.4  paf       288:        }*/
1.54      paf       289: 
                    290:        return result;
1.57      paf       291: }

E-mail: