Skip to content

Commit 78915c8

Browse files
committed
Fix SRombauts#190 Add Statement move constructor
1 parent af8e2ce commit 78915c8

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

include/SQLiteCpp/Database.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ class Database
124124
/**
125125
* @brief Move an SQLite database connection.
126126
*
127-
* @param[in] aDb Database to move
127+
* @param[in] aDatabase Database to move
128128
*/
129-
inline Database(Database&& aDb) noexcept :
130-
mpSQLite(aDb.mpSQLite),
131-
mFilename(std::move(aDb.mFilename))
129+
inline Database(Database&& aDatabase) noexcept :
130+
mpSQLite(aDatabase.mpSQLite),
131+
mFilename(std::move(aDatabase.mFilename))
132132
{
133-
aDb.mpSQLite = nullptr;
133+
aDatabase.mpSQLite = nullptr;
134134
}
135135
#endif
136136

include/SQLiteCpp/Statement.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ class Statement
7373
*/
7474
Statement(Database& aDatabase, const std::string& aQuery);
7575

76+
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
77+
/**
78+
* @brief Move an SQLite statement.
79+
*
80+
* @param[in] aStatement Statement to move
81+
*/
82+
Statement(Statement&& aStatement) noexcept;
83+
#endif
84+
7685
/// Finalize and unregister the SQL query from the SQLite Database Connection.
7786
~Statement();
7887

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

src/Statement.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ Statement::Statement(Database &aDatabase, const std::string& aQuery) :
4242
mColumnCount = sqlite3_column_count(mStmtPtr);
4343
}
4444

45+
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
46+
Statement::Statement(Statement&& aStatement) noexcept :
47+
mQuery(std::move(aStatement.mQuery)),
48+
mStmtPtr(std::move(aStatement.mStmtPtr)),
49+
mColumnCount(aStatement.mColumnCount),
50+
mbHasRow(aStatement.mbHasRow),
51+
mbDone(aStatement.mbDone)
52+
{
53+
aStatement.mColumnCount = 0;
54+
aStatement.mbHasRow = false;
55+
aStatement.mbDone = false;
56+
}
57+
#endif
4558

4659
// Finalize and unregister the SQL query from the SQLite Database Connection.
4760
Statement::~Statement()

tests/Statement_test.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,40 @@ TEST(Statement, invalid) {
9696
EXPECT_THROW(query.exec(), SQLite::Exception); // exec() shall throw as it does not expect a result
9797
}
9898

99+
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
100+
101+
SQLite::Statement StatementBuilder(SQLite::Database& aDb, const char* apQuery)
102+
{
103+
return SQLite::Statement(aDb, apQuery);
104+
}
105+
106+
TEST(Statement, moveConstructor) {
107+
// Create a new database
108+
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
109+
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"));
110+
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\")"));
111+
EXPECT_EQ(1, db.getLastInsertRowid());
112+
113+
SQLite::Statement query = StatementBuilder(db, "SELECT * FROM test");
114+
EXPECT_FALSE(query.getQuery().empty());
115+
EXPECT_FALSE(query.hasRow());
116+
EXPECT_FALSE(query.isDone());
117+
EXPECT_EQ(2, query.getColumnCount());
118+
SQLite::Statement moved = std::move(query);
119+
EXPECT_TRUE(query.getQuery().empty());
120+
EXPECT_EQ(0, query.getColumnCount());
121+
EXPECT_FALSE(moved.getQuery().empty());
122+
EXPECT_EQ(2, moved.getColumnCount());
123+
// Execute
124+
moved.executeStep();
125+
EXPECT_TRUE(moved.hasRow());
126+
EXPECT_FALSE(moved.isDone());
127+
EXPECT_FALSE(query.hasRow());
128+
EXPECT_FALSE(query.isDone());
129+
}
130+
131+
#endif
132+
99133
TEST(Statement, executeStep) {
100134
// Create a new database
101135
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);

0 commit comments

Comments
 (0)