Annotation of parser3/src/include/pa_db_connection.h, revision 1.21
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:
1.20 paf 5: Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.21 ! paf 6: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 parser 7:
1.21 ! paf 8: $Id: pa_db_connection.h,v 1.20 2002/02/08 07:27:43 paf Exp $
1.1 parser 9: */
10:
11: #ifndef PA_DB_CONNECTION_H
12: #define PA_DB_CONNECTION_H
13:
14: #include "pa_pool.h"
1.9 paf 15: #include "pa_hash.h"
1.10 paf 16: #include "pa_db_table.h"
1.17 paf 17: #include "pa_value.h"
1.1 parser 18:
19: // defines
20:
1.2 parser 21: // forwards
22:
1.9 paf 23: class DB_Table;
1.10 paf 24: class DB_Connection_ptr;
1.1 parser 25:
1.9 paf 26: /// sql driver connection
1.1 parser 27: class DB_Connection : public Pooled {
1.15 paf 28: friend class DB_Table;
29: friend class DB_Connection_ptr;
1.1 parser 30: public:
31:
1.10 paf 32: DB_Connection(Pool& apool, const String& db_home);
33: ~DB_Connection();
1.1 parser 34:
35: bool expired(time_t older_dies) {
1.10 paf 36: return !used && time_used<older_dies;
1.1 parser 37: }
1.17 paf 38: const String& db_home() { return fdb_home; }
1.16 paf 39: time_t get_time_used() { return time_used; }
40: int get_users_count() { return used; }
1.1 parser 41:
1.9 paf 42: /**
1.16 paf 43: connect to specified file_name
1.9 paf 44: */
1.11 paf 45: DB_Table_ptr get_table_ptr(const String& file_name, const String *source);
1.9 paf 46:
1.10 paf 47: private: // connection usage methods
48:
49: void use() {
50: time_used=time(0); // they started to use at this time
51: used++;
52: }
53: void unuse() {
54: used--;
55: }
56:
57: private: // connection usage data
58:
59: int used;
60:
1.9 paf 61: private: // table cache
62:
63: DB_Table *get_table_from_cache(const String& file_name);
64: void put_table_to_cache(const String& file_name, DB_Table& table);
65: void maybe_expire_table_cache();
1.1 parser 66: private:
1.9 paf 67: time_t prev_expiration_pass_time;
1.1 parser 68:
1.9 paf 69: private: // for DB_Table
1.1 parser 70:
1.9 paf 71: /// caches table
72: void close_table(const String& file_name, DB_Table& table);
1.2 parser 73:
1.9 paf 74: private:
1.2 parser 75:
1.9 paf 76: time_t time_used;
1.2 parser 77:
1.9 paf 78: const String& fdb_home;
79: DB_ENV dbenv;
80: Hash table_cache;
1.2 parser 81:
1.1 parser 82: private:
83:
84: void check(const char *operation, const String *source, int error);
1.17 paf 85:
86: public:
87:
88: Value& get_status(Pool& pool, const String *source);
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:
1.19 paf 95: explicit DB_Connection_ptr(DB_Connection *aconnection) : fconnection(aconnection) {
1.10 paf 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: