Annotation of parser3/src/sql/pa_sql_driver.h, revision 1.34.2.1

1.22      paf         1: /** @file
                      2:        Parser: sql driver interface.
                      3: 
1.34      paf         4:        Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.26      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.22      paf         6: 
                      7: 
                      8:        driver dynamic library must look like this:
                      9:        @code
                     10:                class X_SQL_Driver : public SQL_Driver {
                     11:                public:
                     12: 
                     13:                        X_SQL_Driver() : SQL_driver() {}
                     14: 
                     15:                        int api_version() { return SQL_DRIVER_API_VERSION; }
                     16:                        
                     17:                        //...
                     18:                };
                     19: 
                     20:                extern "C" SQL_Driver *create() {
                     21:                        return new X_SQL_Driver();
                     22:                }       
                     23:        @endcode
                     24: */
                     25: 
                     26: #ifndef PA_SQL_DRIVER_H
                     27: #define PA_SQL_DRIVER_H
1.27      paf        28: 
1.34.2.1! paf        29: static const char* IDENT_SQL_DRIVER_H="$Date: 2003/01/21 15:51:16 $";
1.22      paf        30: 
                     31: #include <sys/types.h>
                     32: 
1.34.2.1! paf        33: #define SQL_DRIVER_API_VERSION 0x0006
1.29      paf        34: #define SQL_DRIVER_CREATE create /* used in driver implementation */
                     35: #define SQL_DRIVER_CREATE_NAME "create" /* could not figure out how to # it :( */
                     36: 
                     37: /// fields are freed elsewhere
1.32      paf        38: class SQL_Error {
1.29      paf        39:        bool fdefined;
                     40:        const char *ftype;
                     41:        const char *fcomment;
                     42: public:
1.32      paf        43:        SQL_Error():
1.29      paf        44:                fdefined(false) {}
1.32      paf        45:        SQL_Error(
1.29      paf        46:                const char *atype,
                     47:                const char *acomment):
                     48:                fdefined(true),
                     49:                ftype(atype),
                     50:                fcomment(acomment) {}
1.32      paf        51:        SQL_Error(const char *acomment):
1.29      paf        52:                fdefined(true),
                     53:                ftype(0),
                     54:                fcomment(acomment) {}
1.32      paf        55:        SQL_Error& operator =(const SQL_Error& src) {
1.29      paf        56:                fdefined=src.fdefined;
                     57:                ftype=src.ftype;
                     58:                fcomment=src.fcomment;
                     59:                return *this;
                     60:        }
                     61: 
1.32      paf        62:        bool defined() const { return fdefined; }
                     63:        const char *type() const { return ftype; }
                     64:        const char *comment() const { return fcomment; }
1.29      paf        65: };
                     66: 
1.22      paf        67: /// service functions for SQL driver to use
                     68: class SQL_Driver_services {
                     69: public:
1.34.2.1! paf        70:        /// allocates some bytes
1.22      paf        71:        virtual void *malloc(size_t size) =0;
                     72:        /// allocates some bytes clearing them with zeros
                     73:        virtual void *calloc(size_t size) =0;
1.34.2.1! paf        74:        /// reallocates bytes 
        !            75:        virtual void *realloc(void *ptr, size_t size) =0;
1.22      paf        76:        /// prepare throw exception
1.32      paf        77:        virtual void _throw(const SQL_Error& e) =0;
1.22      paf        78:        /// throw C++ exception from prepared
                     79:        virtual void propagate_exception() =0;
1.29      paf        80:        /// helper func
1.34.2.1! paf        81:        void _throw(const char *comment) { _throw(SQL_Error("sql.connect", comment)); }
1.22      paf        82: public:
                     83:        /// regretrully public, because can't make stack frames: "nowhere to return to"
                     84:        jmp_buf mark;
                     85: };
                     86: 
1.32      paf        87: /** events, occuring when SQL_Driver::query()-ing. 
                     88: 
                     89:                when OK must return false.
                     90:                must NOT throw exceptions, must store them to error & return true.
                     91: */
1.22      paf        92: class SQL_Driver_query_event_handlers {
                     93: public:
1.32      paf        94:        virtual bool add_column(SQL_Error& error, void *ptr, size_t size) =0;
                     95:        virtual bool before_rows(SQL_Error& error) =0;
                     96:        virtual bool add_row(SQL_Error& error) =0;
                     97:        virtual bool add_row_cell(SQL_Error& error, void *ptr, size_t size) =0;
1.22      paf        98: };
                     99: 
                    100: /// SQL driver API
                    101: class SQL_Driver {
                    102: public:
                    103: 
                    104:        /// get api version
                    105:        virtual int api_version() =0;
                    106:        /// initialize driver by loading sql dynamic link library
                    107:        virtual const char *initialize(char *dlopen_file_spec) =0;
                    108:        /**     connect to sql database using 
                    109:                @param used_only_to_connect_url 
                    110:                        format is driver specific
                    111:                        WARNING: must be used only to connect, for buffer doesn't live long enough
                    112: 
                    113:                @returns true+'connection' on success. 'error' on failure
                    114:        */
                    115:        virtual void connect(char *used_only_in_connect_url_cstr, 
                    116:                SQL_Driver_services& services, void **connection) =0;
                    117:        virtual void disconnect(void *connection) =0;
                    118:        virtual void commit(
                    119:                SQL_Driver_services& services, void *connection) =0;
                    120:        virtual void rollback(
                    121:                SQL_Driver_services& services, void *connection) =0;
                    122:        /// @returns true to indicate that connection still alive 
                    123:        virtual bool ping(
                    124:                SQL_Driver_services& services, void *connection) =0;
                    125:        /// encodes the string in 'from' to an escaped SQL string
                    126:        virtual unsigned int quote(
                    127:                SQL_Driver_services& services, void *connection,
                    128:                char *to, const char *from, unsigned int length) =0;
                    129:        virtual void query(
                    130:                SQL_Driver_services& services, void *connection,
                    131:                const char *statement, unsigned long offset, unsigned long limit,
                    132:                SQL_Driver_query_event_handlers& handlers) =0;
                    133: };
                    134: 
                    135: typedef SQL_Driver *(*SQL_Driver_create_func)();
                    136: 
                    137: #endif

E-mail: