has many fetch with join
This commit is contained in:
@@ -22,7 +22,7 @@ public:
|
||||
object_ptr &operator=(object_ptr &&other) = default;
|
||||
|
||||
using value_type = Type;
|
||||
Type* operator->() { return ptr_.get(); }
|
||||
Type* operator->() const { return ptr_.get(); }
|
||||
Type& operator*() const { return *ptr_; }
|
||||
|
||||
[[nodiscard]] bool empty() const { return ptr_ == nullptr; }
|
||||
|
||||
@@ -8,8 +8,17 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace matador::utils {
|
||||
enum class constraints : unsigned char;
|
||||
}
|
||||
namespace matador::sql {
|
||||
|
||||
enum struct field_type {
|
||||
Attribute,
|
||||
PrimaryKey,
|
||||
ForeignKey
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -17,11 +26,12 @@ class field {
|
||||
public:
|
||||
explicit field(std::string name);
|
||||
template<typename Type>
|
||||
field(std::string name, Type value, const size_t size = 0, const int index = -1)
|
||||
field(std::string name, Type value, const field_type type = field_type::Attribute, const size_t size = 0, const int index = -1)
|
||||
: name_(std::move(name))
|
||||
, type_(type)
|
||||
, index_(index)
|
||||
, value_(value, size) {}
|
||||
field(std::string name, utils::basic_type dt, size_t size = 0, int index = -1);
|
||||
field(std::string name, utils::basic_type dt, field_type type = field_type::Attribute, size_t size = 0, int index = -1);
|
||||
field(const field &x) = default;
|
||||
field& operator=(const field &x) = default;
|
||||
field(field &&x) noexcept;
|
||||
@@ -35,6 +45,7 @@ public:
|
||||
}
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] field_type type() const;
|
||||
[[nodiscard]] size_t size() const;
|
||||
[[nodiscard]] int index() const;
|
||||
|
||||
@@ -53,18 +64,25 @@ public:
|
||||
[[nodiscard]] bool is_blob() const;
|
||||
[[nodiscard]] bool is_null() const;
|
||||
|
||||
[[nodiscard]] bool is_primary_key() const;
|
||||
[[nodiscard]] bool is_foreign_key() const;
|
||||
[[nodiscard]] bool is_attribute() const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream &out, const field &col);
|
||||
|
||||
private:
|
||||
static utils::constraints determine_constraint(field_type type);
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
op.on_attribute(name_.c_str(), value_, value_.size());
|
||||
op.on_attribute(name_.c_str(), value_, { value_.size(), determine_constraint(type_) } );
|
||||
}
|
||||
|
||||
private:
|
||||
friend class record;
|
||||
|
||||
std::string name_;
|
||||
field_type type_{field_type::Attribute};
|
||||
int index_{-1};
|
||||
|
||||
utils::value value_;
|
||||
|
||||
@@ -16,6 +16,7 @@ public:
|
||||
[[nodiscard]] virtual const char* column(size_t index) const = 0;
|
||||
[[nodiscard]] virtual utils::result<bool, utils::error> fetch() = 0;
|
||||
[[nodiscard]] virtual size_t start_column_index() const = 0;
|
||||
virtual void unshift() = 0;
|
||||
|
||||
template<class Type>
|
||||
void bind(Type &obj) {
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include "matador/utils/identifier.hpp"
|
||||
|
||||
#include "matador/sql/interface/query_result_reader.hpp"
|
||||
#include "matador/sql/internal/query_result_pk_resolver.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
@@ -40,19 +42,19 @@ public:
|
||||
template<typename ValueType>
|
||||
void on_primary_key(const char *id, ValueType &value, std::enable_if_t<std::is_integral_v<ValueType> && !std::is_same_v<bool, ValueType>>* = nullptr);
|
||||
void on_primary_key(const char *id, std::string &value, size_t size);
|
||||
void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
|
||||
void on_revision(const char * /*id*/, unsigned long long &/*rev*/) { ++column_index_; }
|
||||
|
||||
template < class Type >
|
||||
void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
||||
void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) { ++column_index_; }
|
||||
template < class Pointer >
|
||||
void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) { ++column_index_; }
|
||||
template < class Pointer >
|
||||
void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) { ++column_index_; }
|
||||
|
||||
template<class ContainerType>
|
||||
void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
||||
void on_has_many(const char * /*id*/, ContainerType &, const char * /*join_column*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class ContainerType>
|
||||
void on_has_many_to_many(const char *id, ContainerType &c, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) {}
|
||||
void on_has_many_to_many(const char *id, ContainerType &c, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class ContainerType>
|
||||
void on_has_many_to_many(const char *id, ContainerType &c, const utils::foreign_attributes &/*attr*/) {}
|
||||
|
||||
@@ -86,6 +88,7 @@ public:
|
||||
{
|
||||
utils::data_type_traits<Type>::read_value(*reader_, id, column_index_++, x);
|
||||
}
|
||||
|
||||
void on_attribute(const char *id, char *value, const utils::field_attributes &attr = utils::null_attributes);
|
||||
void on_attribute(const char *id, std::string &value, const utils::field_attributes &attr = utils::null_attributes);
|
||||
void on_attribute(const char *id, utils::value &val, const utils::field_attributes &attr = utils::null_attributes);
|
||||
@@ -97,23 +100,25 @@ public:
|
||||
}
|
||||
if (attr.fetch() == utils::fetch_type::LAZY) {
|
||||
pk_reader_.read(*x, column_index_++);
|
||||
} else if (const auto ti = std::type_index(typeid(*x)); processed_types_.count(ti) == 0) {
|
||||
processed_types_.insert(ti);
|
||||
} else {
|
||||
const auto ti = std::type_index(typeid(*x));
|
||||
type_stack_.push(ti);
|
||||
access::process(*this, *x);
|
||||
type_stack_.pop();
|
||||
}
|
||||
}
|
||||
template < class Pointer >
|
||||
void on_has_one(const char * /*id*/, Pointer &x, const utils::foreign_attributes &attr)
|
||||
{
|
||||
void on_has_one(const char * /*id*/, Pointer &x, const utils::foreign_attributes &attr) {
|
||||
if (x.empty()) {
|
||||
x.reset(new typename Pointer::value_type);
|
||||
}
|
||||
if (attr.fetch() == utils::fetch_type::LAZY) {
|
||||
pk_reader_.read(*x, column_index_++);
|
||||
} else if (const auto ti = std::type_index(typeid(*x)); processed_types_.count(ti) == 0) {
|
||||
processed_types_.insert(ti);
|
||||
} else {
|
||||
const auto ti = std::type_index(typeid(*x));
|
||||
type_stack_.push(ti);
|
||||
access::process(*this, *x);
|
||||
type_stack_.pop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,9 +130,8 @@ public:
|
||||
void on_has_many(const char * /*id*/, ContainerType &cont, const char * /*join_column*/, const utils::foreign_attributes &attr) {
|
||||
if ( attr.fetch() == utils::fetch_type::LAZY ) {
|
||||
// pk_reader_.read(*id, column_index_++);
|
||||
} else /*if (const auto ti = std::type_index(typeid(typename ContainerType::value_type::value_type)); processed_types_.count(ti) == 0)*/ {
|
||||
} else {
|
||||
const auto ti = std::type_index(typeid(typename ContainerType::value_type::value_type));
|
||||
processed_types_.insert(ti);
|
||||
auto obj = std::make_unique<typename ContainerType::value_type::value_type>();
|
||||
type_stack_.push(ti);
|
||||
access::process(*this, *obj);
|
||||
@@ -148,37 +152,58 @@ public:
|
||||
reader_->bind(obj);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool pk_has_changed() const {
|
||||
return !last_pk_.is_null() && last_pk_ != current_pk_;
|
||||
}
|
||||
|
||||
template<class Type>
|
||||
bool fetch(Type &obj) {
|
||||
bool result = false;
|
||||
bool first = true;
|
||||
do {
|
||||
column_index_ = reader_->start_column_index();
|
||||
auto fetched = reader_->fetch();
|
||||
if (!fetched.is_ok()) {
|
||||
return false;
|
||||
if (auto fetched = reader_->fetch(); !fetched.is_ok() || !*fetched) {
|
||||
return !first;
|
||||
}
|
||||
if (!*fetched) {
|
||||
return false;
|
||||
last_pk_ = current_pk_;
|
||||
current_pk_ = discover_current_primary_key(obj);
|
||||
if (pk_has_changed()) {
|
||||
reader_->unshift();
|
||||
last_pk_.clear();
|
||||
current_pk_.clear();
|
||||
break;
|
||||
}
|
||||
processed_types_.insert(typeid(Type));
|
||||
first = false;
|
||||
type_stack_.push(typeid(Type));
|
||||
column_index_ = reader_->start_column_index();
|
||||
access::process(*this, obj);
|
||||
type_stack_.pop();
|
||||
std::cout << "last pk: " << last_pk_.str() << std::endl;
|
||||
std::cout << "current pk: " << current_pk_.str() << std::endl;
|
||||
std::cout << "last == current: " << std::boolalpha << (last_pk_ == current_pk_) << std::endl;
|
||||
} while (last_pk_ == current_pk_);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool fetch(record &rec) {
|
||||
if (auto fetched = reader_->fetch(); !fetched.is_ok() || !*fetched) {
|
||||
return false;
|
||||
}
|
||||
|
||||
column_index_ = reader_->start_column_index();
|
||||
access::process(*this, rec);
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] const std::vector<object::attribute_definition>& prototype() const;
|
||||
|
||||
private:
|
||||
template<class Type>
|
||||
utils::identifier discover_current_primary_key(const Type &obj) {
|
||||
internal::query_result_pk_resolver resolver(*reader_);
|
||||
return resolver.discover(obj);
|
||||
}
|
||||
|
||||
protected:
|
||||
size_t column_index_ = 0;
|
||||
std::vector<object::attribute_definition> prototype_;
|
||||
std::unique_ptr<query_result_reader> reader_;
|
||||
detail::pk_reader pk_reader_;
|
||||
std::unordered_set<std::type_index> processed_types_;
|
||||
std::stack<std::type_index> type_stack_;
|
||||
utils::identifier current_pk_{};
|
||||
utils::identifier last_pk_{};
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
#ifndef QUERY_RESULT_PK_RESOLVER_HPP
|
||||
#define QUERY_RESULT_PK_RESOLVER_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
#include "matador/utils/foreign_attributes.hpp"
|
||||
#include "matador/utils/identifier.hpp"
|
||||
|
||||
#include "matador/sql/interface/query_result_reader.hpp"
|
||||
|
||||
#include <stack>
|
||||
#include <string>
|
||||
|
||||
namespace matador::sql::internal {
|
||||
|
||||
class query_result_pk_resolver final {
|
||||
public:
|
||||
explicit query_result_pk_resolver(query_result_reader &reader) : reader_(reader) {}
|
||||
|
||||
template<class Type>
|
||||
utils::identifier discover(Type &obj) {
|
||||
column_index_ = reader_.start_column_index();
|
||||
pk_.clear();
|
||||
access::process(*this, obj);
|
||||
|
||||
return pk_;
|
||||
}
|
||||
|
||||
template<typename ValueType>
|
||||
void on_primary_key(const char *id, ValueType &/*value*/, std::enable_if_t<std::is_integral_v<ValueType> && !std::is_same_v<bool, ValueType>>* = nullptr) {
|
||||
if (!type_stack_.empty()) {
|
||||
return;
|
||||
}
|
||||
ValueType value;
|
||||
utils::data_type_traits<ValueType>::read_value(reader_, id, column_index_++, value);
|
||||
pk_ = value;
|
||||
}
|
||||
void on_primary_key(const char *id, std::string &value, size_t size);
|
||||
void on_revision(const char * /*id*/, unsigned long long &/*rev*/) { ++column_index_; }
|
||||
|
||||
template < class Type >
|
||||
void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) { ++column_index_; }
|
||||
void on_attribute(const char *id, const utils::value &x, const utils::field_attributes &attr = utils::null_attributes);
|
||||
|
||||
template < class Pointer >
|
||||
void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &attr) { on_foreign_key<typename Pointer::value_type>(attr.fetch() ); }
|
||||
template < class Pointer >
|
||||
void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &attr) { on_foreign_key<typename Pointer::value_type>(attr.fetch() ); }
|
||||
|
||||
template<class ContainerType>
|
||||
void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class ContainerType>
|
||||
void on_has_many_to_many(const char *id, ContainerType &c, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class ContainerType>
|
||||
void on_has_many_to_many(const char *id, ContainerType &c, const utils::foreign_attributes &/*attr*/) {}
|
||||
|
||||
private:
|
||||
template<class Type>
|
||||
void on_foreign_key(const utils::fetch_type fetch) {
|
||||
if (fetch == utils::fetch_type::LAZY) {
|
||||
++column_index_;
|
||||
} else {
|
||||
const Type obj;
|
||||
type_stack_.push(typeid(Type));
|
||||
access::process(*this, obj);
|
||||
type_stack_.pop();
|
||||
}
|
||||
}
|
||||
private:
|
||||
size_t column_index_{};
|
||||
utils::identifier pk_;
|
||||
query_result_reader &reader_;
|
||||
std::stack<std::type_index> type_stack_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_RESULT_PK_RESOLVER_HPP
|
||||
@@ -1,8 +1,6 @@
|
||||
#ifndef QUERY_RECORD_HPP
|
||||
#define QUERY_RECORD_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
#include "matador/sql/field.hpp"
|
||||
|
||||
#include <vector>
|
||||
@@ -34,8 +32,7 @@ public:
|
||||
~record() = default;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op)
|
||||
{
|
||||
void process(Operator &op) {
|
||||
for(auto &f : fields_) {
|
||||
f.get().process(op);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user