Diff for /parser3/src/main/pa_sql_driver_manager.C between versions 1.28 and 1.68

version 1.28, 2001/08/27 12:02:24 version 1.68, 2002/12/09 12:19:16
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, 2002 ArtLebedev Group (http://www.artlebedev.com)
           Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
         Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)  */
 */  
 static const char *RCSId="$Id$";  static const char* IDENT_SQL_DRIVER_MANAGER_C="$Date$";
   
 #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_sapi.h"  #include "pa_vhash.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=60*2;  const int CHECK_EXPIRED_CONNECTIONS_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
   
   // helpers
 /// SQL_Driver_services Pooled implementation  
 class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled {  const String& url_without_login(Pool& pool, const String& url) {
 public:          String& result=*new(pool) String(pool);
         SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool),          result << url.mid(0, url.pos(":")) << "://****";
                 furl(aurl) {  
         }          int at_pos=url.pos("@");
           if(at_pos>0)
         /// allocates some bytes on pool                  result << url.mid(at_pos, url.size());
         void *malloc(size_t size) { return Pooled::malloc(size); }  
         /// allocates some bytes clearing them with zeros          return result;
         void *calloc(size_t size) { return Pooled::calloc(size); }  }
         /// throw exception  
         void _throw(const char *comment) {  /// SQL_Driver_services Pooled implementation
                 THROW(0, 0,  class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled {
                         &furl,  public:
                         comment);          SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool),
         }                  furl(aurl) {
           }
 private:  
         const String& furl;          virtual void *malloc(size_t size) { return Pooled::malloc(size, 8); }
 };          virtual void *calloc(size_t size) { return Pooled::calloc(size); }
   
 // SQL_Driver_manager          /**
                   normally we can't 'throw' from dynamic library, so
 /// @param request_url protocol://[driver-dependent]                  the idea is to #1 jump to C++ some function to main body, where
 SQL_Connection& SQL_Driver_manager::get_connection(const String& request_url,                  every function stack frame has exception unwind information
                                                                                                    Table *protocol2driver_and_client) {                  and from there... #2 propagate_exception()
         Pool& pool=request_url.pool(); // request pool                                                                                      
           but when parser configured --with-sjlj-exceptions
         // we have table for locating protocol's library                  one can simply 'throw' from dynamic library.
         if(!protocol2driver_and_client)                  [sad story: one can not longjump/throw due to some bug in gcc as of 3.2.1 version]
                 PTHROW(0, 0,          */
                         &request_url,          virtual void _throw(const SQL_Error& aexception) { 
                         "$SQL:drivers table must be defined");                  // converting SQL_exception to parser Exception
                   // hiding passwords and addresses from accidental show [imagine user forgot @exception]
         // first trying to get cached connection  #ifdef PA_WITH_SJLJ_EXCEPTIONS
         SQL_Connection *result=get_connection_from_cache(request_url);                  throw
         if(result && !result->ping()) { // we have some cached connection, is it pingable?  #else
                 result->disconnect(); // kill unpingabe=dead connection                  fexception=
                 result=0;  #endif
         }                          Exception(aexception.type(), 
                                   aexception.problem_source()?static_cast<const String*>(aexception.problem_source())
         char *request_url_cstr;                                          :&url_without_login(pool(), furl),
         if(result)                                  aexception.comment()); 
                 request_url_cstr=0; // calm, compiler  
         else { // no cached connection or it were unpingabe: connect/reconnect  #ifndef PA_WITH_SJLJ_EXCEPTIONS
                 int pos=request_url.pos("://", 3);                  longjmp(mark, 1);
                 if(pos<0)  #endif
                         PTHROW(0, 0,          }
                                 &request_url,          virtual void propagate_exception() {
                                 "no protocol specified"); // NOTE: not THROW, but PTHROW  #ifndef PA_WITH_SJLJ_EXCEPTIONS
                   throw fexception;
                 // make global_url C-string on global pool  #endif
                 request_url_cstr=request_url.cstr(String::UL_AS_IS);          }
                 char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);  
                 strcpy(global_url_cstr, request_url_cstr);  private:
                 // make global_url string on global pool          const String& furl;
                 String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);          Exception fexception;
                  };
                 char *request_protocol_cstr=lsplit(&request_url_cstr, ':');  
                 // skip "//" after ':'  // helpers
                 while(*request_url_cstr=='/')  
                         request_url_cstr++;  static void expire_connection(Array::Item *value, void *info) {
                 // make global_protocol C-string on global pool          SQL_Connection& connection=*static_cast<SQL_Connection *>(value);
                 char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);          time_t older_dies=reinterpret_cast<time_t>(info);
                 strcpy(global_protocol_cstr, request_protocol_cstr);  
                 // make global_protocol string on global pool          if(connection.connected() && connection.expired(older_dies))
                 String& global_protocol=*new(this->pool()) String(this->pool(),                  connection.disconnect();
                         global_protocol_cstr);  }
   static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {
                 SQL_Driver *driver;          Stack& stack=*static_cast<Stack *>(value);
                 const String *dlopen_file_spec=0;          for(int i=0; i<=stack.top_index(); i++)
                 // first trying to get cached driver                  expire_connection(stack.get(i), info);
                 if(!(driver=get_driver_from_cache(global_protocol))) {  }
                         // no cached  
                         const String *library=0;  // SQL_Driver_manager
                         if(protocol2driver_and_client->locate(0, global_protocol)) {  
                                 if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)  SQL_Driver_manager::SQL_Driver_manager(Pool& apool) : Cache_manager(apool),
                                         PTHROW(0, 0,                  driver_cache(apool),
                                                 protocol2driver_and_client->origin_string(),                  connection_cache(apool),
                                                 "driver library column for protocol '%s' is empty",                  prev_expiration_pass_time(0) {
                                                         request_protocol_cstr);  }
                                 dlopen_file_spec=protocol2driver_and_client->item(2);  
                         } else  SQL_Driver_manager::~SQL_Driver_manager() {
                                 PTHROW(0, 0,          connection_cache.for_each(expire_connections, 
                                         &request_url,                  reinterpret_cast<void *>(time(0)+1/*=in future=expire all*/));
                                         "undefined protocol '%s'",  }
                                                 request_protocol_cstr);  
   /// @param request_url protocol://[driver-dependent]
                         if(lt_dlinit())  SQL_Connection_ptr SQL_Driver_manager::get_connection(const String& request_url,
                                 PTHROW(0, 0,                                                                                                     const String& request_origin,
                                         library,                                                                                                     Table *protocol2driver_and_client) {
                                         "prepare to dynamic loading failed, %s", lt_dlerror());          Pool& pool=request_origin.pool(); // request pool                                                                                          
   
                         const char *filename=library->cstr(String::UL_FILE_NAME);          // we have table for locating protocol's library
                         lt_dlhandle handle=lt_dlopen(filename);          if(!protocol2driver_and_client)
                         if (!handle)                  throw Exception("parser.runtime",
                                 PTHROW(0, 0,                          &request_url,
                                         library,                          "$"MAIN_SQL_NAME":"MAIN_SQL_DRIVERS_NAME" table must be defined");
                                         "can not open the module, %s", lt_dlerror());  
           // construct services[request]  (deassociates at close)
                         SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle,          SQL_Driver_services *services=new(pool) SQL_Driver_services_impl(pool, request_url); 
                                 LIBRARY_CREATE_FUNC_NAME);    
                         if(!create)          // first trying to get cached connection
                                 PTHROW(0, 0,          SQL_Connection *connection=get_connection_from_cache(request_url);
                                         library,          if(connection) {
                                         "function '%s' was not found", LIBRARY_CREATE_FUNC_NAME);                  connection->set_services(services);
                   if(!connection->ping()) { // we have some cached connection, is it pingable?
                         // create library-driver!                          connection->disconnect(); // kill unpingabe=dead connection
                         driver=(*create)();                          connection=0;
                   }
                         // validate driver api version          }
                         int driver_api_version=driver->api_version();  
                         if(driver_api_version!=SQL_DRIVER_API_VERSION)          char *request_url_cstr;
                                 PTHROW(0, 0,          if(connection)
                                         library,                  request_url_cstr=0; // calm, compiler
                                         "driver implements API version 0x%04X not equal to 0x%04X",          else { // no cached connection or it were unpingabe: connect/reconnect
                                                 driver_api_version, SQL_DRIVER_API_VERSION);                  int pos=request_url.pos("://", 3);
                   if(pos<0)
                         // initialise by connecting to sql client dynamic link library                          throw Exception("parser.runtime",
                         bool specified_dlopen_file_spec=dlopen_file_spec && dlopen_file_spec->size();                                  request_url.size()?&request_url:&request_origin,
                         const char *dlopen_file_spec_cstr=                                  "connection string must start with protocol://");
                                 specified_dlopen_file_spec?  
                                 dlopen_file_spec->cstr(String::UL_FILE_NAME):0;                  // make global_url C-string on global pool
                         if(const char *error=driver->initialize(                  request_url_cstr=request_url.cstr();
                                 dlopen_file_spec_cstr))                  char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);
                                 PTHROW(0, 0,                  strcpy(global_url_cstr, request_url_cstr);
                                         library,                  // make global_url string on global pool
                                         "driver failed to initialize client library '%s', %s",                  String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);
                                                 specified_dlopen_file_spec?dlopen_file_spec_cstr:"unspecifed",                  
                                                 error);                  char *request_protocol_cstr=lsplit(&request_url_cstr, ':');
                   // skip "//" after ':'
                         // cache it                  while(*request_url_cstr=='/')
                         put_driver_to_cache(global_protocol, *driver);                          request_url_cstr++;
                 }                  // make global_protocol C-string on global pool
                          char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);
                 // allocate in global pool                  strcpy(global_protocol_cstr, request_protocol_cstr);
                 // associate with services[request]                  // make global_protocol string on global pool
                 // NOTE: never freed up!                  String& global_protocol=*new(this->pool()) String(this->pool(), 
                 result=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver);                          global_protocol_cstr);
         }  
                   SQL_Driver *driver;
         // associate with services[request]  (deassociates at close)                  // first trying to get cached driver
         result->set_services(new(pool) SQL_Driver_services_impl(pool, request_url));                  if(!(driver=get_driver_from_cache(global_protocol))) {
         // if not connected yet, do that now, when result has services                          // no cached
         if(!result->connected())                          const String *library=0;
                 result->connect(request_url_cstr);                          const String *dlopen_file_spec=0;
         // return it                          if(protocol2driver_and_client->locate(0, global_protocol)) {
         return *result;                                  if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
 }                                          throw Exception("parser.runtime",
                                                   protocol2driver_and_client->origin_string(),
 void SQL_Driver_manager::close_connection(const String& url,                                                  "driver library column for protocol '%s' is empty", 
                                                                                   SQL_Connection& connection) {                                                          request_protocol_cstr);
         // deassociate from services[request]                                  dlopen_file_spec=protocol2driver_and_client->item(2);
         connection.set_services(0);                          } else
         put_connection_to_cache(url, connection);                                  throw Exception("parser.runtime",
 }                                          &request_url,
                                           "undefined protocol '%s'", 
                                                   request_protocol_cstr);
 // driver cache  
                           if(lt_dlinit())
 SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {                                  throw Exception(0,
         SYNCHRONIZED;                                          library,
                                           "prepare to dynamic loading failed, %s", lt_dlerror());
         return static_cast<SQL_Driver *>(driver_cache.get(protocol));  
 }                          const char *filename=library->cstr(String::UL_FILE_SPEC);
                           lt_dlhandle handle=lt_dlopen(filename);
 void SQL_Driver_manager::put_driver_to_cache(const String& protocol,                          if (!handle)
                                                                                          SQL_Driver& driver) {                                  throw Exception(0,
         SYNCHRONIZED;                                          library,
                                           "can not open the module, %s", lt_dlerror());
         driver_cache.put(protocol, &driver);  
 }                          SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, 
                                   SQL_DRIVER_CREATE_NAME);
 // connection cache                          if(!create)
 /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]                                  throw Exception(0,
 SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) {                                          library,
         SYNCHRONIZED;                                          "function '"SQL_DRIVER_CREATE_NAME"' was not found");
   
         maybe_expire_connection_cache();                          // create library-driver!
                           driver=(*create)();
         if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))  
                 while(connections->top_index()>=0) { // there are cached connections to that 'url'                          // validate driver api version
                         SQL_Connection *result=static_cast<SQL_Connection *>(connections->pop());                          int driver_api_version=driver->api_version();
                         if(result->connected()) // not expired?                          if(driver_api_version!=SQL_DRIVER_API_VERSION)
                                 return result;                                  throw Exception(0,
                 }                                          library,
                                           "driver implements API version 0x%04X not equal to 0x%04X",
         return 0;                                                  driver_api_version, SQL_DRIVER_API_VERSION);
 }  
                           // initialise by connecting to sql client dynamic link library
 void SQL_Driver_manager::put_connection_to_cache(const String& url,                          char *dlopen_file_spec_cstr=
                                                                                                  SQL_Connection& connection) {                                  dlopen_file_spec && dlopen_file_spec->size()?
         SYNCHRONIZED;                                  dlopen_file_spec->cstr(String::UL_AS_IS):0;
                           if(const char *error=driver->initialize(
         Stack *connections=static_cast<Stack *>(connection_cache.get(url));                                  dlopen_file_spec_cstr))
         if(!connections) { // there are no cached connections to that 'url' yet?                                  throw Exception(0,
                 connections=NEW Stack(pool()); // NOTE: never freed up!                                          library,
                 connection_cache.put(url, connections);                                          "driver failed to initialize client library '%s', %s",
         }                                                        dlopen_file_spec_cstr?dlopen_file_spec_cstr:"unspecifed", 
         connections->push(&connection);                                                  error);
 }  
                           // cache it
 static void expire_connection(Array::Item *value, void *info) {                          put_driver_to_cache(global_protocol, *driver);
         SQL_Connection& connection=*static_cast<SQL_Connection *>(value);                  }
         time_t older_dies=reinterpret_cast<time_t>(info);          
                   // allocate in global pool 
         if(connection.connected() && connection.expired(older_dies))                  // associate with services[request]
                 connection.disconnect();                  // NOTE: never freed up!
 }                  connection=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver);
 static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {                  // associate with services[request]  (deassociates at close)
         Stack& stack=*static_cast<Stack *>(value);                  connection->set_services(services); 
         for(int i=0; i<=stack.top_index(); i++)          }
                 expire_connection(stack.get(i), info);  
 }          // if not connected yet, do that now, when connection has services
 void SQL_Driver_manager::maybe_expire_connection_cache() {          if(!connection->connected())
         time_t now=time(0);                  connection->connect(request_url_cstr);
           // return autoclosing object for it
         if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {          return SQL_Connection_ptr(connection);
                 connection_cache.for_each(expire_connections,  }
                         reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));  
   void SQL_Driver_manager::close_connection(const String& url, 
                 prev_expiration_pass_time=now;                                                                                    SQL_Connection& connection) {
         }          // deassociate from services[request]
 }          connection.set_services(0);
           put_connection_to_cache(url, connection);
   }
   
   
   // driver cache
   
   SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
           SYNCHRONIZED;
   
           return static_cast<SQL_Driver *>(driver_cache.get(protocol));
   }
   
   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_used=connection.get_time_used();
                   const char *unsafe_time_cstr=ctime(&time_used);
                   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;
   }

Removed from v.1.28  
changed lines
  Added in v.1.68


E-mail: