Annotation of parser3/src/main/pa_sql_driver_manager.C, revision 1.66
1.54 paf 1: /** @file
2: Parser: sql driver manager implementation.
3:
1.60 paf 4: Copyright (c) 2001, 2002 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.66 ! paf 8: static const char* IDENT_SQL_DRIVER_MANAGER_C="$Date: 2002/09/04 14:59:54 $";
1.54 paf 9:
10: #include "pa_sql_driver_manager.h"
11: #include "ltdl.h"
12: #include "pa_sql_connection.h"
13: #include "pa_exception.h"
14: #include "pa_common.h"
15: #include "pa_threads.h"
16: #include "pa_stack.h"
17: #include "pa_vhash.h"
18: #include "pa_vtable.h"
19:
20: // globals
21:
22: SQL_Driver_manager *SQL_driver_manager;
23:
24: // consts
25:
26: const int EXPIRE_UNUSED_CONNECTION_SECONDS=60;
27: const int CHECK_EXPIRED_CONNECTIONS_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
28:
29: // helpers
30:
31: const String& url_without_login(Pool& pool, const String& url) {
32: String& result=*new(pool) String(pool);
33: result << url.mid(0, url.pos(":")) << "://****";
34:
35: int at_pos=url.pos("@");
36: if(at_pos>0)
37: result << url.mid(at_pos, url.size());
38:
39: return result;
40: }
41:
42: /// SQL_Driver_services Pooled implementation
43: class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled {
44: public:
45: SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool),
46: furl(aurl) {
47: }
48:
49: virtual void *malloc(size_t size) { return Pooled::malloc(size, 8); }
50: virtual void *calloc(size_t size) { return Pooled::calloc(size); }
51:
52: /**
1.65 paf 53: normally we can't 'throw' from dynamic library, so
1.54 paf 54: the idea is to #1 jump to C++ some function to main body, where
55: every function stack frame has exception unwind information
56: and from there... #2 propagate_exception()
1.65 paf 57:
58: but when parser configured --with-sjlj-exceptions
59: one can simply 'throw' from dynamic library.
60: [sad story: one can not longjump/throw due to some bug in gcc as of 3.2.1 version]
1.54 paf 61: */
1.66 ! paf 62: virtual void _throw(const SQL_Exception& e) {
! 63: // converting SQL_exception to parser Exception
1.54 paf 64: // hiding passwords and addresses from accidental show [imagine user forgot @exception]
1.65 paf 65: #ifdef PA_WITH_SJLJ_EXCEPTIONS
66: throw
67: #else
68: e=
69: #endif
1.66 ! paf 70: Exception(e.ftype,
! 71: e.fproblem_source?static_cast<const String*>(e.fproblem_source)
! 72: :&url_without_login(pool(), furl),
! 73: e.fcomment);
1.54 paf 74:
1.65 paf 75: #ifndef PA_WITH_SJLJ_EXCEPTIONS
1.54 paf 76: longjmp(mark, 1);
1.65 paf 77: #endif
1.54 paf 78: }
79: virtual void propagate_exception() {
1.65 paf 80: #ifndef PA_WITH_SJLJ_EXCEPTIONS
1.54 paf 81: throw e;
1.65 paf 82: #endif
1.54 paf 83: }
84:
85: private:
86: const String& furl;
87: Exception e;
88: };
89:
90: // helpers
91:
92: static void expire_connection(Array::Item *value, void *info) {
93: SQL_Connection& connection=*static_cast<SQL_Connection *>(value);
94: time_t older_dies=reinterpret_cast<time_t>(info);
95:
96: if(connection.connected() && connection.expired(older_dies))
97: connection.disconnect();
98: }
99: static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {
100: Stack& stack=*static_cast<Stack *>(value);
101: for(int i=0; i<=stack.top_index(); i++)
102: expire_connection(stack.get(i), info);
103: }
104:
105: // SQL_Driver_manager
106:
107: SQL_Driver_manager::SQL_Driver_manager(Pool& apool) : Cache_manager(apool),
108: driver_cache(apool),
109: connection_cache(apool),
110: prev_expiration_pass_time(0) {
111: }
112:
113: SQL_Driver_manager::~SQL_Driver_manager() {
114: connection_cache.for_each(expire_connections,
1.56 paf 115: reinterpret_cast<void *>(time(0)+1/*=in future=expire all*/));
1.54 paf 116: }
117:
118: /// @param request_url protocol://[driver-dependent]
1.58 paf 119: SQL_Connection_ptr SQL_Driver_manager::get_connection(const String& request_url,
1.54 paf 120: const String& request_origin,
121: Table *protocol2driver_and_client) {
122: Pool& pool=request_origin.pool(); // request pool
123:
124: // we have table for locating protocol's library
125: if(!protocol2driver_and_client)
1.62 paf 126: throw Exception("parser.runtime",
1.54 paf 127: &request_url,
128: "$"MAIN_SQL_NAME":"MAIN_SQL_DRIVERS_NAME" table must be defined");
129:
130: // construct services[request] (deassociates at close)
131: SQL_Driver_services *services=new(pool) SQL_Driver_services_impl(pool, request_url);
132:
133: // first trying to get cached connection
1.58 paf 134: SQL_Connection *connection=get_connection_from_cache(request_url);
135: if(connection) {
136: connection->set_services(services);
137: if(!connection->ping()) { // we have some cached connection, is it pingable?
138: connection->disconnect(); // kill unpingabe=dead connection
139: connection=0;
1.54 paf 140: }
141: }
142:
143: char *request_url_cstr;
1.58 paf 144: if(connection)
1.54 paf 145: request_url_cstr=0; // calm, compiler
146: else { // no cached connection or it were unpingabe: connect/reconnect
147: int pos=request_url.pos("://", 3);
148: if(pos<0)
1.62 paf 149: throw Exception("parser.runtime",
1.54 paf 150: request_url.size()?&request_url:&request_origin,
1.62 paf 151: "connection string must start with protocol://");
1.54 paf 152:
153: // make global_url C-string on global pool
154: request_url_cstr=request_url.cstr();
155: char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);
156: strcpy(global_url_cstr, request_url_cstr);
157: // make global_url string on global pool
158: String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);
159:
160: char *request_protocol_cstr=lsplit(&request_url_cstr, ':');
161: // skip "//" after ':'
162: while(*request_url_cstr=='/')
163: request_url_cstr++;
164: // make global_protocol C-string on global pool
165: char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);
166: strcpy(global_protocol_cstr, request_protocol_cstr);
167: // make global_protocol string on global pool
168: String& global_protocol=*new(this->pool()) String(this->pool(),
169: global_protocol_cstr);
170:
171: SQL_Driver *driver;
172: // first trying to get cached driver
173: if(!(driver=get_driver_from_cache(global_protocol))) {
174: // no cached
175: const String *library=0;
176: const String *dlopen_file_spec=0;
177: if(protocol2driver_and_client->locate(0, global_protocol)) {
178: if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
1.62 paf 179: throw Exception("parser.runtime",
1.54 paf 180: protocol2driver_and_client->origin_string(),
181: "driver library column for protocol '%s' is empty",
182: request_protocol_cstr);
183: dlopen_file_spec=protocol2driver_and_client->item(2);
184: } else
1.62 paf 185: throw Exception("parser.runtime",
1.54 paf 186: &request_url,
187: "undefined protocol '%s'",
188: request_protocol_cstr);
189:
190: if(lt_dlinit())
1.62 paf 191: throw Exception(0,
1.54 paf 192: library,
193: "prepare to dynamic loading failed, %s", lt_dlerror());
194:
195: const char *filename=library->cstr(String::UL_FILE_SPEC);
196: lt_dlhandle handle=lt_dlopen(filename);
197: if (!handle)
1.62 paf 198: throw Exception(0,
1.54 paf 199: library,
200: "can not open the module, %s", lt_dlerror());
201:
202: SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle,
203: SQL_DRIVER_CREATE_NAME);
204: if(!create)
1.62 paf 205: throw Exception(0,
1.54 paf 206: library,
207: "function '"SQL_DRIVER_CREATE_NAME"' was not found");
208:
209: // create library-driver!
210: driver=(*create)();
211:
212: // validate driver api version
213: int driver_api_version=driver->api_version();
214: if(driver_api_version!=SQL_DRIVER_API_VERSION)
1.62 paf 215: throw Exception(0,
1.54 paf 216: library,
217: "driver implements API version 0x%04X not equal to 0x%04X",
218: driver_api_version, SQL_DRIVER_API_VERSION);
219:
220: // initialise by connecting to sql client dynamic link library
221: char *dlopen_file_spec_cstr=
222: dlopen_file_spec && dlopen_file_spec->size()?
223: dlopen_file_spec->cstr(String::UL_AS_IS):0;
224: if(const char *error=driver->initialize(
225: dlopen_file_spec_cstr))
1.62 paf 226: throw Exception(0,
1.54 paf 227: library,
228: "driver failed to initialize client library '%s', %s",
229: dlopen_file_spec_cstr?dlopen_file_spec_cstr:"unspecifed",
230: error);
231:
232: // cache it
233: put_driver_to_cache(global_protocol, *driver);
234: }
235:
236: // allocate in global pool
237: // associate with services[request]
238: // NOTE: never freed up!
1.58 paf 239: connection=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver);
1.54 paf 240: // associate with services[request] (deassociates at close)
1.58 paf 241: connection->set_services(services);
1.54 paf 242: }
243:
1.58 paf 244: // if not connected yet, do that now, when connection has services
245: if(!connection->connected())
246: connection->connect(request_url_cstr);
247: // return autoclosing object for it
1.59 paf 248: return SQL_Connection_ptr(connection);
1.54 paf 249: }
250:
251: void SQL_Driver_manager::close_connection(const String& url,
252: SQL_Connection& connection) {
253: // deassociate from services[request]
254: connection.set_services(0);
255: put_connection_to_cache(url, connection);
256: }
257:
258:
259: // driver cache
260:
261: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
262: SYNCHRONIZED;
263:
264: return static_cast<SQL_Driver *>(driver_cache.get(protocol));
265: }
266:
267: void SQL_Driver_manager::put_driver_to_cache(const String& protocol,
268: SQL_Driver& driver) {
269: SYNCHRONIZED;
270:
271: driver_cache.put(protocol, &driver);
272: }
273:
274: // connection cache
275: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
276: SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) {
277: SYNCHRONIZED;
278:
279: if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
280: while(connections->top_index()>=0) { // there are cached connections to that 'url'
281: SQL_Connection *result=static_cast<SQL_Connection *>(connections->pop());
282: if(result->connected()) // not expired?
283: return result;
284: }
285:
286: return 0;
287: }
288:
289: void SQL_Driver_manager::put_connection_to_cache(const String& url,
290: SQL_Connection& connection) {
291: SYNCHRONIZED;
292:
293: Stack *connections=static_cast<Stack *>(connection_cache.get(url));
294: if(!connections) { // there are no cached connections to that 'url' yet?
295: connections=NEW Stack(pool()); // NOTE: never freed up!
296: connection_cache.put(url, connections);
297: }
298: connections->push(&connection);
299: }
300:
301: void SQL_Driver_manager::maybe_expire_cache() {
302: time_t now=time(0);
303:
304: if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
305: connection_cache.for_each(expire_connections,
306: reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
307:
308: prev_expiration_pass_time=now;
309: }
310: }
311:
312: static void add_connection_to_status_cache_table(Array::Item *value, void *info) {
313: SQL_Connection& connection=*static_cast<SQL_Connection *>(value);
314: Table& table=*static_cast<Table *>(info);
315:
316: if(connection.connected()) {
317: Pool& pool=table.pool();
318: Array& row=*new(pool) Array(pool);
319:
320: // url
321: row+=&url_without_login(pool, connection.get_url());
322: // time
1.58 paf 323: time_t time_used=connection.get_time_used();
324: const char *unsafe_time_cstr=ctime(&time_used);
1.54 paf 325: int time_buf_size=strlen(unsafe_time_cstr);
326: char *safe_time_buf=(char *)pool.malloc(time_buf_size);
327: memcpy(safe_time_buf, unsafe_time_cstr, time_buf_size);
328: row+=new(pool) String(pool, safe_time_buf, time_buf_size);
329:
330: table+=&row;
331: }
332: }
333: static void add_connections_to_status_cache_table(const Hash::Key& key, Hash::Val *value, void *info) {
334: Stack& stack=*static_cast<Stack *>(value);
335: Array_iter iter(stack);
336: for(int countdown=stack.top_index(); countdown-->=0; )
337: add_connection_to_status_cache_table(iter.next(), info);
338: }
339: Value& SQL_Driver_manager::get_status(Pool& pool, const String *source) {
340: VHash& result=*new(pool) VHash(pool);
341:
342: // cache
343: {
344: Array& columns=*new(pool) Array(pool);
345: columns+=new(pool) String(pool, "url");
346: columns+=new(pool) String(pool, "time");
347: Table& table=*new(pool) Table(pool, 0, &columns, connection_cache.size());
348:
349: connection_cache.for_each(add_connections_to_status_cache_table, &table);
350:
351: result.hash(source).put(*new(pool) String(pool, "cache"), new(pool) VTable(pool, &table));
352: }
353:
354: return result;
1.57 paf 355: }
E-mail: