Add bindNoCopy() for C-style null-terminated text strings

This commit is contained in:
Sébastien Rombauts 2016-07-03 16:55:27 +02:00
parent e7384b30a8
commit 9c03f4e86b
2 changed files with 97 additions and 60 deletions

View File

@ -81,8 +81,7 @@ public:
/** /**
* @brief Clears away all the bindings of a prepared statement. * @brief Clears away all the bindings of a prepared statement.
* *
* Contrary to the intuition of many, reset() does not reset * Contrary to the intuition of many, reset() does not reset the bindings on a prepared statement.
* the bindings on a prepared statement.
* Use this routine to reset all parameters to NULL. * Use this routine to reset all parameters to NULL.
*/ */
void clearBindings(); // throw(SQLite::Exception) void clearBindings(); // throw(SQLite::Exception)
@ -102,7 +101,7 @@ public:
// This is under-optimized for static data (a static text define in code) // This is under-optimized for static data (a static text define in code)
// as well as for dynamic allocated buffer which could be transfer to sqlite // as well as for dynamic allocated buffer which could be transfer to sqlite
// instead of being copied. // instead of being copied.
// => if you now what you are doing, use bindNoCopy() // => if you know what you are doing, use bindNoCopy() instead of bind()
/** /**
* @brief Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
@ -123,39 +122,43 @@ public:
/** /**
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
void bind(const int aIndex, const std::string& aValue); void bind(const int aIndex, const std::string& aValue);
/**
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @note This uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
*/
void bindNoCopy(const int aIndex, const std::string& aValue);
/** /**
* @brief Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
void bind(const int aIndex, const char* apValue); void bind(const int aIndex, const char* apValue);
/** /**
* @brief Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
void bind(const int aIndex, const void* apValue, const int aSize); void bind(const int aIndex, const void* apValue, const int aSize);
/**
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1).
*
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
*/
void bindNoCopy(const int aIndex, const std::string& aValue);
/**
* @brief Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
*/
void bindNoCopy(const int aIndex, const char* apValue);
/** /**
* @brief Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_STATIC flag, NOT making a copy of the data. It must exist while executing the statement. * @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
*/ */
void bindNoCopy(const int aIndex, const void* apValue, const int aSize); void bindNoCopy(const int aIndex, const void* apValue, const int aSize);
/** /**
* @brief Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @see clearBindings() to set all bound parameters to NULL.
*/ */
void bind(const int aIndex); void bind(const int aIndex);
@ -178,35 +181,43 @@ public:
/** /**
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
void bind(const char* apName, const std::string& aValue); void bind(const char* apName, const std::string& aValue);
/**
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @note This uses the SQLITE_STATIC flag, NOT making a copy of the data.
*/
void bindNoCopy(const char* apName, const std::string& aValue);
/** /**
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
void bind(const char* apName, const char* apValue); void bind(const char* apName, const char* apValue);
/** /**
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
void bind(const char* apName, const void* apValue, const int aSize); void bind(const char* apName, const void* apValue, const int aSize);
/**
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
*/
void bindNoCopy(const char* apName, const std::string& aValue);
/**
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
*/
void bindNoCopy(const char* apName, const char* apValue);
/** /**
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_STATIC flag, making a copy of the data. * @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
*/ */
void bindNoCopy(const char* apName, const void* apValue, const int aSize); void bindNoCopy(const char* apName, const void* apValue, const int aSize);
/** /**
* @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @see clearBindings() to set all bound parameters to NULL.
*/ */
void bind(const char* apName); // bind NULL value void bind(const char* apName); // bind NULL value
@ -242,25 +253,16 @@ public:
/** /**
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
inline void bind(const std::string& aName, const std::string& aValue) inline void bind(const std::string& aName, const std::string& aValue)
{ {
bind(aName.c_str(), aValue); bind(aName.c_str(), aValue);
} }
/**
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @note This uses the SQLITE_STATIC flag, NOT making a copy of the data.
*/
inline void bindNoCopy(const std::string& aName, const std::string& aValue)
{
bindNoCopy(aName.c_str(), aValue);
}
/** /**
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
inline void bind(const std::string& aName, const char* apValue) inline void bind(const std::string& aName, const char* apValue)
{ {
@ -269,16 +271,34 @@ public:
/** /**
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
inline void bind(const std::string& aName, const void* apValue, const int aSize) inline void bind(const std::string& aName, const void* apValue, const int aSize)
{ {
bind(aName.c_str(), apValue, aSize); bind(aName.c_str(), apValue, aSize);
} }
/**
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
*/
inline void bindNoCopy(const std::string& aName, const std::string& aValue)
{
bindNoCopy(aName.c_str(), aValue);
}
/**
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
*/
inline void bindNoCopy(const std::string& aName, const char* apValue)
{
bindNoCopy(aName.c_str(), apValue);
}
/** /**
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_STATIC flag, NOT making a copy of the data. * @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
*/ */
inline void bindNoCopy(const std::string& aName, const void* apValue, const int aSize) inline void bindNoCopy(const std::string& aName, const void* apValue, const int aSize)
{ {
@ -286,6 +306,8 @@ public:
} }
/** /**
* @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*
* @see clearBindings() to set all bound parameters to NULL.
*/ */
inline void bind(const std::string& aName) // bind NULL value inline void bind(const std::string& aName) // bind NULL value
{ {

View File

@ -102,14 +102,6 @@ void Statement::bind(const int aIndex, const std::string& aValue)
check(ret); check(ret);
} }
// Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bindNoCopy(const int aIndex, const std::string& aValue)
{
const int ret = sqlite3_bind_text(mStmtPtr, aIndex, aValue.c_str(),
static_cast<int>(aValue.size()), SQLITE_STATIC);
check(ret);
}
// Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bind(const int aIndex, const char* apValue) void Statement::bind(const int aIndex, const char* apValue)
{ {
@ -124,6 +116,21 @@ void Statement::bind(const int aIndex, const void* apValue, const int aSize)
check(ret); check(ret);
} }
// Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bindNoCopy(const int aIndex, const std::string& aValue)
{
const int ret = sqlite3_bind_text(mStmtPtr, aIndex, aValue.c_str(),
static_cast<int>(aValue.size()), SQLITE_STATIC);
check(ret);
}
// Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bindNoCopy(const int aIndex, const char* apValue)
{
const int ret = sqlite3_bind_text(mStmtPtr, aIndex, apValue, -1, SQLITE_STATIC);
check(ret);
}
// Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bindNoCopy(const int aIndex, const void* apValue, const int aSize) void Statement::bindNoCopy(const int aIndex, const void* apValue, const int aSize)
{ {
@ -180,15 +187,6 @@ void Statement::bind(const char* apName, const std::string& aValue)
check(ret); check(ret);
} }
// Bind a string value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bindNoCopy(const char* apName, const std::string& aValue)
{
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
const int ret = sqlite3_bind_text(mStmtPtr, index, aValue.c_str(),
static_cast<int>(aValue.size()), SQLITE_STATIC);
check(ret);
}
// Bind a text value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // Bind a text value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bind(const char* apName, const char* apValue) void Statement::bind(const char* apName, const char* apValue)
{ {
@ -205,6 +203,23 @@ void Statement::bind(const char* apName, const void* apValue, const int aSize)
check(ret); check(ret);
} }
// Bind a string value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bindNoCopy(const char* apName, const std::string& aValue)
{
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
const int ret = sqlite3_bind_text(mStmtPtr, index, aValue.c_str(),
static_cast<int>(aValue.size()), SQLITE_STATIC);
check(ret);
}
// Bind a text value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bindNoCopy(const char* apName, const char* apValue)
{
const int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
const int ret = sqlite3_bind_text(mStmtPtr, index, apValue, -1, SQLITE_STATIC);
check(ret);
}
// Bind a binary blob value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // Bind a binary blob value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bindNoCopy(const char* apName, const void* apValue, const int aSize) void Statement::bindNoCopy(const char* apName, const void* apValue, const int aSize)
{ {