RTSQLBatch_Impl.hh

00001 //********************************************************************
00002 //*** ResourcePool/SQL/RTSQLBatch_Impl.hh
00003 //*** Copyright (c) 2003-2009 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: RTSQLBatch_Impl.hh,v 1.4 2009-05-02 18:47:19 mws Exp $
00005 //********************************************************************
00006 /*
00007 This file is part of ResourcePool.
00008 
00009 ResourcePool is free software; you can redistribute it
00010 and/or modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2 of
00012 the License, or (at your option) any later version.
00013 
00014 ResourcePool is distributed in the hope that it will be
00015 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00016 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with ResourcePool; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00022 02111-1307  USA */
00023 
00024 #ifndef RESOURCEPOOL_SQL_RTSQLBATCH_IMPL_HH
00025 #define RESOURCEPOOL_SQL_RTSQLBATCh_IMPL_HH
00026 
00027 #include "config.h"
00028 #include "util/Clone.hh"
00029 
00030 #include "ResourcePool/SQL/Factory.hh"
00031 #include "ResourcePool/SQL/ConcreteWrapper.hh"
00032 
00033 #ifdef RESOURCEPOOL_HAVE_MYSQL
00034 #include "ResourcePool/mysql/ResourcePool.hh"
00035 #endif
00036 
00037 #ifdef RESOURCEPOOL_HAVE_ORACLE
00038 #include "ResourcePool/oracle/ResourcePool.hh"
00039 #include "ResourcePool/oracle/Resource.hh"
00040 #include "ResourcePool/oracle/oracleType.hh"
00041 #endif
00042 
00043 
00044 #include <sstream>
00045 
00046 namespace fatalmind {
00047 namespace SQL {
00048 
00049 template<class TM>
00050 class RTSQLBatch_Impl: public Clone {
00051     public:
00052         RTSQLBatch_Impl() {
00053         };
00054         virtual ~RTSQLBatch_Impl() {
00055         }
00056 
00057         virtual void execute(SQLResource<TM>& hand) = 0;
00058         virtual int getError() = 0;
00059 
00060         virtual int getExecOffset() const = 0;
00061 
00062         virtual void add(const Command<ResourceType<TM> > &) = 0;
00063 
00064         virtual bool doesContinue() const = 0;
00065         virtual bool empty() const = 0;
00066         virtual void outputoperator(std::ostream&) const = 0;
00067 
00068         template<class CS>
00069         static typename CS::Impl* newRealCommand(
00070               typename SQLFactory<TM>::databaseType_t a_type
00071             , typename CS::RTC& a_that
00072             , const bool cont
00073         ) {
00074             switch(a_type) {
00075 #ifdef RESOURCEPOOL_HAVE_MYSQL
00076             case SQLFactory<TM>::MYSQLTX :
00077             case SQLFactory<TM>::MYSQL :
00078                 return new typename CS::template Specialized<fatalmind::ResourceType::mysql>::Impl(a_that, cont);
00079                 break;
00080 #endif
00081 #ifdef RESOURCEPOOL_HAVE_ORACLE
00082             case SQLFactory<TM>::OCI:
00083                 return new typename CS::template Specialized<fatalmind::ResourceType::oracle>::Impl(a_that, cont);
00084                 break;
00085 #endif
00086             default:
00087                 std::ostringstream oss;
00088                 oss << "Assertaion failure (type=" << a_type << ")";
00089                 throw Exception(oss.str());
00090                 break;
00091         }
00092     }
00093 };
00094 
00095 template<class TM, template<class TM2>class B, class RT, class CW, class RTC>
00096 class RTSQLBatch_SpecializedImplCloneHelper
00097 {
00098     public:
00099     static Clone* DoClone(const Clone * const that) {
00100         throw CloneException(that); 
00101     }
00102 };
00103 
00104 // fwd decl
00105 template<class TM, template<class TM2>class B, class RT, class CW, class RTC>
00106 class RTSQLBatch_SpecializedImpl ;
00107 
00108 template<class TM, class RT, class CW, class RTC>
00109 class RTSQLBatch_SpecializedImplCloneHelper<TM, RTSQLBatch_Impl, RT, CW, RTC>
00110 {
00111     public:
00112     typedef  RTSQLBatch_SpecializedImpl<TM, RTSQLBatch_Impl, RT, CW, RTC> target;
00113     static Clone* DoClone(const Clone *const that) {
00114         const target *x = dynamic_cast<const target*>(that);    
00115         return new target(*x);
00116     }
00117 };
00118 
00119 
00120 template<class TM, template<class TM2>class B, class RT, class CW, class RTC>
00121 class RTSQLBatch_SpecializedImpl 
00122 : public B<TM>
00123 {
00124     public:
00125         RTSQLBatch_SpecializedImpl(RTC& rtc, const bool cont) 
00126         : sqlBatch_(rtc, cont)
00127         {
00128         }
00129 
00130         virtual void execute(SQLResource<TM>& hand) {
00131             sqlBatch_.execute(
00132                 dynamic_cast<
00133                     SQLResourceSpecialized<TM, typename RT::resource_t>&
00134                 >(hand).getRealResource()
00135             );
00136         }
00137         virtual int getError() {
00138             return sqlBatch_.getError();
00139         }
00140 
00141         virtual int getExecOffset() const {
00142             if (sqlBatch_.getExecPosition() == sqlBatch_.end()) {
00143                 return -1;
00144             } else {
00145                 return std::distance(sqlBatch_.begin(), sqlBatch_.getExecPosition());
00146             }
00147         }
00148 
00149         virtual bool doesContinue() const {
00150             return sqlBatch_.doesContinue();    
00151         }
00152 
00153         virtual bool empty() const {
00154             return sqlBatch_.empty();   
00155         }
00156 
00157         virtual void outputoperator(std::ostream& s) const {
00158             sqlBatch_.outputoperator(s);
00159         }
00160 
00161         virtual void add(const Command<ResourceType<TM> >& cmd) {
00162             const RTSQLCommand<TM>* rtcmd = dynamic_cast<const RTSQLCommand<TM>* >(&cmd);
00163             if (rtcmd != 0) {
00164                 sqlBatch_.add(dynamic_cast<const Command<RT>& >(*rtcmd->getPlainSpecializedCommand()));
00165             } else {
00166                 ConcreteWrapper<RT , ResourceType<TM> > wrap(cmd);
00167                 sqlBatch_.add(wrap);
00168             }
00169         }
00170 
00171     protected:
00172         virtual Clone* DoClone() const {
00173             return RTSQLBatch_SpecializedImplCloneHelper<TM, B, RT, CW, RTC>::DoClone(this);
00174         }
00175 
00176         CW sqlBatch_;
00177 };
00178 
00179 
00180 } // namespace SQL
00181 } // namespace fatalmind
00182 
00183 #endif

Generated on Mon Nov 9 16:21:24 2009 for ResourcePool by  doxygen 1.5.3