Skip to content

Commit

Permalink
fix(RecordSet): copy, move #4525
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-f committed Oct 16, 2024
1 parent 5c40b5e commit f599a17
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
27 changes: 21 additions & 6 deletions Data/SQLite/testsuite/src/SQLiteTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3684,7 +3684,6 @@ void SQLiteTest::testRecordsetCopyMove()
session << "INSERT INTO Vectors VALUES (?,?,?)", use(v), now;

RecordSet rset(session, "SELECT * FROM Vectors");

std::ostringstream osLoop;
RecordSet::Iterator it = rset.begin();
RecordSet::Iterator end = rset.end();
Expand All @@ -3694,11 +3693,11 @@ void SQLiteTest::testRecordsetCopyMove()
osLoop << *it;
}
assertTrue(!osLoop.str().empty());

std::ostringstream osCopy;
std::copy(rset.begin(), rset.end(), std::ostream_iterator<Row>(osCopy));
assertTrue(osLoop.str() == osCopy.str());

// copy
RecordSet rsetCopy(rset);
osLoop.str("");
it = rsetCopy.begin();
Expand All @@ -3713,10 +3712,9 @@ void SQLiteTest::testRecordsetCopyMove()
osCopy.str("");
std::copy(rsetCopy.begin(), rsetCopy.end(), std::ostream_iterator<Row>(osCopy));
assertTrue(osLoop.str() == osCopy.str());
/*

// move
RecordSet rsetMove(std::move(rsetCopy));
rsetCopy.reset(session);
assertEqual(0, rsetCopy.rowCount());
osLoop.str("");
it = rsetMove.begin();
end = rsetMove.end();
Expand All @@ -3730,7 +3728,24 @@ void SQLiteTest::testRecordsetCopyMove()
osCopy.str("");
std::copy(rsetMove.begin(), rsetMove.end(), std::ostream_iterator<Row>(osCopy));
assertTrue(osLoop.str() == osCopy.str());
*/

// moved from object must remain in valid unspecified state
// and can be reused
assertEqual(0, rsetCopy.rowCount());
rsetCopy = (session << "SELECT * FROM Vectors", now);
assertEqual(v.size(), rsetCopy.rowCount());
osLoop.str("");
it = rsetCopy.begin();
end = rsetCopy.end();
for (int i = 1; it != end; ++it, ++i)
{
assertTrue(it->get(0) == i);
osLoop << *it;
}
assertTrue(!osLoop.str().empty());
osCopy.str("");
std::copy(rsetCopy.begin(), rsetCopy.end(), std::ostream_iterator<Row>(osCopy));
assertTrue(osLoop.str() == osCopy.str());
}


Expand Down
28 changes: 19 additions & 9 deletions Data/src/RecordSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ RecordSet::RecordSet(Session& rSession,


RecordSet::RecordSet(const RecordSet& other):
Statement(other.impl()),
Statement(other),
_currentRow(other._currentRow),
_pBegin(new RowIterator(this, 0 == rowsExtracted())),
_pEnd(new RowIterator(this, true)),
Expand All @@ -72,17 +72,18 @@ RecordSet::RecordSet(const RecordSet& other):


RecordSet::RecordSet(RecordSet&& other) noexcept:
Statement(other.impl()),
Statement(std::move(other)),
_currentRow(other._currentRow),
_pBegin(other._pBegin),
_pEnd(other._pEnd),
_pBegin(new RowIterator(this, 0 == rowsExtracted())),
_pEnd(new RowIterator(this, true)),
_rowMap(std::move(other._rowMap)),
_pFilter(other._pFilter),
_totalRowCount(std::move(other._totalRowCount))
_totalRowCount(other._totalRowCount)
{
other.clear();
other._currentRow = 0;
delete other._pBegin;
other._pBegin = nullptr;
delete other._pEnd;
other._pEnd = nullptr;
other._rowMap.clear();
other._pFilter.reset();
Expand All @@ -108,10 +109,19 @@ RecordSet& RecordSet::operator = (RecordSet&& other) noexcept
{
Statement::operator = (std::move(other));
_currentRow = std::move(other._currentRow);
_pBegin = std::move(other._pBegin);
_pEnd = std::move(other._pEnd);
other._currentRow = 0;
_pBegin = new RowIterator(this, 0 == rowsExtracted());
delete other._pBegin;
other._pBegin = nullptr;
_pEnd = new RowIterator(this, true);
delete other._pEnd;
other._pEnd = nullptr;
_rowMap = std::move(other._rowMap);
other._rowMap.clear();
_pFilter = std::move(other._pFilter);
other._pFilter.reset();
_totalRowCount = std::move(other._totalRowCount);
other._totalRowCount = UNKNOWN_TOTAL_ROW_COUNT;

return *this;
}
Expand Down Expand Up @@ -499,7 +509,7 @@ Row& RecordSet::row(std::size_t pos)

std::size_t RecordSet::rowCount() const
{
if (extractions().size() == 0) return 0;
if (!impl() || extractions().size() == 0) return 0;

std::size_t rc = subTotalRowCount();
if (!isFiltered()) return rc;
Expand Down
5 changes: 3 additions & 2 deletions Data/src/Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Statement::Statement(Statement&& stmt) noexcept:
_parseError(std::move(stmt._parseError)),
#endif
_pImpl(std::move(stmt._pImpl)),
_async(std::move(stmt._async)),
_async(stmt._async),
_pResult(std::move(stmt._pResult)),
_pAsyncExec(std::move(stmt._pAsyncExec)),
_arguments(std::move(stmt._arguments)),
Expand All @@ -96,6 +96,7 @@ void Statement::clear() noexcept
_pRowFormatter = nullptr;
_stmtString.clear();
#ifndef POCO_DATA_NO_SQL_PARSER
_pParseResult = nullptr;
_parseError.clear();
#endif
}
Expand Down Expand Up @@ -129,7 +130,7 @@ Statement& Statement::operator = (Statement&& stmt) noexcept
_pRowFormatter = std::move(stmt._pRowFormatter);
stmt._pRowFormatter = nullptr;
_stmtString = std::move(stmt._stmtString);
_stmtString.clear();
stmt._stmtString.clear();

return *this;
}
Expand Down

0 comments on commit f599a17

Please sign in to comment.