Skip to content

Commit 2e0dc0d

Browse files
committed
Improved efficiency and code reuse with templates and pure functions
1 parent a054212 commit 2e0dc0d

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
@@ -220,110 +220,52 @@ class Statement
220220
*/
221221
void bind(const int aIndex);
222222

223-
/**
224-
* @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
225-
*/
226-
void bind(const char* apName, const int aValue)
227-
{
228-
bind(getIndex(apName), aValue);
229-
}
230-
/**
231-
* @brief Bind a 32bits unsigned 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 unsigned aValue)
223+
template<typename T>
224+
inline void bind(const char* apName, const T &aValue)
234225
{
235226
bind(getIndex(apName), aValue);
236227
}
237228

238-
#if (LONG_MAX == INT_MAX) // 4 bytes "long" type means the data model is ILP32 or LLP64 (Win64 Visual C++ and MinGW)
239-
/**
240-
* @brief Bind a 32bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
241-
*/
242-
void bind(const char* apName, const long aValue)
229+
template<typename T> inline void bindNoCopy(const char* apName, const T& aValue)
243230
{
244-
bind(apName, static_cast<int>(aValue));
245-
}
246-
#else // 8 bytes "long" type means the data model is LP64 (Most Unix-like, Windows when using Cygwin; z/OS)
247-
/**
248-
* @brief Bind a 64bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
249-
*/
250-
void bind(const char* apName, const long aValue)
251-
{
252-
bind(apName, static_cast<long long>(aValue));
253-
}
254-
#endif
255-
/**
256-
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
257-
*/
258-
void bind(const char* apName, const long long aValue)
259-
{
260-
bind(getIndex(apName), aValue);
261-
}
262-
/**
263-
* @brief Bind a double (64bits float) 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 double aValue)
266-
{
267-
bind(getIndex(apName), aValue);
268-
}
269-
/**
270-
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
271-
*
272-
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
273-
*/
274-
void bind(const char* apName, const std::string& aValue)
275-
{
276-
bind(getIndex(apName), aValue);
231+
bindNoCopy(getIndex(apName), aValue);
277232
}
278-
/**
279-
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
280-
*
281-
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
282-
*/
283-
void bind(const char* apName, const char* apValue)
233+
234+
#if __cplusplus >= 201103L
235+
#define ENABLE_IF_CONST_CHAR_OR_VOID \
236+
template<\
237+
typename T\
238+
, class = typename std::enable_if<\
239+
std::is_same<T, const char>::value\
240+
|| std::is_same<T, const void>::value\
241+
>::type\
242+
>
243+
244+
245+
ENABLE_IF_CONST_CHAR_OR_VOID
246+
void bind(const char* apName, T* apValue)
284247
{
285248
bind(getIndex(apName), apValue);
286249
}
287-
/**
288-
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
289-
*
290-
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
291-
*/
292-
void bind(const char* apName, const void* apValue, const int aSize)
293-
{
294-
bind(getIndex(apName), apValue, aSize);
295-
}
296-
/**
297-
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
298-
*
299-
* The string can contain null characters as it is binded using its size.
300-
*
301-
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
302-
*/
303-
void bindNoCopy(const char* apName, const std::string& aValue)
250+
ENABLE_IF_CONST_CHAR_OR_VOID
251+
void bindNoCopy(const char* apName, T* apValue)
304252
{
305-
bindNoCopy(getIndex(apName), aValue);
253+
bindNoCopy(getIndex(apName), apValue);
306254
}
307-
/**
308-
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
309-
*
310-
* Main usage is with null-terminated literal text (aka in code static strings)
311-
*
312-
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
313-
*/
314-
void bindNoCopy(const char* apName, const char* apValue)
255+
256+
ENABLE_IF_CONST_CHAR_OR_VOID
257+
void bind(const char* apName, T* apValue, const int aSize)
315258
{
316-
bindNoCopy(getIndex(apName), apValue);
259+
bind(getIndex(apName), apValue, aSize);
317260
}
318-
/**
319-
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
320-
*
321-
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
322-
*/
323-
void bindNoCopy(const char* apName, const void* apValue, const int aSize)
261+
ENABLE_IF_CONST_CHAR_OR_VOID
262+
void bindNoCopy(const char* apName, T* apValue, const int aSize)
324263
{
325264
bindNoCopy(getIndex(apName), apValue, aSize);
326265
}
266+
#endif
267+
268+
327269
/**
328270
* @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
329271
*
@@ -334,120 +276,37 @@ class Statement
334276
bind(getIndex(apName));
335277
}
336278

337-
338-
/**
339-
* @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
340-
*/
341-
inline void bind(const std::string& aName, const int aValue)
279+
template<typename T>
280+
void bind(const std::string& aName, T &v)
342281
{
343-
bind(aName.c_str(), aValue);
344-
}
345-
/**
346-
* @brief Bind a 32bits unsigned 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 unsigned aValue)
349-
{
350-
bind(aName.c_str(), aValue);
282+
bind(aName.c_str(), v);
351283
}
352284

353-
#if (LONG_MAX == INT_MAX) // 4 bytes "long" type means the data model is ILP32 or LLP64 (Win64 Visual C++ and MinGW)
354-
/**
355-
* @brief Bind a 32bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
356-
*/
357-
void bind(const std::string& aName, const long aValue)
358-
{
359-
bind(aName.c_str(), static_cast<int>(aValue));
360-
}
361-
#else // 8 bytes "long" type means the data model is LP64 (Most Unix-like, Windows when using Cygwin; z/OS)
362-
/**
363-
* @brief Bind a 64bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
364-
*/
365-
void bind(const std::string& aName, const long aValue)
366-
{
367-
bind(aName.c_str(), static_cast<long long>(aValue));
368-
}
369-
#endif
370-
/**
371-
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
372-
*/
373-
inline void bind(const std::string& aName, const long long aValue)
374-
{
375-
bind(aName.c_str(), aValue);
376-
}
377-
/**
378-
* @brief Bind a double (64bits float) 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 double aValue)
381-
{
382-
bind(aName.c_str(), aValue);
383-
}
384-
/**
385-
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
386-
*
387-
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
388-
*/
389-
inline void bind(const std::string& aName, const std::string& aValue)
390-
{
391-
bind(aName.c_str(), aValue);
392-
}
393-
/**
394-
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
395-
*
396-
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
397-
*/
398-
inline void bind(const std::string& aName, const char* apValue)
399-
{
400-
bind(aName.c_str(), apValue);
401-
}
402-
/**
403-
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
404-
*
405-
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
406-
*/
407-
inline void bind(const std::string& aName, const void* apValue, const int aSize)
285+
#if __cplusplus >= 201103L
286+
ENABLE_IF_CONST_CHAR_OR_VOID
287+
inline void bind(const std::string& aName, T* v)
408288
{
409-
bind(aName.c_str(), apValue, aSize);
289+
bind(aName.c_str(), v);
410290
}
411-
/**
412-
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
413-
*
414-
* The string can contain null characters as it is binded using its size.
415-
*
416-
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
417-
*/
418-
inline void bindNoCopy(const std::string& aName, const std::string& aValue)
419-
{
420-
bindNoCopy(aName.c_str(), aValue);
421-
}
422-
/**
423-
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
424-
*
425-
* Main usage is with null-terminated literal text (aka in code static strings)
426-
*
427-
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
428-
*/
429-
inline void bindNoCopy(const std::string& aName, const char* apValue)
291+
292+
ENABLE_IF_CONST_CHAR_OR_VOID
293+
inline void bind(const std::string& aName, T* v, const int aSize)
430294
{
431-
bindNoCopy(aName.c_str(), apValue);
295+
bind(aName.c_str(), v, aSize);
432296
}
433-
/**
434-
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
435-
*
436-
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
437-
*/
438-
inline void bindNoCopy(const std::string& aName, const void* apValue, const int aSize)
297+
298+
ENABLE_IF_CONST_CHAR_OR_VOID
299+
inline void bindNoCopy(const std::string& aName, T* v)
439300
{
440-
bindNoCopy(aName.c_str(), apValue, aSize);
301+
bindNoCopy(aName.c_str(), v);
441302
}
442-
/**
443-
* @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
444-
*
445-
* @see clearBindings() to set all bound parameters to NULL.
446-
*/
447-
inline void bind(const std::string& aName) // bind NULL value
303+
304+
ENABLE_IF_CONST_CHAR_OR_VOID
305+
inline void bindNoCopy(const std::string& aName, T* v, const int aSize)
448306
{
449-
bind(aName.c_str());
307+
bindNoCopy(aName.c_str(), v, aSize);
450308
}
309+
#endif
451310

452311
////////////////////////////////////////////////////////////////////////////
453312

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)