Skip to content

Commit

Permalink
Fix #190 Add Statement move constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
SRombauts committed Mar 4, 2019
1 parent af8e2ce commit 78915c8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
10 changes: 5 additions & 5 deletions include/SQLiteCpp/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ class Database
/**
* @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

Expand Down
11 changes: 10 additions & 1 deletion include/SQLiteCpp/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ class Statement
*/
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();

Expand Down Expand Up @@ -719,7 +728,7 @@ class Statement
Ptr mStmtPtr; //!< Shared Pointer to the prepared SQLite Statement Object
int mColumnCount; //!< Number of columns in the result of the prepared statement
mutable TColumnNames mColumnNames; //!< Map of columns index by name (mutable so getColumnIndex can be const)
bool mbHasRow; //!< true when a row has been fetched with executeStep()
bool mbHasRow; //!< true when a row has been fetched with executeStep()
bool mbDone; //!< true when the last executeStep() had no more row to fetch
};

Expand Down
13 changes: 13 additions & 0 deletions src/Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
34 changes: 34 additions & 0 deletions tests/Statement_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 78915c8

Please sign in to comment.