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

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

E-mail: