Merge pull request #148 from xforce/master

Allows long int for bind when used with name
This commit is contained in:
Sébastien Rombauts 2017-11-09 20:48:49 +01:00 committed by GitHub
commit 1832a35fec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 3 deletions

View File

@ -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) * @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);
#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<int>(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<long long>(aValue));
}
#endif
/** /**
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @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); 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<int>(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<long long>(aValue));
}
#endif
/** /**
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */

View File

@ -384,15 +384,16 @@ TEST(Statement, bindByName) {
EXPECT_EQ(SQLite::OK, db.getErrorCode()); EXPECT_EQ(SQLite::OK, db.getErrorCode());
// Create a new table // 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()); EXPECT_EQ(SQLite::OK, db.getErrorCode());
// Insertion with bindable parameters // 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 // First row with text/int/double
insert.bind("@msg", "first"); insert.bind("@msg", "first");
insert.bind("@int", 123); insert.bind("@int", 123);
insert.bind("@long", -123);
insert.bind("@double", 0.123); insert.bind("@double", 0.123);
EXPECT_EQ(1, insert.exec()); EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode()); EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
@ -400,7 +401,7 @@ TEST(Statement, bindByName) {
// Compile a SQL query to check the result // Compile a SQL query to check the result
SQLite::Statement query(db, "SELECT * FROM test"); SQLite::Statement query(db, "SELECT * FROM test");
EXPECT_STREQ("SELECT * FROM test", query.getQuery().c_str()); EXPECT_STREQ("SELECT * FROM test", query.getQuery().c_str());
EXPECT_EQ(4, query.getColumnCount()); EXPECT_EQ(5, query.getColumnCount());
// Check the result // Check the result
query.executeStep(); query.executeStep();
@ -410,6 +411,7 @@ TEST(Statement, bindByName) {
EXPECT_STREQ("first", query.getColumn(1).getText()); EXPECT_STREQ("first", query.getColumn(1).getText());
EXPECT_EQ (123, query.getColumn(2).getInt()); EXPECT_EQ (123, query.getColumn(2).getInt());
EXPECT_EQ (0.123, query.getColumn(3).getDouble()); EXPECT_EQ (0.123, query.getColumn(3).getDouble());
EXPECT_EQ (-123, query.getColumn(4).getInt());
// reset() with clearbindings() and new bindings // reset() with clearbindings() and new bindings
insert.reset(); insert.reset();
@ -419,10 +421,12 @@ TEST(Statement, bindByName) {
{ {
const std::string second("second"); const std::string second("second");
const long long int64 = 12345678900000LL; const long long int64 = 12345678900000LL;
const long integer = -123;
const float float32 = 0.234f; const float float32 = 0.234f;
insert.bind("@msg", second); insert.bind("@msg", second);
insert.bind("@int", int64); insert.bind("@int", int64);
insert.bind("@double", float32); insert.bind("@double", float32);
insert.bind("@long", integer);
EXPECT_EQ(1, insert.exec()); EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode()); EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
@ -434,6 +438,7 @@ TEST(Statement, bindByName) {
EXPECT_EQ(second, query.getColumn(1).getText()); EXPECT_EQ(second, query.getColumn(1).getText());
EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64()); EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64());
EXPECT_EQ(0.234f, query.getColumn(3).getDouble()); EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
EXPECT_EQ(-123, query.getColumn(4).getInt());
} }
// reset() without clearbindings() // reset() without clearbindings()