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