RTSQLTransaction_Impl.hh

00001 //********************************************************************
00002 //*** ResourcePool/SQL/RTSQLTransaction_Impl.hh
00003 //*** Copyright (c) 2003-2009 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: RTSQLTransaction_Impl.hh,v 1.7 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_RTSQLTRANSACTION_IMPL_HH
00025 #define RESOURCEPOOL_SQL_RTSQLTRANSACTION_IMPL_HH
00026 
00027 #include "config.h"
00028 #include "util/Clone.hh"
00029 
00030 #include "ResourcePool/SQL/Factory.hh"
00031 
00032 #ifdef RESOURCEPOOL_HAVE_MYSQL_TRANSACTIONS
00033 #include "ResourcePool/mysql/ResourcePool.hh"
00034 #endif
00035 
00036 #ifdef RESOURCEPOOL_HAVE_ORACLE
00037 #include "ResourcePool/oracle/ResourcePool.hh"
00038 #include "ResourcePool/oracle/Resource.hh"
00039 #include "ResourcePool/oracle/oracleType.hh"
00040 #endif
00041 
00042 
00043 #include <sstream>
00044 
00045 namespace fatalmind {
00046 namespace SQL {
00047 
00048 template<class TM>
00049 class RTSQLTransaction_Impl: public Clone {
00050     public:
00051         RTSQLTransaction_Impl() {
00052         };
00053         virtual ~RTSQLTransaction_Impl() {
00054         }
00055 
00056         virtual void execute(SQLResource<TM>& hand) = 0;
00057 
00058         template<class CS>
00059         static typename CS::Impl* newRealCommand(
00060               typename SQLFactory<TM>::databaseType_t a_type
00061             , typename CS::RTC& a_that
00062             , const typename CS::RTC::innercommand_t& a_cmd
00063         ) {
00064             switch(a_type) {
00065 #ifdef RESOURCEPOOL_HAVE_MYSQL
00066             case SQLFactory<TM>::MYSQLTX :
00067 #ifdef RESOURCEPOOL_HAVE_MYSQL_TRANSACTIONS
00068                 return new typename CS::template Specialized<fatalmind::ResourceType::mysql>::Impl(a_that, a_cmd);
00069 #else
00070                 throw Exception("This version of ResourcePool was not build with mysql transaction support");
00071 #endif
00072                 break;
00073             case SQLFactory<TM>::MYSQL :
00074                 // TODO, nop emulate
00075                 throw Exception("This version of ResourcePool was not build with mysql transaction support");
00076                 break;
00077 #endif
00078 #ifdef RESOURCEPOOL_HAVE_ORACLE
00079             case SQLFactory<TM>::OCI:
00080                 return new typename CS::template Specialized<fatalmind::ResourceType::oracle>::Impl(a_that, a_cmd);
00081                 break;
00082 #endif
00083             default:
00084                 std::ostringstream oss;
00085                 oss << "Assertaion failure (type=" << a_type << ")";
00086                 throw Exception(oss.str());
00087                 break;
00088         }
00089     }
00090 };
00091 
00092 template<class TM, template<class TM2>class B, class RT, class CW, class RTC>
00093 class RTSQLTransaction_SpecializedImplCloneHelper
00094 {
00095     public:
00096     static Clone* DoClone(const Clone * const that) {
00097         throw CloneException(that); 
00098     }
00099 };
00100 
00101 // fwd decl
00102 template<class TM, template<class TM2>class B, class RT, class CW, class RTC>
00103 class RTSQLTransaction_SpecializedImpl ;
00104 
00105 template<class TM, class RT, class CW, class RTC>
00106 class RTSQLTransaction_SpecializedImplCloneHelper<TM, RTSQLTransaction_Impl, RT, CW, RTC>
00107 {
00108     public:
00109     typedef  RTSQLTransaction_SpecializedImpl<TM, RTSQLTransaction_Impl, RT, CW, RTC> target;
00110     static Clone* DoClone(const Clone *const that) {
00111         const target *x = dynamic_cast<const target*>(that);    
00112         return new target(*x);
00113     }
00114 };
00115 
00116 
00117 template<class TM, template<class TM2>class B, class RT, class CW, class RTC>
00118 class RTSQLTransaction_SpecializedImpl 
00119 : public B<TM>
00120 {
00121     public:
00122         RTSQLTransaction_SpecializedImpl(RTC& rtc, const typename RTC::innercommand_t& cmd) 
00123         : sqlTransaction_(rtc, cmd)
00124         {
00125         }
00126 
00127         virtual void execute(SQLResource<TM>& hand) {
00128             sqlTransaction_.execute(
00129                 dynamic_cast<
00130                     SQLResourceSpecialized<TM, typename RT::resource_t>&
00131                 >(hand).getRealResource()
00132             );
00133         }
00134 
00135     protected:
00136         virtual Clone* DoClone() const {
00137             return RTSQLTransaction_SpecializedImplCloneHelper<TM, B, RT, CW, RTC>::DoClone(this);
00138         }
00139 
00140         CW sqlTransaction_;
00141 };
00142 
00143 
00144 } // namespace SQL
00145 } // namespace fatalmind
00146 
00147 #endif

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