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

1.1       paf         1: /** @file
1.9       parser      2:        Parser: sql fconnection decl.
1.1       paf         3: 
1.22      paf         4:        Copyright (c) 2001, 2002 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.28    ! paf        11: static const char* IDENT_SQL_CONNECTION_H="$Date: 2002/08/15 10:38:18 $";
1.1       paf        12: 
                     13: #include "pa_pool.h"
                     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) \
        !            24:                if(!fservices || !setjmp(fservices->mark)) { \
        !            25:                        actions; \
        !            26:                } else \
        !            27:                        fservices->propagate_exception();
        !            28: #endif
1.15      paf        29: 
1.14      parser     30: /// SQL connection. handy wrapper around low level SQL_Driver
1.1       paf        31: class SQL_Connection : public Pooled {
                     32: 
1.21      paf        33:        friend class SQL_Connection_ptr;
                     34: 
1.1       paf        35: public:
                     36: 
1.9       parser     37:        SQL_Connection(Pool& pool, const String& aurl, SQL_Driver& adriver) : Pooled(pool),
1.1       paf        38:                furl(aurl),
1.10      parser     39:                fdriver(adriver),
                     40:                fconnection(0),
1.21      paf        41:                time_used(0), used(0),
                     42:                marked_to_rollback(false) {
1.9       parser     43:        }
1.12      parser     44:        
1.18      paf        45:        const String& get_url() { return furl; }
1.12      parser     46: 
1.9       parser     47:        void set_services(SQL_Driver_services *aservices) {
                     48:                fservices=aservices;
1.1       paf        49:        }
1.10      parser     50:        bool expired(time_t older_dies) {
1.21      paf        51:                return !used && time_used<older_dies;
1.1       paf        52:        }
1.21      paf        53:        time_t get_time_used() { return time_used; }
1.1       paf        54: 
1.10      parser     55:        bool connected() { return fconnection!=0; }
1.9       parser     56:        void connect(char *used_only_in_connect_url_cstr) { 
1.17      paf        57:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15      paf        58:                        fdriver.connect(used_only_in_connect_url_cstr, *fservices, &fconnection)
                     59:                );
                     60:        }
                     61:        void disconnect() { 
1.16      paf        62:                fdriver.disconnect(fconnection); fconnection=0; 
1.15      paf        63:        }
                     64:        bool ping() { 
1.17      paf        65:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15      paf        66:                        return fdriver.ping(*fservices, fconnection)
                     67:                );
                     68:                return 0; // never reached
1.9       parser     69:        }
1.4       paf        70:        uint quote(char *to, const char *from, unsigned int length) {
1.17      paf        71:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15      paf        72:                        return fdriver.quote(*fservices, fconnection, to, from, length)
                     73:                );
                     74:                return 0; // never reached
1.4       paf        75:        }
                     76: 
1.2       paf        77:        void query(
1.3       paf        78:                const char *statement, unsigned long offset, unsigned long limit,
1.27      paf        79:                SQL_Driver_query_event_handlers& handlers, 
                     80:                const String& source) {
                     81:                try {
                     82:                        SQL_CONNECTION_SERVICED_FUNC_GUARDED(
                     83:                                fdriver.query(*fservices, fconnection, 
                     84:                                        statement, offset, limit, 
                     85:                                        handlers)
                     86:                        );      
                     87:                } catch(const Exception& e) { // query problem
                     88:                        // give more specific source [were url]
                     89:                        throw Exception("sql.execute",
                     90:                                &source, 
                     91:                                "%s", e.comment());
                     92:                }
1.2       paf        93:        }
                     94: 
1.21      paf        95:        void mark_to_rollback() {
                     96:                marked_to_rollback=true;
                     97:        }
                     98: 
                     99: private: // closing process
                    100: 
                    101:        void commit() { 
                    102:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
                    103:                        fdriver.commit(*fservices, fconnection) 
                    104:                );
                    105:        }
                    106:        void rollback() { 
                    107:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
                    108:                        fdriver.rollback(*fservices, fconnection)
                    109:                );
                    110:        }
                    111: 
                    112:        /// return to cache
                    113:        void close() {
1.24      paf       114:                if(marked_to_rollback) {
1.21      paf       115:                        rollback();
1.24      paf       116:                        marked_to_rollback=false;
                    117:                } else
1.21      paf       118:                        commit();
                    119: 
                    120:                SQL_driver_manager->close_connection(furl, *this);
                    121:        }
                    122: 
                    123: private: // connection usage methods
                    124: 
                    125:        void use() {
                    126:                time_used=time(0); // they started to use at this time
                    127:                used++;
                    128:        }
                    129:        void unuse() {
                    130:                used--;
                    131:                if(!used)
                    132:                        close();
                    133:        }
                    134: 
                    135: private: // connection usage data
                    136: 
                    137:        int used;
1.1       paf       138: 
                    139: private:
                    140: 
1.9       parser    141:        const String& furl;
1.1       paf       142:        SQL_Driver& fdriver;
1.9       parser    143:        SQL_Driver_services *fservices;
                    144:        void *fconnection;
1.21      paf       145:        time_t time_used;
                    146:        bool marked_to_rollback;
                    147: };
                    148: 
                    149: /// Auto-object used to track SQL_Connection usage
                    150: class SQL_Connection_ptr {
                    151:        SQL_Connection *fconnection;
                    152: public:
                    153:        explicit SQL_Connection_ptr(SQL_Connection *aconnection) : fconnection(aconnection) {
                    154:                fconnection->use();
                    155:        }
                    156:        ~SQL_Connection_ptr() {
                    157:                fconnection->unuse();
                    158:        }
                    159:        SQL_Connection* operator->() {
                    160:                return fconnection;
                    161:        }
                    162:        SQL_Connection* get() const {
                    163:                return fconnection; 
                    164:        }
                    165: 
                    166:        // copying
                    167:        SQL_Connection_ptr(const SQL_Connection_ptr& src) : fconnection(src.fconnection) {
                    168:                fconnection->use();
                    169:        }
                    170:        SQL_Connection_ptr& operator =(const SQL_Connection_ptr& src) {
                    171:                // may do without this=src check
                    172:                fconnection->unuse();
                    173:                fconnection=src.fconnection;
                    174:                fconnection->use();
                    175: 
                    176:                return *this;
                    177:        }
1.1       paf       178: };
                    179: 
                    180: #endif

E-mail: