Skip to content

Commit 883782f

Browse files
committed
Add support to bind parameters with various unicode encodings.
Support for char16_t, u16string & u16string_view. Support for char8_t, u8string & u8string_view. Support for wchar_t, wstring & wstring_view (on 2-byte platforms).
1 parent 8b71316 commit 883782f

File tree

3 files changed

+392
-0
lines changed

3 files changed

+392
-0
lines changed

include/SQLiteCpp/Statement.h

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,83 @@ class Statement
256256
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
257257
*/
258258
void bind(const int aIndex, const char* apValue);
259+
#ifdef __cpp_unicode_characters
260+
/**
261+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
262+
*
263+
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
264+
*/
265+
void bind(const int aIndex, const std::u16string& aValue);
266+
#if WCHAR_MAX == 0xffff
267+
/**
268+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
269+
*
270+
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
271+
*/
272+
void bind(const int aIndex, const std::wstring& aValue)
273+
{
274+
#ifdef __cpp_lib_string_view
275+
bind(aIndex, std::u16string_view(reinterpret_cast<const char16_t*>(aValue.data()), aValue.size()));
276+
#else
277+
bind(aIndex, std::u16string(reinterpret_cast<const char16_t*>(aValue.data()), aValue.size()));
278+
#endif
279+
}
280+
#endif
281+
#endif
282+
#ifdef __cpp_lib_string_view
283+
/**
284+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
285+
*
286+
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
287+
*/
288+
void bind(const int aIndex, const std::string_view aValue);
289+
#ifdef __cpp_char8_t
290+
/**
291+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
292+
*
293+
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
294+
*/
295+
void bind(const int aIndex, const std::u8string_view aValue)
296+
{
297+
bind(aIndex, std::string_view(reinterpret_cast<const char*>(aValue.data()), aValue.size()));
298+
}
299+
#endif
300+
/**
301+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
302+
*
303+
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
304+
*/
305+
void bind(const int aIndex, const std::u16string_view aValue);
306+
/**
307+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
308+
*
309+
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
310+
*/
311+
void bind(const int aIndex, const char16_t* aValue)
312+
{
313+
bind(aIndex, std::u16string_view(aValue));
314+
}
315+
#if WCHAR_MAX == 0xffff
316+
/**
317+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
318+
*
319+
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
320+
*/
321+
void bind(const int aIndex, const wchar_t* aValue)
322+
{
323+
bind(aIndex, std::u16string_view(reinterpret_cast<const char16_t*>(aValue)));
324+
}
325+
/**
326+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
327+
*
328+
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
329+
*/
330+
void bind(const int aIndex, const std::wstring_view aValue)
331+
{
332+
bind(aIndex, std::u16string_view(reinterpret_cast<const char16_t*>(aValue.data()), aValue.size()));
333+
}
334+
#endif
335+
#endif
259336
/**
260337
* @brief Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
261338
*
@@ -278,6 +355,95 @@ class Statement
278355
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
279356
*/
280357
void bindNoCopy(const int aIndex, const char* apValue);
358+
#ifdef __cpp_unicode_characters
359+
/**
360+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
361+
*
362+
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
363+
*/
364+
void bindNoCopy(const int aIndex, const std::u16string& aValue);
365+
#if WCHAR_MAX == 0xffff
366+
/**
367+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
368+
*
369+
* @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
370+
*/
371+
void bindNoCopy(const int aIndex, const std::wstring& aValue)
372+
{
373+
#if __cpp_lib_string_view
374+
bindNoCopy(aIndex, std::u16string_view(reinterpret_cast<const char16_t*>(aValue.data()), aValue.size()));
375+
#else
376+
bindNoCopy(aIndex, std::u16string(reinterpret_cast<const char16_t*>(aValue.data()), aValue.size()));
377+
#endif
378+
}
379+
#endif
380+
#endif
381+
#if __cpp_lib_string_view
382+
/**
383+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1).
384+
*
385+
* The string can contain null characters as it is binded using its size.
386+
*
387+
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
388+
*/
389+
void bindNoCopy(const int aIndex, const std::string_view aValue);
390+
#ifdef __cpp_char8_t
391+
/**
392+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1).
393+
*
394+
* The string can contain null characters as it is binded using its size.
395+
*
396+
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
397+
*/
398+
void bindNoCopy(const int aIndex, const std::u8string_view aValue)
399+
{
400+
bindNoCopy(aIndex, std::string_view(reinterpret_cast<const char*>(aValue.data()), aValue.size()));
401+
}
402+
#endif
403+
/**
404+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1).
405+
*
406+
* The string can contain null characters as it is binded using its size.
407+
*
408+
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
409+
*/
410+
void bindNoCopy(const int aIndex, const std::u16string_view aValue);
411+
/**
412+
* @brief Bind a string value to a 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+
void bindNoCopy(const int aIndex, const char16_t* aValue)
419+
{
420+
bindNoCopy(aIndex, std::u16string_view(aValue));
421+
}
422+
#if WCHAR_MAX == 0xffff
423+
/**
424+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1).
425+
*
426+
* The string can contain null characters as it is binded using its size.
427+
*
428+
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
429+
*/
430+
void bindNoCopy(const int aIndex, const wchar_t* aValue)
431+
{
432+
bindNoCopy(aIndex, std::u16string_view(reinterpret_cast<const char16_t*>(aValue)));
433+
}
434+
/**
435+
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1).
436+
*
437+
* The string can contain null characters as it is binded using its size.
438+
*
439+
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
440+
*/
441+
void bindNoCopy(const int aIndex, const std::wstring_view aValue)
442+
{
443+
bindNoCopy(aIndex, std::u16string_view(reinterpret_cast<const char16_t*>(aValue.data()), aValue.size()));
444+
}
445+
#endif
446+
#endif
281447
/**
282448
* @brief Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
283449
*
@@ -291,6 +457,28 @@ class Statement
291457
*/
292458
void bind(const int aIndex);
293459

