diff --git a/include/SQLiteCpp/Database.h b/include/SQLiteCpp/Database.h index 9f1b5cb..1b40287 100644 --- a/include/SQLiteCpp/Database.h +++ b/include/SQLiteCpp/Database.h @@ -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 diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index ecf417b..c1b4e3c 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -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(); @@ -719,7 +728,7 @@ private: 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 }; diff --git a/src/Statement.cpp b/src/Statement.cpp index bcf5d95..9cb5856 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -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() diff --git a/tests/Statement_test.cpp b/tests/Statement_test.cpp index 3e4d919..1fe071b 100644 --- a/tests/Statement_test.cpp +++ b/tests/Statement_test.cpp @@ -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);