Skip to content

Commit

Permalink
Add rowset default ctor and clear()
Browse files Browse the repository at this point in the history
This allows creating empty rowset objects or resetting them to the empty
state later, which seems to make sense.

Closes #198.

Closes #1057.

Closes #1086.
  • Loading branch information
cstiborg authored and vadz committed Oct 19, 2023
1 parent a3bf0ba commit 9bcc5f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
19 changes: 17 additions & 2 deletions include/soci/rowset.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class rowset_impl

typedef rowset_iterator<T> iterator;

rowset_impl()
: refs_(1), st_(nullptr), define_(nullptr)
{
}

rowset_impl(details::prepare_temp_type const & prep)
: refs_(1), st_(new statement(prep)), define_(new T())
{
Expand All @@ -135,8 +140,8 @@ class rowset_impl

iterator begin() const
{
// No ownership transfer occurs here
return iterator(*st_, *define_);
// No ownership transfer occurs here. Empty rowset doesn't have any valid begin iterator.
return st_ ? iterator(*st_, *define_) : iterator();
}

iterator end() const
Expand Down Expand Up @@ -182,6 +187,11 @@ class rowset
pimpl_->incRef();
}

rowset()
: pimpl_(new details::rowset_impl<T>())
{
}

// Due to the existence of conversion from session to prepare_temp_type, it
// would have been possible to construct a rowset from session if we didn't
// delete this ctor -- so do delete it because it doesn't make sense to
Expand All @@ -204,6 +214,11 @@ class rowset
return *this;
}

void clear()
{
*this = rowset();
}

const_iterator begin() const
{
return pimpl_->begin();
Expand Down
15 changes: 13 additions & 2 deletions tests/common-tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -2888,7 +2888,12 @@ TEST_CASE_METHOD(common_tests, "Rowset creation and copying", "[core][rowset]")
// create and populate the test table
auto_table_creator tableCreator(tc_.table_creator_1(sql));
{
// Open empty rowset
// Create empty rowset
rowset<row> rs1;
CHECK(rs1.begin() == rs1.end());
}
{
// Load empty rowset
rowset<row> rs1 = (sql.prepare << "select * from soci_test");
CHECK(rs1.begin() == rs1.end());
}
Expand All @@ -2909,7 +2914,7 @@ TEST_CASE_METHOD(common_tests, "Rowset creation and copying", "[core][rowset]")
if (!tc_.has_multiple_select_bug())
{
// Assignment
rowset<row> rs1 = (sql.prepare << "select * from soci_test");
rowset<row> rs1;
rowset<row> rs2 = (sql.prepare << "select * from soci_test");
rowset<row> rs3 = (sql.prepare << "select * from soci_test");
rs1 = rs2;
Expand Down Expand Up @@ -2940,6 +2945,12 @@ TEST_CASE_METHOD(common_tests, "Rowset iteration", "[core][rowset]")

CHECK(5 == std::distance(rs.begin(), rs.end()));
}
{
rowset<row> rs = (sql.prepare << "select * from soci_test");

rs.clear();
CHECK(rs.begin() == rs.end());
}
}

}
Expand Down

0 comments on commit 9bcc5f8

Please sign in to comment.