Samchon Framework for CPP  1.0.0
SQLStatement.hpp
1 #pragma once
2 #include <samchon/API.hpp>
3 
4 #include <vector>
5 #include <memory>
6 #include <samchon/Map.hpp>
7 #include <string>
8 #include <samchon/ByteArray.hpp>
9 
10 namespace samchon
11 {
12 namespace library
13 {
14  class SQLi;
15  class XML;
16 
44  class SAMCHON_FRAMEWORK_API SQLStatement
45  {
46  friend class SQLi;
47  private:
48 #ifdef _WIN64
49  typedef long long SQL_SIZE_T;
50 #else
51  typedef long SQL_SIZE_T;
52 #endif
53 
54  protected:
55  /* --------------------------------------------------------------
56  BASIC MEMBER VARIABLES
57  -------------------------------------------------------------- */
62 
66  void *hstmt;
67 
68  /* --------------------------------------------------------------
69  MEMBER VARIABLES FOR BINDING
70  -------------------------------------------------------------- */
75 
83 
84  protected:
96  SQLStatement(SQLi *sqli);
97  void reset(SQLi *sqli);
98 
99  public:
100  virtual ~SQLStatement();
101 
102  public:
106  void free();
107 
111  void refresh();
112 
113  /* -----------------------------------------------------------------------
114  QUERY
115  ----------------------------------------------------------------------- */
125  template <typename T, typename ... _Args>
126  void prepare(const std::string &sql, const T& val, const _Args& ... args)
127  {
128  prepare(sql);
129 
130  bindParameter(val);
131  bindParameter(args...);
132  };
133  template <typename T> void prepare(const std::string &str, const T& val)
134  {
135  prepare(str);
136 
137  bindParameter(val);
138  };
139  void prepare(const std::string &);
140  void prepare(const std::wstring &);
141 
142  template <typename T, typename ... _Args>
143  void prepare(const std::wstring &sql, const T& val, const _Args& ... args)
144  {
145  prepare(sql);
146 
147  bindParameter(val);
148  bindParameter(args...);
149  };
150  template <typename T> void prepare(const std::wstring &str, const T& val)
151  {
152  prepare(str);
153 
154  bindParameter(val);
155  };
156 
162  void execute();
163 
180  void executeDirectly(const std::string&);
181  void executeDirectly(const std::wstring&);
182 
183  /* -----------------------------------------------------------------------
184  CURSOR
185  ----------------------------------------------------------------------- */
193  auto fetch() const -> bool;
194 
200  auto next() const -> bool;
201 
202  /* -------------------------------------------------------------------------
203  GET DATA IN A COLUMN
204  ------------------------------------------------------------------------- */
211  auto size() const -> size_t;
212 
220  template <typename T> auto at(size_t index) const -> T
221  {
222  T val;
223  sql_get_data(index + 1, C_TYPE(T()), &val);
224 
225  return val;
226  };
227  template<> auto at(size_t index) const -> std::string
228  {
229  return _atAsString(index);
230  }
231  template<> auto at(size_t index) const -> std::wstring
232  {
233  return _atAsWString(index);
234  };
235  template<> auto at(size_t index) const -> ByteArray
236  {
237  return _atAsByteArray(index);
238  };
239 
247  template <typename T> auto get(const std::string &) const -> T
248  {
249  return this->at<T>(0);
250  };
251 
261  virtual auto toXML() const->std::shared_ptr<XML>;
262 
263  private:
264  /* -------------------------------------------------------------------
265  BIND
266  ------------------------------------------------------------------- */
267  template <typename T, typename ... _Args>
268  void bindParameter(const T& val, const _Args& ... args)
269  {
270  bindParameter(val);
271  bindParameter(args...);
272  };
273  template <typename T> void bindParameter(const T &val)
274  {
275  sql_bind_parameter(C_TYPE(val), SQL_TYPE(val), (void*)&val);
276  };
277  template<> void bindParameter(const std::string &val);
278  template<> void bindParameter(const std::wstring &val);
279  template<> void bindParameter(const ByteArray &val);
280 
281  /* -------------------------------------------------------------------
282  ODBC'S FUNCTION
283  ------------------------------------------------------------------- */
284  void sql_get_data(size_t, short, void*) const;
285  void sql_bind_parameter(short, short, void*);
286 
287  template <typename T> auto C_TYPE(const T &) const -> short;
288  template<> auto C_TYPE(const bool &) const -> short;
289  template<> auto C_TYPE(const char &) const -> short;
290  template<> auto C_TYPE(const short &) const -> short;
291  template<> auto C_TYPE(const long &) const -> short;
292  template<> auto C_TYPE(const long long &) const -> short;
293  template<> auto C_TYPE(const int &) const -> short;
294  template<> auto C_TYPE(const float &) const -> short;
295  template<> auto C_TYPE(const double &) const -> short;
296 
297  template<> auto C_TYPE(const unsigned char &) const -> short;
298  template<> auto C_TYPE(const unsigned short &) const -> short;
299  template<> auto C_TYPE(const unsigned long &) const -> short;
300  template<> auto C_TYPE(const unsigned long long &) const -> short;
301  template<> auto C_TYPE(const unsigned int &) const -> short;
302  template<> auto C_TYPE(const long double &) const -> short;
303 
304  template<> auto C_TYPE(const std::string &) const -> short;
305  template<> auto C_TYPE(const std::wstring &) const -> short;
306  template<> auto C_TYPE(const ByteArray &) const -> short;
307 
308  template <typename T> auto SQL_TYPE(const T &) const -> short;
309  template<> auto SQL_TYPE(const bool &) const -> short;
310  template<> auto SQL_TYPE(const char &) const -> short;
311  template<> auto SQL_TYPE(const short &) const -> short;
312  template<> auto SQL_TYPE(const long &) const -> short;
313  template<> auto SQL_TYPE(const long long &) const -> short;
314  template<> auto SQL_TYPE(const int &) const -> short;
315  template<> auto SQL_TYPE(const float &) const -> short;
316  template<> auto SQL_TYPE(const double &) const -> short;
317 
318  template<> auto SQL_TYPE(const unsigned char &) const -> short;
319  template<> auto SQL_TYPE(const unsigned short &) const -> short;
320  template<> auto SQL_TYPE(const unsigned long &) const -> short;
321  template<> auto SQL_TYPE(const unsigned long long &) const -> short;
322  template<> auto SQL_TYPE(const unsigned int &) const -> short;
323  template<> auto SQL_TYPE(const long double &) const -> short;
324 
325  template<> auto SQL_TYPE(const std::string &) const -> short;
326  template<> auto SQL_TYPE(const std::wstring &) const -> short;
327  template<> auto SQL_TYPE(const ByteArray &) const -> short;
328 
329  private:
330  /* -------------------------------------------------------------------
331  HIDDEN TEMPLATE HELPERS
332  ------------------------------------------------------------------- */
333  auto _atAsString(size_t index) const -> std::string;
334  auto _atAsWString(size_t index) const -> std::wstring;
335 
336  auto _atAsByteArray(size_t index) const -> ByteArray;
337  };
338 };
339 };
void * hstmt
Handler of sql statement (OBDC)
size_t bindParameterCount
Count of binded parameters .
auto at(size_t index) const -> T
Get column&#39;s data by its index.
SQLi * sqli
SQLi who created the SQLStatement.
A SQL interface; DBMS connector.
Definition: SQLi.hpp:42
Binary data class.
Definition: ByteArray.hpp:30
Map< size_t, SQL_SIZE_T > bindParameterBASizeMap
A map for binary size.
XML is a class representing xml object.
Definition: XML.hpp:72
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
void prepare(const std::string &sql, const T &val, const _Args &...args)
Prepare a sql statement.