Annotation of parser3/src/include/pa_db_connection.h, revision 1.17
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)
1.17 ! paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.1 parser 7:
1.17 ! paf 8: $Id: pa_db_connection.h,v 1.16 2001/11/05 10:42:59 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.17 ! paf 18: #include "pa_value.h"
1.1 parser 19:
20: // defines
21:
1.2 parser 22: // forwards
23:
1.9 paf 24: class DB_Table;
1.10 paf 25: class DB_Connection_ptr;
1.1 parser 26:
1.9 paf 27: /// sql driver connection
1.1 parser 28: class DB_Connection : public Pooled {
1.15 paf 29: friend class DB_Table;
30: friend class DB_Connection_ptr;
1.1 parser 31: public:
32:
1.10 paf 33: DB_Connection(Pool& apool, const String& db_home);
34: ~DB_Connection();
1.1 parser 35:
36: bool expired(time_t older_dies) {
1.10 paf 37: return !used && time_used<older_dies;
1.1 parser 38: }
1.17 ! paf 39: const String& db_home() { return fdb_home; }
1.16 paf 40: time_t get_time_used() { return time_used; }
41: int get_users_count() { return used; }
1.1 parser 42:
1.9 paf 43: /**
1.16 paf 44: connect to specified file_name
1.9 paf 45: */
1.11 paf 46: DB_Table_ptr get_table_ptr(const String& file_name, const String *source);
1.9 paf 47:
1.10 paf 48: private: // connection usage methods
49:
50: void use() {
51: time_used=time(0); // they started to use at this time
52: used++;
53: }
54: void unuse() {
55: used--;
56: }
57:
58: private: // connection usage data
59:
60: int used;
61:
1.9 paf 62: private: // table cache
63:
64: DB_Table *get_table_from_cache(const String& file_name);
65: void put_table_to_cache(const String& file_name, DB_Table& table);
66: void maybe_expire_table_cache();
1.1 parser 67: private:
1.9 paf 68: time_t prev_expiration_pass_time;
1.1 parser 69:
1.9 paf 70: private: // for DB_Table
1.1 parser 71:
1.9 paf 72: /// caches table
73: void close_table(const String& file_name, DB_Table& table);
1.2 parser 74:
1.9 paf 75: private:
1.2 parser 76:
1.9 paf 77: time_t time_used;
1.2 parser 78:
1.9 paf 79: const String& fdb_home;
80: DB_ENV dbenv;
81: Hash table_cache;
1.2 parser 82:
1.1 parser 83: private:
84:
85: void check(const char *operation, const String *source, int error);
1.17 ! paf 86:
! 87: public:
! 88:
! 89: Value& get_status(Pool& pool, const String *source);
1.10 paf 90: };
91:
92: /// Auto-object used to track DB_Connection usage
93: class DB_Connection_ptr {
94: DB_Connection *fconnection;
95: public:
96: DB_Connection_ptr(DB_Connection *aconnection) : fconnection(aconnection) {
97: fconnection->use();
98: }
99: ~DB_Connection_ptr() {
100: fconnection->unuse();
101: }
102: DB_Connection* operator->() {
103: return fconnection;
104: }
105:
106: // copying
107: DB_Connection_ptr(const DB_Connection_ptr& src) : fconnection(src.fconnection) {
108: fconnection->use();
109: }
110: DB_Connection_ptr& operator =(const DB_Connection_ptr& src) {
111: // may do without this=src check
112: fconnection->unuse();
113: fconnection=src.fconnection;
114: fconnection->use();
115:
116: return *this;
117: }
1.1 parser 118: };
119:
120: #endif
E-mail: