RTSQLExecute_Impl.hh

00001 //********************************************************************
00002 //*** ResourcePool/SQL/RTSQLExecute_Impl.hh
00003 //*** Copyright (c) 2003-2009 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: RTSQLExecute_Impl.hh,v 1.27 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_RTSQLEXECUTE_IMPL_HH
00025 #define RESOURCEPOOL_SQL_RTSQLEXECUTE_IMPL_HH
00026 
00027 #include "config.h"
00028 #include "util/Clone.hh"
00029 
00030 #include "ResourcePool/SQL/Factory.hh"
00031 #include "ResourcePool/SQL/BindOutInterface.hh"
00032 
00033 #ifdef RESOURCEPOOL_HAVE_MYSQL
00034 #include "ResourcePool/mysql/ResourcePool.hh"
00035 #include "ResourcePool/mysql/Resource.hh"
00036 #include <mysql/mysql.h>
00037 #endif
00038 
00039 #ifdef RESOURCEPOOL_HAVE_ORACLE
00040 #include "ResourcePool/oracle/ResourcePool.hh"
00041 #include "ResourcePool/oracle/Resource.hh"
00042 #include "ResourcePool/oracle/oracleType.hh"
00043 #endif
00044 
00045 
00046 #include <sstream>
00047 
00048 namespace fatalmind {
00049 namespace SQL {
00050 
00051 template<class TM>
00052 class RTSQLExecute_Impl: public Clone {
00053     public:
00054         RTSQLExecute_Impl() {
00055         };
00056         virtual ~RTSQLExecute_Impl() {
00057         }
00058 
00059         virtual void bind(const int pos, const char, bool nul = false) = 0;
00060         virtual void bind(const int pos, const signed char, bool nul = false) = 0;
00061         virtual void bind(const int pos, const unsigned char, bool nul = false) = 0;
00062 
00063         virtual void bind(const int pos, const short int val, bool nul = false) = 0;
00064         virtual void bind(const int pos, const int val, bool nul = false) = 0;
00065         virtual void bind(const int pos, const long int val, bool nul = false) = 0;
00066 
00067         virtual void bind(const int pos, const unsigned short int val, bool nul = false) = 0;
00068         virtual void bind(const int pos, const unsigned int val, bool nul = false) = 0;
00069         virtual void bind(const int pos, const unsigned long int val, bool nul = false) = 0;
00070 
00071         virtual void bind(const int pos, const float val, bool nul = false) = 0;
00072         virtual void bind(const int pos, const double val, bool nul = false) = 0;
00073         virtual void bind(const int pos, const long double val, bool nul = false) = 0;
00074         virtual void bind(const int pos, const std::string& val, bool nul = false) = 0;
00075 
00076         virtual void clearParameters() = 0;
00077 
00078         virtual void execute(SQLResource<TM>& hand) = 0;
00079         virtual void outputoperator(std::ostream&) const = 0;
00080 
00081         virtual const Clone* getPlainSpecializedCommand() const = 0;
00082 
00083         template<class CS>
00084         static typename CS::Impl* newRealCommand(
00085               typename SQLFactory<TM>::databaseType_t a_type
00086             , typename CS::RTC& a_that
00087             , const SQLStatement& a_sql
00088         ) {
00089             switch(a_type) {
00090 #ifdef RESOURCEPOOL_HAVE_MYSQL
00091             case SQLFactory<TM>::MYSQL :
00092             case SQLFactory<TM>::MYSQLTX :
00093                 return new typename CS::template Specialized<fatalmind::ResourceType::mysql>::Impl(a_that, a_sql);
00094                 break;
00095 #endif
00096 #ifdef RESOURCEPOOL_HAVE_ORACLE
00097             case SQLFactory<TM>::OCI:
00098                 return new typename CS::template Specialized<fatalmind::ResourceType::oracle>::Impl(a_that, a_sql);
00099                 break;
00100 #endif
00101             default:
00102                 std::ostringstream oss;
00103                 oss << "Assertaion failure (type=" << a_type << ")";
00104                 throw Exception(oss.str());
00105                 break;
00106         }
00107     }
00108 };
00109 
00110 template<class TM>
00111 class RTSQLSelectRow_Impl
00112 : public RTSQLExecute_Impl<TM>
00113 {
00114     public:
00115         RTSQLSelectRow_Impl() {
00116         };
00117 
00118         virtual ~RTSQLSelectRow_Impl() {
00119         };
00120 
00121         virtual void bindout(const int pos, char& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00122         virtual void bindout(const int pos, signed char& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00123         virtual void bindout(const int pos, unsigned char& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00124 
00125         virtual void bindout(const int pos, short int& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00126         virtual void bindout(const int pos, int& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00127         virtual void bindout(const int pos, long int& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00128 
00129         virtual void bindout(const int pos, unsigned short int& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00130         virtual void bindout(const int pos, unsigned int& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00131         virtual void bindout(const int pos, unsigned long int& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00132 
00133         virtual void bindout(const int pos, float& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00134         virtual void bindout(const int pos, double& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00135         virtual void bindout(const int pos, long double& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00136 
00137         virtual void bindout(const int pos, std::string& val, bool& isnull = BindOutInterface::notnullconstant) = 0;
00138 
00139 //      virtual bool isnull(const unsigned int pos) const = 0;
00140 
00141 
00142         template<class CS>
00143         static typename CS::Impl* newRealCommand(
00144               typename SQLFactory<TM>::databaseType_t a_type
00145             , typename CS::RTC& a_that
00146             , const SQLStatement& a_sql
00147             , SQL::SQLFetcher& fetcher
00148         ) {
00149             switch(a_type) {
00150 #ifdef RESOURCEPOOL_HAVE_MYSQL
00151             case SQLFactory<TM>::MYSQL :
00152             case SQLFactory<TM>::MYSQLTX :
00153                 return new typename CS::template Specialized<fatalmind::ResourceType::mysql>::Impl(a_that, a_sql, fetcher);
00154                 break;
00155 #endif
00156 #ifdef RESOURCEPOOL_HAVE_ORACLE
00157             case SQLFactory<TM>::OCI:
00158                 return new typename CS::template Specialized<fatalmind::ResourceType::oracle>::Impl(a_that, a_sql, fetcher);
00159                 break;
00160 #endif
00161             default:
00162                 std::ostringstream oss;
00163                 oss << "Assertaion failure (type=" << a_type << ")";
00164                 throw Exception(oss.str());
00165                 break;
00166         }
00167     }
00168 };
00169 
00170 
00171 template<class TM, template<class TM2>class B, class RT, class CW, class RTC>
00172 class RTSQLExecute_SpecializedImplCloneHelper
00173 {
00174     public:
00175     static Clone* DoClone(const Clone * const that) {
00176         throw CloneException(that); 
00177     }
00178 };
00179 
00180 // fwd decl
00181 template<class TM, template<class TM2>class B, class RT, class CW, class RTC>
00182 class RTSQLExecute_SpecializedImpl ;
00183 
00184 template<class TM, class RT, class CW, class RTC>
00185 class RTSQLExecute_SpecializedImplCloneHelper<TM, RTSQLExecute_Impl, RT, CW, RTC>
00186 {
00187     public:
00188     typedef  RTSQLExecute_SpecializedImpl<TM, RTSQLExecute_Impl, RT, CW, RTC> target;
00189     static Clone* DoClone(const Clone *const that) {
00190         const target *x = dynamic_cast<const target*>(that);    
00191         return new target(*x);
00192     }
00193 };
00194 
00195 
00196 template<class TM, template<class TM2>class B, class RT, class CW, class RTC>
00197 class RTSQLExecute_SpecializedImpl 
00198 : public B<TM>
00199 {
00200     public:
00201         RTSQLExecute_SpecializedImpl(RTC& rtc, const SQLStatement& sql) 
00202         : sqlExecute_(rtc, sql)
00203         {
00204         }
00205     
00206         // also used for SQLSelect(Row)
00207         RTSQLExecute_SpecializedImpl(RTC& rtc, const SQLStatement& sql, SQL::SQLFetcher& fetcher) 
00208         : sqlExecute_(rtc, sql, fetcher)
00209         {
00210         }
00211 
00212         void bind(const int pos, const char val, bool nul = false) {
00213             sqlExecute_.bind(pos, val, nul);
00214         }
00215         void bind(const int pos, const signed char val, bool nul = false) {
00216             sqlExecute_.bind(pos, val, nul);
00217         }
00218         void bind(const int pos, const unsigned char val, bool nul = false) {
00219             sqlExecute_.bind(pos, val, nul);
00220         }
00221 
00222         void bind(const int pos, const short int val, bool nul = false) {
00223             sqlExecute_.bind(pos, val, nul);
00224         }
00225         void bind(const int pos, const int val, bool nul = false) {
00226             sqlExecute_.bind(pos, val, nul);
00227         }
00228         void bind(const int pos, const long int val, bool nul = false) {
00229             sqlExecute_.bind(pos, val, nul);
00230         }
00231 
00232         void bind(const int pos, const unsigned short int val, bool nul = false) {
00233             sqlExecute_.bind(pos, val, nul);
00234         }
00235         void bind(const int pos, const unsigned int val, bool nul = false) {
00236             sqlExecute_.bind(pos, val, nul);
00237         }
00238         void bind(const int pos, const unsigned long int val, bool nul = false) {
00239             sqlExecute_.bind(pos, val, nul);
00240         }
00241 
00242 
00243         void bind(const int pos, const float val, bool nul = false) {
00244             sqlExecute_.bind(pos, val, nul);
00245         }
00246         void bind(const int pos, const double val, bool nul = false) {
00247             sqlExecute_.bind(pos, val, nul);
00248         }
00249         void bind(const int pos, const long double val, bool nul = false) {
00250             sqlExecute_.bind(pos, val, nul);
00251         }
00252 
00253         void bind(const int pos, const std::string& val, bool nul = false) {
00254             sqlExecute_.bind(pos, val, nul);
00255         }
00256 
00257         void clearParameters() {
00258             sqlExecute_.clearParameters();
00259         }
00260 
00261         virtual void execute(SQLResource<TM>& hand) {
00262             sqlExecute_.execute(
00263                 dynamic_cast<
00264                     SQLResourceSpecialized<TM, typename RT::resource_t>&
00265                 >(hand).getRealResource()
00266             );
00267         }
00268 
00269         virtual void outputoperator(std::ostream& s) const {
00270             sqlExecute_.outputoperator(s);
00271         }
00272 
00273     protected:
00274         virtual Clone* DoClone() const {
00275             return RTSQLExecute_SpecializedImplCloneHelper<TM, B, RT, CW, RTC>::DoClone(this);
00276         }
00277 
00278         virtual const Clone* getPlainSpecializedCommand() const {
00279             return &sqlExecute_;
00280         };
00281 
00282         CW sqlExecute_;
00283 };
00284 
00285 
00286 } // namespace SQL
00287 } // namespace fatalmind
00288 
00289 #endif

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