added sqlite query result and enhanced column generator and value extractor

This commit is contained in:
2023-11-14 20:21:51 +01:00
parent 4ed01a617d
commit d76ac60278
44 changed files with 1052 additions and 192 deletions
+58 -14
View File
@@ -1,6 +1,8 @@
#ifndef QUERY_QUERY_RESULT_HPP
#define QUERY_QUERY_RESULT_HPP
#include "matador/sql/query_result_impl.hpp"
#include <memory>
namespace matador::sql {
@@ -15,13 +17,17 @@ public:
using iterator_category = std::forward_iterator_tag;
using value_type = Type;
using difference_type = std::ptrdiff_t;
using self = query_result_iterator<Type>; /**< Shortcut for this class. */
using pointer = value_type*; /**< Shortcut for the pointer type. */
using reference = value_type&; /**< Shortcut for the reference type */
public:
query_result_iterator() = default;
explicit query_result_iterator(query_result<Type> &res, Type *obj = nullptr)
: obj_(obj)
explicit query_result_iterator(query_result<Type> &res)
: result_(res)
{}
query_result_iterator(query_result<Type> &res, std::unique_ptr<Type> obj)
: obj_(std::move(obj))
, result_(res)
{}
query_result_iterator(query_result_iterator&& x) noexcept
@@ -48,19 +54,38 @@ public:
return obj_ != rhs.obj_;
}
self& operator++()
{
obj_.reset(result_.create());
result_.bind(*obj_);
if (!result_.fetch(*obj_)) {
obj_.reset();
}
return *this;
}
self operator++(int)
{
const self tmp(result_, obj_);
obj_.reset(result_.create());
result_.bind(*obj_);
if (!result_.fetch(*obj_)) {
obj_.reset();
}
return std::move(tmp);
}
pointer operator->()
{
return obj_.get();
}
reference operator&()
reference operator*()
{
return &obj_.get();
}
std::unique_ptr<Type> operator*()
{
return std::move(obj_);
return *obj_;
}
pointer get()
@@ -73,7 +98,7 @@ public:
return obj_.release();
}
protected:
private:
std::unique_ptr<Type> obj_;
query_result<Type> &result_;
};
@@ -82,13 +107,32 @@ template < typename Type >
class query_result
{
public:
query_result(std::string sql) : sql_(std::move(sql)) {}
using iterator = query_result_iterator<Type>;
[[nodiscard]] const std::string& str() const { return sql_; }
public:
explicit query_result(std::unique_ptr<query_result_impl> impl)
: impl_(std::move(impl)) {}
iterator begin() { return std::move(++iterator(*this)); }
iterator end() { return {}; }
Type begin() { return {}; }
private:
std::string sql_;
friend class query_result_iterator<Type>;
Type* create() { return new Type; }
void bind(const Type &obj)
{
impl_->bind(obj);
}
bool fetch(Type &obj)
{
return impl_->fetch(obj);
}
private:
std::unique_ptr<query_result_impl> impl_;
};
}