Annotation of parser3/src/include/pa_db_connection.h, revision 1.10

1.1       parser      1: /** @file
1.9       paf         2:        Parser: sql driver connection decl.
                      3:        global sql driver connection, must be thread-safe
1.1       parser      4: 
                      5:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: 
1.10    ! paf         8:        $Id: pa_db_connection.h,v 1.9 2001/10/25 13:17:53 paf Exp $
1.1       parser      9: */
                     10: 
                     11: #ifndef PA_DB_CONNECTION_H
                     12: #define PA_DB_CONNECTION_H
                     13: 
                     14: #include "pa_config_includes.h"
                     15: #include "pa_pool.h"
1.9       paf        16: #include "pa_hash.h"
1.10    ! paf        17: #include "pa_db_table.h"
1.1       parser     18: 
                     19: #ifdef HAVE_DB_H
                     20: #      include <db.h>
                     21: #endif
                     22: 
                     23: // defines
                     24: 
1.2       parser     25: // forwards
                     26: 
1.9       paf        27: class DB_Table;
1.10    ! paf        28: class DB_Connection_ptr;
1.1       parser     29: 
1.9       paf        30: /// sql driver connection
1.1       parser     31: class DB_Connection : public Pooled {
1.9       paf        32:        friend DB_Table;
1.10    ! paf        33:        friend DB_Connection_ptr;
1.1       parser     34: public:
                     35: 
1.10    ! paf        36:        DB_Connection(Pool& apool, const String& db_home);
        !            37:        ~DB_Connection();
1.1       parser     38: 
                     39:        bool expired(time_t older_dies) {
1.10    ! paf        40:                return !used && time_used<older_dies;
1.1       parser     41:        }
                     42: 
1.9       paf        43:        /**
                     44:                connect to specified file_name, 
                     45:                using driver dynamic library found in table, if not loaded yet
                     46:                checks driver version
                     47:        */
1.10    ! paf        48:        DB_Table_ptr get_table_ptr(const String& file_name, const String *origin);
1.9       paf        49:        void clear_dbfile(const String& file_name);
                     50: 
1.10    ! paf        51: private: // connection usage methods
        !            52: 
        !            53:        void use() {
        !            54:                time_used=time(0); // they started to use at this time
        !            55:                used++;
        !            56:        }
        !            57:        void unuse() {
        !            58:                used--;
        !            59:        }
        !            60: 
        !            61: private: // connection usage data
        !            62: 
        !            63:        int used;
        !            64: 
1.9       paf        65: private: // table cache
                     66: 
                     67:        DB_Table *get_table_from_cache(const String& file_name);
                     68:        void put_table_to_cache(const String& file_name, DB_Table& table);
                     69:        void maybe_expire_table_cache();
1.1       parser     70: private:
1.9       paf        71:        time_t prev_expiration_pass_time;
1.1       parser     72: 
1.9       paf        73: private: // for DB_Table
1.1       parser     74: 
1.9       paf        75:        /// caches table
                     76:        void close_table(const String& file_name, DB_Table& table);
1.2       parser     77: 
1.9       paf        78: private:
1.2       parser     79: 
1.9       paf        80:        time_t time_used;
1.2       parser     81: 
1.9       paf        82:        const String& fdb_home;
                     83:        DB_ENV dbenv;
                     84:        Hash table_cache;
1.2       parser     85: 
1.1       parser     86: private:
                     87: 
                     88:        void check(const char *operation, const String *source, int error);
1.10    ! paf        89: };
        !            90: 
        !            91: /// Auto-object used to track DB_Connection usage
        !            92: class DB_Connection_ptr {
        !            93:        DB_Connection *fconnection;
        !            94: public:
        !            95:        DB_Connection_ptr(DB_Connection *aconnection) : fconnection(aconnection) {
        !            96:                fconnection->use();
        !            97:        }
        !            98:        ~DB_Connection_ptr() {
        !            99:                fconnection->unuse();
        !           100:        }
        !           101:        DB_Connection* operator->() {
        !           102:                return fconnection;
        !           103:        }
        !           104: 
        !           105:        // copying
        !           106:        DB_Connection_ptr(const DB_Connection_ptr& src) : fconnection(src.fconnection) {
        !           107:                fconnection->use();
        !           108:        }
        !           109:        DB_Connection_ptr& operator =(const DB_Connection_ptr& src) {
        !           110:                // may do without this=src check
        !           111:                fconnection->unuse();
        !           112:                fconnection=src.fconnection;
        !           113:                fconnection->use();
        !           114: 
        !           115:                return *this;
        !           116:        }
1.1       parser    117: };
                    118: 
                    119: #endif

E-mail: