|
|
| version 1.36, 2001/09/18 16:05:42 | version 1.53, 2001/11/11 10:52:50 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: sql driver manager implementation. | Parser: sql driver manager implementation. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru) | |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | |
| */ | $Id$ |
| static const char *RCSId="$Id$"; | */ |
| #include "pa_sql_driver_manager.h" | #include "pa_sql_driver_manager.h" |
| #include "ltdl.h" | #include "ltdl.h" |
| #include "pa_sql_connection.h" | #include "pa_sql_connection.h" |
| #include "pa_exception.h" | #include "pa_exception.h" |
| #include "pa_common.h" | #include "pa_common.h" |
| #include "pa_threads.h" | #include "pa_threads.h" |
| #include "pa_stack.h" | |
| // globals | #include "pa_vhash.h" |
| #include "pa_vtable.h" | |
| SQL_Driver_manager *SQL_driver_manager; | |
| // globals | |
| // consts | |
| SQL_Driver_manager *SQL_driver_manager; | |
| const int EXPIRE_UNUSED_CONNECTION_SECONDS=60; | |
| const int CHECK_EXPIRED_CONNECTIONS_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2; | // consts |
| const int EXPIRE_UNUSED_CONNECTION_SECONDS=60; | |
| /// SQL_Driver_services Pooled implementation | const int CHECK_EXPIRED_CONNECTIONS_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2; |
| class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled { | |
| public: | // helpers |
| SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool), | |
| furl(aurl) { | const String& url_without_login(Pool& pool, const String& url) { |
| } | String& result=*new(pool) String(pool); |
| result << url.mid(0, url.pos(":")) << "://****"; | |
| /// allocates some bytes on pool | |
| void *malloc(size_t size) { return Pooled::malloc(size); } | int at_pos=url.pos("@"); |
| /// allocates some bytes clearing them with zeros | if(at_pos>0) |
| void *calloc(size_t size) { return Pooled::calloc(size); } | result << url.mid(at_pos, url.size()); |
| /// throw exception | |
| void _throw(const char *comment) { | return result; |
| THROW(0, 0, | } |
| &furl, | |
| comment); | /// SQL_Driver_services Pooled implementation |
| } | class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled { |
| public: | |
| private: | SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool), |
| const String& furl; | furl(aurl) { |
| }; | } |
| // SQL_Driver_manager | virtual void *malloc(size_t size) { return Pooled::malloc(size, 8); } |
| virtual void *calloc(size_t size) { return Pooled::calloc(size); } | |
| /// @param request_url protocol://[driver-dependent] | |
| SQL_Connection& SQL_Driver_manager::get_connection(const String& request_url, | /** |
| const String& request_origin, | the idea is to #1 jump to C++ some function to main body, where |
| Table *protocol2driver_and_client) { | every function stack frame has exception unwind information |
| Pool& pool=request_origin.pool(); // request pool | and from there... #2 propagate_exception() |
| */ | |
| // we have table for locating protocol's library | virtual void _throw(const char *comment) { |
| if(!protocol2driver_and_client) | // hiding passwords and addresses from accidental show [imagine user forgot @exception] |
| PTHROW(0, 0, | e=Exception(0, 0, |
| &request_url, | &url_without_login(pool(), furl), |
| "$"MAIN_SQL_NAME":"MAIN_SQL_DRIVERS_NAME" table must be defined"); | comment); |
| // first trying to get cached connection | longjmp(mark, 1); |
| SQL_Connection *result=get_connection_from_cache(request_url); | } |
| if(result && !result->ping()) { // we have some cached connection, is it pingable? | virtual void propagate_exception() { |
| result->disconnect(); // kill unpingabe=dead connection | throw e; |
| result=0; | } |
| } | |
| private: | |
| char *request_url_cstr; | const String& furl; |
| if(result) | Exception e; |
| request_url_cstr=0; // calm, compiler | }; |
| else { // no cached connection or it were unpingabe: connect/reconnect | |
| int pos=request_url.pos("://", 3); | // helpers |
| if(pos<0) | |
| PTHROW(0, 0, | static void expire_connection(Array::Item *value, void *info) { |
| request_url.size()?&request_url:&request_origin, | SQL_Connection& connection=*static_cast<SQL_Connection *>(value); |
| "connection string must start with protocol://"); // NOTE: not THROW, but PTHROW | time_t older_dies=reinterpret_cast<time_t>(info); |
| // make global_url C-string on global pool | if(connection.connected() && connection.expired(older_dies)) |
| request_url_cstr=request_url.cstr(String::UL_AS_IS); | connection.disconnect(); |
| char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1); | } |
| strcpy(global_url_cstr, request_url_cstr); | static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) { |
| // make global_url string on global pool | Stack& stack=*static_cast<Stack *>(value); |
| String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr); | for(int i=0; i<=stack.top_index(); i++) |
| expire_connection(stack.get(i), info); | |
| char *request_protocol_cstr=lsplit(&request_url_cstr, ':'); | } |
| // skip "//" after ':' | |
| while(*request_url_cstr=='/') | // SQL_Driver_manager |
| request_url_cstr++; | |
| // make global_protocol C-string on global pool | SQL_Driver_manager::SQL_Driver_manager(Pool& apool) : Cache_manager(apool), |
| char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1); | driver_cache(apool), |
| strcpy(global_protocol_cstr, request_protocol_cstr); | connection_cache(apool), |
| // make global_protocol string on global pool | prev_expiration_pass_time(0) { |
| String& global_protocol=*new(this->pool()) String(this->pool(), | } |
| global_protocol_cstr); | |
| SQL_Driver_manager::~SQL_Driver_manager() { | |
| SQL_Driver *driver; | connection_cache.for_each(expire_connections, |
| const String *dlopen_file_spec=0; | reinterpret_cast<void *>((time_t)0/*=in past=expire all*/)); |
| // first trying to get cached driver | } |
| if(!(driver=get_driver_from_cache(global_protocol))) { | |
| // no cached | /// @param request_url protocol://[driver-dependent] |
| const String *library=0; | SQL_Connection& SQL_Driver_manager::get_connection(const String& request_url, |
| if(protocol2driver_and_client->locate(0, global_protocol)) { | const String& request_origin, |
| if(!(library=protocol2driver_and_client->item(1)) || library->size()==0) | Table *protocol2driver_and_client) { |
| PTHROW(0, 0, | Pool& pool=request_origin.pool(); // request pool |
| protocol2driver_and_client->origin_string(), | |
| "driver library column for protocol '%s' is empty", | // we have table for locating protocol's library |
| request_protocol_cstr); | if(!protocol2driver_and_client) |
| dlopen_file_spec=protocol2driver_and_client->item(2); | throw Exception(0, 0, |
| } else | &request_url, |
| PTHROW(0, 0, | "$"MAIN_SQL_NAME":"MAIN_SQL_DRIVERS_NAME" table must be defined"); |
| &request_url, | |
| "undefined protocol '%s'", | // construct services[request] (deassociates at close) |
| request_protocol_cstr); | SQL_Driver_services *services=new(pool) SQL_Driver_services_impl(pool, request_url); |
| if(lt_dlinit()) | // first trying to get cached connection |
| PTHROW(0, 0, | SQL_Connection *result=get_connection_from_cache(request_url); |
| library, | if(result) { |
| "prepare to dynamic loading failed, %s", lt_dlerror()); | result->set_services(services); |
| if(!result->ping()) { // we have some cached connection, is it pingable? | |
| const char *filename=library->cstr(String::UL_FILE_SPEC); | result->disconnect(); // kill unpingabe=dead connection |
| lt_dlhandle handle=lt_dlopen(filename); | result=0; |
| if (!handle) | } |
| PTHROW(0, 0, | } |
| library, | |
| "can not open the module, %s", lt_dlerror()); | char *request_url_cstr; |
| if(result) | |
| SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, | request_url_cstr=0; // calm, compiler |
| SQL_DRIVER_CREATE_NAME); | else { // no cached connection or it were unpingabe: connect/reconnect |
| if(!create) | int pos=request_url.pos("://", 3); |
| PTHROW(0, 0, | if(pos<0) |
| library, | throw Exception(0, 0, |
| "function '"SQL_DRIVER_CREATE_NAME"' was not found"); | request_url.size()?&request_url:&request_origin, |
| "connection string must start with protocol://"); // NOTE: not THROW, but PTHROW | |
| // create library-driver! | |
| driver=(*create)(); | // make global_url C-string on global pool |
| request_url_cstr=request_url.cstr(); | |
| // validate driver api version | char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1); |
| int driver_api_version=driver->api_version(); | strcpy(global_url_cstr, request_url_cstr); |
| if(driver_api_version!=SQL_DRIVER_API_VERSION) | // make global_url string on global pool |
| PTHROW(0, 0, | String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr); |
| library, | |
| "driver implements API version 0x%04X not equal to 0x%04X", | char *request_protocol_cstr=lsplit(&request_url_cstr, ':'); |
| driver_api_version, SQL_DRIVER_API_VERSION); | // skip "//" after ':' |
| while(*request_url_cstr=='/') | |
| // initialise by connecting to sql client dynamic link library | request_url_cstr++; |
| bool specified_dlopen_file_spec=dlopen_file_spec && dlopen_file_spec->size(); | // make global_protocol C-string on global pool |
| const char *dlopen_file_spec_cstr= | char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1); |
| specified_dlopen_file_spec? | strcpy(global_protocol_cstr, request_protocol_cstr); |
| dlopen_file_spec->cstr(String::UL_FILE_SPEC):0; | // make global_protocol string on global pool |
| if(const char *error=driver->initialize( | String& global_protocol=*new(this->pool()) String(this->pool(), |
| dlopen_file_spec_cstr)) | global_protocol_cstr); |
| PTHROW(0, 0, | |
| library, | SQL_Driver *driver; |
| "driver failed to initialize client library '%s', %s", | // first trying to get cached driver |
| specified_dlopen_file_spec?dlopen_file_spec_cstr:"unspecifed", | if(!(driver=get_driver_from_cache(global_protocol))) { |
| error); | // no cached |
| const String *library=0; | |
| // cache it | const String *dlopen_file_spec=0; |
| put_driver_to_cache(global_protocol, *driver); | if(protocol2driver_and_client->locate(0, global_protocol)) { |
| } | if(!(library=protocol2driver_and_client->item(1)) || library->size()==0) |
| throw Exception(0, 0, | |
| // allocate in global pool | protocol2driver_and_client->origin_string(), |
| // associate with services[request] | "driver library column for protocol '%s' is empty", |
| // NOTE: never freed up! | request_protocol_cstr); |
| result=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver); | dlopen_file_spec=protocol2driver_and_client->item(2); |
| } | } else |
| throw Exception(0, 0, | |
| // associate with services[request] (deassociates at close) | &request_url, |
| result->set_services(new(pool) SQL_Driver_services_impl(pool, request_url)); | "undefined protocol '%s'", |
| // if not connected yet, do that now, when result has services | request_protocol_cstr); |
| if(!result->connected()) | |
| result->connect(request_url_cstr); | if(lt_dlinit()) |
| // return it | throw Exception(0, 0, |
| return *result; | library, |
| } | "prepare to dynamic loading failed, %s", lt_dlerror()); |
| void SQL_Driver_manager::close_connection(const String& url, | const char *filename=library->cstr(String::UL_FILE_SPEC); |
| SQL_Connection& connection) { | lt_dlhandle handle=lt_dlopen(filename); |
| // deassociate from services[request] | if (!handle) |
| connection.set_services(0); | throw Exception(0, 0, |
| put_connection_to_cache(url, connection); | library, |
| } | "can not open the module, %s", lt_dlerror()); |
| SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, | |
| // driver cache | SQL_DRIVER_CREATE_NAME); |
| if(!create) | |
| SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) { | throw Exception(0, 0, |
| SYNCHRONIZED; | library, |
| "function '"SQL_DRIVER_CREATE_NAME"' was not found"); | |
| return static_cast<SQL_Driver *>(driver_cache.get(protocol)); | |
| } | // create library-driver! |
| driver=(*create)(); | |
| void SQL_Driver_manager::put_driver_to_cache(const String& protocol, | |
| SQL_Driver& driver) { | // validate driver api version |
| SYNCHRONIZED; | int driver_api_version=driver->api_version(); |
| if(driver_api_version!=SQL_DRIVER_API_VERSION) | |
| driver_cache.put(protocol, &driver); | throw Exception(0, 0, |
| } | library, |
| "driver implements API version 0x%04X not equal to 0x%04X", | |
| // connection cache | driver_api_version, SQL_DRIVER_API_VERSION); |
| /// @todo get rid of memory spending Stack [zeros deep inside got accumulated] | |
| SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) { | // initialise by connecting to sql client dynamic link library |
| SYNCHRONIZED; | char *dlopen_file_spec_cstr= |
| dlopen_file_spec && dlopen_file_spec->size()? | |
| maybe_expire_connection_cache(); | dlopen_file_spec->cstr(String::UL_AS_IS):0; |
| if(const char *error=driver->initialize( | |
| if(Stack *connections=static_cast<Stack *>(connection_cache.get(url))) | dlopen_file_spec_cstr)) |
| while(connections->top_index()>=0) { // there are cached connections to that 'url' | throw Exception(0, 0, |
| SQL_Connection *result=static_cast<SQL_Connection *>(connections->pop()); | library, |
| if(result->connected()) // not expired? | "driver failed to initialize client library '%s', %s", |
| return result; | dlopen_file_spec_cstr?dlopen_file_spec_cstr:"unspecifed", |
| } | error); |
| return 0; | // cache it |
| } | put_driver_to_cache(global_protocol, *driver); |
| } | |
| void SQL_Driver_manager::put_connection_to_cache(const String& url, | |
| SQL_Connection& connection) { | // allocate in global pool |
| SYNCHRONIZED; | // associate with services[request] |
| // NOTE: never freed up! | |
| Stack *connections=static_cast<Stack *>(connection_cache.get(url)); | result=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver); |
| if(!connections) { // there are no cached connections to that 'url' yet? | // associate with services[request] (deassociates at close) |
| connections=NEW Stack(pool()); // NOTE: never freed up! | result->set_services(services); |
| connection_cache.put(url, connections); | } |
| } | |
| connections->push(&connection); | // if not connected yet, do that now, when result has services |
| } | if(!result->connected()) |
| result->connect(request_url_cstr); | |
| static void expire_connection(Array::Item *value, void *info) { | // return it |
| SQL_Connection& connection=*static_cast<SQL_Connection *>(value); | return *result; |
| time_t older_dies=reinterpret_cast<time_t>(info); | } |
| if(connection.connected() && connection.expired(older_dies)) | void SQL_Driver_manager::close_connection(const String& url, |
| connection.disconnect(); | SQL_Connection& connection) { |
| } | // deassociate from services[request] |
| static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) { | connection.set_services(0); |
| Stack& stack=*static_cast<Stack *>(value); | put_connection_to_cache(url, connection); |
| for(int i=0; i<=stack.top_index(); i++) | } |
| expire_connection(stack.get(i), info); | |
| } | |
| void SQL_Driver_manager::maybe_expire_connection_cache() { | // driver cache |
| time_t now=time(0); | |
| SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) { | |
| if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) { | SYNCHRONIZED; |
| connection_cache.for_each(expire_connections, | |
| reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS)); | return static_cast<SQL_Driver *>(driver_cache.get(protocol)); |
| } | |
| prev_expiration_pass_time=now; | |
| } | void SQL_Driver_manager::put_driver_to_cache(const String& protocol, |
| } | SQL_Driver& driver) { |
| SYNCHRONIZED; | |
| driver_cache.put(protocol, &driver); | |
| } | |
| // connection cache | |
| /// @todo get rid of memory spending Stack [zeros deep inside got accumulated] | |
| SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) { | |
| SYNCHRONIZED; | |
| if(Stack *connections=static_cast<Stack *>(connection_cache.get(url))) | |
| while(connections->top_index()>=0) { // there are cached connections to that 'url' | |
| SQL_Connection *result=static_cast<SQL_Connection *>(connections->pop()); | |
| if(result->connected()) // not expired? | |
| return result; | |
| } | |
| return 0; | |
| } | |
| void SQL_Driver_manager::put_connection_to_cache(const String& url, | |
| SQL_Connection& connection) { | |
| SYNCHRONIZED; | |
| Stack *connections=static_cast<Stack *>(connection_cache.get(url)); | |
| if(!connections) { // there are no cached connections to that 'url' yet? | |
| connections=NEW Stack(pool()); // NOTE: never freed up! | |
| connection_cache.put(url, connections); | |
| } | |
| connections->push(&connection); | |
| } | |
| void SQL_Driver_manager::maybe_expire_cache() { | |
| time_t now=time(0); | |
| if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) { | |
| connection_cache.for_each(expire_connections, | |
| reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS)); | |
| prev_expiration_pass_time=now; | |
| } | |
| } | |
| static void add_connection_to_status_cache_table(Array::Item *value, void *info) { | |
| SQL_Connection& connection=*static_cast<SQL_Connection *>(value); | |
| Table& table=*static_cast<Table *>(info); | |
| if(connection.connected()) { | |
| Pool& pool=table.pool(); | |
| Array& row=*new(pool) Array(pool); | |
| // url | |
| row+=&url_without_login(pool, connection.get_url()); | |
| // time | |
| time_t time_stamp=connection.get_time_stamp(); | |
| const char *unsafe_time_cstr=ctime(&time_stamp); | |
| int time_buf_size=strlen(unsafe_time_cstr); | |
| char *safe_time_buf=(char *)pool.malloc(time_buf_size); | |
| memcpy(safe_time_buf, unsafe_time_cstr, time_buf_size); | |
| row+=new(pool) String(pool, safe_time_buf, time_buf_size); | |
| table+=&row; | |
| } | |
| } | |
| static void add_connections_to_status_cache_table(const Hash::Key& key, Hash::Val *value, void *info) { | |
| Stack& stack=*static_cast<Stack *>(value); | |
| Array_iter iter(stack); | |
| for(int countdown=stack.top_index(); countdown-->=0; ) | |
| add_connection_to_status_cache_table(iter.next(), info); | |
| } | |
| Value& SQL_Driver_manager::get_status(Pool& pool, const String *source) { | |
| VHash& result=*new(pool) VHash(pool); | |
| // cache | |
| { | |
| Array& columns=*new(pool) Array(pool); | |
| columns+=new(pool) String(pool, "url"); | |
| columns+=new(pool) String(pool, "time"); | |
| Table& table=*new(pool) Table(pool, 0, &columns, connection_cache.size()); | |
| connection_cache.for_each(add_connections_to_status_cache_table, &table); | |
| result.hash(source).put(*new(pool) String(pool, "cache"), new(pool) VTable(pool, &table)); | |
| } | |
| return result; | |
| } | |