Annotation of parser3/src/main/pa_sql_driver_manager.C, revision 1.3
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.3 ! paf 8: $Id: pa_sql_driver_manager.C,v 1.2 2001/04/04 11:47:29 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:
! 28: /// Services_for_SQL_driver implementation
! 29: class Services_for_SQL_driver_impl : public Services_for_SQL_driver, public Pooled {
! 30: public:
! 31: Services_for_SQL_driver_impl(Pool& apool, const String& aurl) : Pooled(apool),
! 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
55: SQL_Connection& SQL_Driver_manager::get_connection(const String& url,
56: Table *protocol2library) {
57: SYNCHRONIZED(true);
58:
59: Pool& pool=url.pool(); // request pool
60:
61: // we have table for locating protocol's library
62: if(!protocol2library)
63: PTHROW(0, 0,
64: &url,
65: "SQL:drivers table must be defined");
66:
67: // first trying to get cached connection
68: if(SQL_Connection *result=get_connection_from_cache(url))
69: return *result;
70: // no cached connection
71:
72: int pos=url.pos("://", 3);
73: if(pos<0)
74: PTHROW(0, 0,
75: &url,
76: "no protocol specified"); // NOTE: not THROW, but PTHROW
77:
78: // make url string on global pool
79: char *url_cstr=(char *)malloc(MAX_STRING);
80: strncpy(url_cstr, url.cstr(String::UL_AS_IS), MAX_STRING);
81:
82: char *protocol_cstr=lsplit(&url_cstr, ':');
83: String& protocol=*new(this->pool()) String(this->pool(), protocol_cstr);
84:
85: // skip // after :
86: while(*url_cstr=='/')
87: url_cstr++;
88:
89: SQL_Driver *driver;
90: // first trying to get cached driver
91: if(!(driver=get_driver_from_cache(protocol))) {
92: // no cached
93: const String *library=0;
94: if(protocol2library->locate(0, protocol)) {
1.2 paf 95: if(!(library=protocol2library->item(1)) || library->size()==0)
1.1 paf 96: PTHROW(0, 0,
97: protocol2library->origin_string(),
98: "library column for protocol '%s' is empty", protocol_cstr);
99: } else
100: PTHROW(0, 0,
1.2 paf 101: &url,
102: "undefined protocol '%s'", protocol_cstr);
1.1 paf 103:
104: const char *filename=library->cstr(String::UL_FILE_NAME);
1.2 paf 105: lt_dlhandle handle=lt_dlopen(filename);
1.1 paf 106: if (!handle)
107: PTHROW(0, 0,
108: library,
109: "can not open the module, %s", lt_dlerror());
110:
111: SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle,
112: LIBRARY_CREATE_FUNC_NAME);
113: if(!create)
114: PTHROW(0, 0,
115: library,
116: "function '%s' was not found", LIBRARY_CREATE_FUNC_NAME);
117:
1.2 paf 118: // create library-driver!
1.1 paf 119: driver=(*create)();
120:
121: // validate driver api version
122: int driver_api_version=driver->api_version();
1.3 ! paf 123: if(driver_api_version<SQL_DRIVER_API_VERSION)
1.1 paf 124: PTHROW(0, 0,
125: library,
126: "driver API version is 0x%04X while current minimum is 0x%04X",
1.3 ! paf 127: driver_api_version, SQL_DRIVER_API_VERSION);
1.1 paf 128:
129: // cache it
130: put_driver_to_cache(protocol, *driver);
131: }
132:
1.3 ! paf 133: // associate with request
! 134: Services_for_SQL_driver_impl& services=
! 135: *new(pool) Services_for_SQL_driver_impl(pool, url);
! 136:
! 137: // allocate in global pool. associate with services[request]
! 138: SQL_Connection& result=
! 139: *new(this->pool()) SQL_Connection(this->pool(), url, *driver, services, url_cstr);
! 140:
! 141: return result;
1.1 paf 142: }
143:
144: void SQL_Driver_manager::close_connection(const String& url,
145: SQL_Connection& connection) {
146: SYNCHRONIZED(true);
147:
148: put_connection_to_cache(url, connection);
149: }
150:
151:
152: // driver cache
153:
154: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
155: if(SQL_Driver *result=static_cast<SQL_Driver *>(driver_cache.get(protocol)))
156: return result;
157:
158: return 0;
159: }
160:
161: void SQL_Driver_manager::put_driver_to_cache(const String& protocol,
162: SQL_Driver& driver) {
163: driver_cache.put(protocol, &driver);
164: }
165:
166: // connection cache
167:
168: SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) {
169: if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
170: if(connections->size()) // there are cached connections to that 'url'
171: return static_cast<SQL_Connection *>(connections->pop());
172:
173: return 0;
174: }
175:
1.2 paf 176: /// @todo cache expiration[use SQL_Driver::disconnect], pinging.
1.1 paf 177: void SQL_Driver_manager::put_connection_to_cache(const String& url,
178: SQL_Connection& connection) {
179: Stack *connections;
180: if(!(connections=static_cast<Stack *>(connection_cache.get(url)))) {
181: // there are no cached connections to that 'url' yet
182: connections=NEW Stack(pool()); // NOTE: never freed up!
183: connection_cache.put(url, connections);
184: }
185: connections->push(&connection);
186: }
E-mail: