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

1.54      paf         1: /** @file
                      2:        Parser: sql driver manager implementation.
                      3: 
1.71      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.80    ! paf         8: static const char * const IDENT_SQL_DRIVER_MANAGER_C="$Date: 2003/12/19 14:30:52 $";
1.54      paf         9: 
                     10: #include "pa_sql_driver_manager.h"
                     11: #include "ltdl.h"
1.71      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"
1.80    ! paf        18: #include "pa_charsets.h"
1.54      paf        19: 
                     20: // globals
                     21: 
1.78      paf        22: SQL_Driver_manager* SQL_driver_manager=0;
1.54      paf        23: 
                     24: // consts
                     25: 
1.71      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.71      paf        31: const String& SQL_Driver_services_impl::url_without_login() const {
                     32:        String& result=*new String;
                     33:        result << furl->mid(0, furl->pos(':')) << "://****";
                     34: 
                     35:        size_t at_pos=furl->pos('@');
                     36:        if(at_pos!=STRING_NOT_FOUND)
                     37:                result << furl->mid(at_pos, furl->length());
1.54      paf        38: 
                     39:        return result;
                     40: }
                     41: 
1.80    ! paf        42: void SQL_Driver_services_impl::transcode(const char* src, size_t src_length,
        !            43:        const char*& dst, size_t& dst_length,
        !            44:        const char* charset_from_name,
        !            45:        const char* charset_to_name
        !            46:        ) 
        !            47: {
        !            48:        try {
        !            49:                String::C result=Charset::transcode(String::C(src, src_length), 
        !            50:                        charsets.get(charset_from_name),
        !            51:                        charsets.get(charset_to_name));
        !            52:                dst=result.str;
        !            53:                dst_length=result.length;
        !            54:        } catch(const Exception& e) {
        !            55:                _throw(SQL_Error(e.type(), e.comment()));
        !            56:        } catch(...) {
        !            57:                _throw(SQL_Error(0, "unknown error while transcoding in sql driver"));
        !            58:        }
        !            59: }
        !            60: 
1.54      paf        61: // helpers
                     62: 
1.71      paf        63: static void expire_connection(SQL_Connection& connection, time_t older_dies) {
1.54      paf        64:        if(connection.connected() && connection.expired(older_dies))
                     65:                connection.disconnect();
                     66: }
1.71      paf        67: static void expire_connections(SQL_Driver_manager::connection_cache_type::key_type /*key*/, 
                     68:                               SQL_Driver_manager::connection_cache_type::value_type stack, 
                     69:                               time_t older_dies) {
1.73      paf        70:        for(size_t i=0; i<stack->top_index(); i++)
1.71      paf        71:                expire_connection(*stack->get(i), older_dies);
1.54      paf        72: }
                     73: 
                     74: // SQL_Driver_manager
                     75: 
1.71      paf        76: SQL_Driver_manager::SQL_Driver_manager(): 
1.75      paf        77:        is_dlinited(false), prev_expiration_pass_time(0) {
1.54      paf        78: }
                     79: 
                     80: SQL_Driver_manager::~SQL_Driver_manager() {
1.71      paf        81:        connection_cache.for_each(expire_connections, time(0)+(time_t)1/*=in future=expire all*/);
                     82: 
                     83:        if(is_dlinited)
                     84:                lt_dlexit();
1.54      paf        85: }
                     86: 
1.71      paf        87: /// @param aurl protocol://[driver-dependent]
                     88: SQL_Connection* SQL_Driver_manager::get_connection(const String& aurl,
1.80    ! paf        89:                                                   Table *protocol2driver_and_client,
        !            90:                                                   const char* arequest_charset) {
1.54      paf        91:        // we have table for locating protocol's library
                     92:        if(!protocol2driver_and_client)
1.62      paf        93:                throw Exception("parser.runtime",
1.71      paf        94:                        &aurl,
1.54      paf        95:                        "$"MAIN_SQL_NAME":"MAIN_SQL_DRIVERS_NAME" table must be defined");
                     96: 
                     97:        // first trying to get cached connection
1.71      paf        98:        SQL_Connection* connection=get_connection_from_cache(aurl);
1.58      paf        99:        if(connection) {
1.71      paf       100:                connection->set_url();
1.58      paf       101:                if(!connection->ping()) { // we have some cached connection, is it pingable?
                    102:                        connection->disconnect(); // kill unpingabe=dead connection
                    103:                        connection=0;
1.54      paf       104:                }
                    105:        }
                    106: 
1.71      paf       107:        char *url_cstr;
1.58      paf       108:        if(connection)
1.71      paf       109:                url_cstr=0; // calm, compiler
1.54      paf       110:        else { // no cached connection or it were unpingabe: connect/reconnect
1.71      paf       111:                url_cstr=aurl.cstrm();
                    112:                if(!strstr(url_cstr, "://"))
1.62      paf       113:                        throw Exception("parser.runtime",
1.71      paf       114:                                aurl.length()?&aurl:0,
1.62      paf       115:                                "connection string must start with protocol://");
1.54      paf       116: 
1.71      paf       117: 
                    118:                char *protocol_cstr=lsplit(&url_cstr, ':');
1.54      paf       119:                // skip "//" after ':'
1.71      paf       120:                while(*url_cstr=='/')
                    121:                        url_cstr++;
                    122:                const String& protocol=*new String(protocol_cstr);
1.54      paf       123: 
                    124:                SQL_Driver *driver;
                    125:                // first trying to get cached driver
1.71      paf       126:                if(!(driver=get_driver_from_cache(protocol))) {
1.54      paf       127:                        // no cached
1.71      paf       128:                        const String* library=0;
                    129:                        const String* dlopen_file_spec=0;
1.70      paf       130:                        Table::Action_options options;
1.71      paf       131:                        if(protocol2driver_and_client->locate(0, protocol, options)) {
                    132:                                if(!(library=protocol2driver_and_client->item(1)) || library->length()==0)
1.62      paf       133:                                        throw Exception("parser.runtime",
1.71      paf       134:                                                0,
1.54      paf       135:                                                "driver library column for protocol '%s' is empty", 
1.71      paf       136:                                                        protocol_cstr);
1.54      paf       137:                                dlopen_file_spec=protocol2driver_and_client->item(2);
                    138:                        } else
1.62      paf       139:                                throw Exception("parser.runtime",
1.71      paf       140:                                        &aurl,
1.54      paf       141:                                        "undefined protocol '%s'", 
1.71      paf       142:                                                protocol_cstr);
                    143: 
                    144:                        if(!is_dlinited) {
                    145:                                if(lt_dlinit())
                    146:                                        throw Exception(0,
                    147:                                                library,
                    148:                                                "prepare to dynamic loading failed, %s", lt_dlerror());
1.54      paf       149: 
1.71      paf       150:                                is_dlinited=true;
                    151:                        }
1.54      paf       152: 
1.71      paf       153:                        const char* filename=library->cstr(String::L_FILE_SPEC);
1.54      paf       154:                        lt_dlhandle handle=lt_dlopen(filename);
1.79      paf       155:                        if (!handle) {
                    156:                                const char* error=lt_dlerror();
1.62      paf       157:                                throw Exception(0,
1.54      paf       158:                                        library,
1.79      paf       159:                                        error?error:"can not open the module");
                    160:                        }
1.54      paf       161: 
                    162:                        SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, 
                    163:                                SQL_DRIVER_CREATE_NAME);
                    164:                        if(!create)
1.62      paf       165:                                throw Exception(0,
1.54      paf       166:                                        library,
                    167:                                        "function '"SQL_DRIVER_CREATE_NAME"' was not found");
                    168: 
                    169:                        // create library-driver!
                    170:                        driver=(*create)();
                    171: 
                    172:                        // validate driver api version
                    173:                        int driver_api_version=driver->api_version();
                    174:                        if(driver_api_version!=SQL_DRIVER_API_VERSION)
1.62      paf       175:                                throw Exception(0,
1.54      paf       176:                                        library,
                    177:                                        "driver implements API version 0x%04X not equal to 0x%04X",
                    178:                                                driver_api_version, SQL_DRIVER_API_VERSION);
                    179: 
                    180:                        // initialise by connecting to sql client dynamic link library
1.71      paf       181:                        char* dlopen_file_spec_cstr=
                    182:                                dlopen_file_spec && dlopen_file_spec->length()?
                    183:                                dlopen_file_spec->cstrm(String::L_AS_IS):0;
                    184:                        if(const char* error=driver->initialize(dlopen_file_spec_cstr))
1.62      paf       185:                                throw Exception(0,
1.54      paf       186:                                        library,
                    187:                                        "driver failed to initialize client library '%s', %s",
1.71      paf       188:                                                dlopen_file_spec_cstr?dlopen_file_spec_cstr:"unspecifed",
1.54      paf       189:                                                error);
                    190: 
                    191:                        // cache it
1.71      paf       192:                        put_driver_to_cache(protocol, driver);
1.54      paf       193:                }
                    194:        
1.80    ! paf       195:                connection=new SQL_Connection(aurl, *driver, arequest_charset);
1.71      paf       196:                // associate with pool[request]  (deassociates at close)
                    197:                connection->set_url(); 
1.54      paf       198:        }
                    199: 
1.58      paf       200:        // if not connected yet, do that now, when connection has services
                    201:        if(!connection->connected())
1.71      paf       202:                connection->connect(url_cstr);
1.58      paf       203:        // return autoclosing object for it
1.71      paf       204:        return connection;
1.54      paf       205: }
                    206: 
1.71      paf       207: void SQL_Driver_manager::close_connection(connection_cache_type::key_type url, 
                    208:                                          SQL_Connection* connection) {
1.54      paf       209:        put_connection_to_cache(url, connection);
                    210: }
                    211: 
                    212: 
                    213: // driver cache
                    214: 
1.71      paf       215: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(driver_cache_type::key_type protocol) {
1.54      paf       216:        SYNCHRONIZED;
                    217: 
1.71      paf       218:        return driver_cache.get(protocol);
1.54      paf       219: }
                    220: 
1.71      paf       221: void SQL_Driver_manager::put_driver_to_cache(
                    222:                                                                                         driver_cache_type::key_type protocol, 
                    223:                                                                                         driver_cache_type::value_type driver) {
1.54      paf       224:        SYNCHRONIZED;
                    225: 
1.71      paf       226:        driver_cache.put(protocol, driver);
1.54      paf       227: }
                    228: 
                    229: // connection cache
                    230: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
1.71      paf       231: SQL_Connection* SQL_Driver_manager::get_connection_from_cache(connection_cache_type::key_type url) { 
1.54      paf       232:        SYNCHRONIZED;
                    233: 
1.71      paf       234:        if(connection_cache_type::value_type connections=connection_cache.get(url))
                    235:                while(!connections->is_empty()) { // there are cached connections to that 'url'
                    236:                        SQL_Connection* result=connections->pop();
1.54      paf       237:                        if(result->connected()) // not expired?
                    238:                                return result;
                    239:                }
                    240: 
                    241:        return 0;
                    242: }
                    243: 
1.71      paf       244: void SQL_Driver_manager::put_connection_to_cache(
                    245:                                                 connection_cache_type::key_type url, 
                    246:                                                 SQL_Connection* connection) { 
1.54      paf       247:        SYNCHRONIZED;
                    248: 
1.71      paf       249:        connection_cache_type::value_type connections=connection_cache.get(url);
1.54      paf       250:        if(!connections) { // there are no cached connections to that 'url' yet?
1.71      paf       251:                connections=new connection_cache_element_base_type;
1.54      paf       252:                connection_cache.put(url, connections);
                    253:        }       
1.71      paf       254:        connections->push(connection);
1.54      paf       255: }
                    256: 
                    257: void SQL_Driver_manager::maybe_expire_cache() {
                    258:        time_t now=time(0);
                    259: 
                    260:        if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
1.71      paf       261:                connection_cache.for_each(expire_connections, time_t(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
1.54      paf       262: 
                    263:                prev_expiration_pass_time=now;
                    264:        }
                    265: }
                    266: 
1.76      paf       267: static void add_connection_to_status_cache_table(SQL_Connection& connection, Table* table) {
1.54      paf       268:        if(connection.connected()) {
1.76      paf       269:                ArrayString& row=*new ArrayString;
1.54      paf       270: 
                    271:                // url
1.76      paf       272:                row+=&connection.services().url_without_login();
1.54      paf       273:                // time
1.58      paf       274:                time_t time_used=connection.get_time_used();
1.76      paf       275:                row+=new String(pa_strdup(ctime(&time_used)));
1.54      paf       276: 
1.76      paf       277:                *table+=&row;
1.54      paf       278:        }
                    279: }
1.76      paf       280: static void add_connections_to_status_cache_table(
                    281:        SQL_Driver_manager::connection_cache_type::key_type /*key*/, 
                    282:        SQL_Driver_manager::connection_cache_type::value_type stack, Table* table) 
                    283: {
1.77      paf       284:        for(Array_iterator<SQL_Connection*> i(*stack); i.has_next(); )
                    285:                add_connection_to_status_cache_table(*i.next(), table);
1.76      paf       286: }
                    287: 
1.71      paf       288: Value* SQL_Driver_manager::get_status() {
                    289:        Value* result=new VHash;
1.76      paf       290: 
1.54      paf       291:        // cache
                    292:        {
1.76      paf       293:                ArrayString& columns=*new ArrayString;
1.71      paf       294:                columns+=new String("url");
                    295:                columns+=new String("time");
1.76      paf       296:                Table& table=*new Table(&columns, connection_cache.count());
1.54      paf       297: 
                    298:                connection_cache.for_each(add_connections_to_status_cache_table, &table);
                    299: 
1.76      paf       300:                result->get_hash()->put(*new String("cache"), new VTable(&table));
                    301:        }
1.54      paf       302: 
                    303:        return result;
1.57      paf       304: }

E-mail: