Fix #190 Add Statement move constructor

This commit is contained in:
Sébastien Rombauts 2019-03-04 08:52:47 +01:00
parent af8e2cea9e
commit 78915c8f43
4 changed files with 62 additions and 6 deletions

View File

@ -124,13 +124,13 @@ public:
/**
* @brief Move an SQLite database connection.
*
* @param[in] aDb Database to move
* @param[in] aDatabase Database to move
*/
inline Database(Database&& aDb) noexcept :
mpSQLite(aDb.mpSQLite),
mFilename(std::move(aDb.mFilename))
inline Database(Database&& aDatabase) noexcept :
mpSQLite(aDatabase.mpSQLite),
mFilename(std::move(aDatabase.mFilename))
{
aDb.mpSQLite = nullptr;
aDatabase.mpSQLite = nullptr;
}
#endif

View File

@ -73,6 +73,15 @@ public:
*/
Statement(Database& aDatabase, const std::string& aQuery);
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
/**
* @brief Move an SQLite statement.
*
* @param[in] aStatement Statement to move
*/
Statement(Statement&& aStatement) noexcept;
#endif
/// Finalize and unregister the SQL query from the SQLite Database Connection.
~Statement();

View File

@ -42,6 +42,19 @@ Statement::Statement(Database &aDatabase, const std::string& aQuery) :
mColumnCount = sqlite3_column_count(mStmtPtr);
}
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
Statement::Statement(Statement&& aStatement) noexcept :
mQuery(std::move(aStatement.mQuery)),
mStmtPtr(std::move(aStatement.mStmtPtr)),
mColumnCount(aStatement.mColumnCount),
mbHasRow(aStatement.mbHasRow),
mbDone(aStatement.mbDone)
{
aStatement.mColumnCount = 0;
aStatement.mbHasRow = false;
aStatement.mbDone = false;
}
#endif
// Finalize and unregister the SQL query from the SQLite Database Connection.
Statement::~Statement()

View File

@ -96,6 +96,40 @@ TEST(Statement, invalid) {
EXPECT_THROW(query.exec(), SQLite::Exception); // exec() shall throw as it does not expect a result
}
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
SQLite::Statement StatementBuilder(SQLite::Database& aDb, const char* apQuery)
{
return SQLite::Statement(aDb, apQuery);
}
TEST(Statement, moveConstructor) {
// Create a new database
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\")"));
EXPECT_EQ(1, db.getLastInsertRowid());
SQLite::Statement query = StatementBuilder(db, "SELECT * FROM test");
EXPECT_FALSE(query.getQuery().empty());
EXPECT_FALSE(query.hasRow());
EXPECT_FALSE(query.isDone());
EXPECT_EQ(2, query.getColumnCount());
SQLite::Statement moved = std::move(query);
EXPECT_TRUE(query.getQuery().empty());
EXPECT_EQ(0, query.getColumnCount());
EXPECT_FALSE(moved.getQuery().empty());
EXPECT_EQ(2, moved.getColumnCount());
// Execute
moved.executeStep();
EXPECT_TRUE(moved.hasRow());
EXPECT_FALSE(moved.isDone());
EXPECT_FALSE(query.hasRow());
EXPECT_FALSE(query.isDone());
}
#endif
TEST(Statement, executeStep) {
// Create a new database
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);