Annotation of parser3/src/main/pa_sql_driver_manager.C, revision 1.70
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.70 ! paf 8: static const char* IDENT_SQL_DRIVER_MANAGER_C="$Date: 2003/01/21 15:51:15 $";
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.68 paf 62: virtual void _throw(const SQL_Error& aexception) {
1.66 paf 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
1.67 paf 68: fexception=
1.65 paf 69: #endif
1.68 paf 70: Exception(aexception.type(),
71: aexception.problem_source()?static_cast<const String*>(aexception.problem_source())
1.66 paf 72: :&url_without_login(pool(), furl),
1.68 paf 73: aexception.comment());
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.67 paf 81: throw fexception;
1.65 paf 82: #endif
1.54 paf 83: }
84:
85: private:
86: const String& furl;
1.67 paf 87: Exception fexception;
1.54 paf 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;
1.70 ! paf 177: Table::Action_options options;
! 178: if(protocol2driver_and_client->locate(0, global_protocol, options)) {
1.54 paf 179: if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
1.62 paf 180: throw Exception("parser.runtime",
1.54 paf 181: protocol2driver_and_client->origin_string(),
182: "driver library column for protocol '%s' is empty",
183: request_protocol_cstr);
184: dlopen_file_spec=protocol2driver_and_client->item(2);
185: } else
1.62 paf 186: throw Exception("parser.runtime",
1.54 paf 187: &request_url,
188: "undefined protocol '%s'",
189: request_protocol_cstr);
190:
191: if(lt_dlinit())
1.62 paf 192: throw Exception(0,
1.54 paf 193: library,
194: "prepare to dynamic loading failed, %s", lt_dlerror());
195:
196: const char *filename=library->cstr(String::UL_FILE_SPEC);
197: lt_dlhandle handle=lt_dlopen(filename);
198: if (!handle)
1.62 paf 199: throw Exception(0,
1.54 paf 200: library,
201: "can not open the module, %s", lt_dlerror());
202:
203: SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle,
204: SQL_DRIVER_CREATE_NAME);
205: if(!create)
1.62 paf 206: throw Exception(0,
1.54 paf 207: library,
208: "function '"SQL_DRIVER_CREATE_NAME"' was not found");
209:
210: // create library-driver!
211: driver=(*create)();
212:
213: // validate driver api version
214: int driver_api_version=driver->api_version();
215: if(driver_api_version!=SQL_DRIVER_API_VERSION)
1.62 paf 216: throw Exception(0,
1.54 paf 217: library,
218: "driver implements API version 0x%04X not equal to 0x%04X",
219: driver_api_version, SQL_DRIVER_API_VERSION);
220:
221: // initialise by connecting to sql client dynamic link library
222: char *dlopen_file_spec_cstr=
223: dlopen_file_spec && dlopen_file_spec->size()?
224: dlopen_file_spec->cstr(String::UL_AS_IS):0;
225: if(const char *error=driver->initialize(
226: dlopen_file_spec_cstr))
1.62 paf 227: throw Exception(0,
1.54 paf 228: library,
229: "driver failed to initialize client library '%s', %s",
230: dlopen_file_spec_cstr?dlopen_file_spec_cstr:"unspecifed",
231: error);
232:
233: // cache it
234: put_driver_to_cache(global_protocol, *driver);
235: }
236:
237: // allocate in global pool
238: // associate with services[request]
239: // NOTE: never freed up!
1.58 paf 240: connection=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver);
1.54 paf 241: // associate with services[request] (deassociates at close)
1.58 paf 242: connection->set_services(services);
1.54 paf 243: }
244:
1.58 paf 245: // if not connected yet, do that now, when connection has services
246: if(!connection->connected())
247: connection->connect(request_url_cstr);
248: // return autoclosing object for it
1.59 paf 249: return SQL_Connection_ptr(connection);
1.54 paf 250: }
251:
252: void SQL_Driver_manager::close_connection(const String& url,
253: SQL_Connection& connection) {
254: // deassociate from services[request]
255: connection.set_services(0);
256: put_connection_to_cache(url, connection);
257: }
258:
259:
260: // driver cache
261:
262: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
263: SYNCHRONIZED;
264:
265: return static_cast<SQL_Driver *>(driver_cache.get(protocol));
266: }
267:
268: void SQL_Driver_manager::put_driver_to_cache(const String& protocol,
269: SQL_Driver& driver) {
270: SYNCHRONIZED;
271:
272: driver_cache.put(protocol, &driver);
273: }
274:
275: // connection cache
276: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
277: SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) {
278: SYNCHRONIZED;
279:
280: if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
281: while(connections->top_index()>=0) { // there are cached connections to that 'url'
282: SQL_Connection *result=static_cast<SQL_Connection *>(connections->pop());
283: if(result->connected()) // not expired?
284: return result;
285: }
286:
287: return 0;
288: }
289:
290: void SQL_Driver_manager::put_connection_to_cache(const String& url,
291: SQL_Connection& connection) {
292: SYNCHRONIZED;
293:
294: Stack *connections=static_cast<Stack *>(connection_cache.get(url));
295: if(!connections) { // there are no cached connections to that 'url' yet?
296: connections=NEW Stack(pool()); // NOTE: never freed up!
297: connection_cache.put(url, connections);
298: }
299: connections->push(&connection);
300: }
301:
302: void SQL_Driver_manager::maybe_expire_cache() {
303: time_t now=time(0);
304:
305: if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
306: connection_cache.for_each(expire_connections,
307: reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
308:
309: prev_expiration_pass_time=now;
310: }
311: }
312:
313: static void add_connection_to_status_cache_table(Array::Item *value, void *info) {
314: SQL_Connection& connection=*static_cast<SQL_Connection *>(value);
315: Table& table=*static_cast<Table *>(info);
316:
317: if(connection.connected()) {
318: Pool& pool=table.pool();
319: Array& row=*new(pool) Array(pool);
320:
321: // url
322: row+=&url_without_login(pool, connection.get_url());
323: // time
1.58 paf 324: time_t time_used=connection.get_time_used();
325: const char *unsafe_time_cstr=ctime(&time_used);
1.54 paf 326: int time_buf_size=strlen(unsafe_time_cstr);
327: char *safe_time_buf=(char *)pool.malloc(time_buf_size);
328: memcpy(safe_time_buf, unsafe_time_cstr, time_buf_size);
329: row+=new(pool) String(pool, safe_time_buf, time_buf_size);
330:
331: table+=&row;
332: }
333: }
334: static void add_connections_to_status_cache_table(const Hash::Key& key, Hash::Val *value, void *info) {
335: Stack& stack=*static_cast<Stack *>(value);
336: Array_iter iter(stack);
337: for(int countdown=stack.top_index(); countdown-->=0; )
338: add_connection_to_status_cache_table(iter.next(), info);
339: }
340: Value& SQL_Driver_manager::get_status(Pool& pool, const String *source) {
341: VHash& result=*new(pool) VHash(pool);
342:
343: // cache
344: {
345: Array& columns=*new(pool) Array(pool);
346: columns+=new(pool) String(pool, "url");
347: columns+=new(pool) String(pool, "time");
348: Table& table=*new(pool) Table(pool, 0, &columns, connection_cache.size());
349:
350: connection_cache.for_each(add_connections_to_status_cache_table, &table);
351:
352: result.hash(source).put(*new(pool) String(pool, "cache"), new(pool) VTable(pool, &table));
353: }
354:
355: return result;
1.57 paf 356: }
E-mail: