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