Skip to content

Commit 1832a35

Browse files
authored
Merge pull request SRombauts#148 from xforce/master
Allows long int for bind when used with name
2 parents 7c4689e + cebea88 commit 1832a35

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

include/SQLiteCpp/Statement.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,24 @@ class Statement
196196
* @brief Bind a 32bits unsigned int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
197197
*/
198198
void bind(const char* apName, const unsigned aValue);
199+
200+
#if (LONG_MAX == INT_MAX) // sizeof(long)==4 means the data model of the system is ILP32 (32bits OS or Windows 64bits)
201+
/**
202+
* @brief Bind a 32bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
203+
*/
204+
void bind(const char* apName, const long aValue)
205+
{
206+
bind(apName, static_cast<int>(aValue));
207+
}
208+
#else
209+
/**
210+
* @brief Bind a 64bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
211+
*/
212+
void bind(const char* apName, const long aValue)
213+
{
214+
bind(apName, static_cast<long long>(aValue));
215+
}
216+
#endif
199217
/**
200218
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
201219
*/
@@ -266,6 +284,24 @@ class Statement
266284
{
267285
bind(aName.c_str(), aValue);
268286
}
287+
288+
#if (LONG_MAX == INT_MAX) // sizeof(long)==4 means the data model of the system is ILP32 (32bits OS or Windows 64bits)
289+
/**
290+
* @brief Bind a 32bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
291+
*/
292+
void bind(const std::string& aName, const long aValue)
293+
{
294+
bind(aName.c_str(), static_cast<int>(aValue));
295+
}
296+
#else
297+
/**
298+
* @brief Bind a 64bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
299+
*/
300+
void bind(const std::string& aName, const long aValue)
301+
{
302+
bind(aName.c_str(), static_cast<long long>(aValue));
303+
}
304+
#endif
269305
/**
270306
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
271307
*/

tests/Statement_test.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,23 +384,24 @@ TEST(Statement, bindByName) {
384384
EXPECT_EQ(SQLite::OK, db.getErrorCode());
385385

386386
// Create a new table
387-
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT, int INTEGER, double REAL)"));
387+
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT, int INTEGER, double REAL, long INTEGER)"));
388388
EXPECT_EQ(SQLite::OK, db.getErrorCode());
389389

390390
// Insertion with bindable parameters
391-
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, @msg, @int, @double)");
391+
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, @msg, @int, @double, @long)");
392392

393393
// First row with text/int/double
394394
insert.bind("@msg", "first");
395395
insert.bind("@int", 123);
396+
insert.bind("@long", -123);
396397
insert.bind("@double", 0.123);
397398
EXPECT_EQ(1, insert.exec());
398399
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
399400

400401
// Compile a SQL query to check the result
401402
SQLite::Statement query(db, "SELECT * FROM test");
402403
EXPECT_STREQ("SELECT * FROM test", query.getQuery().c_str());
403-
EXPECT_EQ(4, query.getColumnCount());
404+
EXPECT_EQ(5, query.getColumnCount());
404405

405406
// Check the result
406407
query.executeStep();
@@ -410,6 +411,7 @@ TEST(Statement, bindByName) {
410411
EXPECT_STREQ("first", query.getColumn(1).getText());
411412
EXPECT_EQ (123, query.getColumn(2).getInt());
412413
EXPECT_EQ (0.123, query.getColumn(3).getDouble());
414+
EXPECT_EQ (-123, query.getColumn(4).getInt());
413415

414416
// reset() with clearbindings() and new bindings
415417
insert.reset();
@@ -419,10 +421,12 @@ TEST(Statement, bindByName) {
419421
{
420422
const std::string second("second");
421423
const long long int64 = 12345678900000LL;
424+
const long integer = -123;
422425
const float float32 = 0.234f;
423426
insert.bind("@msg", second);
424427
insert.bind("@int", int64);
425428
insert.bind("@double", float32);
429+
insert.bind("@long", integer);
426430
EXPECT_EQ(1, insert.exec());
427431
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
428432

@@ -434,6 +438,7 @@ TEST(Statement, bindByName) {
434438
EXPECT_EQ(second, query.getColumn(1).getText());
435439
EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64());
436440
EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
441+
EXPECT_EQ(-123, query.getColumn(4).getInt());
437442
}
438443

439444
// reset() without clearbindings()

0 commit comments

Comments
 (0)