Skip to content

Commit a6eb4f4

Browse files
committed
Improved efficiency and code reuse with templates and pure functions
1 parent 741ffcd commit a6eb4f4

File tree

2 files changed

+51
-191
lines changed

2 files changed

+51
-191
lines changed

include/SQLiteCpp/Statement.h

Lines changed: 50 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -227,110 +227,52 @@ class Statement
227227
*/
228228
void bind(const int aIndex);
229229

230-
/**
231-
* @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
232-
*/
233-
void bind(const char* apName, const int aValue)
234-
{
235-
bind(getIndex(apName), aValue);
236-
}
237-
/**
238-
* @brief Bind a 32bits unsigned int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
239-
*/
240-
void bind(const char* apName, const unsigned aValue)
230+
template<typename T>
231+
inline void bind(const char* apName, const T &aValue)
241232
{
242233
bind(getIndex(apName), aValue);
243234
}
244235

245-
#if (LONG_MAX == INT_MAX) // 4 bytes "long" type means the data model is ILP32 or LLP64 (Win64 Visual C++ and MinGW)
246-
/**
247-
* @brief Bind a 32bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
248-
*/
249-
void bind(const char* apName, const long aValue)
236+
template<typename T> inline void bindNoCopy(const char* apName, const T& aValue)
250237
{
251-
bind(apName, static_cast<int>(aValue));
252-
}
253-
#else // 8 bytes "long" type means the data model is LP64 (Most Unix-like, Windows when using Cygwin; z/OS)
254-
/**
255-
* @brief Bind a 64bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
256-
*/
257-
void bind(const char* apName, const long aValue)
258-
{
259-
bind(apName, static_cast<long long>(aValue));
260-
}
261-
#endif
262-
/**
263-
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
264-
*/
265-
void bind(const char* apName, const long long aValue)
266-
{
267-
bind(getIndex(apName), aValue);
268-
}
269-
/**
270-
* @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
271-
*/
272-
void bind(const char* apName, const double aValue)
273-
{
274-
bind(getIndex(apName), aValue);
275-
}
276-
/**
277-
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
278-
*
279-
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
280-
*/
281-
void bind(const char* apName, const std::string& aValue)
282-
{
283-
bind(getIndex(apName), aValue);
238+
bindNoCopy(getIndex(apName), aValue);
284239
}
285-
/**
286-
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
287-
*
288-
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
289-
*/
290-
void bind(const char* apName, const char* apValue)
240+
241+
#if __cplusplus >= 201103L
242+
#define ENABLE_IF_CONST_CHAR_OR_VOID \
243+
template<\
244+
typename T\
245+
, class = typename std::enable_if<\
246+
std::is_same<T, const char>::value\
247+
|| std::is_same<T, const void>::value\
248+
>::type\
249+
>
250+
251+
252+
ENABLE_IF_CONST_CHAR_OR_VOID
253+
void bind(const char* apName, T* apValue)
291254
{
292255
bind(getIndex(apName), apValue);
293256
}
294-
/**
295-
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
296-
*
297-
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
298-
*/
299-
void bind(const char* apName, const void* apValue, const int aSize)
300-
{
301-
bind(getIndex(apName), apValue, aSize);
302-
}
303-
/**
304-
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
305-
*
306-
* The string can contain null characters as it is binded using its size.
307-
*
308-
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
309-
*/
310-
void bindNoCopy(const char* apName, const std::string& aValue)
257+
ENABLE_IF_CONST_CHAR_OR_VOID
258+
void bindNoCopy(const char* apName, T* apValue)
311259
{
312-
bindNoCopy(getIndex(apName), aValue);
260+
bindNoCopy(getIndex(apName), apValue);
313261
}
314-
/**
315-
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
316-
*
317-
* Main usage is with null-terminated literal text (aka in code static strings)
318-
*
319-
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
320-
*/
321-
void bindNoCopy(const char* apName, const char* apValue)
262+
263+
ENABLE_IF_CONST_CHAR_OR_VOID
264+
void bind(const char* apName, T* apValue, const int aSize)
322265
{
323-
bindNoCopy(getIndex(apName), apValue);
266+
bind(getIndex(apName), apValue, aSize);
324267
}
325-
/**
326-
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
327-
*
328-
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
329-
*/
330-
void bindNoCopy(const char* apName, const void* apValue, const int aSize)
268+
ENABLE_IF_CONST_CHAR_OR_VOID
269+
void bindNoCopy(const char* apName, T* apValue, const int aSize)
331270
{
332271
bindNoCopy(getIndex(apName), apValue, aSize);
333272
}
273+
#endif
274+
275+
334276
/**
335277
* @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
336278
*
@@ -341,120 +283,37 @@ class Statement
341283
bind(getIndex(apName));
342284
}
343285

344-
345-
/**
346-
* @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
347-
*/
348-
inline void bind(const std::string& aName, const int aValue)
286+
template<typename T>
287+
void bind(const std::string& aName, T &v)
349288
{
350-
bind(aName.c_str(), aValue);
351-
}
352-
/**
353-
* @brief Bind a 32bits unsigned int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
354-
*/
355-
inline void bind(const std::string& aName, const unsigned aValue)
356-
{
357-
bind(aName.c_str(), aValue);
289+
bind(aName.c_str(), v);
358290
}
359291

