bindvalues.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 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_MYSQL_BINDVALUES_H
30 #define TNTDB_MYSQL_BINDVALUES_H
31 
32 #include <mysql.h>
33 #include <string>
34 #include <tntdb/decimal.h>
35 #include <tntdb/mysql/bindutils.h>
36 
37 namespace tntdb
38 {
39  namespace mysql
40  {
41  class BindValues
42  {
43  unsigned valuesSize;
44  MYSQL_BIND* values;
45  struct BindAttributes
46  {
47  unsigned long length;
48  my_bool isNull;
49  std::string name;
50  BindAttributes()
51  : length(0),
52  isNull(true)
53  { }
54  }* bindAttributes;
55 
56  // non copyable
57  BindValues(const BindValues&);
58  BindValues& operator=(const BindValues&);
59 
60  public:
61  explicit BindValues(unsigned n);
62  BindValues();
63  ~BindValues();
64 
65  MYSQL_BIND* getMysqlBind() const { return values; }
66  void setSize(unsigned n);
67  unsigned getSize() const { return valuesSize; }
68 
69  void setNull(unsigned n)
70  { mysql::setNull(values[n]); }
71  void setBool(unsigned n, bool data)
72  { mysql::setBool(values[n], data); }
73  void setShort(unsigned n, short data)
74  { mysql::setShort(values[n], data); }
75  void setInt(unsigned n, int data)
76  { mysql::setInt(values[n], data); }
77  void setLong(unsigned n, int data)
78  { mysql::setLong(values[n], data); }
79  void setUnsignedShort(unsigned n, unsigned short data)
80  { mysql::setUnsignedShort(values[n], data); }
81  void setUnsigned(unsigned n, unsigned data)
82  { mysql::setUnsigned(values[n], data); }
83  void setUnsignedLong(unsigned n, unsigned data)
84  { mysql::setUnsignedLong(values[n], data); }
85  void setInt32(unsigned n, int32_t data)
86  { mysql::setInt32(values[n], data); }
87  void setUnsigned32(unsigned n, uint32_t data)
88  { mysql::setUnsigned32(values[n], data); }
89  void setInt64(unsigned n, int64_t data)
90  { mysql::setInt64(values[n], data); }
91  void setUnsigned64(unsigned n, uint64_t data)
92  { mysql::setUnsigned64(values[n], data); }
93  void setDecimal(unsigned n, const Decimal& data)
94  { mysql::setDecimal(values[n], bindAttributes[n].length, data); }
95  void setFloat(unsigned n, float data)
96  { mysql::setFloat(values[n], data); }
97  void setDouble(unsigned n, double data)
98  { mysql::setDouble(values[n], data); }
99  void setChar(unsigned n, char data)
100  { mysql::setChar(values[n], bindAttributes[n].length, data); }
101  void setString(unsigned n, const char* data)
102  { mysql::setString(values[n], bindAttributes[n].length, data); }
103  void setString(unsigned n, const std::string& data)
104  { mysql::setString(values[n], bindAttributes[n].length, data); }
105  void setBlob(unsigned n, const Blob& data)
106  { mysql::setBlob(values[n], bindAttributes[n].length, data); }
107  void setDate(unsigned n, const Date& data)
108  { mysql::setDate(values[n], data); }
109  void setTime(unsigned n, const Time& data)
110  { mysql::setTime(values[n], data); }
111  void setDatetime(unsigned n, const Datetime& data)
112  { mysql::setDatetime(values[n], data); }
113 
114  bool isNull(unsigned n) const
115  { return mysql::isNull(values[n]); }
116  bool getBool(unsigned n) const
117  { return mysql::getBool(values[n]); }
118  int getInt(unsigned n) const
119  { return mysql::getInt(values[n]); }
120  unsigned getUnsigned(unsigned n) const
121  { return mysql::getUnsigned(values[n]); }
122  int32_t getInt32(unsigned n) const
123  { return mysql::getInt32(values[n]); }
124  uint32_t getUnsigned32(unsigned n) const
125  { return mysql::getUnsigned32(values[n]); }
126  int64_t getInt64(unsigned n) const
127  { return mysql::getInt64(values[n]); }
128  uint64_t getUnsigned64(unsigned n) const
129  { return mysql::getUnsigned64(values[n]); }
130  Decimal getDecimal(unsigned n) const
131  { return mysql::getDecimal(values[n]); }
132  long getLong(unsigned n) const
133  { return mysql::getLong(values[n]); }
134  float getFloat(unsigned n) const
135  { return mysql::getFloat(values[n]); }
136  double getDouble(unsigned n) const
137  { return mysql::getDouble(values[n]); }
138  char getChar(unsigned n) const
139  { return mysql::getChar(values[n]); }
140  void getString(unsigned n, std::string& ret) const
141  { mysql::getString(values[n], ret); }
142 
143  const std::string& getName(unsigned n) const
144  { return bindAttributes[n].name; }
145 
146  void initOutBuffer(unsigned n, MYSQL_FIELD& f);
147  void clear();
148  };
149  }
150 }
151 
152 #endif // TNTDB_MYSQL_BINDVALUES_H
153