Annotation of parser3/src/include/pa_db_connection.h, revision 1.13
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.13 ! paf 8: $Id: pa_db_connection.h,v 1.12 2001/10/27 10:14:45 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:
1.13 ! paf 19: #ifdef DB2
1.1 parser 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.11 paf 48: DB_Table_ptr get_table_ptr(const String& file_name, const String *source);
1.9 paf 49:
1.10 paf 50: private: // connection usage methods
51:
52: void use() {
53: time_used=time(0); // they started to use at this time
54: used++;
55: }
56: void unuse() {
57: used--;
58: }
59:
60: private: // connection usage data
61:
62: int used;
63:
1.9 paf 64: private: // table cache
65:
66: DB_Table *get_table_from_cache(const String& file_name);
67: void put_table_to_cache(const String& file_name, DB_Table& table);
68: void maybe_expire_table_cache();
1.1 parser 69: private:
1.9 paf 70: time_t prev_expiration_pass_time;
1.1 parser 71:
1.9 paf 72: private: // for DB_Table
1.1 parser 73:
1.9 paf 74: /// caches table
75: void close_table(const String& file_name, DB_Table& table);
1.2 parser 76:
1.9 paf 77: private:
1.2 parser 78:
1.9 paf 79: time_t time_used;
1.2 parser 80:
1.9 paf 81: const String& fdb_home;
82: DB_ENV dbenv;
83: Hash table_cache;
1.2 parser 84:
1.1 parser 85: private:
86:
87: void check(const char *operation, const String *source, int error);
1.10 paf 88: };
89:
90: /// Auto-object used to track DB_Connection usage
91: class DB_Connection_ptr {
92: DB_Connection *fconnection;
93: public:
94: DB_Connection_ptr(DB_Connection *aconnection) : fconnection(aconnection) {
95: fconnection->use();
96: }
97: ~DB_Connection_ptr() {
98: fconnection->unuse();
99: }
100: DB_Connection* operator->() {
101: return fconnection;
102: }
103:
104: // copying
105: DB_Connection_ptr(const DB_Connection_ptr& src) : fconnection(src.fconnection) {
106: fconnection->use();
107: }
108: DB_Connection_ptr& operator =(const DB_Connection_ptr& src) {
109: // may do without this=src check
110: fconnection->unuse();
111: fconnection=src.fconnection;
112: fconnection->use();
113:
114: return *this;
115: }
1.1 parser 116: };
117:
118: #endif
E-mail: