--- parser3/src/include/Attic/pa_db_connection.h 2001/10/25 13:17:53 1.9 +++ parser3/src/include/Attic/pa_db_connection.h 2002/02/08 08:30:12 1.21 @@ -2,60 +2,61 @@ Parser: sql driver connection decl. global sql driver connection, must be thread-safe - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) - $Id: pa_db_connection.h,v 1.9 2001/10/25 13:17:53 paf Exp $ + $Id: pa_db_connection.h,v 1.21 2002/02/08 08:30:12 paf Exp $ */ #ifndef PA_DB_CONNECTION_H #define PA_DB_CONNECTION_H -#include "pa_config_includes.h" #include "pa_pool.h" #include "pa_hash.h" -#include "pa_db_manager.h" - -#ifdef HAVE_DB_H -# include -#endif +#include "pa_db_table.h" +#include "pa_value.h" // defines // forwards class DB_Table; +class DB_Connection_ptr; /// sql driver connection class DB_Connection : public Pooled { - friend DB_Table; + friend class DB_Table; + friend class DB_Connection_ptr; public: - DB_Connection(Pool& pool, const String& db_home); + DB_Connection(Pool& apool, const String& db_home); + ~DB_Connection(); - void set_services(Pool *aservices_pool) { - time_used=time(0); // they started to use at this time - fservices_pool=aservices_pool; - } bool expired(time_t older_dies) { - return time_usedclose_connection(fdb_home, *this); - } - bool connected() { return fconnected; } - void connect(); - void disconnect(); - bool ping() { return errors==0; } + const String& db_home() { return fdb_home; } + time_t get_time_used() { return time_used; } + int get_users_count() { return used; } /** - connect to specified file_name, - using driver dynamic library found in table, if not loaded yet - checks driver version + connect to specified file_name */ - DB_Table& get_table(const String& file_name, const String& request_origin); - void clear_dbfile(const String& file_name); + DB_Table_ptr get_table_ptr(const String& file_name, const String *source); + +private: // connection usage methods + + void use() { + time_used=time(0); // they started to use at this time + used++; + } + void unuse() { + used--; + } + +private: // connection usage data + + int used; private: // table cache @@ -73,17 +74,46 @@ private: // for DB_Table private: time_t time_used; - Pool *fservices_pool; const String& fdb_home; - bool fconnected; DB_ENV dbenv; - int errors; Hash table_cache; private: void check(const char *operation, const String *source, int error); + +public: + + Value& get_status(Pool& pool, const String *source); +}; + +/// Auto-object used to track DB_Connection usage +class DB_Connection_ptr { + DB_Connection *fconnection; +public: + explicit DB_Connection_ptr(DB_Connection *aconnection) : fconnection(aconnection) { + fconnection->use(); + } + ~DB_Connection_ptr() { + fconnection->unuse(); + } + DB_Connection* operator->() { + return fconnection; + } + + // copying + DB_Connection_ptr(const DB_Connection_ptr& src) : fconnection(src.fconnection) { + fconnection->use(); + } + DB_Connection_ptr& operator =(const DB_Connection_ptr& src) { + // may do without this=src check + fconnection->unuse(); + fconnection=src.fconnection; + fconnection->use(); + + return *this; + } }; #endif