Sqlite: Add callback method

Sometimes it is better to have a callback instead of returning a container.
The call has to manage the state if an exception is called but otherwise
it will reduce the memory footprint. There will be to a RETURNING
to Sqlite which will read back values as you write.

Change-Id: I7eb49850e2c76f883a03277b31c5e713e9774c92
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Marco Bubke
2021-02-10 15:25:00 +01:00
parent 2197eeb4aa
commit 18fe4233f8
6 changed files with 126 additions and 3 deletions

View File

@@ -37,9 +37,10 @@
#include <utils/span.h>
#include <cstdint>
#include <functional>
#include <memory>
#include <type_traits>
#include <tuple>
#include <type_traits>
using std::int64_t;
@@ -316,6 +317,25 @@ public:
return statement.template fetchValue<Type>(0);
}
template<int ResultTypeCount = 1, typename Callable, typename... QueryTypes>
void readCallback(Callable &&callable, const QueryTypes &...queryValues)
{
BaseStatement::checkColumnCount(ResultTypeCount);
Resetter resetter{*this};
bindValues(queryValues...);
while (BaseStatement::next()) {
auto control = callCallable<ResultTypeCount>(callable);
if (control == CallbackControl::Abort)
break;
}
resetter.reset();
}
protected:
~StatementImplementation() = default;
@@ -398,6 +418,18 @@ private:
return assignValue<ResultOptionalType>(std::make_integer_sequence<int, ResultTypeCount>{});
}
template<typename Callable, int... ColumnIndices>
CallbackControl callCallable(Callable &&callable, std::integer_sequence<int, ColumnIndices...>)
{
return std::invoke(callable, ValueGetter(*this, ColumnIndices)...);
}
template<int ResultTypeCount, typename Callable>
CallbackControl callCallable(Callable &&callable)
{
return callCallable(callable, std::make_integer_sequence<int, ResultTypeCount>{});
}
template<typename ValueType>
void bindValuesByIndex(int index, const ValueType &value)
{