Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions include/SQLiteCpp/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ class Statement
* @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<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)
*/
Expand Down Expand Up @@ -266,6 +284,24 @@ class Statement
{
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)
*/
Expand Down
11 changes: 8 additions & 3 deletions tests/Statement_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,23 +384,24 @@ 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());

// 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();
Expand All @@ -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();
Expand All @@ -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());

Expand All @@ -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()
Expand Down