ClanLib  2.3.7
db_connection.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
31 
32 #pragma once
33 
34 #include "api_database.h"
35 #include "db_command.h"
36 #include "db_transaction.h"
37 
38 class CL_DBTransaction;
39 class CL_DBReader;
41 class CL_DBConnection_Impl;
42 
47 {
50 public:
53 
58 
59  ~CL_DBConnection();
61 
64 public:
66 
69 public:
71  CL_DBCommand create_command(const CL_StringRef &text, CL_DBCommand::Type type = CL_DBCommand::sql_statement);
72 
74  template <class Arg1>
76  { return begin_arg(format, type).set_arg(arg1).get_result(); }
77 
79  template <class Arg1, class Arg2>
81  { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).get_result(); }
82 
84  template <class Arg1, class Arg2, class Arg3>
85  CL_DBCommand create_command(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, CL_DBCommand::Type type = CL_DBCommand::sql_statement)
86  { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).set_arg(arg3).get_result(); }
87 
89  template <class Arg1, class Arg2, class Arg3, class Arg4>
90  CL_DBCommand create_command(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, CL_DBCommand::Type type = CL_DBCommand::sql_statement)
91  { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).set_arg(arg3).set_arg(arg4).get_result(); }
92 
94  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
95  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)
96  { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).set_arg(arg3).set_arg(arg4).set_arg(arg5).get_result(); }
97 
99  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
100  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)
101  { 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(); }
102 
104  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
105  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)
106  { 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(); }
107 
110 
112  CL_DBReader execute_reader(CL_DBCommand &command);
113 
115  CL_String execute_scalar_string(CL_DBCommand &command);
116 
118  int execute_scalar_int(CL_DBCommand &command);
119 
121  void execute_non_query(CL_DBCommand &command);
123 
126 private:
127  class DBArg
128  {
129  public:
130  DBArg(CL_DBConnection &db, const CL_StringRef &format, CL_DBCommand::Type type) : cmd(db.create_command(format, type)), i(1){}
131 
132  DBArg &set_arg(const CL_StringRef &arg)
133  {
134  cmd.set_input_parameter_string(i, arg);
135  i++;
136  return *this;
137  }
138 
139  DBArg &set_arg(const char *arg)
140  {
141  cmd.set_input_parameter_string(i, arg);
142  i++;
143  return *this;
144  }
145 
146  DBArg &set_arg(bool arg)
147  {
148  cmd.set_input_parameter_bool(i, arg);
149  i++;
150  return *this;
151  }
152 
153  DBArg &set_arg(int arg)
154  {
155  cmd.set_input_parameter_int(i, arg);
156  i++;
157  return *this;
158  }
159 
160  DBArg &set_arg(double arg)
161  {
162  cmd.set_input_parameter_double(i, arg);
163  i++;
164  return *this;
165  }
166 
167  DBArg &set_arg(const CL_DateTime &arg)
168  {
169  cmd.set_input_parameter_datetime(i, arg);
170  i++;
171  return *this;
172  }
173 
174  DBArg &set_arg(const CL_DataBuffer &arg)
175  {
176  cmd.set_input_parameter_binary(i, arg);
177  i++;
178  return *this;
179  }
180 
181  CL_DBCommand get_result() const
182  {
183  return cmd;
184  }
185 
186  private:
187  CL_DBCommand cmd;
188  int i;
189  };
190 
191  DBArg begin_arg(const CL_StringRef &format, CL_DBCommand::Type type)
192  {
193  return DBArg(*this, format, type);
194  }
195 
196  CL_SharedPtr<CL_DBConnection_Impl> impl;
197 
199 };
200