db_connection.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 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 
29 
30 #pragma once
31 
32 #include "api_database.h"
33 #include "db_command.h"
34 #include "db_transaction.h"
35 
36 namespace clan
37 {
40 
41 class DBTransaction;
42 class DBReader;
43 class DBConnectionProvider;
44 class DBConnection_Impl;
45 
47 class CL_API_DATABASE DBConnection
48 {
51 public:
53  DBConnection();
54 
59 
60  ~DBConnection();
62 
65 public:
67 
70 public:
72  DBCommand create_command(const std::string &text, DBCommand::Type type = DBCommand::sql_statement);
73 
75  template <class Arg1>
76  DBCommand create_command(const std::string &format, Arg1 arg1, DBCommand::Type type = DBCommand::sql_statement)
77  { return begin_arg(format, type).set_arg(arg1).get_result(); }
78 
80  template <class Arg1, class Arg2>
81  DBCommand create_command(const std::string &format, Arg1 arg1, Arg2 arg2, DBCommand::Type type = DBCommand::sql_statement)
82  { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).get_result(); }
83 
85  template <class Arg1, class Arg2, class Arg3>
86  DBCommand create_command(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, DBCommand::Type type = DBCommand::sql_statement)
87  { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).set_arg(arg3).get_result(); }
88 
90  template <class Arg1, class Arg2, class Arg3, class Arg4>
91  DBCommand create_command(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, DBCommand::Type type = DBCommand::sql_statement)
92  { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).set_arg(arg3).set_arg(arg4).get_result(); }
93 
95  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
96  DBCommand create_command(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, DBCommand::Type type = DBCommand::sql_statement)
97  { return begin_arg(format, type).set_arg(arg1).set_arg(arg2).set_arg(arg3).set_arg(arg4).set_arg(arg5).get_result(); }
98 
100  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
101  DBCommand create_command(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, DBCommand::Type type = DBCommand::sql_statement)
102  { 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(); }
103 
105  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
106  DBCommand create_command(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7, DBCommand::Type type = DBCommand::sql_statement)
107  { 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(); }
108 
111 
113  DBReader execute_reader(DBCommand &command);
114 
116  std::string execute_scalar_string(DBCommand &command);
117 
119  int execute_scalar_int(DBCommand &command);
120 
122  void execute_non_query(DBCommand &command);
124 
127 private:
128  class DBArg
129  {
130  public:
131  DBArg(DBConnection &db, const std::string &format, DBCommand::Type type) : cmd(db.create_command(format, type)), i(1){}
132 
133  DBArg &set_arg(const std::string &arg)
134  {
135  cmd.set_input_parameter_string(i, arg);
136  i++;
137  return *this;
138  }
139 
140  DBArg &set_arg(const char *arg)
141  {
142  cmd.set_input_parameter_string(i, arg);
143  i++;
144  return *this;
145  }
146 
147  DBArg &set_arg(bool arg)
148  {
149  cmd.set_input_parameter_bool(i, arg);
150  i++;
151  return *this;
152  }
153 
154  DBArg &set_arg(int arg)
155  {
156  cmd.set_input_parameter_int(i, arg);
157  i++;
158  return *this;
159  }
160 
161  DBArg &set_arg(double arg)
162  {
163  cmd.set_input_parameter_double(i, arg);
164  i++;
165  return *this;
166  }
167 
168  DBArg &set_arg(const DateTime &arg)
169  {
170  cmd.set_input_parameter_datetime(i, arg);
171  i++;
172  return *this;
173  }
174 
175  DBArg &set_arg(const DataBuffer &arg)
176  {
177  cmd.set_input_parameter_binary(i, arg);
178  i++;
179  return *this;
180  }
181 
183  {
184  return cmd;
185  }
186 
187  private:
188  DBCommand cmd;
189  int i;
190  };
191 
192  DBArg begin_arg(const std::string &format, DBCommand::Type type)
193  {
194  return DBArg(*this, format, type);
195  }
196 
197  std::shared_ptr<DBConnection_Impl> impl;
198 
200 };
201 
202 }
203 
DBCommand create_command(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, DBCommand::Type type=DBCommand::sql_statement)
Create database command with 5 input arguments.
Definition: db_connection.h:96
Database command.
Definition: db_command.h:46
Database reader.
Definition: db_reader.h:47
Database transaction.
Definition: db_transaction.h:44
Date/Time class.
Definition: datetime.h:44
DBArg & set_arg(const char *arg)
Definition: db_connection.h:140
DBCommand create_command(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, DBCommand::Type type=DBCommand::sql_statement)
Create database command with 3 input arguments.
Definition: db_connection.h:86
Database connection provider.
Definition: db_connection_provider.h:46
DBArg(DBConnection &db, const std::string &format, DBCommand::Type type)
Definition: db_connection.h:131
DBArg & set_arg(const DataBuffer &arg)
Definition: db_connection.h:175
DBCommand create_command(const std::string &format, Arg1 arg1, DBCommand::Type type=DBCommand::sql_statement)
Create database command with 1 input argument.
Definition: db_connection.h:76
Database connection.
Definition: db_connection.h:47
DBCommand create_command(const std::string &format, Arg1 arg1, Arg2 arg2, DBCommand::Type type=DBCommand::sql_statement)
Create database command with 2 input arguments.
Definition: db_connection.h:81
Type
Definition: db_command.h:51
Definition: db_command.h:54
DBCommand create_command(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, DBCommand::Type type=DBCommand::sql_statement)
Create database command with 6 input arguments.
Definition: db_connection.h:101
DBCommand create_command(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7, DBCommand::Type type=DBCommand::sql_statement)
Create database command with 7 input arguments.
Definition: db_connection.h:106
Type
Definition: db_transaction.h:49
DBCommand get_result() const
Definition: db_connection.h:182
DBArg & set_arg(int arg)
Definition: db_connection.h:154
DBArg & set_arg(bool arg)
Definition: db_connection.h:147
DBCommand create_command(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, DBCommand::Type type=DBCommand::sql_statement)
Create database command with 4 input arguments.
Definition: db_connection.h:91
DBArg & set_arg(double arg)
Definition: db_connection.h:161
Definition: db_transaction.h:51
General purpose data buffer.
Definition: databuffer.h:43
DBArg & set_arg(const std::string &arg)
Definition: db_connection.h:133
DBArg & set_arg(const DateTime &arg)
Definition: db_connection.h:168