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