|
|
| version 1.19, 2001/05/17 18:43:06 | 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$ | |
| $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" |
| #include "pa_vhash.h" | |
| #include "pa_sapi.h" | #include "pa_vtable.h" |
| // globals | // globals |
| SQL_Driver_manager *SQL_driver_manager; | SQL_Driver_manager *SQL_driver_manager; |
| // consts | // consts |
| const char *LIBRARY_CREATE_FUNC_NAME="create"; | const int EXPIRE_UNUSED_CONNECTION_SECONDS=60; |
| const int EXPIRE_UNUSED_CONNECTION_SECONDS=60; | const int CHECK_EXPIRED_CONNECTIONS_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2; |
| const int CHECK_EXPIRED_CONNECTIONS_SECONDS=60*2; | |
| // helpers | |
| /// SQL_Driver_services Pooled implementation | const String& url_without_login(Pool& pool, const String& url) { |
| class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled { | String& result=*new(pool) String(pool); |
| public: | result << url.mid(0, url.pos(":")) << "://****"; |
| SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool), | |
| furl(aurl) { | int at_pos=url.pos("@"); |
| } | if(at_pos>0) |
| result << url.mid(at_pos, url.size()); | |
| /// allocates some bytes on pool | |
| void *malloc(size_t size) { return Pooled::malloc(size); } | return result; |
| /// allocates some bytes clearing them with zeros | } |
| void *calloc(size_t size) { return Pooled::calloc(size); } | |
| /// throw exception | /// SQL_Driver_services Pooled implementation |
| void _throw(const char *comment) { | class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled { |
| THROW(0, 0, | public: |
| &furl, | SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool), |
| comment); | furl(aurl) { |
| } | } |
| private: | virtual void *malloc(size_t size) { return Pooled::malloc(size, 8); } |
| const String& furl; | virtual void *calloc(size_t size) { return Pooled::calloc(size); } |
| }; | |
| /** | |
| // SQL_Driver_manager | the idea is to #1 jump to C++ some function to main body, where |
| every function stack frame has exception unwind information | |
| /// @param request_url protocol://[driver-dependent] | and from there... #2 propagate_exception() |
| SQL_Connection& SQL_Driver_manager::get_connection(const String& request_url, | */ |
| Table *protocol2driver_and_client) { | virtual void _throw(const char *comment) { |
| ////__asm int 3; | // hiding passwords and addresses from accidental show [imagine user forgot @exception] |
| Pool& pool=request_url.pool(); // request pool | e=Exception(0, 0, |
| &url_without_login(pool(), furl), | |
| // we have table for locating protocol's library | comment); |
| if(!protocol2driver_and_client) | |
| PTHROW(0, 0, | longjmp(mark, 1); |
| &request_url, | } |
| "$SQL:drivers table must be defined"); | virtual void propagate_exception() { |
| throw e; | |
| // first trying to get cached connection | } |
| SQL_Connection *result=get_connection_from_cache(request_url); | |
| if(result && !result->ping()) { // we have some cached connection, is it pingable? | private: |
| result->disconnect(); // kill unpingabe=dead connection | const String& furl; |
| result=0; | Exception e; |
| } | }; |
| char *request_url_cstr; | // helpers |
| if(result) | |
| request_url_cstr=0; // calm, compiler | static void expire_connection(Array::Item *value, void *info) { |
| else { // no cached connection or it were unpingabe: connect/reconnect | SQL_Connection& connection=*static_cast<SQL_Connection *>(value); |
| int pos=request_url.pos("://", 3); | time_t older_dies=reinterpret_cast<time_t>(info); |
| if(pos<0) | |
| PTHROW(0, 0, | if(connection.connected() && connection.expired(older_dies)) |
| &request_url, | connection.disconnect(); |
| "no protocol specified"); // NOTE: not THROW, but PTHROW | } |
| static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) { | |
| // make global_url C-string on global pool | Stack& stack=*static_cast<Stack *>(value); |
| request_url_cstr=request_url.cstr(String::UL_AS_IS); | for(int i=0; i<=stack.top_index(); i++) |
| char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1); | expire_connection(stack.get(i), info); |
| strcpy(global_url_cstr, request_url_cstr); | } |
| // make global_url string on global pool | |
| String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr); | // SQL_Driver_manager |
| char *request_protocol_cstr=lsplit(&request_url_cstr, ':'); | SQL_Driver_manager::SQL_Driver_manager(Pool& apool) : Cache_manager(apool), |
| // skip "//" after ':' | driver_cache(apool), |
| while(*request_url_cstr=='/') | connection_cache(apool), |
| request_url_cstr++; | prev_expiration_pass_time(0) { |
| // make global_protocol C-string on global pool | } |
| char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1); | |
| strcpy(global_protocol_cstr, request_protocol_cstr); | SQL_Driver_manager::~SQL_Driver_manager() { |
| // make global_protocol string on global pool | connection_cache.for_each(expire_connections, |
| String& global_protocol=*new(this->pool()) String(this->pool(), | reinterpret_cast<void *>((time_t)0/*=in past=expire all*/)); |
| global_protocol_cstr); | } |
| SQL_Driver *driver; | /// @param request_url protocol://[driver-dependent] |
| const String *dlopen_file_spec=0; | SQL_Connection& SQL_Driver_manager::get_connection(const String& request_url, |
| // first trying to get cached driver | const String& request_origin, |
| if(!(driver=get_driver_from_cache(global_protocol))) { | Table *protocol2driver_and_client) { |
| // no cached | Pool& pool=request_origin.pool(); // request pool |
| const String *library=0; | |
| if(protocol2driver_and_client->locate(0, global_protocol)) { | // we have table for locating protocol's library |
| if(!(library=protocol2driver_and_client->item(1)) || library->size()==0) | if(!protocol2driver_and_client) |
| PTHROW(0, 0, | throw Exception(0, 0, |
| protocol2driver_and_client->origin_string(), | &request_url, |
| "driver library column for protocol '%s' is empty", | "$"MAIN_SQL_NAME":"MAIN_SQL_DRIVERS_NAME" table must be defined"); |
| request_protocol_cstr); | |
| if(!(dlopen_file_spec=protocol2driver_and_client->item(2)) || dlopen_file_spec->size()==0) | // construct services[request] (deassociates at close) |
| PTHROW(0, 0, | SQL_Driver_services *services=new(pool) SQL_Driver_services_impl(pool, request_url); |
| protocol2driver_and_client->origin_string(), | |
| "client library column for protocol '%s' is empty", | // first trying to get cached connection |
| request_protocol_cstr); | SQL_Connection *result=get_connection_from_cache(request_url); |
| } else | if(result) { |
| PTHROW(0, 0, | result->set_services(services); |
| &request_url, | if(!result->ping()) { // we have some cached connection, is it pingable? |
| "undefined protocol '%s'", | result->disconnect(); // kill unpingabe=dead connection |
| request_protocol_cstr); | result=0; |
| } | |
| const char *filename=library->cstr(String::UL_FILE_NAME); | } |
| lt_dlhandle handle=lt_dlopen(filename); | |
| if (!handle) | char *request_url_cstr; |
| PTHROW(0, 0, | if(result) |
| library, | request_url_cstr=0; // calm, compiler |
| "can not open the module, %s", lt_dlerror()); | else { // no cached connection or it were unpingabe: connect/reconnect |
| int pos=request_url.pos("://", 3); | |
| SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, | if(pos<0) |
| LIBRARY_CREATE_FUNC_NAME); | throw Exception(0, 0, |
| if(!create) | request_url.size()?&request_url:&request_origin, |
| PTHROW(0, 0, | "connection string must start with protocol://"); // NOTE: not THROW, but PTHROW |
| library, | |
| "function '%s' was not found", LIBRARY_CREATE_FUNC_NAME); | // make global_url C-string on global pool |
| request_url_cstr=request_url.cstr(); | |
| // create library-driver! | char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1); |
| driver=(*create)(); | strcpy(global_url_cstr, request_url_cstr); |
| // make global_url string on global pool | |
| // validate driver api version | String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr); |
| int driver_api_version=driver->api_version(); | |
| if(driver_api_version!=SQL_DRIVER_API_VERSION) | char *request_protocol_cstr=lsplit(&request_url_cstr, ':'); |
| PTHROW(0, 0, | // skip "//" after ':' |
| library, | while(*request_url_cstr=='/') |
| "driver implements API version 0x%04X not equal to 0x%04X", | request_url_cstr++; |
| driver_api_version, SQL_DRIVER_API_VERSION); | // make global_protocol C-string on global pool |
| char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1); | |
| // initialise by connecting to sql client dynamic link library | strcpy(global_protocol_cstr, request_protocol_cstr); |
| const char *dlopen_file_spec_cstr=dlopen_file_spec->cstr(String::UL_FILE_NAME); | // 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 |
| dlopen_file_spec_cstr, error); | if(!(driver=get_driver_from_cache(global_protocol))) { |
| // no cached | |
| // cache it | const String *library=0; |
| put_driver_to_cache(global_protocol, *driver); | const String *dlopen_file_spec=0; |
| } | if(protocol2driver_and_client->locate(0, global_protocol)) { |
| if(!(library=protocol2driver_and_client->item(1)) || library->size()==0) | |
| // allocate in global pool | throw Exception(0, 0, |
| // associate with services[request] | protocol2driver_and_client->origin_string(), |
| // NOTE: never freed up! | "driver library column for protocol '%s' is empty", |
| result=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver); | request_protocol_cstr); |
| } | dlopen_file_spec=protocol2driver_and_client->item(2); |
| } else | |
| // associate with services[request] (deassociates at close) | throw Exception(0, 0, |
| result->set_services(new(pool) SQL_Driver_services_impl(pool, request_url)); | &request_url, |
| // if not connected yet, do that now, when result has services | "undefined protocol '%s'", |
| if(!result->connected()) | request_protocol_cstr); |
| result->connect(request_url_cstr); | |
| // return it | if(lt_dlinit()) |
| return *result; | throw Exception(0, 0, |
| } | library, |
| "prepare to dynamic loading failed, %s", lt_dlerror()); | |
| void SQL_Driver_manager::close_connection(const String& url, | |
| SQL_Connection& connection) { | const char *filename=library->cstr(String::UL_FILE_SPEC); |
| // deassociate from services[request] | lt_dlhandle handle=lt_dlopen(filename); |
| connection.set_services(0); | if (!handle) |
| put_connection_to_cache(url, connection); | throw Exception(0, 0, |
| } | library, |
| "can not open the module, %s", lt_dlerror()); | |
| // driver cache | SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, |
| SQL_DRIVER_CREATE_NAME); | |
| SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) { | if(!create) |
| SYNCHRONIZED; | throw Exception(0, 0, |
| library, | |
| return static_cast<SQL_Driver *>(driver_cache.get(protocol)); | "function '"SQL_DRIVER_CREATE_NAME"' was not found"); |
| } | |
| // create library-driver! | |
| void SQL_Driver_manager::put_driver_to_cache(const String& protocol, | driver=(*create)(); |
| SQL_Driver& driver) { | |
| SYNCHRONIZED; | // validate driver api version |
| int driver_api_version=driver->api_version(); | |
| driver_cache.put(protocol, &driver); | if(driver_api_version!=SQL_DRIVER_API_VERSION) |
| } | throw Exception(0, 0, |
| library, | |
| // connection cache | "driver implements API version 0x%04X not equal to 0x%04X", |
| driver_api_version, SQL_DRIVER_API_VERSION); | |
| SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) { | |
| SYNCHRONIZED; | // initialise by connecting to sql client dynamic link library |
| char *dlopen_file_spec_cstr= | |
| maybe_expire_connection_cache(); | dlopen_file_spec && dlopen_file_spec->size()? |
| dlopen_file_spec->cstr(String::UL_AS_IS):0; | |
| if(Stack *connections=static_cast<Stack *>(connection_cache.get(url))) | if(const char *error=driver->initialize( |
| while(connections->top_index()>=0) { // there are cached connections to that 'url' | dlopen_file_spec_cstr)) |
| SQL_Connection *result=static_cast<SQL_Connection *>(connections->pop()); | throw Exception(0, 0, |
| if(result->connected()) // not expired? | library, |
| return result; | "driver failed to initialize client library '%s', %s", |
| } | 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) { | |
| SYNCHRONIZED; | // allocate in global pool |
| // associate with services[request] | |
| Stack *connections=static_cast<Stack *>(connection_cache.get(url)); | // NOTE: never freed up! |
| if(!connections) { // there are no cached connections to that 'url' yet? | result=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver); |
| connections=NEW Stack(pool()); // NOTE: never freed up! | // associate with services[request] (deassociates at close) |
| connection_cache.put(url, connections); | result->set_services(services); |
| } | } |
| connections->push(&connection); | |
| } | // if not connected yet, do that now, when result has services |
| if(!result->connected()) | |
| static void expire_connection(Array::Item *value, void *info) { | result->connect(request_url_cstr); |
| SQL_Connection& connection=*static_cast<SQL_Connection *>(value); | // return it |
| time_t older_dies=reinterpret_cast<time_t>(info); | return *result; |
| } | |
| if(connection.expired(older_dies)) | |
| connection.disconnect(); | void SQL_Driver_manager::close_connection(const String& url, |
| } | SQL_Connection& connection) { |
| static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) { | // deassociate from services[request] |
| Stack& stack=*static_cast<Stack *>(value); | connection.set_services(0); |
| for(int i=0; i<=stack.top_index(); i++) | put_connection_to_cache(url, connection); |
| expire_connection(stack.get(i), info); | } |
| } | |
| void SQL_Driver_manager::maybe_expire_connection_cache() { | |
| time_t now=time(0); | // driver cache |
| if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) { | SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) { |
| connection_cache.for_each(expire_connections, | SYNCHRONIZED; |
| 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; | |
| } | |