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