Annotation of parser3/src/include/pa_sql_connection.h, revision 1.30.2.7.2.7
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.7! paf 11: static const char* IDENT_SQL_CONNECTION_H="$Date: 2003/03/25 13:58:37 $";
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.10 parser 102: bool expired(time_t older_dies) {
1.30.2.2 paf 103: return /*!freferences && */time_used<older_dies;
1.1 paf 104: }
1.21 paf 105: time_t get_time_used() { return time_used; }
1.1 paf 106:
1.10 parser 107: bool connected() { return fconnection!=0; }
1.9 parser 108: void connect(char *used_only_in_connect_url_cstr) {
1.17 paf 109: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.30.2.4 paf 110: fdriver.connect(used_only_in_connect_url_cstr, fservices, &fconnection)
1.15 paf 111: );
112: }
113: void disconnect() {
1.16 paf 114: fdriver.disconnect(fconnection); fconnection=0;
1.15 paf 115: }
116: bool ping() {
1.17 paf 117: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.30.2.4 paf 118: return fdriver.ping(fservices, fconnection)
1.15 paf 119: );
120: return 0; // never reached
1.9 parser 121: }
1.30.2.7.2.6 paf 122: const char* quote(const char* str, unsigned int length) {
1.17 paf 123: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.30.2.7.2.3 paf 124: return fdriver.quote(fservices, fconnection, str, length)
1.15 paf 125: );
1.30.2.7.2.3 paf 126: // return 0; // never reached
1.4 paf 127: }
128:
1.2 paf 129: void query(
1.30.2.6 paf 130: const char* statement, unsigned long offset, unsigned long limit,
1.27 paf 131: SQL_Driver_query_event_handlers& handlers,
1.30.2.7.2.4 paf 132: const String& source) {
1.27 paf 133: try {
134: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.30.2.4 paf 135: fdriver.query(fservices, fconnection,
1.27 paf 136: statement, offset, limit,
137: handlers)
138: );
139: } catch(const Exception& e) { // query problem
1.29 paf 140: if(strcmp(e.type(), "sql.connect")==0) { // if it is _throw exception,
141: // give more specific source [were url]
142: throw Exception("sql.execute",
1.30.2.7.2.4 paf 143: &source,
1.29 paf 144: "%s", e.comment());
145: } else
1.30.2.7 paf 146: rethrow;
1.27 paf 147: }
1.2 paf 148: }
149:
1.21 paf 150: void mark_to_rollback() {
151: marked_to_rollback=true;
152: }
153:
154: private: // closing process
155:
156: void commit() {
157: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.30.2.4 paf 158: fdriver.commit(fservices, fconnection)
1.21 paf 159: );
160: }
161: void rollback() {
162: SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.30.2.4 paf 163: fdriver.rollback(fservices, fconnection)
1.21 paf 164: );
165: }
166:
167: /// return to cache
168: void close() {
1.24 paf 169: if(marked_to_rollback) {
1.21 paf 170: rollback();
1.24 paf 171: marked_to_rollback=false;
172: } else
1.21 paf 173: commit();
174:
1.30.2.7.2.3 paf 175: SQL_driver_manager.close_connection(furl, this);
1.21 paf 176: }
177:
1.1 paf 178: };
179:
180: #endif
E-mail: