bindutils.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_BINDUTILS_H
30 #define TNTDB_MYSQL_BINDUTILS_H
31 
32 #include <mysql.h>
33 #include <string>
34 #include <stdint.h>
35 
36 namespace tntdb
37 {
38  class Blob;
39  class Date;
40  class Time;
41  class Datetime;
42  class Decimal;
43 
44  namespace mysql
45  {
46  void setNull(MYSQL_BIND& value);
47  void setBool(MYSQL_BIND& value, bool data);
48  void setShort(MYSQL_BIND& value, short data);
49  void setInt(MYSQL_BIND& value, int data);
50  void setLong(MYSQL_BIND& value, long data);
51  void setUnsignedShort(MYSQL_BIND& value, unsigned short data);
52  void setUnsigned(MYSQL_BIND& value, unsigned data);
53  void setUnsignedLong(MYSQL_BIND& value, unsigned long data);
54  void setInt32(MYSQL_BIND& value, int32_t data);
55  void setUnsigned32(MYSQL_BIND& value, uint32_t data);
56  void setInt64(MYSQL_BIND& value, int64_t data);
57  void setUnsigned64(MYSQL_BIND& value, uint64_t data);
58  void setDecimal(MYSQL_BIND& value, unsigned long& length, const Decimal& data);
59  void setFloat(MYSQL_BIND& value, float data);
60  void setDouble(MYSQL_BIND& value, double data);
61  void setChar(MYSQL_BIND& value, unsigned long& length, char data);
62  void setString(MYSQL_BIND& value, unsigned long& length, const char* data);
63  void setString(MYSQL_BIND& value, unsigned long& length, const std::string& data);
64  void setBlob(MYSQL_BIND& value, unsigned long& length, const Blob& data);
65  void setDate(MYSQL_BIND& value, const Date& data);
66  void setTime(MYSQL_BIND& value, const Time& data);
67  void setDatetime(MYSQL_BIND& value, const Datetime& data);
68 
69  bool isNull(const MYSQL_BIND& value);
70  bool getBool(const MYSQL_BIND& value);
71  short getShort(const MYSQL_BIND& value);
72  int getInt(const MYSQL_BIND& value);
73  long getLong(const MYSQL_BIND& value);
74  unsigned getUnsigned(const MYSQL_BIND& value);
75  unsigned short getUnsignedShort(const MYSQL_BIND& value);
76  unsigned long getUnsignedLong(const MYSQL_BIND& value);
77  int32_t getInt32(const MYSQL_BIND& value);
78  uint32_t getUnsigned32(const MYSQL_BIND& value);
79  int64_t getInt64(const MYSQL_BIND& value);
80  uint64_t getUnsigned64(const MYSQL_BIND& value);
81  Decimal getDecimal(const MYSQL_BIND& value);
82  long getLong(const MYSQL_BIND& value);
83  float getFloat(const MYSQL_BIND& value);
84  double getDouble(const MYSQL_BIND& value);
85  char getChar(const MYSQL_BIND& value);
86  void getString(const MYSQL_BIND& value, std::string& ret);
87  void getBlob(const MYSQL_BIND& value, Blob& ret);
88  Date getDate(const MYSQL_BIND& value);
89  Time getTime(const MYSQL_BIND& value);
90  Datetime getDatetime(const MYSQL_BIND& value);
91 
92  void transferValue(MYSQL_BIND& fromValue, MYSQL_BIND& toValue,
93  bool doRelease = true);
94  void copyValue(const MYSQL_BIND& fromValue, MYSQL_BIND& toValue,
95  bool doRelease = true);
96  void refValue(const MYSQL_BIND& fromValue, MYSQL_BIND& toValue,
97  bool doRelease = true);
98  void releaseValue(MYSQL_BIND& value);
99 
100  void release(MYSQL_BIND& bind);
101  void reserve(MYSQL_BIND& bind, unsigned long size);
102  void reserveKeep(MYSQL_BIND& bind, unsigned long size);
103 
104  template <typename c_type>
105  void setValue(MYSQL_BIND& bind, c_type value, enum_field_types mysql_type)
106  {
107  reserve(bind, sizeof(c_type));
108  *static_cast<c_type*>(bind.buffer) = value;
109  bind.buffer_type = mysql_type;
110  bind.is_null = 0;
111  }
112 
113  }
114 }
115 
116 #endif // TNTDB_MYSQL_BINDUTILS_H
117