mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-04 09:46:02 -04:00
Add unit tests for statement bindings + removed an assert() for ~Transaction()
This commit is contained in:
parent
b913932be2
commit
0c43747065
@ -130,8 +130,7 @@ bool Database::tableExists(const char* apTableName)
|
||||
Statement query(*this, "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?");
|
||||
query.bind(1, apTableName);
|
||||
(void)query.executeStep(); // Cannot return false, as the above query always return a result
|
||||
const int Nb = query.getColumn(0);
|
||||
return (1 == Nb);
|
||||
return (1 == query.getColumn(0).getInt());
|
||||
}
|
||||
|
||||
// Attach a custom function to your sqlite database. Assumes UTF8 text representation.
|
||||
|
@ -49,7 +49,7 @@ Statement::~Statement() noexcept // nothrow
|
||||
// the finalization will be done by the destructor of the last shared pointer
|
||||
}
|
||||
|
||||
// Reset the statement to make it ready for a new execution
|
||||
// Reset the statement to make it ready for a new execution (see also #clearBindings() bellow)
|
||||
void Statement::reset()
|
||||
{
|
||||
mbOk = false;
|
||||
@ -58,11 +58,9 @@ void Statement::reset()
|
||||
check(ret);
|
||||
}
|
||||
|
||||
// Clears away all the bindings of a prepared statement.
|
||||
// Clears away all the bindings of a prepared statement (can be associated with #reset() above).
|
||||
void Statement::clearBindings()
|
||||
{
|
||||
mbOk = false;
|
||||
mbDone = false;
|
||||
const int ret = sqlite3_clear_bindings(mStmtPtr);
|
||||
check(ret);
|
||||
}
|
||||
|
@ -35,11 +35,9 @@ Transaction::~Transaction() noexcept // nothrow
|
||||
{
|
||||
mDatabase.exec("ROLLBACK");
|
||||
}
|
||||
catch (SQLite::Exception& e)
|
||||
catch (SQLite::Exception&)
|
||||
{
|
||||
// Never throw an exception in a destructor
|
||||
(void)e; // warning proof
|
||||
SQLITECPP_ASSERT(false, e.what()); // See SQLITECPP_ENABLE_ASSERT_HANDLER
|
||||
// Never throw an exception in a destructor: error if already rollbacked, but no harm is caused by this.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,8 +86,6 @@ TEST(Statement, invalid) {
|
||||
EXPECT_THROW(query.exec(), SQLite::Exception); // exec() shall throw as it does not expect a result
|
||||
}
|
||||
|
||||
// TODO: test every kind of binding + clearBindings()
|
||||
|
||||
TEST(Statement, executeStep) {
|
||||
// Create a new database
|
||||
SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||
@ -128,6 +126,99 @@ TEST(Statement, executeStep) {
|
||||
EXPECT_THROW(query.executeStep(), SQLite::Exception);
|
||||
}
|
||||
|
||||
TEST(Statement, bindings) {
|
||||
// Create a new database
|
||||
SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||
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(SQLITE_OK, db.getErrorCode());
|
||||
|
||||
// Insertion with binded values
|
||||
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, ?, ?, ?)");
|
||||
|
||||
// First row with text/int/double
|
||||
insert.bind(1, "first");
|
||||
insert.bind(2, 123);
|
||||
insert.bind(3, 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());
|
||||
|
||||
// Check the first row
|
||||
query.executeStep();
|
||||
EXPECT_TRUE (query.isOk());
|
||||
EXPECT_FALSE(query.isDone());
|
||||
EXPECT_EQ (1, query.getColumn(0).getInt64());
|
||||
EXPECT_STREQ("first", query.getColumn(1).getText());
|
||||
EXPECT_EQ (123, query.getColumn(2).getInt());
|
||||
EXPECT_EQ (0.123, query.getColumn(3).getDouble());
|
||||
|
||||
// reset() without clearbindings()
|
||||
insert.reset();
|
||||
|
||||
// Second row with the same exact values because clearbindings() was not called
|
||||
EXPECT_EQ(1, insert.exec());
|
||||
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
|
||||
|
||||
// Check the second row
|
||||
query.executeStep();
|
||||
EXPECT_TRUE (query.isOk());
|
||||
EXPECT_FALSE(query.isDone());
|
||||
EXPECT_EQ (2, query.getColumn(0).getInt64());
|
||||
EXPECT_STREQ("first", query.getColumn(1).getText());
|
||||
EXPECT_EQ (123, query.getColumn(2).getInt());
|
||||
EXPECT_EQ (0.123, query.getColumn(3).getDouble());
|
||||
|
||||
// reset() with clearbindings() and no more bindings
|
||||
insert.reset();
|
||||
insert.clearBindings();
|
||||
|
||||
// Third row with the all null values because clearbindings() was called
|
||||
EXPECT_EQ(1, insert.exec());
|
||||
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
|
||||
|
||||
// Check the third row
|
||||
query.executeStep();
|
||||
EXPECT_TRUE (query.isOk());
|
||||
EXPECT_FALSE(query.isDone());
|
||||
EXPECT_EQ (3, query.getColumn(0).getInt64());
|
||||
EXPECT_TRUE (query.isColumnNull(1));
|
||||
EXPECT_STREQ("", query.getColumn(1).getText());
|
||||
EXPECT_TRUE (query.isColumnNull(2));
|
||||
EXPECT_EQ (0, query.getColumn(2).getInt());
|
||||
EXPECT_TRUE (query.isColumnNull(3));
|
||||
EXPECT_EQ (0.0, query.getColumn(3).getDouble());
|
||||
|
||||
// reset() with clearbindings() and new bindings
|
||||
insert.reset();
|
||||
insert.clearBindings();
|
||||
|
||||
// Fourth row with string/int64/float
|
||||
const std::string second("second");
|
||||
const sqlite_int64 int64 = 12345678900000LL;
|
||||
const float fl32 = 0.123f;
|
||||
insert.bind(1, second);
|
||||
insert.bind(2, int64);
|
||||
insert.bind(3, fl32);
|
||||
EXPECT_EQ(1, insert.exec());
|
||||
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
|
||||
|
||||
// Check the fourth row
|
||||
query.executeStep();
|
||||
EXPECT_TRUE (query.isOk());
|
||||
EXPECT_FALSE(query.isDone());
|
||||
EXPECT_EQ(4, query.getColumn(0).getInt64());
|
||||
EXPECT_EQ(second, query.getColumn(1).getText());
|
||||
EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64());
|
||||
EXPECT_EQ(0.123f, query.getColumn(3).getDouble());
|
||||
}
|
||||
|
||||
TEST(Statement, isColumnNull) {
|
||||
// Create a new database
|
||||
SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||
|
@ -52,7 +52,7 @@ TEST(Transaction, commitRollback) {
|
||||
EXPECT_EQ(2, db.getLastInsertRowid());
|
||||
|
||||
// Execute with an error to rollback
|
||||
db.exec("Obvious syntax error to raise an exception");
|
||||
db.exec("DesiredSyntaxError to raise an exception to rollback the transaction");
|
||||
GTEST_FATAL_FAILURE_("we should never get there");
|
||||
|
||||
// Commit transaction
|
||||
|
Loading…
x
Reference in New Issue
Block a user