|
|
| version 1.27, 2002/08/15 10:38:18 | version 1.30.2.5, 2003/01/31 12:10:45 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: sql fconnection decl. | Parser: sql fconnection decl. |
| Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 17 static const char* IDENT_SQL_CONNECTION_ | Line 17 static const char* IDENT_SQL_CONNECTION_ |
| // defines | // defines |
| /// @see SQL_Driver_services_impl::_throw | /// @see SQL_Driver_services_impl::_throw |
| #define SQL_CONNECTION_SERVICED_FUNC_GUARDED(actions) \ | #ifdef PA_WITH_SJLJ_EXCEPTIONS |
| if(!fservices || !setjmp(fservices->mark)) { \ | #define SQL_CONNECTION_SERVICED_FUNC_GUARDED(actions) actions |
| actions; \ | #else |
| } else \ | #define SQL_CONNECTION_SERVICED_FUNC_GUARDED(actions) \ |
| fservices->propagate_exception(); | if(!setjmp(fservices.mark)) { \ |
| actions; \ | |
| } else \ | |
| fservices.propagate_exception(); | |
| #endif | |
| /// SQL connection. handy wrapper around low level SQL_Driver | /// SQL_Driver_services Pooled implementation |
| class SQL_Connection : public Pooled { | class SQL_Driver_services_impl: public SQL_Driver_services { |
| Pool *fpool; | |
| StringPtr furl; | |
| Exception fexception; | |
| public: | |
| SQL_Driver_services_impl(): fpool(0), furl(0) {} | |
| void set_pool_and_url(Pool *apool, StringPtr aurl) { fpool=apool; furl=aurl;} | |
| friend class SQL_Connection_ptr; | override void *malloc(size_t size) { return fpool->malloc(size); } |
| override void *calloc(size_t size) { return fpool->calloc(size); } | |
| override void *realloc(void *ptr, size_t size) { return fpool->realloc(ptr, size); } | |
| /** | |
| normally we can't 'throw' from dynamic library, so | |
| the idea is to #1 jump to C++ some function to main body, where | |
| every function stack frame has exception unwind information | |
| and from there... #2 propagate_exception() | |
| but when parser configured --with-sjlj-exceptions | |
| one can simply 'throw' from dynamic library. | |
| [sad story: one can not longjump/throw due to some bug in gcc as of 3.2.1 version] | |
| */ | |
| override void _throw(const SQL_Error& aexception) { | |
| // converting SQL_exception to parser Exception | |
| // hiding passwords and addresses from accidental show [imagine user forgot @exception] | |
| #ifdef PA_WITH_SJLJ_EXCEPTIONS | |
| throw | |
| #else | |
| fexception= | |
| #endif | |
| Exception(aexception.type(), | |
| url_without_login(), | |
| aexception.comment()); | |
| #ifndef PA_WITH_SJLJ_EXCEPTIONS | |
| longjmp(mark, 1); | |
| #endif | |
| } | |
| virtual void propagate_exception() { | |
| #ifndef PA_WITH_SJLJ_EXCEPTIONS | |
| throw fexception; | |
| #endif | |
| } | |
| private: | |
| StringPtr url_without_login() const; | |
| }; | |
| /// SQL connection. handy wrapper around low level SQL_Driver | |
| class SQL_Connection: public PA_Object { | |
| StringPtr furl; | |
| SQL_Driver& fdriver; | |
| SQL_Driver_services_impl fservices; | |
| void *fconnection; | |
| time_t time_used; | |
| bool marked_to_rollback; | |
| public: | public: |
| SQL_Connection(Pool& pool, const String& aurl, SQL_Driver& adriver) : Pooled(pool), | SQL_Connection(StringPtr aurl, SQL_Driver& adriver): |
| furl(aurl), | furl(aurl), |
| fdriver(adriver), | fdriver(adriver), |
| fconnection(0), | fconnection(0), |
| time_used(0), used(0), | time_used(0), |
| marked_to_rollback(false) { | marked_to_rollback(false) { |
| } | } |
| const String& get_url() { return furl; } | StringPtr get_url() { return furl; } |
| void set_services(SQL_Driver_services *aservices) { | void set_pool(Pool *pool) { |
| fservices=aservices; | fservices.set_pool_and_url(pool, pool?furl:StringPtr(0)); |
| } | } |
| bool expired(time_t older_dies) { | bool expired(time_t older_dies) { |
| return !used && time_used<older_dies; | return /*!freferences && */time_used<older_dies; |
| } | } |
| time_t get_time_used() { return time_used; } | time_t get_time_used() { return time_used; } |
| bool connected() { return fconnection!=0; } | bool connected() { return fconnection!=0; } |
| void connect(char *used_only_in_connect_url_cstr) { | void connect(char *used_only_in_connect_url_cstr) { |
| SQL_CONNECTION_SERVICED_FUNC_GUARDED( | SQL_CONNECTION_SERVICED_FUNC_GUARDED( |
| fdriver.connect(used_only_in_connect_url_cstr, *fservices, &fconnection) | fdriver.connect(used_only_in_connect_url_cstr, fservices, &fconnection) |
| ); | ); |
| } | } |
| void disconnect() { | void disconnect() { |
| Line 59 public: | Line 116 public: |
| } | } |
| bool ping() { | bool ping() { |
| SQL_CONNECTION_SERVICED_FUNC_GUARDED( | SQL_CONNECTION_SERVICED_FUNC_GUARDED( |
| return fdriver.ping(*fservices, fconnection) | return fdriver.ping(fservices, fconnection) |
| ); | ); |
| return 0; // never reached | return 0; // never reached |
| } | } |
| uint quote(char *to, const char *from, unsigned int length) { | uint quote(char *to, const char *from, unsigned int length) { |
| SQL_CONNECTION_SERVICED_FUNC_GUARDED( | SQL_CONNECTION_SERVICED_FUNC_GUARDED( |
| return fdriver.quote(*fservices, fconnection, to, from, length) | return fdriver.quote(fservices, fconnection, to, from, length) |
| ); | ); |
| return 0; // never reached | return 0; // never reached |
| } | } |
| Line 73 public: | Line 130 public: |
| void query( | void query( |
| const char *statement, unsigned long offset, unsigned long limit, | const char *statement, unsigned long offset, unsigned long limit, |
| SQL_Driver_query_event_handlers& handlers, | SQL_Driver_query_event_handlers& handlers, |
| const String& source) { | StringPtr source) { |
| try { | try { |
| SQL_CONNECTION_SERVICED_FUNC_GUARDED( | SQL_CONNECTION_SERVICED_FUNC_GUARDED( |
| fdriver.query(*fservices, fconnection, | fdriver.query(fservices, fconnection, |
| statement, offset, limit, | statement, offset, limit, |
| handlers) | handlers) |
| ); | ); |
| } catch(const Exception& e) { // query problem | } catch(const Exception& e) { // query problem |
| // give more specific source [were url] | if(strcmp(e.type(), "sql.connect")==0) { // if it is _throw exception, |
| throw Exception("sql.execute", | // give more specific source [were url] |
| &source, | throw Exception("sql.execute", |
| "%s", e.comment()); | source, |
| "%s", e.comment()); | |
| } else | |
| /*re*/throw; | |
| } | } |
| } | } |
| Line 96 private: // closing process | Line 156 private: // closing process |
| void commit() { | void commit() { |
| SQL_CONNECTION_SERVICED_FUNC_GUARDED( | SQL_CONNECTION_SERVICED_FUNC_GUARDED( |
| fdriver.commit(*fservices, fconnection) | fdriver.commit(fservices, fconnection) |
| ); | ); |
| } | } |
| void rollback() { | void rollback() { |
| SQL_CONNECTION_SERVICED_FUNC_GUARDED( | SQL_CONNECTION_SERVICED_FUNC_GUARDED( |
| fdriver.rollback(*fservices, fconnection) | fdriver.rollback(fservices, fconnection) |
| ); | ); |
| } | } |
| Line 113 private: // closing process | Line 173 private: // closing process |
| } else | } else |
| commit(); | commit(); |
| SQL_driver_manager->close_connection(furl, *this); | SQL_driver_manager.close_connection(furl, SQL_ConnectionPtr(this)); |
| } | } |
| private: // connection usage methods | |
| void use() { | |
| time_used=time(0); // they started to use at this time | |
| used++; | |
| } | |
| void unuse() { | |
| used--; | |
| if(!used) | |
| close(); | |
| } | |
| private: // connection usage data | |
| int used; | |
| private: | |
| const String& furl; | |
| SQL_Driver& fdriver; | |
| SQL_Driver_services *fservices; | |
| void *fconnection; | |
| time_t time_used; | |
| bool marked_to_rollback; | |
| }; | |
| /// Auto-object used to track SQL_Connection usage | |
| class SQL_Connection_ptr { | |
| SQL_Connection *fconnection; | |
| public: | |
| explicit SQL_Connection_ptr(SQL_Connection *aconnection) : fconnection(aconnection) { | |
| fconnection->use(); | |
| } | |
| ~SQL_Connection_ptr() { | |
| fconnection->unuse(); | |
| } | |
| SQL_Connection* operator->() { | |
| return fconnection; | |
| } | |
| SQL_Connection* get() const { | |
| return fconnection; | |
| } | |
| // copying | |
| SQL_Connection_ptr(const SQL_Connection_ptr& src) : fconnection(src.fconnection) { | |
| fconnection->use(); | |
| } | |
| SQL_Connection_ptr& operator =(const SQL_Connection_ptr& src) { | |
| // may do without this=src check | |
| fconnection->unuse(); | |
| fconnection=src.fconnection; | |
| fconnection->use(); | |
| return *this; | |
| } | |
| }; | }; |
| #endif | #endif |