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

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.19      paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: 
1.22    ! paf         7:        $Id: pa_sql_connection.h,v 1.21 2002/01/16 10:28:34 paf Exp $
1.1       paf         8: */
                      9: 
                     10: #ifndef PA_SQL_CONNECTION_H
                     11: #define PA_SQL_CONNECTION_H
                     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.17      paf        20: #define SQL_CONNECTION_SERVICED_FUNC_GUARDED(actions) \
1.16      paf        21:        if(!fservices || !setjmp(fservices->mark)) { \
1.15      paf        22:                actions; \
                     23:        } else \
                     24:                fservices->propagate_exception();
                     25: 
1.14      parser     26: /// SQL connection. handy wrapper around low level SQL_Driver
1.1       paf        27: class SQL_Connection : public Pooled {
                     28: 
1.21      paf        29:        friend class SQL_Connection_ptr;
                     30: 
1.1       paf        31: public:
                     32: 
1.9       parser     33:        SQL_Connection(Pool& pool, const String& aurl, SQL_Driver& adriver) : Pooled(pool),
1.1       paf        34:                furl(aurl),
1.10      parser     35:                fdriver(adriver),
                     36:                fconnection(0),
1.21      paf        37:                time_used(0), used(0),
                     38:                marked_to_rollback(false) {
1.9       parser     39:        }
1.12      parser     40:        
1.18      paf        41:        const String& get_url() { return furl; }
1.12      parser     42: 
1.9       parser     43:        void set_services(SQL_Driver_services *aservices) {
                     44:                fservices=aservices;
1.1       paf        45:        }
1.10      parser     46:        bool expired(time_t older_dies) {
1.21      paf        47:                return !used && time_used<older_dies;
1.1       paf        48:        }
1.21      paf        49:        time_t get_time_used() { return time_used; }
1.1       paf        50: 
1.10      parser     51:        bool connected() { return fconnection!=0; }
1.9       parser     52:        void connect(char *used_only_in_connect_url_cstr) { 
1.17      paf        53:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15      paf        54:                        fdriver.connect(used_only_in_connect_url_cstr, *fservices, &fconnection)
                     55:                );
                     56:        }
                     57:        void disconnect() { 
1.16      paf        58:                fdriver.disconnect(fconnection); fconnection=0; 
1.15      paf        59:        }
                     60:        bool ping() { 
1.17      paf        61:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15      paf        62:                        return fdriver.ping(*fservices, fconnection)
                     63:                );
                     64:                return 0; // never reached
1.9       parser     65:        }
1.4       paf        66:        uint quote(char *to, const char *from, unsigned int length) {
1.17      paf        67:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15      paf        68:                        return fdriver.quote(*fservices, fconnection, to, from, length)
                     69:                );
                     70:                return 0; // never reached
1.4       paf        71:        }
                     72: 
1.2       paf        73:        void query(
1.3       paf        74:                const char *statement, unsigned long offset, unsigned long limit,
1.11      parser     75:                SQL_Driver_query_event_handlers& handlers) { 
1.17      paf        76:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15      paf        77:                        fdriver.query(*fservices, fconnection, 
                     78:                                statement, offset, limit, 
                     79:                                handlers)
                     80:                );
1.2       paf        81:        }
                     82: 
1.21      paf        83:        void mark_to_rollback() {
                     84:                marked_to_rollback=true;
                     85:        }
                     86: 
                     87: private: // closing process
                     88: 
                     89:        void commit() { 
                     90:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
                     91:                        fdriver.commit(*fservices, fconnection) 
                     92:                );
                     93:        }
                     94:        void rollback() { 
                     95:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
                     96:                        fdriver.rollback(*fservices, fconnection)
                     97:                );
                     98:        }
                     99: 
                    100:        /// return to cache
                    101:        void close() {
                    102:                if(marked_to_rollback)
                    103:                        rollback();
                    104:                else
                    105:                        commit();
                    106: 
                    107:                SQL_driver_manager->close_connection(furl, *this);
                    108:        }
                    109: 
                    110: private: // connection usage methods
                    111: 
                    112:        void use() {
                    113:                time_used=time(0); // they started to use at this time
                    114:                used++;
                    115:        }
                    116:        void unuse() {
                    117:                used--;
                    118:                if(!used)
                    119:                        close();
                    120:        }
                    121: 
                    122: private: // connection usage data
                    123: 
                    124:        int used;
1.1       paf       125: 
                    126: private:
                    127: 
1.9       parser    128:        const String& furl;
1.1       paf       129:        SQL_Driver& fdriver;
1.9       parser    130:        SQL_Driver_services *fservices;
                    131:        void *fconnection;
1.21      paf       132:        time_t time_used;
                    133:        bool marked_to_rollback;
                    134: };
                    135: 
                    136: /// Auto-object used to track SQL_Connection usage
                    137: class SQL_Connection_ptr {
                    138:        SQL_Connection *fconnection;
                    139: public:
                    140:        explicit SQL_Connection_ptr(SQL_Connection *aconnection) : fconnection(aconnection) {
                    141:                fconnection->use();
                    142:        }
                    143:        ~SQL_Connection_ptr() {
                    144:                fconnection->unuse();
                    145:        }
                    146:        SQL_Connection* operator->() {
                    147:                return fconnection;
                    148:        }
                    149:        SQL_Connection* get() const {
                    150:                return fconnection; 
                    151:        }
                    152: 
                    153:        // copying
                    154:        SQL_Connection_ptr(const SQL_Connection_ptr& src) : fconnection(src.fconnection) {
                    155:                fconnection->use();
                    156:        }
                    157:        SQL_Connection_ptr& operator =(const SQL_Connection_ptr& src) {
                    158:                // may do without this=src check
                    159:                fconnection->unuse();
                    160:                fconnection=src.fconnection;
                    161:                fconnection->use();
                    162: 
                    163:                return *this;
                    164:        }
1.1       paf       165: };
                    166: 
                    167: #endif

E-mail: