From cebea884c37d1fa58ee4202c077ee34815223be2 Mon Sep 17 00:00:00 2001 From: Alexander Guettler Date: Thu, 9 Nov 2017 11:05:01 -0800 Subject: [PATCH] Add the fix from a3160dcfc2f80f692f3477a67c202ff87f75fa5a also to the bind by name functions --- include/SQLiteCpp/Statement.h | 36 +++++++++++++++++++++++++++++++++++ tests/Statement_test.cpp | 11 ++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index 9c3097a..d80e39b 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -196,6 +196,24 @@ public: * @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); + +#if (LONG_MAX == INT_MAX) // sizeof(long)==4 means the data model of the system is ILP32 (32bits OS or Windows 64bits) + /** + * @brief Bind a 32bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) + */ + void bind(const char* apName, const long aValue) + { + bind(apName, static_cast(aValue)); + } +#else + /** + * @brief Bind a 64bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) + */ + void bind(const char* apName, const long aValue) + { + bind(apName, static_cast(aValue)); + } +#endif /** * @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) */ @@ -266,6 +284,24 @@ public: { bind(aName.c_str(), aValue); } + +#if (LONG_MAX == INT_MAX) // sizeof(long)==4 means the data model of the system is ILP32 (32bits OS or Windows 64bits) + /** + * @brief Bind a 32bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) + */ + void bind(const std::string& aName, const long aValue) + { + bind(aName.c_str(), static_cast(aValue)); + } +#else + /** + * @brief Bind a 64bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) + */ + void bind(const std::string& aName, const long aValue) + { + bind(aName.c_str(), static_cast(aValue)); + } +#endif /** * @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) */ diff --git a/tests/Statement_test.cpp b/tests/Statement_test.cpp index 79f7e80..76139dd 100644 --- a/tests/Statement_test.cpp +++ b/tests/Statement_test.cpp @@ -384,15 +384,16 @@ TEST(Statement, bindByName) { EXPECT_EQ(SQLite::OK, db.getErrorCode()); // Create a new table - EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT, int INTEGER, double REAL)")); + EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT, int INTEGER, double REAL, long INTEGER)")); EXPECT_EQ(SQLite::OK, db.getErrorCode()); // Insertion with bindable parameters - SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, @msg, @int, @double)"); + SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, @msg, @int, @double, @long)"); // First row with text/int/double insert.bind("@msg", "first"); insert.bind("@int", 123); + insert.bind("@long", -123); insert.bind("@double", 0.123); EXPECT_EQ(1, insert.exec()); EXPECT_EQ(SQLITE_DONE, db.getErrorCode()); @@ -400,7 +401,7 @@ TEST(Statement, bindByName) { // Compile a SQL query to check the result SQLite::Statement query(db, "SELECT * FROM test"); EXPECT_STREQ("SELECT * FROM test", query.getQuery().c_str()); - EXPECT_EQ(4, query.getColumnCount()); + EXPECT_EQ(5, query.getColumnCount()); // Check the result query.executeStep(); @@ -410,6 +411,7 @@ TEST(Statement, bindByName) { EXPECT_STREQ("first", query.getColumn(1).getText()); EXPECT_EQ (123, query.getColumn(2).getInt()); EXPECT_EQ (0.123, query.getColumn(3).getDouble()); + EXPECT_EQ (-123, query.getColumn(4).getInt()); // reset() with clearbindings() and new bindings insert.reset(); @@ -419,10 +421,12 @@ TEST(Statement, bindByName) { { const std::string second("second"); const long long int64 = 12345678900000LL; + const long integer = -123; const float float32 = 0.234f; insert.bind("@msg", second); insert.bind("@int", int64); insert.bind("@double", float32); + insert.bind("@long", integer); EXPECT_EQ(1, insert.exec()); EXPECT_EQ(SQLITE_DONE, db.getErrorCode()); @@ -434,6 +438,7 @@ TEST(Statement, bindByName) { EXPECT_EQ(second, query.getColumn(1).getText()); EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64()); EXPECT_EQ(0.234f, query.getColumn(3).getDouble()); + EXPECT_EQ(-123, query.getColumn(4).getInt()); } // reset() without clearbindings()