360-
#if (LONG_MAX == INT_MAX) // 4 bytes "long" type means the data model is ILP32 or LLP64 (Win64 Visual C++ and MinGW)
361-
/**
362-
* @brief Bind a 32bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
363-
*/
364-
void bind(const std::string& aName, const long aValue)
365-
{
366-
bind(aName.c_str(), static_cast<int>(aValue));
367-
}
368-
#else // 8 bytes "long" type means the data model is LP64 (Most Unix-like, Windows when using Cygwin; z/OS)
369-
/**
370-
* @brief Bind a 64bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
371-
*/
372-
void bind(const std::string& aName, const long aValue)
373-
{
374-
bind(aName.c_str(), static_cast<long long>(aValue));
375-
}
376-
#endif
377-
/**
378-
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
379-
*/
380-
inline void bind(const std::string& aName, const long long aValue)
381-
{
382-
bind(aName.c_str(), aValue);
383-
}
384-
/**
385-
* @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
386-
*/
387-
inline void bind(const std::string& aName, const double aValue)
388-
{
389-
bind(aName.c_str(), aValue);
390-
}
391-
/**
392-
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
393-
*
394-
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
395-
*/
396-
inline void bind(const std::string& aName, const std::string& aValue)
397-
{
398-
bind(aName.c_str(), aValue);
399-
}
400-
/**
401-
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
402-
*
403-
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
404-
*/
405-
inline void bind(const std::string& aName, const char* apValue)
406-
{
407-
bind(aName.c_str(), apValue);
408-
}
409-
/**
410-
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
411-
*
412-
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
413-
*/
414-
inline void bind(const std::string& aName, const void* apValue, const int aSize)
292+
#if __cplusplus >= 201103L
293+
ENABLE_IF_CONST_CHAR_OR_VOID
294+
inline void bind(const std::string& aName, T* v)
415295
{
416-
bind(aName.c_str(), apValue, aSize);
296+
bind(aName.c_str(), v);
417297
}
418-
/**
419-
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
420-
*
421-
* The string can contain null characters as it is binded using its size.
422-
*
423-
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
424-
*/
425-
inline void bindNoCopy(const std::string& aName, const std::string& aValue)
426-
{
427-
bindNoCopy(aName.c_str(), aValue);
428-
}
429-
/**
430-
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
431-
*
432-
* Main usage is with null-terminated literal text (aka in code static strings)
433-
*
434-
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
435-
*/
436-
inline void bindNoCopy(const std::string& aName, const char* apValue)
298+
299+
ENABLE_IF_CONST_CHAR_OR_VOID
300+
inline void bind(const std::string& aName, T* v, const int aSize)
437301
{
438-
bindNoCopy(aName.c_str(), apValue);
302+
bind(aName.c_str(), v, aSize);
439303
}
440-
/**
441-
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
442-
*
443-
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
444-
*/
445-
inline void bindNoCopy(const std::string& aName, const void* apValue, const int aSize)
304+
305+
ENABLE_IF_CONST_CHAR_OR_VOID
306+
inline void bindNoCopy(const std::string& aName, T* v)
446307
{
447-
bindNoCopy(aName.c_str(), apValue, aSize);
308+
bindNoCopy(aName.c_str(), v);
448309
}
449-
/**
450-
* @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
451-
*
452-
* @see clearBindings() to set all bound parameters to NULL.
453-
*/
454-
inline void bind(const std::string& aName) // bind NULL value
310+
311+
ENABLE_IF_CONST_CHAR_OR_VOID
312+
inline void bindNoCopy(const std::string& aName, T* v, const int aSize)
455313
{
456-
bind(aName.c_str());
314+
bindNoCopy(aName.c_str(), v, aSize);
457315
}
316+
#endif
458317

459318
////////////////////////////////////////////////////////////////////////////
460319

src/Statement.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ void Statement::clearBindings()
8383
check(ret);
8484
}
8585

86+
8687
int Statement::getIndex(const char * const apName)
8788
{
8889
return sqlite3_bind_parameter_index(mStmtPtr, apName);

0 commit comments

Comments
 (0)