|
|
| version 1.30.2.5, 2003/01/31 12:10:45 | version 1.40, 2005/08/09 08:14:50 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: sql fconnection decl. | Parser: sql fconnection decl. |
| Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2005 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) |
| */ | */ |
| #ifndef PA_SQL_CONNECTION_H | #ifndef PA_SQL_CONNECTION_H |
| #define PA_SQL_CONNECTION_H | #define PA_SQL_CONNECTION_H |
| static const char* IDENT_SQL_CONNECTION_H="$Date$"; | static const char * const IDENT_SQL_CONNECTION_H="$Date$"; |
| #include "pa_pool.h" | |
| #include "pa_sql_driver.h" | #include "pa_sql_driver.h" |
| #include "pa_sql_driver_manager.h" | #include "pa_sql_driver_manager.h" |
| Line 18 static const char* IDENT_SQL_CONNECTION_ | Line 18 static const char* IDENT_SQL_CONNECTION_ |
| /// @see SQL_Driver_services_impl::_throw | /// @see SQL_Driver_services_impl::_throw |
| #ifdef PA_WITH_SJLJ_EXCEPTIONS | #ifdef PA_WITH_SJLJ_EXCEPTIONS |
| #define SQL_CONNECTION_SERVICED_FUNC_GUARDED(actions) actions | #define SQL_CONNECTION_SERVICED_FUNC_GUARDED(actions) \ |
| use(); \ | |
| actions | |
| #else | #else |
| #define SQL_CONNECTION_SERVICED_FUNC_GUARDED(actions) \ | #define SQL_CONNECTION_SERVICED_FUNC_GUARDED(actions) \ |
| use(); \ | |
| if(!setjmp(fservices.mark)) { \ | if(!setjmp(fservices.mark)) { \ |
| actions; \ | actions; \ |
| } else \ | } else \ |
| Line 29 static const char* IDENT_SQL_CONNECTION_ | Line 32 static const char* IDENT_SQL_CONNECTION_ |
| /// SQL_Driver_services Pooled implementation | /// SQL_Driver_services Pooled implementation |
| class SQL_Driver_services_impl: public SQL_Driver_services { | class SQL_Driver_services_impl: public SQL_Driver_services { |
| Pool *fpool; | const String* furl; |
| StringPtr furl; | |
| Exception fexception; | Exception fexception; |
| const char* frequest_charset; | |
| public: | public: |
| SQL_Driver_services_impl(): fpool(0), furl(0) {} | SQL_Driver_services_impl(const char* arequest_charset): furl(0), frequest_charset(arequest_charset) {} |
| void set_pool_and_url(Pool *apool, StringPtr aurl) { fpool=apool; furl=aurl;} | void set_url(const String& aurl) { furl=&aurl;} |
| const String& url_without_login() const; | |
| override void *malloc(size_t size) { return fpool->malloc(size); } | |
| override void *calloc(size_t size) { return fpool->calloc(size); } | override void* malloc(size_t size) { return pa_malloc(size); } |
| override void *realloc(void *ptr, size_t size) { return fpool->realloc(ptr, size); } | override void* malloc_atomic(size_t size) { return pa_malloc_atomic(size); } |
| override void* realloc(void *ptr, size_t size) { return pa_realloc(ptr, size); } | |
| override const char* request_charset() { return frequest_charset; } | |
| override void transcode(const char* src, size_t src_length, | |
| const char*& dst, size_t& dst_length, | |
| const char* charset_from_name, | |
| const char* charset_to_name | |
| ); | |
| /** | /** |
| normally we can't 'throw' from dynamic library, so | normally we can't 'throw' from dynamic library, so |
| Line 58 public: | Line 69 public: |
| #else | #else |
| fexception= | fexception= |
| #endif | #endif |
| Exception(aexception.type(), | Exception(aexception.type(), |
| url_without_login(), | &url_without_login(), |
| aexception.comment()); | aexception.comment()); |
| #ifndef PA_WITH_SJLJ_EXCEPTIONS | #ifndef PA_WITH_SJLJ_EXCEPTIONS |
| Line 71 public: | Line 82 public: |
| throw fexception; | throw fexception; |
| #endif | #endif |
| } | } |
| private: | |
| StringPtr url_without_login() const; | |
| }; | }; |
| /// SQL connection. handy wrapper around low level SQL_Driver | /// SQL connection. handy wrapper around low level SQL_Driver |
| class SQL_Connection: public PA_Object { | class SQL_Connection: public PA_Object { |
| StringPtr furl; | const String& furl; |
| SQL_Driver& fdriver; | SQL_Driver& fdriver; |
| SQL_Driver_services_impl fservices; | SQL_Driver_services_impl fservices; |
| void *fconnection; | void *fconnection; |
| time_t time_used; | time_t time_used; |
| bool marked_to_rollback; | |
| public: | public: |
| SQL_Connection(StringPtr aurl, SQL_Driver& adriver): | SQL_Connection(const String& aurl, SQL_Driver& adriver, const char* arequest_charset): |
| furl(aurl), | furl(aurl), |
| fdriver(adriver), | fdriver(adriver), |
| fservices(arequest_charset), | |
| fconnection(0), | fconnection(0), |
| time_used(0), | time_used(0) { |
| marked_to_rollback(false) { | |
| } | } |
| SQL_Driver_services_impl& services() { return fservices; } | |
| StringPtr get_url() { return furl; } | const String& get_url() { return furl; } |
| void set_pool(Pool *pool) { | void set_url() { |
| fservices.set_pool_and_url(pool, pool?furl:StringPtr(0)); | fservices.set_url(furl); |
| } | |
| void use() { | |
| time_used=time(0); // they started to use at this time | |
| } | } |
| bool expired(time_t older_dies) { | bool expired(time_t older_dies) { |
| return /*!freferences && */time_used<older_dies; | return time_used<older_dies; |
| } | } |
| time_t get_time_used() { return time_used; } | time_t get_time_used() { return time_used; } |
| Line 116 public: | Line 128 public: |
| } | } |
| bool ping() { | bool ping() { |
| SQL_CONNECTION_SERVICED_FUNC_GUARDED( | SQL_CONNECTION_SERVICED_FUNC_GUARDED( |
| return fdriver.ping(fservices, fconnection) | return fdriver.ping(fconnection) |
| ); | ); |
| return 0; // never reached | |
| } | } |
| uint quote(char *to, const char *from, unsigned int length) { | const char* quote(const char* str, unsigned int length) { |
| SQL_CONNECTION_SERVICED_FUNC_GUARDED( | SQL_CONNECTION_SERVICED_FUNC_GUARDED( |
| return fdriver.quote(fservices, fconnection, to, from, length) | return fdriver.quote(fconnection, str, length) |
| ); | ); |
| return 0; // never reached | |
| } | } |
| void query( | void query( |
| const char *statement, unsigned long offset, unsigned long limit, | const char* statement, |
| size_t placeholders_count, SQL_Driver::Placeholder* placeholders, | |
| unsigned long offset, unsigned long limit, | |
| SQL_Driver_query_event_handlers& handlers, | SQL_Driver_query_event_handlers& handlers, |
| StringPtr source) { | const String& source) { |
| try { | try { |
| SQL_CONNECTION_SERVICED_FUNC_GUARDED( | SQL_CONNECTION_SERVICED_FUNC_GUARDED( |
| fdriver.query(fservices, fconnection, | fdriver.query(fconnection, |
| statement, offset, limit, | statement, |
| placeholders_count, placeholders, | |
| offset, limit, | |
| handlers) | handlers) |
| ); | ); |
| } catch(const Exception& e) { // query problem | } catch(const Exception& e) { // query problem |
| if(strcmp(e.type(), "sql.connect")==0) { // if it is _throw exception, | if(strcmp(e.type(), "sql.connect")==0) { // if it is _throw exception, |
| // give more specific source [were url] | // give more specific source [were url] |
| throw Exception("sql.execute", | throw Exception("sql.execute", |
| source, | &source, |
| "%s", e.comment()); | "%s", e.comment()); |
| } else | } else |
| /*re*/throw; | rethrow; |
| } | } |
| } | } |
| void mark_to_rollback() { | |
| marked_to_rollback=true; | |
| } | |
| private: // closing process | |
| void commit() { | void commit() { |
| SQL_CONNECTION_SERVICED_FUNC_GUARDED( | SQL_CONNECTION_SERVICED_FUNC_GUARDED( |
| fdriver.commit(fservices, fconnection) | fdriver.commit(fconnection) |
| ); | ); |
| } | } |
| void rollback() { | void rollback() { |
| SQL_CONNECTION_SERVICED_FUNC_GUARDED( | SQL_CONNECTION_SERVICED_FUNC_GUARDED( |
| fdriver.rollback(fservices, fconnection) | fdriver.rollback(fconnection) |
| ); | ); |
| } | } |
| /// return to cache | /// return to cache |
| void close() { | void close() { |
| if(marked_to_rollback) { | SQL_driver_manager->close_connection(furl, this); |
| rollback(); | |
| marked_to_rollback=false; | |
| } else | |
| commit(); | |
| SQL_driver_manager.close_connection(furl, SQL_ConnectionPtr(this)); | |
| } | } |
| }; | }; |