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