460+
template<typename T>
461+
inline void bind(const char* apName, T&& aValue)
462+
{
463+
bind(getIndex(apName), std::forward<T>(aValue));
464+
}
465+
template<typename T>
466+
inline void bindNoCopy(const char* apName, T&& aValue)
467+
{
468+
bindNoCopy(getIndex(apName), std::forward<T>(aValue));
469+
}
470+
template<typename T>
471+
inline void bind(const std::string& aName, T&& aValue)
472+
{
473+
bind(getIndex(aName.c_str()), std::forward<T>(aValue));
474+
}
475+
template<typename T>
476+
inline void bindNoCopy(const std::string& aName, T&& aValue)
477+
{
478+
bindNoCopy(getIndex(aName.c_str()), std::forward<T>(aValue));
479+
}
480+
481+
294482
/**
295483
* @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
296484
*/

src/Statement.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,31 @@ void Statement::bind(const int aIndex, const char* apValue)
149149
check(ret);
150150
}
151151

152+
#ifdef __cpp_unicode_characters
153+
void Statement::bind(const int aIndex, const std::u16string& aValue)
154+
{
155+
const int ret = sqlite3_bind_text16(getPreparedStatement(), aIndex, aValue.data(),
156+
static_cast<int>(aValue.size() * sizeof(char16_t)), SQLITE_TRANSIENT);
157+
check(ret);
158+
}
159+
#endif
160+
161+
#ifdef __cpp_lib_string_view
162+
void Statement::bind(const int aIndex, const std::string_view aValue)
163+
{
164+
const int ret = sqlite3_bind_text(getPreparedStatement(), aIndex, aValue.data(),
165+
static_cast<int>(aValue.size()), SQLITE_TRANSIENT);
166+
check(ret);
167+
}
168+
169+
void Statement::bind(const int aIndex, const std::u16string_view aValue)
170+
{
171+
const int ret = sqlite3_bind_text16(getPreparedStatement(), aIndex, aValue.data(),
172+
static_cast<int>(aValue.size() * sizeof(char16_t)), SQLITE_TRANSIENT);
173+
check(ret);
174+
}
175+
#endif
176+
152177
// Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
153178
void Statement::bind(const int aIndex, const void* apValue, const int aSize)
154179
{
@@ -171,6 +196,31 @@ void Statement::bindNoCopy(const int aIndex, const char* apValue)
171196
check(ret);
172197
}
173198

199+
#ifdef __cpp_unicode_characters
200+
void Statement::bindNoCopy(const int aIndex, const std::u16string& aValue)
201+
{
202+
const int ret = sqlite3_bind_text16(getPreparedStatement(), aIndex, aValue.data(),
203+
static_cast<int>(aValue.size() * sizeof(char16_t)), SQLITE_STATIC);
204+
check(ret);
205+
}
206+
#endif
207+
208+
#ifdef __cpp_lib_string_view
209+
void Statement::bindNoCopy(const int aIndex, const std::string_view aValue)
210+
{
211+
const int ret = sqlite3_bind_text(getPreparedStatement(), aIndex, aValue.data(),
212+
static_cast<int>(aValue.size()), SQLITE_STATIC);
213+
check(ret);
214+
}
215+
216+
void Statement::bindNoCopy(const int aIndex, const std::u16string_view aValue)
217+
{
218+
const int ret = sqlite3_bind_text16(getPreparedStatement(), aIndex, aValue.data(),
219+
static_cast<int>(aValue.size() * sizeof(char16_t)), SQLITE_STATIC);
220+
check(ret);
221+
}
222+
#endif
223+
174224
// Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
175225
void Statement::bindNoCopy(const int aIndex, const void* apValue, const int aSize)
176226
{

0 commit comments

Comments
 (0)