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

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.2.2! paf        11: static const char* IDENT_SQL_CONNECTION_H="$Date: 2003/01/27 16:00:41 $";
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.30.2.1  paf        31: class SQL_Connection: public PA_Object {
1.21      paf        32: 
1.1       paf        33: public:
                     34: 
1.30.2.2! paf        35:        SQL_Connection(ConstStringPtr aurl, SQL_Driver& adriver):
1.1       paf        36:                furl(aurl),
1.10      parser     37:                fdriver(adriver),
                     38:                fconnection(0),
1.30.2.2! paf        39:                time_used(0)1.21      paf        40:                marked_to_rollback(false) {
1.9       parser     41:        }
1.12      parser     42:        
1.30.2.2! paf        43:        ConstStringPtr get_url() { return furl; }
1.12      parser     44: 
1.9       parser     45:        void set_services(SQL_Driver_services *aservices) {
                     46:                fservices=aservices;
1.1       paf        47:        }
1.10      parser     48:        bool expired(time_t older_dies) {
1.30.2.2! paf        49:                return /*!freferences && */time_used<older_dies;
1.1       paf        50:        }
1.21      paf        51:        time_t get_time_used() { return time_used; }
1.1       paf        52: 
1.10      parser     53:        bool connected() { return fconnection!=0; }
1.9       parser     54:        void connect(char *used_only_in_connect_url_cstr) { 
1.17      paf        55:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15      paf        56:                        fdriver.connect(used_only_in_connect_url_cstr, *fservices, &fconnection)
                     57:                );
                     58:        }
                     59:        void disconnect() { 
1.16      paf        60:                fdriver.disconnect(fconnection); fconnection=0; 
1.15      paf        61:        }
                     62:        bool ping() { 
1.17      paf        63:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15      paf        64:                        return fdriver.ping(*fservices, fconnection)
                     65:                );
                     66:                return 0; // never reached
1.9       parser     67:        }
1.4       paf        68:        uint quote(char *to, const char *from, unsigned int length) {
1.17      paf        69:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
1.15      paf        70:                        return fdriver.quote(*fservices, fconnection, to, from, length)
                     71:                );
                     72:                return 0; // never reached
1.4       paf        73:        }
                     74: 
1.2       paf        75:        void query(
1.3       paf        76:                const char *statement, unsigned long offset, unsigned long limit,
1.27      paf        77:                SQL_Driver_query_event_handlers& handlers, 
1.30.2.2! paf        78:                ConstStringPtr source) {
1.27      paf        79:                try {
                     80:                        SQL_CONNECTION_SERVICED_FUNC_GUARDED(
                     81:                                fdriver.query(*fservices, fconnection, 
                     82:                                        statement, offset, limit, 
                     83:                                        handlers)
                     84:                        );      
                     85:                } catch(const Exception& e) { // query problem
1.29      paf        86:                        if(strcmp(e.type(), "sql.connect")==0) { // if it is _throw exception, 
                     87:                                // give more specific source [were url]
                     88:                                throw Exception("sql.execute",
1.30.2.2! paf        89:                                        source, 
1.29      paf        90:                                        "%s", e.comment());
                     91:                        } else
                     92:                                /*re*/throw;
1.27      paf        93:                }
1.2       paf        94:        }
                     95: 
1.21      paf        96:        void mark_to_rollback() {
                     97:                marked_to_rollback=true;
                     98:        }
                     99: 
                    100: private: // closing process
                    101: 
                    102:        void commit() { 
                    103:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
                    104:                        fdriver.commit(*fservices, fconnection) 
                    105:                );
                    106:        }
                    107:        void rollback() { 
                    108:                SQL_CONNECTION_SERVICED_FUNC_GUARDED(
                    109:                        fdriver.rollback(*fservices, fconnection)
                    110:                );
                    111:        }
                    112: 
                    113:        /// return to cache
                    114:        void close() {
1.24      paf       115:                if(marked_to_rollback) {
1.21      paf       116:                        rollback();
1.24      paf       117:                        marked_to_rollback=false;
                    118:                } else
1.21      paf       119:                        commit();
                    120: 
1.30.2.2! paf       121:                SQL_driver_manager.close_connection(furl, *this);
1.21      paf       122:        }
                    123: 
1.1       paf       124: private:
                    125: 
1.30.2.2! paf       126:        ConstStringPtr  furl;
1.1       paf       127:        SQL_Driver& fdriver;
1.9       parser    128:        SQL_Driver_services *fservices;
                    129:        void *fconnection;
1.21      paf       130:        time_t time_used;
                    131:        bool marked_to_rollback;
1.1       paf       132: };
                    133: 
                    134: #endif

E-mail: