db_connection.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00031 
00032 #pragma once
00033 
00034 #include "api_database.h"
00035 #include "db_command.h"
00036 #include "db_transaction.h"
00037 
00038 class CL_DBTransaction;
00039 class CL_DBReader;
00040 class CL_DBConnectionProvider;
00041 class CL_DBConnection_Impl;
00042 
00046 class CL_API_DATABASE CL_DBConnection
00047 {
00050 public:
00052         CL_DBConnection();
00053 
00057         CL_DBConnection(CL_DBConnectionProvider *provider);
00058 
00059         ~CL_DBConnection();
00061 
00064 public:
00066 
00069 public:
00071         CL_DBCommand create_command(const CL_StringRef &text, CL_DBCommand::Type type = CL_DBCommand::sql_statement);
00072 
00074         template <class Arg1>
00075         CL_DBCommand create_command(const CL_StringRef &format, Arg1 arg1, CL_DBCommand::Type type = CL_DBCommand::sql_statement)
00076         { return begin_arg(format, type).set_arg(arg1).get_result(); }
00077 
00079         template <class Arg1, class Arg2>
00080         CL_DBCommand create_command(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, CL_DBCommand::Type type = CL_DBCommand::sql_statement)
00081         { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).get_result(); }
00082 
00084         template <class Arg1, class Arg2, class Arg3>
00085         CL_DBCommand create_command(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, CL_DBCommand::Type type = CL_DBCommand::sql_statement)
00086         { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).set_arg(arg3).get_result(); }
00087 
00089         template <class Arg1, class Arg2, class Arg3, class Arg4>
00090         CL_DBCommand create_command(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, CL_DBCommand::Type type = CL_DBCommand::sql_statement)
00091         { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).set_arg(arg3).set_arg(arg4).get_result(); }
00092 
00094         template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
00095         CL_DBCommand create_command(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, CL_DBCommand::Type type = CL_DBCommand::sql_statement)
00096         { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).set_arg(arg3).set_arg(arg4).set_arg(arg5).get_result(); }
00097 
00099         template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
00100         CL_DBCommand create_command(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, CL_DBCommand::Type type = CL_DBCommand::sql_statement)
00101         { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).set_arg(arg3).set_arg(arg4).set_arg(arg5).set_arg(arg6).get_result(); }
00102 
00104         template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
00105         CL_DBCommand create_command(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7, CL_DBCommand::Type type = CL_DBCommand::sql_statement)
00106         { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).set_arg(arg3).set_arg(arg4).set_arg(arg5).set_arg(arg6).set_arg(arg7).get_result(); }
00107 
00109         CL_DBTransaction begin_transaction(CL_DBTransaction::Type type = CL_DBTransaction::deferred);
00110 
00112         CL_DBReader execute_reader(CL_DBCommand &command);
00113 
00115         CL_String execute_scalar_string(CL_DBCommand &command);
00116 
00118         int execute_scalar_int(CL_DBCommand &command);
00119 
00121         void execute_non_query(CL_DBCommand &command);
00123 
00126 private:
00127         class DBArg
00128         {
00129         public:
00130                 DBArg(CL_DBConnection &db, const CL_StringRef &format, CL_DBCommand::Type type) : cmd(db.create_command(format, type)), i(1){}
00131 
00132                 DBArg &set_arg(const CL_StringRef &arg)
00133                 {
00134                         cmd.set_input_parameter_string(i, arg);
00135                         i++;
00136                         return *this;
00137                 }
00138 
00139                 DBArg &set_arg(const char *arg)
00140                 {
00141                         cmd.set_input_parameter_string(i, arg);
00142                         i++;
00143                         return *this;
00144                 }
00145 
00146                 DBArg &set_arg(bool arg)
00147                 {
00148                         cmd.set_input_parameter_bool(i, arg);
00149                         i++;
00150                         return *this;
00151                 }
00152 
00153                 DBArg &set_arg(int arg)
00154                 {
00155                         cmd.set_input_parameter_int(i, arg);
00156                         i++;
00157                         return *this;
00158                 }
00159 
00160                 DBArg &set_arg(double arg)
00161                 {
00162                         cmd.set_input_parameter_double(i, arg);
00163                         i++;
00164                         return *this;
00165                 }
00166 
00167                 DBArg &set_arg(const CL_DateTime &arg)
00168                 {
00169                         cmd.set_input_parameter_datetime(i, arg);
00170                         i++;
00171                         return *this;
00172                 }
00173 
00174                 DBArg &set_arg(const CL_DataBuffer &arg)
00175                 {
00176                         cmd.set_input_parameter_binary(i, arg);
00177                         i++;
00178                         return *this;
00179                 }
00180 
00181                 CL_DBCommand get_result() const
00182                 {
00183                         return cmd;
00184                 }
00185 
00186         private:
00187                 CL_DBCommand cmd;
00188                 int i;
00189         };
00190 
00191         DBArg begin_arg(const CL_StringRef &format, CL_DBCommand::Type type)
00192         {
00193                 return DBArg(*this, format, type);
00194         }
00195 
00196         CL_SharedPtr<CL_DBConnection_Impl> impl;
00197 
00199 };
00200