Annotation of parser3/src/include/pa_sql_connection.h, revision 1.30.2.7.2.8
1.1 paf 1: /** @file
1.9 parser 2: Parser: sql fconnection decl.
1.1 paf 3:
1.30.2.6 paf 4: Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.23 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
8: #ifndef PA_SQL_CONNECTION_H
9: #define PA_SQL_CONNECTION_H
1.25 paf 10:
1.30.2.7.2.8! paf 11: static const char* IDENT_SQL_CONNECTION_H="$Date: 2003/03/26 07:45:22 $";
1.30.2.7.2.1 paf 12:
1.1 paf 13:
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.28 paf 20: #ifdef PA_WITH_SJLJ_EXCEPTIONS
21: #define SQL_CONNECTION_SERVICED_FUNC_GUARDED(actions) actions
22: #else
23: #define SQL_CONNECTION_SERVICED_FUNC_GUARDED(actions) \
1.30.2.3 paf 24: if(!setjmp(fservices.mark)) { \
1.28 paf 25: actions; \
26: } else \
1.30.2.3 paf 27: fservices.propagate_exception();
1.28 paf 28: #endif
1.15 paf 29:
1.30.2.3 paf 30: /// SQL_Driver_services Pooled implementation
31: class SQL_Driver_services_impl: public SQL_Driver_services {
1.30.2.7.2.3 paf 32: const String* furl;
1.30.2.3 paf 33: Exception fexception;
34: public:
1.30.2.7.2.3 paf 35: SQL_Driver_services_impl(): furl(0) {}
36: void set_url(const String& aurl) { furl=&aurl;}
1.30.2.3 paf 37:
1.30.2.7.2.3 paf 38: override void *malloc(size_t size) { return pa_malloc(size); }
39: override void *malloc_atomic(size_t size) { return pa_malloc_atomic(size); }
40: override void *realloc(void *ptr, size_t size) { return pa_realloc(ptr, size); }
1.30.2.3 paf 41:
42: /**
43: normally we can't 'throw' from dynamic library, so
44: the idea is to #1 jump to C++ some function to main body, where
45: every function stack frame has exception unwind information
46: and from there... #2 propagate_exception()
47:
48: but when parser configured --with-sjlj-exceptions
49: one can simply 'throw' from dynamic library.
50: [sad story: one can not longjump/throw due to some bug in gcc as of 3.2.1 version]
51: */
52: override void _throw(const SQL_Error& aexception) {
53: // converting SQL_exception to parser Exception
54: // hiding passwords and addresses from accidental show [imagine user forgot @exception]
55: #ifdef PA_WITH_SJLJ_EXCEPTIONS
56: throw
57: #else
58: fexception=
59: #endif
1.30.2.7.2.3 paf 60: Exception(aexception.type(),
61: &url_without_login(),
1.30.2.3 paf 62: aexception.comment());
63:
64: #ifndef PA_WITH_SJLJ_EXCEPTIONS
65: longjmp(mark, 1);
66: #endif
67: }
68: virtual void propagate_exception() {
69: #ifndef PA_WITH_SJLJ_EXCEPTIONS
70: throw fexception;
71: #endif
72: }
73:
74: private:
1.30.2.7.2.1 paf 75: const String& url_without_login() const;
1.30.2.3 paf 76: };
77:
1.14 parser 78: /// SQL connection. handy wrapper around low level SQL_Driver
1.30.2.1 paf 79: class SQL_Connection: public PA_Object {
1.30.2.7.2.1 paf 80: const String& furl;
1.30.2.3 paf 81: SQL_Driver& fdriver;
82: SQL_Driver_services_impl fservices;
83: void *fconnection;
84: time_t time_used;
85: bool marked_to_rollback;
1.21 paf 86:
1.1 paf 87: public:
88:
1.30.2.7.2.1 paf 89: SQL_Connection(const String& aurl, SQL_Driver& adriver):
1.1 paf 90: furl(aurl),
1.10 parser 91: fdriver(adriver),
1.30.2.7.2.7 paf 92: fconnection(0),
1.30.2.2 paf 93: time_used(0),
1.21 paf 94: marked_to_rollback(false) {
1.9 parser 95: }
1.12 parser 96:
1.30.2.7.2.1 paf 97: const String& get_url() { return furl; }
1.12 parser 98:
1.30.2.7.2.3 paf 99: void set_url() {
100: fservices.set_url(furl);
1.1 paf 101: }
1.30.2.7.2.8! paf 102: void use() {
! 103: time_used=time(0); // they started to use at this time
! 104: }
1.10 parser 105: bool expired(time_t older_dies) {
1.30.2.2 paf 106: return /*!freferences && */time_used<older_dies;
1.1 paf 107: }
1.21 paf 108: time_t get_time_used() { return time_used; }
1.1 paf 109:
1.10 parser 110: bool connected() { return fconnection!=0; }
1.9 parser 111: void connect(char *used_only_in_connect_url_cstr) {
1.17 paf 112: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.30.2.4 paf 113: fdriver.connect(used_only_in_connect_url_cstr, fservices, &fconnection)
1.15 paf 114: );
115: }
116: void disconnect() {
1.16 paf 117: fdriver.disconnect(fconnection); fconnection=0;
1.15 paf 118: }
119: bool ping() {
1.17 paf 120: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.30.2.4 paf 121: return fdriver.ping(fservices, fconnection)
1.15 paf 122: );
123: return 0; // never reached
1.9 parser 124: }
1.30.2.7.2.6 paf 125: const char* quote(const char* str, unsigned int length) {
1.17 paf 126: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.30.2.7.2.3 paf 127: return fdriver.quote(fservices, fconnection, str, length)
1.15 paf 128: );
1.30.2.7.2.3 paf 129: // return 0; // never reached
1.4 paf 130: }
131:
1.2 paf 132: void query(
1.30.2.6 paf 133: const char* statement, unsigned long offset, unsigned long limit,
1.27 paf 134: SQL_Driver_query_event_handlers& handlers,
1.30.2.7.2.4 paf 135: const String& source) {
1.27 paf 136: try {
137: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.30.2.4 paf 138: fdriver.query(fservices, fconnection,
1.27 paf 139: statement, offset, limit,
140: handlers)
141: );
142: } catch(const Exception& e) { // query problem
1.29 paf 143: if(strcmp(e.type(), "sql.connect")==0) { // if it is _throw exception,
144: // give more specific source [were url]
145: throw Exception("sql.execute",
1.30.2.7.2.4 paf 146: &source,
1.29 paf 147: "%s", e.comment());
148: } else
1.30.2.7 paf 149: rethrow;
1.27 paf 150: }
1.2 paf 151: }
1.21 paf 152:
153: void commit() {
154: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.30.2.4 paf 155: fdriver.commit(fservices, fconnection)
1.21 paf 156: );
157: }
158: void rollback() {
159: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.30.2.4 paf 160: fdriver.rollback(fservices, fconnection)
1.21 paf 161: );
162: }
163:
164: /// return to cache
165: void close() {
1.24 paf 166: if(marked_to_rollback) {
1.21 paf 167: rollback();
1.24 paf 168: marked_to_rollback=false;
169: } else
1.21 paf 170: commit();
171:
1.30.2.7.2.3 paf 172: SQL_driver_manager.close_connection(furl, this);
1.21 paf 173: }
174:
1.1 paf 175: };
176:
177: #endif
E-mail: