Do not attempt extra data fetch when hitting the end of a query

This commit is contained in:
emeric
2024-03-14 13:51:28 +01:00
parent a769909ebc
commit e4ad0a5406
19 changed files with 177 additions and 148 deletions
+32 -5
View File
@@ -56,15 +56,20 @@ namespace lms::db::utils
{
res.range.offset = range->offset;
applyRange(query, Range{ range->offset, range->size + 1 });
res.results.reserve(range->size);
}
auto collection{ query.resultList() };
res.results.assign(collection.begin(), collection.end());
if (range && res.results.size() == static_cast<std::size_t>(range->size) + 1)
for (auto itResult{ collection.begin() }; itResult != collection.end(); ++itResult)
{
// TODO may optim by not actually requesting the last one
res.moreResults = true;
res.results.pop_back();
if (range && res.results.size() == range->size)
{
res.moreResults = true;
break;
}
res.results.push_back(std::move(*itResult));
}
res.range.size = res.results.size();
@@ -85,6 +90,28 @@ namespace lms::db::utils
}
}
template <typename ResultType, typename Query>
void execQuery(Query& query, std::optional<Range> range, bool& moreResults, std::function<void(const ResultType&)> func)
{
if (range)
applyRange(query, Range{ range->offset, range->size + 1 });
moreResults = false;
std::size_t count{};
for (const auto& res : query.resultList())
{
if (range && (count++ == static_cast<std::size_t>(range->size)))
{
moreResults = true;
break;
}
LMS_SCOPED_TRACE_DETAILED("Database", "ExecQueryResult");
func(res);
}
}
Wt::WDateTime normalizeDateTime(const Wt::WDateTime& dateTime);
}