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

1.1       paf         1: /** @file
1.9       parser      2:        Parser: sql fconnection decl.
1.1       paf         3: 
1.30    ! 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.30    ! paf        11: static const char* IDENT_SQL_CONNECTION_H="$Date: 2002/09/17 09:30:07 $";
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
1.29      paf        88:                        if(strcmp(e.type(), "sql.connect")==0) { // if it is _throw exception, 
                     89:                                // give more specific source [were url]
                     90:                                throw Exception("sql.execute",
                     91:                                        &source, 
                     92:                                        "%s", e.comment());
                     93:                        } else
                     94:                                /*re*/throw;
1.27      paf        95:                }
1.2       paf        96:        }
                     97: 
1.21      paf        98:        void mark_to_rollback() {
                     99:                marked_to_rollback=true;
                    100:        }
                    101: 
                    102: private: // closing process
                    103: 
                    104:        void commit() { 
                    105:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
                    106:                        fdriver.commit(*fservices, fconnection) 
                    107:                );
                    108:        }
                    109:        void rollback() { 
                    110:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
                    111:                        fdriver.rollback(*fservices, fconnection)
                    112:                );
                    113:        }
                    114: 
                    115:        /// return to cache
                    116:        void close() {
1.24      paf       117:                if(marked_to_rollback) {
1.21      paf       118:                        rollback();
1.24      paf       119:                        marked_to_rollback=false;
                    120:                } else
1.21      paf       121:                        commit();
                    122: 
                    123:                SQL_driver_manager->close_connection(furl, *this);
                    124:        }
                    125: 
                    126: private: // connection usage methods
                    127: 
                    128:        void use() {
                    129:                time_used=time(0); // they started to use at this time
                    130:                used++;
                    131:        }
                    132:        void unuse() {
                    133:                used--;
                    134:                if(!used)
                    135:                        close();
                    136:        }
                    137: 
                    138: private: // connection usage data
                    139: 
                    140:        int used;
1.1       paf       141: 
                    142: private:
                    143: 
1.9       parser    144:        const String& furl;
1.1       paf       145:        SQL_Driver& fdriver;
1.9       parser    146:        SQL_Driver_services *fservices;
                    147:        void *fconnection;
1.21      paf       148:        time_t time_used;
                    149:        bool marked_to_rollback;
                    150: };
                    151: 
                    152: /// Auto-object used to track SQL_Connection usage
                    153: class SQL_Connection_ptr {
                    154:        SQL_Connection *fconnection;
                    155: public:
                    156:        explicit SQL_Connection_ptr(SQL_Connection *aconnection) : fconnection(aconnection) {
                    157:                fconnection->use();
                    158:        }
                    159:        ~SQL_Connection_ptr() {
                    160:                fconnection->unuse();
                    161:        }
                    162:        SQL_Connection* operator->() {
                    163:                return fconnection;
                    164:        }
                    165:        SQL_Connection* get() const {
                    166:                return fconnection; 
                    167:        }
                    168: 
                    169:        // copying
                    170:        SQL_Connection_ptr(const SQL_Connection_ptr& src) : fconnection(src.fconnection) {
                    171:                fconnection->use();
                    172:        }
                    173:        SQL_Connection_ptr& operator =(const SQL_Connection_ptr& src) {
                    174:                // may do without this=src check
                    175:                fconnection->unuse();
                    176:                fconnection=src.fconnection;
                    177:                fconnection->use();
                    178: 
                    179:                return *this;
                    180:        }
1.1       paf       181: };
                    182: 
                    183: #endif

E-mail: