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

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

E-mail: