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

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

E-mail: