db.h

Go to the documentation of this file.
00001 /*                                                              
00002  * A database abstraction layer for C++ and ACE framework
00003  * 
00004  * (C) 2006-2007 Thralling Penguin LLC. All rights reserved.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                                                                 
00019  */
00020 #ifndef _DB_H
00021 #define _DB_H
00022 
00023 #include <fstream>
00024 #include <iomanip>
00025 #include <strstream>
00026 #include "ace/Log_Msg.h"
00027 #include "ace/DLL.h"
00028 
00029 namespace DB
00030 {
00052     class ResultSet
00053     {
00054     public:
00055         virtual ~ResultSet(void) {};
00056 
00057         virtual bool close(void) = 0;
00058         virtual bool next(void) = 0;
00059 
00060         virtual unsigned int findColumn(const char *field) const = 0;
00061         virtual unsigned long recordCount(void) const = 0;
00062 
00063         virtual const char *getString(const int idx) const = 0;
00064         virtual const int getInteger(const int idx) const = 0;
00065         virtual const bool getBool(const int idx) const = 0;
00066         virtual const time_t getUnixTime(const int idx) const = 0;
00067         virtual const double getDouble(const int idx) const = 0;
00068         virtual const float getFloat(const int idx) const = 0;
00069         virtual const long getLong(const int idx) const = 0;
00070         virtual const short getShort(const int idx) const = 0;
00071     };
00072 
00087     class Connection
00088     {
00089     public:
00090         virtual ~Connection(void) {};
00091 
00104         virtual bool open(const char *database, const char *host, const int port, const char *user, const char *pass) = 0;
00105 
00111         virtual bool close(void) = 0;
00112 
00118         virtual bool isConnected(void) = 0;
00119 
00129         virtual bool execute(const char *sql) = 0;
00130 
00143         virtual ResultSet *executeQuery(const char *sql) = 0;
00144 
00152         virtual char *escape(const char *) = 0;
00153 
00163         virtual const char *unixtimeToSql(const time_t) = 0;
00164 
00171         virtual const unsigned long insertId(void) = 0;
00172 
00180         virtual bool beginTrans(void) = 0;
00181 
00187         virtual bool commitTrans(void) = 0;
00188 
00194         virtual bool rollbackTrans(void) = 0;
00195 
00196         enum TRANS_MODE {
00197             READ_UNCOMMITTED, 
00198             READ_COMMITTED, 
00199             REPEATABLE_READ, 
00200             SERIALIZABLE 
00201         };
00209         virtual bool setTransactionMode(const enum TRANS_MODE mode) = 0;
00210 
00216         virtual unsigned int errorno(void) const = 0;
00217 
00224         virtual const char *errormsg(void) const = 0;
00225 
00233         virtual const char *version(void) const = 0;
00234 
00235         typedef Connection* (*Connection_Creator) (void);
00236 
00249         static Connection *
00250         factory(const char *db_dll_name)
00251         {
00252             ACE_DLL dll;
00253 
00254             int ret = dll.open(ACE_TEXT(db_dll_name),
00255                         ACE_DEFAULT_SHLIB_MODE,
00256                         0);
00257             if (ret != 0) {
00258                 ACE_ERROR_RETURN((LM_ERROR, "%p", "dll.open"), 0);
00259             }
00260             Connection_Creator cc;
00261             void *void_ptr = dll.symbol(ACE_TEXT("create_connection"));
00262             ptrdiff_t tmp = reinterpret_cast<ptrdiff_t>(void_ptr);
00263             cc = reinterpret_cast<Connection_Creator>(tmp);
00264             if (cc == NULL) {
00265                 ACE_ERROR_RETURN((LM_ERROR, "%p", "dll.symbol"), 0);
00266             }
00267             return (cc());
00268         }
00269     };
00270 
00283     class Query
00284     {
00285     private:
00286         Connection &conn_;      
00287         std::ostrstream sql_;   
00289     public:
00290         Query(Connection &conn) : conn_(conn) {}
00291         ~Query() { sql_.freeze(false); }
00292 
00293         ACE_INLINE Query& operator<<(const char *sql)
00294         {
00295             sql_ << sql;
00296             return (*this);
00297         }
00298 
00299         ACE_INLINE Query& operator<<(const int val)
00300         {
00301             sql_ << val;
00302             return (*this);
00303         }
00304 
00305         ACE_INLINE Query& operator<<(const long val)
00306         {
00307             sql_ << val;
00308             return (*this);
00309         }
00310 
00311         ACE_INLINE Query& operator<<(const double val)
00312         {
00313             sql_ << val;
00314             return (*this);
00315         }
00316 
00317         ACE_INLINE Query& operator<<(const float val)
00318         {
00319             sql_ << val;
00320             return (*this);
00321         }
00322 
00323         ACE_INLINE Query& operator<<(const short val)
00324         {
00325             sql_ << val;
00326             return (*this);
00327         }
00328 
00329         ACE_INLINE Query& operator<<(const unsigned char *sql)
00330         {
00331             sql_ << sql;
00332             return (*this);
00333         }
00334 
00335         ACE_INLINE Query& operator<<(const unsigned int val)
00336         {
00337             sql_ << val;
00338             return (*this);
00339         }
00340 
00341         ACE_INLINE Query& operator<<(const unsigned long val)
00342         {
00343             sql_ << val;
00344             return (*this);
00345         }
00346 
00347         ACE_INLINE Query& operator<<(const unsigned short val)
00348         {
00349             sql_ << val;
00350             return (*this);
00351         }
00352 
00358         ACE_INLINE const char *str(void)
00359         {
00360             sql_ << std::ends;
00361             return (sql_.str());
00362         }
00363 
00364         // Friend functions for stream manipulators
00365         friend Query &unixtime_impl(Query &Out, const time_t ut);
00366         friend Query &qstr_impl(Query &Out, const char *str);
00367     };
00368 
00369     ACE_INLINE Query &unixtime_impl(Query &Out, const time_t ut)
00370     {
00371         const char *buf = Out.conn_.unixtimeToSql(ut);
00372         Out << buf;
00373         delete [] buf;
00374         return (Out);
00375     }
00376 
00377     ACE_INLINE Query &qstr_impl(Query &Out, const char *str)
00378     {
00379         const char *buf = Out.conn_.escape(str);
00380         Out << buf;
00381         delete [] buf;
00382         return (Out);
00383     }
00384 
00392     class unixtime {
00393         time_t t;
00394     public:
00395         unixtime(time_t ti) : t(ti) {}
00396         friend Query& operator<<(Query& Out, const unixtime& ref) {
00397             return unixtime_impl(Out, ref.t);
00398         }
00399     };
00400 
00401 
00408     class qstr {
00409         std::string s;
00410     public:
00411         qstr(const char *str) : s(str) {}
00412         friend Query& operator<<(Query& Out, const qstr& q) {
00413                 return qstr_impl(Out, q.s.c_str());
00414         }
00415     };
00416 }
00417 
00418 #endif
00419 

Generated on Tue Apr 24 18:59:42 2007 for DbAbstract by  doxygen 1.4.7