Annotation of parser3/src/include/pa_sql_connection.h, revision 1.34

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

E-mail: