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