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