mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-04 09:46:02 -04:00
Fix #190 Add Statement move constructor
This commit is contained in:
parent
af8e2cea9e
commit
78915c8f43
@ -124,13 +124,13 @@ public:
|
|||||||
/**
|
/**
|
||||||
* @brief Move an SQLite database connection.
|
* @brief Move an SQLite database connection.
|
||||||
*
|
*
|
||||||
* @param[in] aDb Database to move
|
* @param[in] aDatabase Database to move
|
||||||
*/
|
*/
|
||||||
inline Database(Database&& aDb) noexcept :
|
inline Database(Database&& aDatabase) noexcept :
|
||||||
mpSQLite(aDb.mpSQLite),
|
mpSQLite(aDatabase.mpSQLite),
|
||||||
mFilename(std::move(aDb.mFilename))
|
mFilename(std::move(aDatabase.mFilename))
|
||||||
{
|
{
|
||||||
aDb.mpSQLite = nullptr;
|
aDatabase.mpSQLite = nullptr;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -73,6 +73,15 @@ public:
|
|||||||
*/
|
*/
|
||||||
Statement(Database& aDatabase, const std::string& aQuery);
|
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.
|
/// Finalize and unregister the SQL query from the SQLite Database Connection.
|
||||||
~Statement();
|
~Statement();
|
||||||
|
|
||||||
@ -719,7 +728,7 @@ private:
|
|||||||
Ptr mStmtPtr; //!< Shared Pointer to the prepared SQLite Statement Object
|
Ptr mStmtPtr; //!< Shared Pointer to the prepared SQLite Statement Object
|
||||||
int mColumnCount; //!< Number of columns in the result of the prepared statement
|
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)
|
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
|
bool mbDone; //!< true when the last executeStep() had no more row to fetch
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -42,6 +42,19 @@ Statement::Statement(Database &aDatabase, const std::string& aQuery) :
|
|||||||
mColumnCount = sqlite3_column_count(mStmtPtr);
|
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.
|
// Finalize and unregister the SQL query from the SQLite Database Connection.
|
||||||
Statement::~Statement()
|
Statement::~Statement()
|
||||||
|
@ -96,6 +96,40 @@ TEST(Statement, invalid) {
|
|||||||
EXPECT_THROW(query.exec(), SQLite::Exception); // exec() shall throw as it does not expect a result
|
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) {
|
TEST(Statement, executeStep) {
|
||||||
// Create a new database
|
// Create a new database
|
||||||
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
|
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user