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

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.27    ! paf        11: static const char* IDENT_SQL_CONNECTION_H="$Date: 2002/08/01 11:41:16 $";
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.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.27    ! paf        75:                SQL_Driver_query_event_handlers& handlers, 
        !            76:                const String& source) {
        !            77:                try {
        !            78:                        SQL_CONNECTION_SERVICED_FUNC_GUARDED(
        !            79:                                fdriver.query(*fservices, fconnection, 
        !            80:                                        statement, offset, limit, 
        !            81:                                        handlers)
        !            82:                        );      
        !            83:                } catch(const Exception& e) { // query problem
        !            84:                        // give more specific source [were url]
        !            85:                        throw Exception("sql.execute",
        !            86:                                &source, 
        !            87:                                "%s", e.comment());
        !            88:                }
1.2       paf        89:        }
                     90: 
1.21      paf        91:        void mark_to_rollback() {
                     92:                marked_to_rollback=true;
                     93:        }
                     94: 
                     95: private: // closing process
                     96: 
                     97:        void commit() { 
                     98:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
                     99:                        fdriver.commit(*fservices, fconnection) 
                    100:                );
                    101:        }
                    102:        void rollback() { 
                    103:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
                    104:                        fdriver.rollback(*fservices, fconnection)
                    105:                );
                    106:        }
                    107: 
                    108:        /// return to cache
                    109:        void close() {
1.24      paf       110:                if(marked_to_rollback) {
1.21      paf       111:                        rollback();
1.24      paf       112:                        marked_to_rollback=false;
                    113:                } else
1.21      paf       114:                        commit();
                    115: 
                    116:                SQL_driver_manager->close_connection(furl, *this);
                    117:        }
                    118: 
                    119: private: // connection usage methods
                    120: 
                    121:        void use() {
                    122:                time_used=time(0); // they started to use at this time
                    123:                used++;
                    124:        }
                    125:        void unuse() {
                    126:                used--;
                    127:                if(!used)
                    128:                        close();
                    129:        }
                    130: 
                    131: private: // connection usage data
                    132: 
                    133:        int used;
1.1       paf       134: 
                    135: private:
                    136: 
1.9       parser    137:        const String& furl;
1.1       paf       138:        SQL_Driver& fdriver;
1.9       parser    139:        SQL_Driver_services *fservices;
                    140:        void *fconnection;
1.21      paf       141:        time_t time_used;
                    142:        bool marked_to_rollback;
                    143: };
                    144: 
                    145: /// Auto-object used to track SQL_Connection usage
                    146: class SQL_Connection_ptr {
                    147:        SQL_Connection *fconnection;
                    148: public:
                    149:        explicit SQL_Connection_ptr(SQL_Connection *aconnection) : fconnection(aconnection) {
                    150:                fconnection->use();
                    151:        }
                    152:        ~SQL_Connection_ptr() {
                    153:                fconnection->unuse();
                    154:        }
                    155:        SQL_Connection* operator->() {
                    156:                return fconnection;
                    157:        }
                    158:        SQL_Connection* get() const {
                    159:                return fconnection; 
                    160:        }
                    161: 
                    162:        // copying
                    163:        SQL_Connection_ptr(const SQL_Connection_ptr& src) : fconnection(src.fconnection) {
                    164:                fconnection->use();
                    165:        }
                    166:        SQL_Connection_ptr& operator =(const SQL_Connection_ptr& src) {
                    167:                // may do without this=src check
                    168:                fconnection->unuse();
                    169:                fconnection=src.fconnection;
                    170:                fconnection->use();
                    171: 
                    172:                return *this;
                    173:        }
1.1       paf       174: };
                    175: 
                    176: #endif

E-mail: