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

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

E-mail: