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