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

1.1       parser      1: /** @file
                      2:        Parser: sql db decl.
                      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      5:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      6: 
1.2     ! parser      7:        $Id: pa_db_connection.h,v 1.1 2001/10/22 16:44:42 parser Exp $
1.1       parser      8: */
                      9: 
                     10: #ifndef PA_DB_CONNECTION_H
                     11: #define PA_DB_CONNECTION_H
                     12: 
                     13: #include "pa_config_includes.h"
                     14: #include "pa_pool.h"
                     15: #include "pa_db_manager.h"
                     16: #include "pa_globals.h"
                     17: 
                     18: #ifdef HAVE_DB_H
                     19: #      include <db.h>
                     20: #endif
                     21: 
                     22: // defines
                     23: 
                     24: #define PA_DB_ACCESS_METHOD DB_BTREE
                     25: 
1.2     ! parser     26: // forwards
        !            27: 
        !            28: class Auto_transaction;
        !            29: 
1.1       parser     30: // class
                     31: 
                     32: /// DB connection. handy wrapper around low level DB_Driver
                     33: class DB_Connection : public Pooled {
1.2     ! parser     34:        friend Auto_transaction;
1.1       parser     35: public:
                     36: 
                     37:        DB_Connection(Pool& pool, const String& afile_spec, DB_ENV& adbenv);
                     38:        
                     39:        //const String& url() { return ffile_spec; }
                     40: 
                     41:        void set_services(Pool *aservices_pool) {
                     42:                time_used=time(0); // they started to use at this time
                     43:                fservices_pool=aservices_pool;
                     44:        }
                     45:        bool expired(time_t older_dies) {
                     46:                return time_used<older_dies;
                     47:        }
                     48: 
                     49:        void close() {
                     50:                DB_manager->close_connection(ffile_spec, *this);
                     51:        }
                     52: 
                     53:        bool connected() { return db!=0; }
                     54:        void connect();
1.2     ! parser     55:        void disconnect();      
1.1       parser     56:        bool ping() { return !needs_recovery; }
                     57: 
                     58:        void put(const String& key, const String& data);
                     59:        String *get(const String& key);
1.2     ! parser     60:        void _delete(const String& key);
1.1       parser     61: 
                     62: private:
                     63: 
                     64:        DB_ENV& fdbenv;
                     65:        const String& ffile_spec;
                     66:        Pool *fservices_pool;
                     67:        DB *db;
                     68:        bool needs_recovery;
1.2     ! parser     69:        DB_TXN *ftid;
1.1       parser     70:        time_t time_used;
                     71: 
1.2     ! parser     72: private: // transaction
        !            73: 
        !            74:        /// commits current transaction, restores previous transaction handle
        !            75:        void commit_restore(DB_TXN *atid) { 
        !            76:                if(ftid)
        !            77:                        check("txn_commit", &ffile_spec, txn_commit(ftid)); 
        !            78: 
        !            79:                ftid=atid;
        !            80:        }
        !            81:        
        !            82:        /// rolls current transaction back, restores previous transaction handle
        !            83:        void rollback_restore(DB_TXN *atid) {
        !            84:                if(ftid)
        !            85:                        check("txn_abort", &ffile_spec, txn_abort(ftid));
        !            86: 
        !            87:                ftid=atid;
        !            88:        }
        !            89:        
        !            90:        /// stars new current trunsaction @returns previous transaction handle
        !            91:        DB_TXN *transaction_begin_save() {
        !            92:                DB_TXN *result=ftid;
        !            93:                check("txn_begin", &ffile_spec, ::txn_begin(fdbenv.tx_info, 0/*parent*/, &ftid));
        !            94: 
        !            95:                return result;
        !            96:        }
        !            97:        
1.1       parser     98: private:
                     99: 
1.2     ! parser    100: 
1.1       parser    101:        void check(const char *operation, const String *source, int error);
                    102:        void *malloc(size_t size) { return fservices_pool->malloc(size); }
                    103:        void *calloc(size_t size) { return fservices_pool->calloc(size); }
1.2     ! parser    104: 
        !           105: };
        !           106: 
        !           107: ///    Auto-object used for temporary changing DB_Connection::tid.
        !           108: class Auto_transaction {
        !           109:        DB_Connection& fconnection;
        !           110:        bool marked_to_rollback;
        !           111:        DB_TXN *saved_tid;
        !           112: public:
        !           113:        Auto_transaction(DB_Connection& aconnection) : 
        !           114:                fconnection(aconnection), marked_to_rollback(false),
        !           115:                saved_tid(aconnection.transaction_begin_save()) {
        !           116:        }
        !           117:        ~Auto_transaction() { 
        !           118:                if(marked_to_rollback)
        !           119:                        fconnection.rollback_restore(saved_tid);
        !           120:                else
        !           121:                        fconnection.commit_restore(saved_tid);
        !           122:        }
        !           123:        void mark_to_rollback() {
        !           124:                marked_to_rollback=true;
        !           125:        }
1.1       parser    126: };
                    127: 
                    128: #endif

E-mail: