Annotation of parser3/src/main/pa_sql_driver_manager.C, revision 1.4
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.4 ! paf 8: $Id: pa_sql_driver_manager.C,v 1.3 2001/04/05 08:09:24 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.4 ! paf 133: // services associated with request
1.3 paf 134: Services_for_SQL_driver_impl& services=
135: *new(pool) Services_for_SQL_driver_impl(pool, url);
136:
1.4 ! paf 137: // allocate in global pool
! 138: // associate with services[request], deassociates at close
1.3 paf 139: SQL_Connection& result=
140: *new(this->pool()) SQL_Connection(this->pool(), url, *driver, services, url_cstr);
141:
142: return result;
1.1 paf 143: }
144:
145: void SQL_Driver_manager::close_connection(const String& url,
146: SQL_Connection& connection) {
147: SYNCHRONIZED(true);
148:
149: put_connection_to_cache(url, connection);
150: }
151:
152:
153: // driver cache
154:
155: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
156: if(SQL_Driver *result=static_cast<SQL_Driver *>(driver_cache.get(protocol)))
157: return result;
158:
159: return 0;
160: }
161:
162: void SQL_Driver_manager::put_driver_to_cache(const String& protocol,
163: SQL_Driver& driver) {
164: driver_cache.put(protocol, &driver);
165: }
166:
167: // connection cache
168:
169: SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) {
170: if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
171: if(connections->size()) // there are cached connections to that 'url'
172: return static_cast<SQL_Connection *>(connections->pop());
173:
174: return 0;
175: }
176:
1.2 paf 177: /// @todo cache expiration[use SQL_Driver::disconnect], pinging.
1.1 paf 178: void SQL_Driver_manager::put_connection_to_cache(const String& url,
179: SQL_Connection& connection) {
180: Stack *connections;
181: if(!(connections=static_cast<Stack *>(connection_cache.get(url)))) {
182: // there are no cached connections to that 'url' yet
183: connections=NEW Stack(pool()); // NOTE: never freed up!
184: connection_cache.put(url, connections);
185: }
186: connections->push(&connection);
187: }
E-mail: