From 5ff189a69c7fb3ac3b38fd12a0bffe77ec86c1dd Mon Sep 17 00:00:00 2001 From: KOLANICH Date: Tue, 12 Feb 2019 12:26:04 +0300 Subject: [PATCH 1/3] Added a `getIndex` method and used it. --- include/SQLiteCpp/Statement.h | 2 ++ src/Statement.cpp | 27 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index f5e082c..4e8f959 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -116,6 +116,8 @@ public: // instead of being copied. // => if you know what you are doing, use bindNoCopy() instead of bind() + int getIndex(const char * const apName); + /** * @brief Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) */ diff --git a/src/Statement.cpp b/src/Statement.cpp index 6af7556..321f751 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -83,6 +83,11 @@ void Statement::clearBindings() check(ret); } +int Statement::getIndex(const char * const apName) +{ + return sqlite3_bind_parameter_index(mStmtPtr, apName); +} + // Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const int aIndex, const int aValue) { @@ -166,7 +171,7 @@ void Statement::bind(const int aIndex) // Bind an int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName, const int aValue) { - const int index = sqlite3_bind_parameter_index(mStmtPtr, apName); + const int index = getIndex(apName); const int ret = sqlite3_bind_int(mStmtPtr, index, aValue); check(ret); } @@ -174,7 +179,7 @@ void Statement::bind(const char* apName, const int aValue) // Bind a 32bits unsigned int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName, const unsigned aValue) { - const int index = sqlite3_bind_parameter_index(mStmtPtr, apName); + const int index = getIndex(apName); const int ret = sqlite3_bind_int64(mStmtPtr, index, aValue); check(ret); } @@ -182,7 +187,7 @@ void Statement::bind(const char* apName, const unsigned aValue) // Bind a 64bits int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName, const long long aValue) { - const int index = sqlite3_bind_parameter_index(mStmtPtr, apName); + const int index = getIndex(apName); const int ret = sqlite3_bind_int64(mStmtPtr, index, aValue); check(ret); } @@ -190,7 +195,7 @@ void Statement::bind(const char* apName, const long long aValue) // Bind a double (64bits float) value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName, const double aValue) { - const int index = sqlite3_bind_parameter_index(mStmtPtr, apName); + const int index = getIndex(apName); const int ret = sqlite3_bind_double(mStmtPtr, index, aValue); check(ret); } @@ -198,7 +203,7 @@ void Statement::bind(const char* apName, const double aValue) // Bind a string value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName, const std::string& aValue) { - const int index = sqlite3_bind_parameter_index(mStmtPtr, apName); + const int index = getIndex(apName); const int ret = sqlite3_bind_text(mStmtPtr, index, aValue.c_str(), static_cast(aValue.size()), SQLITE_TRANSIENT); check(ret); @@ -207,7 +212,7 @@ void Statement::bind(const char* apName, const std::string& aValue) // 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) { - const int index = sqlite3_bind_parameter_index(mStmtPtr, apName); + const int index = getIndex(apName); const int ret = sqlite3_bind_text(mStmtPtr, index, apValue, -1, SQLITE_TRANSIENT); check(ret); } @@ -215,7 +220,7 @@ void Statement::bind(const char* apName, const char* apValue) // Bind a binary blob value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName, const void* apValue, const int aSize) { - const int index = sqlite3_bind_parameter_index(mStmtPtr, apName); + const int index = getIndex(apName); const int ret = sqlite3_bind_blob(mStmtPtr, index, apValue, aSize, SQLITE_TRANSIENT); check(ret); } @@ -223,7 +228,7 @@ void Statement::bind(const char* apName, const void* apValue, const int aSize) // 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 index = getIndex(apName); const int ret = sqlite3_bind_text(mStmtPtr, index, aValue.c_str(), static_cast(aValue.size()), SQLITE_STATIC); check(ret); @@ -232,7 +237,7 @@ void Statement::bindNoCopy(const char* apName, const std::string& aValue) // 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 index = getIndex(apName); const int ret = sqlite3_bind_text(mStmtPtr, index, apValue, -1, SQLITE_STATIC); check(ret); } @@ -240,7 +245,7 @@ void Statement::bindNoCopy(const char* apName, const char* apValue) // 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) { - const int index = sqlite3_bind_parameter_index(mStmtPtr, apName); + const int index = getIndex(apName); const int ret = sqlite3_bind_blob(mStmtPtr, index, apValue, aSize, SQLITE_STATIC); check(ret); } @@ -248,7 +253,7 @@ void Statement::bindNoCopy(const char* apName, const void* apValue, const int aS // Bind a NULL value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName) { - const int index = sqlite3_bind_parameter_index(mStmtPtr, apName); + const int index = getIndex(apName); const int ret = sqlite3_bind_null(mStmtPtr, index); check(ret); } From 55d39591ac49e3d5daa388a616e3fba0373677d3 Mon Sep 17 00:00:00 2001 From: KOLANICH Date: Tue, 12 Feb 2019 12:26:04 +0300 Subject: [PATCH 2/3] Improved code reuse. --- src/Statement.cpp | 46 +++++++++++----------------------------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/src/Statement.cpp b/src/Statement.cpp index 321f751..32931a3 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -171,91 +171,67 @@ void Statement::bind(const int aIndex) // Bind an int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName, const int aValue) { - const int index = getIndex(apName); - const int ret = sqlite3_bind_int(mStmtPtr, index, aValue); - check(ret); + bind(getIndex(apName), aValue); } // Bind a 32bits unsigned int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName, const unsigned aValue) { - const int index = getIndex(apName); - const int ret = sqlite3_bind_int64(mStmtPtr, index, aValue); - check(ret); + bind(getIndex(apName), aValue); } // Bind a 64bits int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName, const long long aValue) { - const int index = getIndex(apName); - const int ret = sqlite3_bind_int64(mStmtPtr, index, aValue); - check(ret); + bind(getIndex(apName), aValue); } // Bind a double (64bits float) value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName, const double aValue) { - const int index = getIndex(apName); - const int ret = sqlite3_bind_double(mStmtPtr, index, aValue); - check(ret); + bind(getIndex(apName), aValue); } // Bind a string value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName, const std::string& aValue) { - const int index = getIndex(apName); - const int ret = sqlite3_bind_text(mStmtPtr, index, aValue.c_str(), - static_cast(aValue.size()), SQLITE_TRANSIENT); - check(ret); + bind(getIndex(apName), aValue); } // 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) { - const int index = getIndex(apName); - const int ret = sqlite3_bind_text(mStmtPtr, index, apValue, -1, SQLITE_TRANSIENT); - check(ret); + bind(getIndex(apName), apValue); } // Bind a binary blob value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName, const void* apValue, const int aSize) { - const int index = getIndex(apName); - const int ret = sqlite3_bind_blob(mStmtPtr, index, apValue, aSize, SQLITE_TRANSIENT); - check(ret); + bind(getIndex(apName), apValue, aSize); } // 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 = getIndex(apName); - const int ret = sqlite3_bind_text(mStmtPtr, index, aValue.c_str(), - static_cast(aValue.size()), SQLITE_STATIC); - check(ret); + bindNoCopy(getIndex(apName), aValue); } // 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 = getIndex(apName); - const int ret = sqlite3_bind_text(mStmtPtr, index, apValue, -1, SQLITE_STATIC); - check(ret); + bindNoCopy(getIndex(apName), apValue); } // 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) { - const int index = getIndex(apName); - const int ret = sqlite3_bind_blob(mStmtPtr, index, apValue, aSize, SQLITE_STATIC); - check(ret); + bindNoCopy(getIndex(apName), apValue, aSize); } // Bind a NULL value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const char* apName) { - const int index = getIndex(apName); - const int ret = sqlite3_bind_null(mStmtPtr, index); - check(ret); + bind(getIndex(apName)); } From b98eabbe9f551960b8480084c6eb1492e8e1db18 Mon Sep 17 00:00:00 2001 From: KOLANICH Date: Wed, 25 Dec 2019 17:30:46 +0300 Subject: [PATCH 3/3] Moved some functions from sources into headers. --- include/SQLiteCpp/Statement.h | 55 ++++++++++++++++++++++------ src/Statement.cpp | 67 ----------------------------------- 2 files changed, 44 insertions(+), 78 deletions(-) diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index 4e8f959..e518a2d 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -203,11 +203,17 @@ public: /** * @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) */ - void bind(const char* apName, const int aValue); + void bind(const char* apName, const int aValue) + { + bind(getIndex(apName), aValue); + } /** * @brief Bind a 32bits unsigned int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) */ - void bind(const char* apName, const unsigned aValue); + void bind(const char* apName, const unsigned aValue) + { + bind(getIndex(apName), aValue); + } #if (LONG_MAX == INT_MAX) // 4 bytes "long" type means the data model is ILP32 or LLP64 (Win64 Visual C++ and MinGW) /** @@ -229,29 +235,44 @@ public: /** * @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) */ - void bind(const char* apName, const long long aValue); + void bind(const char* apName, const long long aValue) + { + bind(getIndex(apName), aValue); + } /** * @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) */ - void bind(const char* apName, const double aValue); + void bind(const char* apName, const double aValue) + { + bind(getIndex(apName), aValue); + } /** * @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * * @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) + { + bind(getIndex(apName), aValue); + } /** * @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * * @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) + { + bind(getIndex(apName), apValue); + } /** * @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * * @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) + { + bind(getIndex(apName), apValue, aSize); + } /** * @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @@ -259,7 +280,10 @@ public: * * @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); + void bindNoCopy(const char* apName, const std::string& aValue) + { + bindNoCopy(getIndex(apName), aValue); + } /** * @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @@ -267,19 +291,28 @@ public: * * @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); + void bindNoCopy(const char* apName, const char* apValue) + { + bindNoCopy(getIndex(apName), apValue); + } /** * @brief Bind a binary blob 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 void* apValue, const int aSize); + void bindNoCopy(const char* apName, const void* apValue, const int aSize) + { + bindNoCopy(getIndex(apName), apValue, aSize); + } /** * @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 + { + bind(getIndex(apName)); + } /** diff --git a/src/Statement.cpp b/src/Statement.cpp index 32931a3..0d5a1a8 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -168,73 +168,6 @@ void Statement::bind(const int aIndex) } -// Bind an int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement -void Statement::bind(const char* apName, const int aValue) -{ - bind(getIndex(apName), aValue); -} - -// Bind a 32bits unsigned int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement -void Statement::bind(const char* apName, const unsigned aValue) -{ - bind(getIndex(apName), aValue); -} - -// Bind a 64bits int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement -void Statement::bind(const char* apName, const long long aValue) -{ - bind(getIndex(apName), aValue); -} - -// Bind a double (64bits float) value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement -void Statement::bind(const char* apName, const double aValue) -{ - bind(getIndex(apName), aValue); -} - -// Bind a string value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement -void Statement::bind(const char* apName, const std::string& aValue) -{ - bind(getIndex(apName), aValue); -} - -// 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) -{ - bind(getIndex(apName), apValue); -} - -// Bind a binary blob value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement -void Statement::bind(const char* apName, const void* apValue, const int aSize) -{ - bind(getIndex(apName), apValue, aSize); -} - -// 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) -{ - bindNoCopy(getIndex(apName), aValue); -} - -// 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) -{ - bindNoCopy(getIndex(apName), apValue); -} - -// 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) -{ - bindNoCopy(getIndex(apName), apValue, aSize); -} - -// Bind a NULL value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement -void Statement::bind(const char* apName) -{ - bind(getIndex(apName)); -} - - // Execute a step of the query to fetch one row of results bool Statement::executeStep() {