|
16 | 16 | #include <map> |
17 | 17 | #include <climits> // For INT_MAX |
18 | 18 |
|
| 19 | +// some macros are taken from https://github.com/nemequ/hedley/blob/master/hedley.h , it was public domain that time |
| 20 | +#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) ||\ |
| 21 | +(defined(__INTEL_COMPILER) && __INTEL_COMPILER > 1600) ||\ |
| 22 | +(defined(__ARMCC_VERSION) && __ARMCC_VERSION > 4010000) ||\ |
| 23 | +(\ |
| 24 | + defined(__TI_COMPILER_VERSION__) && (\ |
| 25 | + __TI_COMPILER_VERSION__ > 8003000 ||\ |
| 26 | + (__TI_COMPILER_VERSION__ > 7003000 && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))\ |
| 27 | + )\ |
| 28 | +) |
| 29 | + #if !defined(SQLITECPP_PURE_FUNC) and __has_attribute(const) |
| 30 | + #define SQLITECPP_PURE_FUNC __attribute__((const)) |
| 31 | + #endif |
| 32 | +#endif |
| 33 | +#if !defined(SQLITECPP_PURE_FUNC) |
| 34 | + #define SQLITECPP_PURE_FUNC |
| 35 | +#endif |
| 36 | + |
| 37 | + |
19 | 38 | // Forward declarations to avoid inclusion of <sqlite3.h> in a header |
20 | 39 | struct sqlite3; |
21 | 40 | struct sqlite3_stmt; |
@@ -116,6 +135,10 @@ class Statement |
116 | 135 | // instead of being copied. |
117 | 136 | // => if you know what you are doing, use bindNoCopy() instead of bind() |
118 | 137 |
|
| 138 | + SQLITECPP_PURE_FUNC |
| 139 | + int getIndex(const char * const apName); |
| 140 | + |
| 141 | + |
119 | 142 | /** |
120 | 143 | * @brief Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
121 | 144 | */ |
@@ -197,203 +220,72 @@ class Statement |
197 | 220 | * @see clearBindings() to set all bound parameters to NULL. |
198 | 221 | */ |
199 | 222 | void bind(const int aIndex); |
200 | | - |
201 | | - /** |
202 | | - * @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
203 | | - */ |
204 | | - void bind(const char* apName, const int aValue); |
205 | | - /** |
206 | | - * @brief Bind a 32bits unsigned int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
207 | | - */ |
208 | | - void bind(const char* apName, const unsigned aValue); |
209 | | - |
210 | | -#if (LONG_MAX == INT_MAX) // 4 bytes "long" type means the data model is ILP32 or LLP64 (Win64 Visual C++ and MinGW) |
211 | | - /** |
212 | | - * @brief Bind a 32bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
213 | | - */ |
214 | | - void bind(const char* apName, const long aValue) |
| 223 | + |
| 224 | + template<typename T> |
| 225 | + inline void bind(const char* apName, const T &aValue) |
215 | 226 | { |
216 | | - bind(apName, static_cast<int>(aValue)); |
| 227 | + bind(getIndex(apName), aValue); |
217 | 228 | } |
218 | | -#else // 8 bytes "long" type means the data model is LP64 (Most Unix-like, Windows when using Cygwin; z/OS) |
219 | | - /** |
220 | | - * @brief Bind a 64bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
221 | | - */ |
222 | | - void bind(const char* apName, const long aValue) |
| 229 | + |
| 230 | + template<typename T> inline void bindNoCopy(const char* apName, const T& aValue) |
223 | 231 | { |
224 | | - bind(apName, static_cast<long long>(aValue)); |
| 232 | + bindNoCopy(getIndex(apName), aValue); |
225 | 233 | } |
226 | | -#endif |
227 | | - /** |
228 | | - * @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
229 | | - */ |
230 | | - void bind(const char* apName, const long long aValue); |
231 | | - /** |
232 | | - * @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
233 | | - */ |
234 | | - void bind(const char* apName, const double aValue); |
235 | | - /** |
236 | | - * @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
237 | | - * |
238 | | - * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use |
239 | | - */ |
240 | | - void bind(const char* apName, const std::string& aValue); |
241 | | - /** |
242 | | - * @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
243 | | - * |
244 | | - * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use |
245 | | - */ |
246 | | - void bind(const char* apName, const char* apValue); |
247 | | - /** |
248 | | - * @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
249 | | - * |
250 | | - * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use |
251 | | - */ |
252 | | - void bind(const char* apName, const void* apValue, const int aSize); |
253 | | - /** |
254 | | - * @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
255 | | - * |
256 | | - * The string can contain null characters as it is binded using its size. |
257 | | - * |
258 | | - * @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement. |
259 | | - */ |
260 | | - void bindNoCopy(const char* apName, const std::string& aValue); |
261 | | - /** |
262 | | - * @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
263 | | - * |
264 | | - * Main usage is with null-terminated literal text (aka in code static strings) |
265 | | - * |
266 | | - * @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement. |
267 | | - */ |
268 | | - void bindNoCopy(const char* apName, const char* apValue); |
269 | | - /** |
270 | | - * @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
271 | | - * |
272 | | - * @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement. |
273 | | - */ |
274 | | - void bindNoCopy(const char* apName, const void* apValue, const int aSize); |
275 | | - /** |
276 | | - * @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
277 | | - * |
278 | | - * @see clearBindings() to set all bound parameters to NULL. |
279 | | - */ |
280 | | - void bind(const char* apName); // bind NULL value |
281 | | - |
282 | 234 |
|
283 | | - /** |
284 | | - * @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
285 | | - */ |
286 | | - inline void bind(const std::string& aName, const int aValue) |
287 | | - { |
288 | | - bind(aName.c_str(), aValue); |
| 235 | + template<typename T, class = typename std::enable_if<std::is_same<T, const char>::value || std::is_same<T, const void>::value>::type> |
| 236 | + void bind(const char* apName, T* apValue){ |
| 237 | + bind(getIndex(apName), apValue); |
289 | 238 | } |
290 | | - /** |
291 | | - * @brief Bind a 32bits unsigned int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
292 | | - */ |
293 | | - inline void bind(const std::string& aName, const unsigned aValue) |
294 | | - { |
295 | | - bind(aName.c_str(), aValue); |
| 239 | + template<typename T, class = typename std::enable_if<std::is_same<T, const char>::value || std::is_same<T, const void>::value>::type> |
| 240 | + void bindNoCopy(const char* apName, T* apValue){ |
| 241 | + bindNoCopy(getIndex(apName), apValue); |
296 | 242 | } |
297 | | - |
298 | | -#if (LONG_MAX == INT_MAX) // 4 bytes "long" type means the data model is ILP32 or LLP64 (Win64 Visual C++ and MinGW) |
299 | | - /** |
300 | | - * @brief Bind a 32bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
301 | | - */ |
302 | | - void bind(const std::string& aName, const long aValue) |
303 | | - { |
304 | | - bind(aName.c_str(), static_cast<int>(aValue)); |
305 | | - } |
306 | | -#else // 8 bytes "long" type means the data model is LP64 (Most Unix-like, Windows when using Cygwin; z/OS) |
307 | | - /** |
308 | | - * @brief Bind a 64bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
309 | | - */ |
310 | | - void bind(const std::string& aName, const long aValue) |
311 | | - { |
312 | | - bind(aName.c_str(), static_cast<long long>(aValue)); |
313 | | - } |
314 | | -#endif |
315 | | - /** |
316 | | - * @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
317 | | - */ |
318 | | - inline void bind(const std::string& aName, const long long aValue) |
319 | | - { |
320 | | - bind(aName.c_str(), aValue); |
321 | | - } |
322 | | - /** |
323 | | - * @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
324 | | - */ |
325 | | - inline void bind(const std::string& aName, const double aValue) |
326 | | - { |
327 | | - bind(aName.c_str(), aValue); |
| 243 | + |
| 244 | + template<typename T, class = typename std::enable_if<std::is_same<T, const char>::value || std::is_same<T, const void>::value>::type> |
| 245 | + void bind(const char* apName, T* apValue, const int aSize){ |
| 246 | + bind(getIndex(apName), apValue, aSize); |
328 | 247 | } |
329 | | - /** |
330 | | - * @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
331 | | - * |
332 | | - * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use |
333 | | - */ |
334 | | - inline void bind(const std::string& aName, const std::string& aValue) |
335 | | - { |
336 | | - bind(aName.c_str(), aValue); |
| 248 | + template<typename T, class = typename std::enable_if<std::is_same<T, const char>::value || std::is_same<T, const void>::value>::type> |
| 249 | + void bindNoCopy(const char* apName, T* apValue, const int aSize){ |
| 250 | + bindNoCopy(getIndex(apName), apValue, aSize); |
337 | 251 | } |
| 252 | + |
| 253 | + |
338 | 254 | /** |
339 | | - * @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
| 255 | + * @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
340 | 256 | * |
341 | | - * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use |
| 257 | + * @see clearBindings() to set all bound parameters to NULL. |
342 | 258 | */ |
343 | | - inline void bind(const std::string& aName, const char* apValue) |
| 259 | + void bind(const char* apName) // bind NULL value |
344 | 260 | { |
345 | | - bind(aName.c_str(), apValue); |
| 261 | + bind(getIndex(apName)); |
346 | 262 | } |
347 | | - /** |
348 | | - * @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
349 | | - * |
350 | | - * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use |
351 | | - */ |
352 | | - inline void bind(const std::string& aName, const void* apValue, const int aSize) |
353 | | - { |
354 | | - bind(aName.c_str(), apValue, aSize); |
| 263 | + |
| 264 | + template<typename T> |
| 265 | + void bind(const std::string& aName, T &v){ |
| 266 | + bind(aName.c_str(), v); |
355 | 267 | } |
356 | | - /** |
357 | | - * @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
358 | | - * |
359 | | - * The string can contain null characters as it is binded using its size. |
360 | | - * |
361 | | - * @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement. |
362 | | - */ |
363 | | - inline void bindNoCopy(const std::string& aName, const std::string& aValue) |
364 | | - { |
365 | | - bindNoCopy(aName.c_str(), aValue); |
| 268 | + |
| 269 | + template<typename T, class = typename std::enable_if<std::is_same<T, const char>::value || std::is_same<T, const void>::value>::type> |
| 270 | + inline void bind(const std::string& aName, T* v){ |
| 271 | + bind(aName.c_str(), v); |
366 | 272 | } |
367 | | - /** |
368 | | - * @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
369 | | - * |
370 | | - * Main usage is with null-terminated literal text (aka in code static strings) |
371 | | - * |
372 | | - * @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement. |
373 | | - */ |
374 | | - inline void bindNoCopy(const std::string& aName, const char* apValue) |
375 | | - { |
376 | | - bindNoCopy(aName.c_str(), apValue); |
| 273 | + |
| 274 | + template<typename T, class = typename std::enable_if<std::is_same<T, const char>::value || std::is_same<T, const void>::value>::type> |
| 275 | + inline void bind(const std::string& aName, T* v, const int aSize){ |
| 276 | + bind(aName.c_str(), v, aSize); |
377 | 277 | } |
378 | | - /** |
379 | | - * @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
380 | | - * |
381 | | - * @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement. |
382 | | - */ |
383 | | - inline void bindNoCopy(const std::string& aName, const void* apValue, const int aSize) |
384 | | - { |
385 | | - bindNoCopy(aName.c_str(), apValue, aSize); |
| 278 | + |
| 279 | + template<typename T, class = typename std::enable_if<std::is_same<T, const char>::value || std::is_same<T, const void>::value>::type> |
| 280 | + inline void bindNoCopy(const std::string& aName, T* v){ |
| 281 | + bindNoCopy(aName.c_str(), v); |
386 | 282 | } |
387 | | - /** |
388 | | - * @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) |
389 | | - * |
390 | | - * @see clearBindings() to set all bound parameters to NULL. |
391 | | - */ |
392 | | - inline void bind(const std::string& aName) // bind NULL value |
393 | | - { |
394 | | - bind(aName.c_str()); |
| 283 | + |
| 284 | + template<typename T, class = typename std::enable_if<std::is_same<T, const char>::value || std::is_same<T, const void>::value>::type> |
| 285 | + inline void bindNoCopy(const std::string& aName, T* v, const int aSize){ |
| 286 | + bindNoCopy(aName.c_str(), v, aSize); |
395 | 287 | } |
396 | | - |
| 288 | + |
397 | 289 | //////////////////////////////////////////////////////////////////////////// |
398 | 290 |
|
399 | 291 | /** |
|
0 commit comments