1 //
2 // Binder.h
3 //
4 // Library: Data/SQLite
5 // Package: SQLite
6 // Module:  Binder
7 //
8 // Definition of the Binder class.
9 //
10 // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
11 // and Contributors.
12 //
13 // SPDX-License-Identifier:	BSL-1.0
14 //
15 
16 
17 #ifndef Data_SQLite_Binder_INCLUDED
18 #define Data_SQLite_Binder_INCLUDED
19 
20 
21 #include "Poco/Data/SQLite/SQLite.h"
22 #include "Poco/Data/AbstractBinder.h"
23 #include "Poco/Data/LOB.h"
24 #include "Poco/Any.h"
25 #include "Poco/DynamicAny.h"
26 #include "sqlite3.h"
27 
28 
29 namespace Poco {
30 namespace Data {
31 namespace SQLite {
32 
33 
34 class SQLite_API Binder: public Poco::Data::AbstractBinder
35 	/// Binds placeholders in the sql query to the provided values. Performs data types mapping.
36 {
37 public:
38 	Binder(sqlite3_stmt* pStmt);
39 		/// Creates the Binder.
40 
41 	~Binder();
42 		/// Destroys the Binder.
43 
44 	void bind(std::size_t pos, const Poco::Int8 &val, Direction dir);
45 		/// Binds an Int8.
46 
47 	void bind(std::size_t pos, const Poco::UInt8 &val, Direction dir);
48 		/// Binds an UInt8.
49 
50 	void bind(std::size_t pos, const Poco::Int16 &val, Direction dir);
51 		/// Binds an Int16.
52 
53 	void bind(std::size_t pos, const Poco::UInt16 &val, Direction dir);
54 		/// Binds an UInt16.
55 
56 	void bind(std::size_t pos, const Poco::Int32 &val, Direction dir);
57 		/// Binds an Int32.
58 
59 	void bind(std::size_t pos, const Poco::UInt32 &val, Direction dir);
60 		/// Binds an UInt32.
61 
62 	void bind(std::size_t pos, const Poco::Int64 &val, Direction dir);
63 		/// Binds an Int64.
64 
65 	void bind(std::size_t pos, const Poco::UInt64 &val, Direction dir);
66 		/// Binds an UInt64.
67 
68 #ifndef POCO_INT64_IS_LONG
69 	void bind(std::size_t pos, const long &val, Direction dir);
70 		/// Binds a long
71 
72 	void bind(std::size_t pos, const unsigned long &val, Direction dir);
73 		/// Binds an unsigned long
74 #endif
75 
76 	void bind(std::size_t pos, const bool &val, Direction dir);
77 		/// Binds a boolean.
78 
79 	void bind(std::size_t pos, const float &val, Direction dir);
80 		/// Binds a float.
81 
82 	void bind(std::size_t pos, const double &val, Direction dir);
83 		/// Binds a double.
84 
85 	void bind(std::size_t pos, const char &val, Direction dir);
86 		/// Binds a single character.
87 
88 	void bind(std::size_t pos, const char* const &pVal, Direction dir);
89 		/// Binds a const char ptr.
90 
91 	void bind(std::size_t pos, const std::string& val, Direction dir);
92 		/// Binds a string.
93 
94 	void bind(std::size_t pos, const Poco::Data::BLOB& val, Direction dir);
95 		/// Binds a BLOB.
96 
97 	void bind(std::size_t pos, const Poco::Data::CLOB& val, Direction dir);
98 		/// Binds a CLOB.
99 
100 	void bind(std::size_t pos, const Date& val, Direction dir);
101 		/// Binds a Date.
102 
103 	void bind(std::size_t pos, const Time& val, Direction dir);
104 		/// Binds a Time.
105 
106 	void bind(std::size_t pos, const DateTime& val, Direction dir);
107 		/// Binds a DateTime.
108 
109 	void bind(std::size_t pos, const NullData& val, Direction dir);
110 		/// Binds a null.
111 
112 private:
113 	void checkReturn(int rc);
114 		/// Checks the SQLite return code and throws an appropriate exception
115 		/// if error has occurred.
116 
117 	template <typename T>
bindLOB(std::size_t pos,const Poco::Data::LOB<T> & val,Direction dir)118 	void bindLOB(std::size_t pos, const Poco::Data::LOB<T>& val, Direction dir)
119 	{
120 		// convert a blob to a an unsigned char* array
121 		const T* pData = reinterpret_cast<const T*>(val.rawContent());
122 		int valSize = static_cast<int>(val.size());
123 
124 		int rc = sqlite3_bind_blob(_pStmt, static_cast<int>(pos), pData, valSize, SQLITE_STATIC); // no deep copy, do not free memory
125 		checkReturn(rc);
126 	}
127 
128 	sqlite3_stmt* _pStmt;
129 };
130 
131 
132 //
133 // inlines
134 //
bind(std::size_t pos,const Poco::Int8 & val,Direction dir)135 inline void Binder::bind(std::size_t pos, const Poco::Int8 &val, Direction dir)
136 {
137 	Poco::Int32 tmp = val;
138 	bind(pos, tmp, dir);
139 }
140 
141 
bind(std::size_t pos,const Poco::UInt8 & val,Direction dir)142 inline void Binder::bind(std::size_t pos, const Poco::UInt8 &val, Direction dir)
143 {
144 	Poco::Int32 tmp = val;
145 	bind(pos, tmp, dir);
146 }
147 
148 
bind(std::size_t pos,const Poco::Int16 & val,Direction dir)149 inline void Binder::bind(std::size_t pos, const Poco::Int16 &val, Direction dir)
150 {
151 	Poco::Int32 tmp = val;
152 	bind(pos, tmp, dir);
153 }
154 
155 
bind(std::size_t pos,const Poco::UInt16 & val,Direction dir)156 inline void Binder::bind(std::size_t pos, const Poco::UInt16 &val, Direction dir)
157 {
158 	Poco::Int32 tmp = val;
159 	bind(pos, tmp, dir);
160 }
161 
162 
bind(std::size_t pos,const Poco::UInt32 & val,Direction dir)163 inline void Binder::bind(std::size_t pos, const Poco::UInt32 &val, Direction dir)
164 {
165 	Poco::Int32 tmp = static_cast<Poco::Int32>(val);
166 	bind(pos, tmp, dir);
167 }
168 
169 
bind(std::size_t pos,const Poco::UInt64 & val,Direction dir)170 inline void Binder::bind(std::size_t pos, const Poco::UInt64 &val, Direction dir)
171 {
172 	Poco::Int64 tmp = static_cast<Poco::Int64>(val);
173 	bind(pos, tmp, dir);
174 }
175 
176 
bind(std::size_t pos,const bool & val,Direction dir)177 inline void Binder::bind(std::size_t pos, const bool &val, Direction dir)
178 {
179 	Poco::Int32 tmp = (val ? 1 : 0);
180 	bind(pos, tmp, dir);
181 }
182 
183 
bind(std::size_t pos,const float & val,Direction dir)184 inline void Binder::bind(std::size_t pos, const float &val, Direction dir)
185 {
186 	double tmp = val;
187 	bind(pos, tmp, dir);
188 }
189 
190 
bind(std::size_t pos,const char & val,Direction dir)191 inline void Binder::bind(std::size_t pos, const char &val, Direction dir)
192 {
193 	Poco::Int32 tmp = val;
194 	bind(pos, tmp, dir);
195 }
196 
197 
bind(std::size_t pos,const char * const & pVal,Direction dir)198 inline void Binder::bind(std::size_t pos, const char* const &pVal, Direction dir)
199 {
200 	std::string val(pVal);
201 	bind(pos, val, dir);
202 }
203 
204 
bind(std::size_t pos,const Poco::Data::BLOB & val,Direction dir)205 inline void Binder::bind(std::size_t pos, const Poco::Data::BLOB& val, Direction dir)
206 {
207 	bindLOB<Poco::Data::BLOB::ValueType>(pos, val, dir);
208 }
209 
210 
bind(std::size_t pos,const Poco::Data::CLOB & val,Direction dir)211 inline void Binder::bind(std::size_t pos, const Poco::Data::CLOB& val, Direction dir)
212 {
213 	bindLOB<Poco::Data::CLOB::ValueType>(pos, val, dir);
214 }
215 
216 
217 } } } // namespace Poco::Data::SQLite
218 
219 
220 #endif // Data_SQLite_Binder_INCLUDED
221