Annotation of parser3/src/main/pa_sql_driver_manager.C, revision 1.15
1.1 paf 1: /** @file
2: Parser: sql driver manager implementation.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5:
6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.15 ! parser 8: $Id: pa_sql_driver_manager.C,v 1.14 2001/05/17 10:12:28 parser Exp $
1.1 paf 9: */
10:
1.14 parser 11: #include "pa_sql_driver_manager.h"
1.1 paf 12: #include "ltdl.h"
1.2 paf 13: #include "pa_sql_connection.h"
1.1 paf 14: #include "pa_exception.h"
15: #include "pa_common.h"
1.14 parser 16: #include "pa_threads.h"
1.1 paf 17:
1.15 ! parser 18: #include "pa_sapi.h"
! 19:
1.1 paf 20: // globals
21:
22: SQL_Driver_manager *SQL_driver_manager;
23:
24: // consts
25:
26: const int MAX_PROTOCOL=20;
27: const char *LIBRARY_CREATE_FUNC_NAME="create";
28:
1.3 paf 29:
1.8 paf 30: /// SQL_Driver_services Pooled implementation
31: class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled {
1.3 paf 32: public:
1.8 paf 33: SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool),
1.3 paf 34: furl(aurl) {
35: }
36:
37: /// allocates some bytes on pool
38: void *malloc(size_t size) { return Pooled::malloc(size); }
39: /// allocates some bytes clearing them with zeros
40: void *calloc(size_t size) { return Pooled::calloc(size); }
41: /// throw exception
42: void _throw(const char *comment) {
43: THROW(0, 0,
44: &furl,
45: comment);
46: }
47:
48: private:
49: const String& furl;
50: };
51:
52: // SQL_Driver_manager
53:
1.12 parser 54: /// @param request_url protocol://[driver-dependent]
55: SQL_Connection& SQL_Driver_manager::get_connection(const String& request_url,
1.7 paf 56: Table *protocol2driver_and_client) {
1.12 parser 57: Pool& pool=request_url.pool(); // request pool
1.1 paf 58:
59: // we have table for locating protocol's library
1.7 paf 60: if(!protocol2driver_and_client)
1.1 paf 61: PTHROW(0, 0,
1.12 parser 62: &request_url,
1.6 paf 63: "$SQL:drivers table must be defined");
1.1 paf 64:
65: // first trying to get cached connection
1.15 ! parser 66: SQL_Connection *result=get_connection_from_cache(request_url);
! 67: if(result && !result->ping()) { // we have some cached connection, is it pingable?
! 68: result->disconnect(); // kill unpingabe=dead connection
! 69: result=0;
! 70: }
1.1 paf 71:
1.15 ! parser 72: if(!result) { // no cached connection or it were unpingabe: connect/reconnect
! 73: int pos=request_url.pos("://", 3);
! 74: if(pos<0)
! 75: PTHROW(0, 0,
! 76: &request_url,
! 77: "no protocol specified"); // NOTE: not THROW, but PTHROW
1.1 paf 78:
1.15 ! parser 79: // make global_url C-string on global pool
! 80: char *request_url_cstr=request_url.cstr(String::UL_AS_IS);
! 81: char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);
! 82: strcpy(global_url_cstr, request_url_cstr);
! 83: // make global_url string on global pool
! 84: String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);
! 85:
! 86: char *request_protocol_cstr=lsplit(&request_url_cstr, ':');
! 87: // skip "//" after ':'
! 88: while(*request_url_cstr=='/')
! 89: request_url_cstr++;
! 90: // make global_protocol C-string on global pool
! 91: char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);
! 92: strcpy(global_protocol_cstr, request_protocol_cstr);
! 93: // make global_protocol string on global pool
! 94: String& global_protocol=*new(this->pool()) String(this->pool(),
! 95: global_protocol_cstr);
! 96:
! 97: SQL_Driver *driver;
! 98: const String *dlopen_file_spec=0;
! 99: // first trying to get cached driver
! 100: if(!(driver=get_driver_from_cache(global_protocol))) {
! 101: // no cached
! 102: const String *library=0;
! 103: if(protocol2driver_and_client->locate(0, global_protocol)) {
! 104: if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
! 105: PTHROW(0, 0,
! 106: protocol2driver_and_client->origin_string(),
! 107: "driver library column for protocol '%s' is empty",
! 108: request_protocol_cstr);
! 109: if(!(dlopen_file_spec=protocol2driver_and_client->item(2)) || dlopen_file_spec->size()==0)
! 110: PTHROW(0, 0,
! 111: protocol2driver_and_client->origin_string(),
! 112: "client library column for protocol '%s' is empty",
! 113: request_protocol_cstr);
! 114: } else
1.1 paf 115: PTHROW(0, 0,
1.15 ! parser 116: &request_url,
! 117: "undefined protocol '%s'",
1.12 parser 118: request_protocol_cstr);
1.15 ! parser 119:
! 120: const char *filename=library->cstr(String::UL_FILE_NAME);
! 121: lt_dlhandle handle=lt_dlopen(filename);
! 122: if (!handle)
1.7 paf 123: PTHROW(0, 0,
1.15 ! parser 124: library,
! 125: "can not open the module, %s", lt_dlerror());
1.1 paf 126:
1.15 ! parser 127: SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle,
! 128: LIBRARY_CREATE_FUNC_NAME);
! 129: if(!create)
! 130: PTHROW(0, 0,
! 131: library,
! 132: "function '%s' was not found", LIBRARY_CREATE_FUNC_NAME);
1.1 paf 133:
1.15 ! parser 134: // create library-driver!
! 135: driver=(*create)();
1.1 paf 136:
1.15 ! parser 137: // validate driver api version
! 138: int driver_api_version=driver->api_version();
! 139: if(driver_api_version!=SQL_DRIVER_API_VERSION)
! 140: PTHROW(0, 0,
! 141: library,
! 142: "driver implements API version 0x%04X not equal to 0x%04X",
! 143: driver_api_version, SQL_DRIVER_API_VERSION);
! 144:
! 145: // initialise by connecting to sql client dynamic link library
! 146: const char *dlopen_file_spec_cstr=dlopen_file_spec->cstr(String::UL_FILE_NAME);
! 147: if(const char *error=driver->initialize(
! 148: dlopen_file_spec_cstr))
! 149: PTHROW(0, 0,
! 150: library,
! 151: "driver failed to initialize client library '%s', %s",
! 152: dlopen_file_spec_cstr, error);
! 153:
! 154: // cache it
! 155: put_driver_to_cache(global_protocol, *driver);
! 156: }
! 157:
! 158: // allocate in global pool
! 159: // associate with services[request], deassociates at close
! 160: result=new(this->pool()) SQL_Connection(this->pool(),
! 161: global_url, *driver, request_url_cstr);
! 162: }
1.1 paf 163:
1.15 ! parser 164: // associate with services[request]
! 165: result->set_services(new(pool) SQL_Driver_services_impl(pool, request_url));
! 166: SAPI::log(pool, "get_connection: %p", result);
! 167: return *result;
1.1 paf 168: }
169:
170: void SQL_Driver_manager::close_connection(const String& url,
1.15 ! parser 171: SQL_Connection& connection,
! 172: Pool& pool) {
! 173: SAPI::log(pool, "close_connection: %p", &connection);
! 174: // deassociate from services[request]
! 175: connection.set_services(0);
1.1 paf 176: put_connection_to_cache(url, connection);
177: }
178:
179:
180: // driver cache
181:
182: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
1.13 parser 183: SYNCHRONIZED;
184:
1.15 ! parser 185: return static_cast<SQL_Driver *>(driver_cache.get(protocol));
1.1 paf 186: }
187:
188: void SQL_Driver_manager::put_driver_to_cache(const String& protocol,
189: SQL_Driver& driver) {
1.13 parser 190: SYNCHRONIZED;
191:
1.1 paf 192: driver_cache.put(protocol, &driver);
193: }
194:
195: // connection cache
196:
197: SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) {
1.13 parser 198: SYNCHRONIZED;
199:
1.1 paf 200: if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
1.15 ! parser 201: if(connections->top_index()>=0) // there are cached connections to that 'url'
1.1 paf 202: return static_cast<SQL_Connection *>(connections->pop());
203:
204: return 0;
205: }
206:
1.10 paf 207: /// @todo cache expiration[use SQL_Driver::disconnect]
1.1 paf 208: void SQL_Driver_manager::put_connection_to_cache(const String& url,
209: SQL_Connection& connection) {
1.13 parser 210: SYNCHRONIZED;
211:
1.15 ! parser 212: Stack *connections=static_cast<Stack *>(connection_cache.get(url));
! 213: if(!connections) { // there are no cached connections to that 'url' yet?
1.1 paf 214: connections=NEW Stack(pool()); // NOTE: never freed up!
215: connection_cache.put(url, connections);
216: }
217: connections->push(&connection);
218: }
E-mail: