Annotation of parser3/src/include/pa_sql_connection.h, revision 1.24
1.1 paf 1: /** @file
1.9 parser 2: Parser: sql fconnection decl.
1.1 paf 3:
1.22 paf 4: Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.23 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6:
1.24 ! paf 7: $Id: pa_sql_connection.h,v 1.23 2002/02/08 08:30:13 paf Exp $
1.1 paf 8: */
9:
10: #ifndef PA_SQL_CONNECTION_H
11: #define PA_SQL_CONNECTION_H
12:
13: #include "pa_pool.h"
14: #include "pa_sql_driver.h"
1.4 paf 15: #include "pa_sql_driver_manager.h"
1.1 paf 16:
1.15 paf 17: // defines
18:
19: /// @see SQL_Driver_services_impl::_throw
1.17 paf 20: #define SQL_CONNECTION_SERVICED_FUNC_GUARDED(actions) \
1.16 paf 21: if(!fservices || !setjmp(fservices->mark)) { \
1.15 paf 22: actions; \
23: } else \
24: fservices->propagate_exception();
25:
1.14 parser 26: /// SQL connection. handy wrapper around low level SQL_Driver
1.1 paf 27: class SQL_Connection : public Pooled {
28:
1.21 paf 29: friend class SQL_Connection_ptr;
30:
1.1 paf 31: public:
32:
1.9 parser 33: SQL_Connection(Pool& pool, const String& aurl, SQL_Driver& adriver) : Pooled(pool),
1.1 paf 34: furl(aurl),
1.10 parser 35: fdriver(adriver),
36: fconnection(0),
1.21 paf 37: time_used(0), used(0),
38: marked_to_rollback(false) {
1.9 parser 39: }
1.12 parser 40:
1.18 paf 41: const String& get_url() { return furl; }
1.12 parser 42:
1.9 parser 43: void set_services(SQL_Driver_services *aservices) {
44: fservices=aservices;
1.1 paf 45: }
1.10 parser 46: bool expired(time_t older_dies) {
1.21 paf 47: return !used && time_used<older_dies;
1.1 paf 48: }
1.21 paf 49: time_t get_time_used() { return time_used; }
1.1 paf 50:
1.10 parser 51: bool connected() { return fconnection!=0; }
1.9 parser 52: void connect(char *used_only_in_connect_url_cstr) {
1.17 paf 53: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15 paf 54: fdriver.connect(used_only_in_connect_url_cstr, *fservices, &fconnection)
55: );
56: }
57: void disconnect() {
1.16 paf 58: fdriver.disconnect(fconnection); fconnection=0;
1.15 paf 59: }
60: bool ping() {
1.17 paf 61: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15 paf 62: return fdriver.ping(*fservices, fconnection)
63: );
64: return 0; // never reached
1.9 parser 65: }
1.4 paf 66: uint quote(char *to, const char *from, unsigned int length) {
1.17 paf 67: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15 paf 68: return fdriver.quote(*fservices, fconnection, to, from, length)
69: );
70: return 0; // never reached
1.4 paf 71: }
72:
1.2 paf 73: void query(
1.3 paf 74: const char *statement, unsigned long offset, unsigned long limit,
1.11 parser 75: SQL_Driver_query_event_handlers& handlers) {
1.17 paf 76: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15 paf 77: fdriver.query(*fservices, fconnection,
78: statement, offset, limit,
79: handlers)
80: );
1.2 paf 81: }
82:
1.21 paf 83: void mark_to_rollback() {
84: marked_to_rollback=true;
85: }
86:
87: private: // closing process
88:
89: void commit() {
90: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
91: fdriver.commit(*fservices, fconnection)
92: );
93: }
94: void rollback() {
95: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
96: fdriver.rollback(*fservices, fconnection)
97: );
98: }
99:
100: /// return to cache
101: void close() {
1.24 ! paf 102: if(marked_to_rollback) {
1.21 paf 103: rollback();
1.24 ! paf 104: marked_to_rollback=false;
! 105: } else
1.21 paf 106: commit();
107:
108: SQL_driver_manager->close_connection(furl, *this);
109: }
110:
111: private: // connection usage methods
112:
113: void use() {
114: time_used=time(0); // they started to use at this time
115: used++;
116: }
117: void unuse() {
118: used--;
119: if(!used)
120: close();
121: }
122:
123: private: // connection usage data
124:
125: int used;
1.1 paf 126:
127: private:
128:
1.9 parser 129: const String& furl;
1.1 paf 130: SQL_Driver& fdriver;
1.9 parser 131: SQL_Driver_services *fservices;
132: void *fconnection;
1.21 paf 133: time_t time_used;
134: bool marked_to_rollback;
135: };
136:
137: /// Auto-object used to track SQL_Connection usage
138: class SQL_Connection_ptr {
139: SQL_Connection *fconnection;
140: public:
141: explicit SQL_Connection_ptr(SQL_Connection *aconnection) : fconnection(aconnection) {
142: fconnection->use();
143: }
144: ~SQL_Connection_ptr() {
145: fconnection->unuse();
146: }
147: SQL_Connection* operator->() {
148: return fconnection;
149: }
150: SQL_Connection* get() const {
151: return fconnection;
152: }
153:
154: // copying
155: SQL_Connection_ptr(const SQL_Connection_ptr& src) : fconnection(src.fconnection) {
156: fconnection->use();
157: }
158: SQL_Connection_ptr& operator =(const SQL_Connection_ptr& src) {
159: // may do without this=src check
160: fconnection->unuse();
161: fconnection=src.fconnection;
162: fconnection->use();
163:
164: return *this;
165: }
1.1 paf 166: };
167:
168: #endif
E-mail: