statement.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005,2010 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef TNTDB_BITS_STATEMENT_H
30 #define TNTDB_BITS_STATEMENT_H
31 
32 #include <string>
33 #include <cxxtools/smartptr.h>
34 #include <tntdb/iface/istatement.h>
35 #include <tntdb/date.h>
36 #include <tntdb/time.h>
37 #include <tntdb/datetime.h>
38 #include <cxxtools/convert.h>
39 #include <tntdb/config.h>
40 #include <vector>
41 #include <list>
42 #include <deque>
43 #include <set>
44 
45 #if __cplusplus >= 201103L
46 
47 #include <forward_list>
48 #include <unordered_set>
49 #include <unordered_map>
50 
51 #endif
52 
53 
54 namespace tntdb
55 {
56  class Connection;
57  class Result;
58  class Row;
59  class Value;
60  class Date;
61  class Time;
62  class Datetime;
63 
72  class Statement
73  {
74  public:
77 
78  private:
80 
81  public:
82  Statement(IStatement* stmt = 0)
83  : _stmt(stmt)
84  { }
85 
88  { _stmt->clear(); return *this; }
89 
91  Statement& setNull(const std::string& col)
92  { _stmt->setNull(col); return *this; }
93 
95  Statement& setBool(const std::string& col, bool data)
96  { _stmt->setBool(col, data); return *this; }
97 
99  Statement& setShort(const std::string& col, short data)
100  { _stmt->setShort(col, data); return *this; }
101 
103  Statement& setInt(const std::string& col, int data)
104  { _stmt->setInt(col, data); return *this; }
105 
107  Statement& setLong(const std::string& col, long data)
108  { _stmt->setLong(col, data); return *this; }
109 
111  Statement& setUnsignedShort(const std::string& col, unsigned short data)
112  { _stmt->setUnsignedShort(col, data); return *this; }
113 
115  Statement& setUnsigned(const std::string& col, unsigned data)
116  { _stmt->setUnsigned(col, data); return *this; }
117 
119  Statement& setUnsignedLong(const std::string& col, unsigned long data)
120  { _stmt->setUnsignedLong(col, data); return *this; }
121 
123  Statement& setInt32(const std::string& col, int32_t data)
124  { _stmt->setInt32(col, data); return *this; }
125 
127  Statement& setUnsigned32(const std::string& col, uint32_t data)
128  { _stmt->setUnsigned32(col, data); return *this; }
129 
131  Statement& setInt64(const std::string& col, int64_t data)
132  { _stmt->setInt64(col, data); return *this; }
133 
135  Statement& setUnsigned64(const std::string& col, uint64_t data)
136  { _stmt->setUnsigned64(col, data); return *this; }
137 
139  Statement& setDecimal(const std::string& col, const Decimal& data)
140  { _stmt->setDecimal(col, data); return *this; }
141 
143  Statement& setFloat(const std::string& col, float data)
144  { _stmt->setFloat(col, data); return *this; }
145 
147  Statement& setDouble(const std::string& col, double data)
148  { _stmt->setDouble(col, data); return *this; }
149 
151  Statement& setChar(const std::string& col, char data)
152  { _stmt->setChar(col, data); return *this; }
153 
155  Statement& setString(const std::string& col, const std::string& data)
156  { _stmt->setString(col, data); return *this; }
157 
159  Statement& setString(const std::string& col, const char* data)
160  { data == 0 ? _stmt->setNull(col)
161  : _stmt->setString(col, data); return *this; }
162 
164  Statement& setUString(const std::string& col, const cxxtools::String& data)
165  { _stmt->setUString(col, data); return *this; }
166 
168  Statement& setBlob(const std::string& col, const Blob& data)
169  { _stmt->setBlob(col, data); return *this; }
170 
172  Statement& setDate(const std::string& col, const Date& data)
173  { data.isNull() ? _stmt->setNull(col)
174  : _stmt->setDate(col, data); return *this; }
175 
177  Statement& setTime(const std::string& col, const Time& data)
178  { data.isNull() ? _stmt->setNull(col)
179  : _stmt->setTime(col, data); return *this; }
180 
182  Statement& setDatetime(const std::string& col, const Datetime& data)
183  { data.isNull() ? _stmt->setNull(col)
184  : _stmt->setDatetime(col, data); return *this; }
185 
192  template <typename T>
193  Statement& set(const std::string& col, const T& data);
194 
215  template <typename Iterator>
216  Statement& set(const std::string& col, Iterator it1, Iterator it2);
217 
223  template <typename T>
224  Statement& setIf(const std::string& col, bool notNull, const T& data)
225  {
226  if (notNull)
227  set(col, data);
228  else
229  setNull(col);
230  return *this;
231  }
232 
235 
240  size_type execute();
241 
246  Result select();
247 
252  Row selectRow();
257  Value selectValue();
259 
261  const_iterator begin(unsigned fetchsize = 100) const;
262 
276  const_iterator end() const;
277 
279  bool operator!() const { return !_stmt; }
280 
283  const IStatement* getImpl() const { return &*_stmt; }
284  IStatement* getImpl() { return &*_stmt; }
286  };
287 
289  class Hostvar
290  {
291  private:
292  Statement& _stmt;
293  const std::string& _name;
294 
295  public:
296  Hostvar(Statement& stmt, const std::string& name)
297  : _stmt(stmt),
298  _name(name)
299  { }
300  Statement& getStatement() { return _stmt; }
301  const std::string& getName() { return _name; }
302 
303  void setNull()
304  { _stmt.setNull(_name); }
305  void setBool(bool data)
306  { _stmt.setBool(_name, data); }
307  void setInt(int data)
308  { _stmt.setInt(_name, data); }
309  void setLong(long data)
310  { _stmt.setLong(_name, data); }
311  void setUnsigned(unsigned data)
312  { _stmt.setUnsigned(_name, data); }
313  void setUnsignedLong(unsigned long data)
314  { _stmt.setUnsignedLong(_name, data); }
315  void setInt32(int32_t data)
316  { _stmt.setInt32(_name, data); }
317  void setUnsigned32(uint32_t data)
318  { _stmt.setUnsigned32(_name, data); }
319  void setInt64(int64_t data)
320  { _stmt.setInt64(_name, data); }
321  void setUnsigned64(uint64_t data)
322  { _stmt.setUnsigned64(_name, data); }
323  void setDecimal(const Decimal& data)
324  { _stmt.setDecimal(_name, data); }
325  void setFloat(float data)
326  { _stmt.setFloat(_name, data); }
327  void setDouble(double data)
328  { _stmt.setDouble(_name, data); }
329  void setChar(char data)
330  { _stmt.setChar(_name, data); }
331  void setString(const std::string& data)
332  { _stmt.setString(_name, data); }
333  void setString(const char* data)
334  { data == 0 ? _stmt.setNull(_name)
335  : _stmt.setString(_name, data); }
336  void setUString(const cxxtools::String& data)
337  { _stmt.setUString(_name, data); }
338  void setBlob(const Blob& data)
339  { _stmt.setBlob(_name, data); }
340  void setDate(const Date& data)
341  { data.isNull() ? _stmt.setNull(_name)
342  : _stmt.setDate(_name, data); }
343  void setTime(const Time& data)
344  { data.isNull() ? _stmt.setNull(_name)
345  : _stmt.setTime(_name, data); }
346  void setDatetime(const Datetime& data)
347  { data.isNull() ? _stmt.setNull(_name)
348  : _stmt.setDatetime(_name, data); }
349 
350  template <typename T>
351  void set(const T& data);
352  };
353 
356 
357  inline void operator<< (Hostvar& hostvar, bool data)
358  { hostvar.setBool(data); }
359 
360  inline void operator<< (Hostvar& hostvar, int data)
361  { hostvar.setInt(data); }
362 
363  inline void operator<< (Hostvar& hostvar, long data)
364  { hostvar.setLong(data); }
365 
366  inline void operator<< (Hostvar& hostvar, unsigned data)
367  { hostvar.setUnsigned(data); }
368 
369  inline void operator<< (Hostvar& hostvar, unsigned long data)
370  { hostvar.setUnsignedLong(data); }
371 
372 #if INT_INT32_T_CONFLICT != 1
373  inline void operator<< (Hostvar& hostvar, int32_t data)
374  { hostvar.setInt32(data); }
375 #endif
376 
377 #if UNSIGNED_UINT32_T_CONFLICT != 1
378  inline void operator<< (Hostvar& hostvar, uint32_t data)
379  { hostvar.setUnsigned32(data); }
380 #endif
381 
382 #if INT_INT64_T_CONFLICT != 1
383  inline void operator<< (Hostvar& hostvar, int64_t data)
384  { hostvar.setInt64(data); }
385 #endif
386 
387 #if UNSIGNED_UINT64_T_CONFLICT != 1
388  inline void operator<< (Hostvar& hostvar, uint64_t data)
389  { hostvar.setUnsigned64(data); }
390 #endif
391 
392  inline void operator<< (Hostvar& hostvar, const Decimal& data)
393  { hostvar.setDecimal(data); }
394 
395  inline void operator<< (Hostvar& hostvar, float data)
396  { hostvar.setFloat(data); }
397 
398  inline void operator<< (Hostvar& hostvar, double data)
399  { hostvar.setDouble(data); }
400 
401  inline void operator<< (Hostvar& hostvar, char data)
402  { hostvar.setChar(data); }
403 
404  inline void operator<< (Hostvar& hostvar, const std::string& data)
405  { hostvar.setString(data); }
406 
407  inline void operator<< (Hostvar& hostvar, const char* data)
408  { hostvar.setString(data); }
409 
410  inline void operator<< (Hostvar& hostvar, const cxxtools::String& data)
411  { hostvar.setUString(data); }
412 
413  inline void operator<< (Hostvar& hostvar, const Blob& data)
414  { hostvar.setBlob(data); }
415 
416  inline void operator<< (Hostvar& hostvar, const Date& data)
417  { hostvar.setDate(data); }
418 
419  inline void operator<< (Hostvar& hostvar, const Time& data)
420  { hostvar.setTime(data); }
421 
422  inline void operator<< (Hostvar& hostvar, const Datetime& data)
423  { hostvar.setDatetime(data); }
424 
425  template <typename T>
426  void operator<< (Hostvar& hostvar, const std::vector<T>& data)
427  { hostvar.getStatement().set(hostvar.getName(), data.begin(), data.end()); }
428 
429  template <typename T>
430  void operator<< (Hostvar& hostvar, const std::list<T>& data)
431  { hostvar.getStatement().set(hostvar.getName(), data.begin(), data.end()); }
432 
433  template <typename T>
434  void operator<< (Hostvar& hostvar, const std::deque<T>& data)
435  { hostvar.getStatement().set(hostvar.getName(), data.begin(), data.end()); }
436 
437  template <typename T>
438  void operator<< (Hostvar& hostvar, const std::set<T>& data)
439  { hostvar.getStatement().set(hostvar.getName(), data.begin(), data.end()); }
440 
441  template <typename T>
442  void operator<< (Hostvar& hostvar, const std::multiset<T>& data)
443  { hostvar.getStatement().set(hostvar.getName(), data.begin(), data.end()); }
444 
445 #if __cplusplus >= 201103L
446 
447  template <typename T>
448  void operator<< (Hostvar& hostvar, const std::forward_list<T>& data)
449  { hostvar.getStatement().set(hostvar.getName(), data.begin(), data.end()); }
450 
451  template <typename T>
452  void operator<< (Hostvar& hostvar, const std::unordered_set<T>& data)
453  { hostvar.getStatement().set(hostvar.getName(), data.begin(), data.end()); }
454 
455  template <typename T>
456  void operator<< (Hostvar& hostvar, const std::unordered_multiset<T>& data)
457  { hostvar.getStatement().set(hostvar.getName(), data.begin(), data.end()); }
458 
459 #endif
460 
462 
463  template <typename T>
464  Statement& Statement::set(const std::string& name, const T& data)
465  {
466  Hostvar h(*this, name);
467  h << data;
468  return *this;
469  }
470 
471  template <typename Iterator>
472  Statement& Statement::set(const std::string& col, Iterator it1, Iterator it2)
473  {
474  for (unsigned n = 0; it1 != it2; ++n, ++it1)
475  {
476  set(col + cxxtools::convert<std::string>(n), *it1);
477  }
478  return *this;
479  }
480 
481  template <typename T>
482  void Hostvar::set(const T& data)
483  { *this << data; }
484 }
485 
486 #endif // TNTDB_BITS_STATEMENT_H
487