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

version 1.28, 2001/08/27 12:02:24 version 1.32, 2001/09/05 09:22:45
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://design.ru/paf)          Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
 */  */
 static const char *RCSId="$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_sapi.h"  #include "pa_sapi.h"
   
 // globals  // helper macros
   
 SQL_Driver_manager *SQL_driver_manager;  #define STRINGIZE(name) #name
   
 // consts  // globals
   
 const char *LIBRARY_CREATE_FUNC_NAME="create";  SQL_Driver_manager *SQL_driver_manager;
 const int EXPIRE_UNUSED_CONNECTION_SECONDS=60;  
 const int CHECK_EXPIRED_CONNECTIONS_SECONDS=60*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:  
         SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool),  /// SQL_Driver_services Pooled implementation
                 furl(aurl) {  class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled {
         }  public:
           SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool),
         /// allocates some bytes on pool                  furl(aurl) {
         void *malloc(size_t size) { return Pooled::malloc(size); }          }
         /// allocates some bytes clearing them with zeros  
         void *calloc(size_t size) { return Pooled::calloc(size); }          /// allocates some bytes on pool
         /// throw exception          void *malloc(size_t size) { return Pooled::malloc(size); }
         void _throw(const char *comment) {          /// allocates some bytes clearing them with zeros
                 THROW(0, 0,          void *calloc(size_t size) { return Pooled::calloc(size); }
                         &furl,          /// throw exception
                         comment);          void _throw(const char *comment) { 
         }                  THROW(0, 0, 
                           &furl, 
 private:                          comment); 
         const String& furl;          }
 };  
   private:
 // SQL_Driver_manager          const String& furl;
   };
 /// @param request_url protocol://[driver-dependent]  
 SQL_Connection& SQL_Driver_manager::get_connection(const String& request_url,  // SQL_Driver_manager
                                                                                                    Table *protocol2driver_and_client) {  
         Pool& pool=request_url.pool(); // request pool                                                                                      /// @param request_url protocol://[driver-dependent]
   SQL_Connection& SQL_Driver_manager::get_connection(const String& request_url, 
         // we have table for locating protocol's library                                                                                                     Table *protocol2driver_and_client) {
         if(!protocol2driver_and_client)          Pool& pool=request_url.pool(); // request pool                                                                                     
                 PTHROW(0, 0,  
                         &request_url,          // we have table for locating protocol's library
                         "$SQL:drivers table must be defined");          if(!protocol2driver_and_client)
                   PTHROW(0, 0,
         // first trying to get cached connection                          &request_url,
         SQL_Connection *result=get_connection_from_cache(request_url);                          "$"MAIN_SQL_NAME":"MAIN_SQL_DRIVERS_NAME" table must be defined");
         if(result && !result->ping()) { // we have some cached connection, is it pingable?  
                 result->disconnect(); // kill unpingabe=dead connection          // first trying to get cached connection
                 result=0;          SQL_Connection *result=get_connection_from_cache(request_url);
         }          if(result && !result->ping()) { // we have some cached connection, is it pingable?
                   result->disconnect(); // kill unpingabe=dead connection
         char *request_url_cstr;                  result=0;
         if(result)          }
                 request_url_cstr=0; // calm, compiler  
         else { // no cached connection or it were unpingabe: connect/reconnect          char *request_url_cstr;
                 int pos=request_url.pos("://", 3);          if(result)
                 if(pos<0)                  request_url_cstr=0; // calm, compiler
                         PTHROW(0, 0,          else { // no cached connection or it were unpingabe: connect/reconnect
                                 &request_url,                  int pos=request_url.pos("://", 3);
                                 "no protocol specified"); // NOTE: not THROW, but PTHROW                  if(pos<0)
                           PTHROW(0, 0,
                 // make global_url C-string on global pool                                  &request_url,
                 request_url_cstr=request_url.cstr(String::UL_AS_IS);                                  "no protocol specified"); // NOTE: not THROW, but PTHROW
                 char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);  
                 strcpy(global_url_cstr, request_url_cstr);                  // make global_url C-string on global pool
                 // make global_url string on global pool                  request_url_cstr=request_url.cstr(String::UL_AS_IS);
                 String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);                  char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);
                                  strcpy(global_url_cstr, request_url_cstr);
                 char *request_protocol_cstr=lsplit(&request_url_cstr, ':');                  // make global_url string on global pool
                 // skip "//" after ':'                  String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);
                 while(*request_url_cstr=='/')                  
                         request_url_cstr++;                  char *request_protocol_cstr=lsplit(&request_url_cstr, ':');
                 // make global_protocol C-string on global pool                  // skip "//" after ':'
                 char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);                  while(*request_url_cstr=='/')
                 strcpy(global_protocol_cstr, request_protocol_cstr);                          request_url_cstr++;
                 // make global_protocol string on global pool                  // make global_protocol C-string on global pool
                 String& global_protocol=*new(this->pool()) String(this->pool(),                  char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);
                         global_protocol_cstr);                  strcpy(global_protocol_cstr, request_protocol_cstr);
                   // make global_protocol string on global pool
                 SQL_Driver *driver;                  String& global_protocol=*new(this->pool()) String(this->pool(), 
                 const String *dlopen_file_spec=0;                          global_protocol_cstr);
                 // first trying to get cached driver  
                 if(!(driver=get_driver_from_cache(global_protocol))) {                  SQL_Driver *driver;
                         // no cached                  const String *dlopen_file_spec=0;
                         const String *library=0;                  // first trying to get cached driver
                         if(protocol2driver_and_client->locate(0, global_protocol)) {                  if(!(driver=get_driver_from_cache(global_protocol))) {
                                 if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)                          // no cached
                                         PTHROW(0, 0,                          const String *library=0;
                                                 protocol2driver_and_client->origin_string(),                          if(protocol2driver_and_client->locate(0, global_protocol)) {
                                                 "driver library column for protocol '%s' is empty",                                  if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
                                                         request_protocol_cstr);                                          PTHROW(0, 0,
                                 dlopen_file_spec=protocol2driver_and_client->item(2);                                                  protocol2driver_and_client->origin_string(),
                         } else                                                  "driver library column for protocol '%s' is empty", 
                                 PTHROW(0, 0,                                                          request_protocol_cstr);
                                         &request_url,                                  dlopen_file_spec=protocol2driver_and_client->item(2);
                                         "undefined protocol '%s'",                          } else
                                                 request_protocol_cstr);                                  PTHROW(0, 0,
                                           &request_url,
                         if(lt_dlinit())                                          "undefined protocol '%s'", 
                                 PTHROW(0, 0,                                                  request_protocol_cstr);
                                         library,  
                                         "prepare to dynamic loading failed, %s", lt_dlerror());                          if(lt_dlinit())
                                   PTHROW(0, 0,
                         const char *filename=library->cstr(String::UL_FILE_NAME);                                          library,
                         lt_dlhandle handle=lt_dlopen(filename);                                          "prepare to dynamic loading failed, %s", lt_dlerror());
                         if (!handle)  
                                 PTHROW(0, 0,                          const char *filename=library->cstr(String::UL_FILE_NAME);
                                         library,                          lt_dlhandle handle=lt_dlopen(filename);
                                         "can not open the module, %s", lt_dlerror());                          if (!handle)
                                   PTHROW(0, 0,
                         SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle,                                          library,
                                 LIBRARY_CREATE_FUNC_NAME);                                            "can not open the module, %s", lt_dlerror());
                         if(!create)  
                                 PTHROW(0, 0,                          SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, 
                                         library,                                  STRINGIZE(SQL_DRIVER_CREATE));
                                         "function '%s' was not found", LIBRARY_CREATE_FUNC_NAME);                          if(!create)
                                   PTHROW(0, 0,
                         // create library-driver!                                          library,
                         driver=(*create)();                                          "function '%s' was not found", STRINGIZE(SQL_DRIVER_CREATE));
   
                         // validate driver api version                          // create library-driver!
                         int driver_api_version=driver->api_version();                          driver=(*create)();
                         if(driver_api_version!=SQL_DRIVER_API_VERSION)  
                                 PTHROW(0, 0,                          // validate driver api version
                                         library,                          int driver_api_version=driver->api_version();
                                         "driver implements API version 0x%04X not equal to 0x%04X",                          if(driver_api_version!=SQL_DRIVER_API_VERSION)
                                                 driver_api_version, SQL_DRIVER_API_VERSION);                                  PTHROW(0, 0,
                                           library,
                         // initialise by connecting to sql client dynamic link library                                          "driver implements API version 0x%04X not equal to 0x%04X",
                         bool specified_dlopen_file_spec=dlopen_file_spec && dlopen_file_spec->size();                                                  driver_api_version, SQL_DRIVER_API_VERSION);
                         const char *dlopen_file_spec_cstr=  
                                 specified_dlopen_file_spec?                          // initialise by connecting to sql client dynamic link library
                                 dlopen_file_spec->cstr(String::UL_FILE_NAME):0;                          bool specified_dlopen_file_spec=dlopen_file_spec && dlopen_file_spec->size();
                         if(const char *error=driver->initialize(                          const char *dlopen_file_spec_cstr=
                                 dlopen_file_spec_cstr))                                  specified_dlopen_file_spec?
                                 PTHROW(0, 0,                                  dlopen_file_spec->cstr(String::UL_FILE_NAME):0;
                                         library,                          if(const char *error=driver->initialize(
                                         "driver failed to initialize client library '%s', %s",                                  dlopen_file_spec_cstr))
                                                 specified_dlopen_file_spec?dlopen_file_spec_cstr:"unspecifed",                                  PTHROW(0, 0,
                                                 error);                                          library,
                                           "driver failed to initialize client library '%s', %s",
                         // cache it                                                  specified_dlopen_file_spec?dlopen_file_spec_cstr:"unspecifed", 
                         put_driver_to_cache(global_protocol, *driver);                                                  error);
                 }  
                                  // cache it
                 // allocate in global pool                          put_driver_to_cache(global_protocol, *driver);
                 // associate with services[request]                  }
                 // NOTE: never freed up!          
                 result=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver);                  // allocate in global pool 
         }                  // associate with services[request]
                   // NOTE: never freed up!
         // associate with services[request]  (deassociates at close)                  result=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver);
         result->set_services(new(pool) SQL_Driver_services_impl(pool, request_url));          }
         // if not connected yet, do that now, when result has services  
         if(!result->connected())          // associate with services[request]  (deassociates at close)
                 result->connect(request_url_cstr);          result->set_services(new(pool) SQL_Driver_services_impl(pool, request_url)); 
         // return it          // if not connected yet, do that now, when result has services
         return *result;          if(!result->connected())
 }                  result->connect(request_url_cstr);
           // return it
 void SQL_Driver_manager::close_connection(const String& url,          return *result;
                                                                                   SQL_Connection& connection) {  }
         // deassociate from services[request]  
         connection.set_services(0);  void SQL_Driver_manager::close_connection(const String& url, 
         put_connection_to_cache(url, connection);                                                                                    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;  // driver cache
   
         return static_cast<SQL_Driver *>(driver_cache.get(protocol));  SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
 }          SYNCHRONIZED;
   
 void SQL_Driver_manager::put_driver_to_cache(const String& protocol,          return static_cast<SQL_Driver *>(driver_cache.get(protocol));
                                                                                          SQL_Driver& driver) {  }
         SYNCHRONIZED;  
   void SQL_Driver_manager::put_driver_to_cache(const String& protocol, 
         driver_cache.put(protocol, &driver);                                                                                           SQL_Driver& driver) {
 }          SYNCHRONIZED;
   
 // connection cache          driver_cache.put(protocol, &driver);
 /// @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;  // connection cache
   /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
         maybe_expire_connection_cache();  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'          maybe_expire_connection_cache();
                         SQL_Connection *result=static_cast<SQL_Connection *>(connections->pop());  
                         if(result->connected()) // not expired?          if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
                                 return result;                  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 0;                                  return result;
 }                  }
   
 void SQL_Driver_manager::put_connection_to_cache(const String& url,          return 0;
                                                                                                  SQL_Connection& connection) {  }
         SYNCHRONIZED;  
   void SQL_Driver_manager::put_connection_to_cache(const String& url, 
         Stack *connections=static_cast<Stack *>(connection_cache.get(url));                                                                                                   SQL_Connection& connection) { 
         if(!connections) { // there are no cached connections to that 'url' yet?          SYNCHRONIZED;
                 connections=NEW Stack(pool()); // NOTE: never freed up!  
                 connection_cache.put(url, connections);          Stack *connections=static_cast<Stack *>(connection_cache.get(url));
         }                if(!connections) { // there are no cached connections to that 'url' yet?
         connections->push(&connection);                  connections=NEW Stack(pool()); // NOTE: never freed up!
 }                  connection_cache.put(url, connections);
           }       
 static void expire_connection(Array::Item *value, void *info) {          connections->push(&connection);
         SQL_Connection& connection=*static_cast<SQL_Connection *>(value);  }
         time_t older_dies=reinterpret_cast<time_t>(info);  
   static void expire_connection(Array::Item *value, void *info) {
         if(connection.connected() && connection.expired(older_dies))          SQL_Connection& connection=*static_cast<SQL_Connection *>(value);
                 connection.disconnect();          time_t older_dies=reinterpret_cast<time_t>(info);
 }  
 static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {          if(connection.connected() && connection.expired(older_dies))
         Stack& stack=*static_cast<Stack *>(value);                  connection.disconnect();
         for(int i=0; i<=stack.top_index(); i++)  }
                 expire_connection(stack.get(i), info);  static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {
 }          Stack& stack=*static_cast<Stack *>(value);
 void SQL_Driver_manager::maybe_expire_connection_cache() {          for(int i=0; i<=stack.top_index(); i++)
         time_t now=time(0);                  expire_connection(stack.get(i), info);
   }
         if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {  void SQL_Driver_manager::maybe_expire_connection_cache() {
                 connection_cache.for_each(expire_connections,          time_t now=time(0);
                         reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));  
           if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
                 prev_expiration_pass_time=now;                  connection_cache.for_each(expire_connections, 
         }                          reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
 }  
                   prev_expiration_pass_time=now;
           }
   }

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


E-mail: