collection resolver progress
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <typeindex>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace matador::utils {
|
||||
class value;
|
||||
@@ -63,9 +64,9 @@ public:
|
||||
template<class ContainerType>
|
||||
static void on_has_many(const char * /*id*/, ContainerType &, const char * /*join_column*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class ContainerType>
|
||||
static 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*/) {}
|
||||
static 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>
|
||||
static void on_has_many_to_many(const char *id, ContainerType &c, const utils::foreign_attributes &/*attr*/) {}
|
||||
static void on_has_many_to_many(const char * /*id*/, ContainerType &c, const utils::foreign_attributes &/*attr*/) {}
|
||||
|
||||
private:
|
||||
size_t column_index_{};
|
||||
@@ -126,32 +127,10 @@ public:
|
||||
}
|
||||
|
||||
template<class CollectionType>
|
||||
void on_has_many(const char * /*id*/, CollectionType &cont, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<object::is_object_ptr<typename CollectionType::value_type>::value> * = nullptr) {
|
||||
using value_type = typename CollectionType::value_type::value_type;
|
||||
auto object_resolver = resolver_->object_resolver<value_type>();
|
||||
auto resolver = resolver_->collection_resolver<typename CollectionType::value_type>(result_type_, join_column);
|
||||
|
||||
std::vector<typename CollectionType::value_type> objects;
|
||||
if (attr.fetch() == utils::fetch_type::Lazy) {
|
||||
cont.reset(std::make_shared<object::collection_proxy<typename CollectionType::value_type>>(resolver, current_pk_));
|
||||
} else {
|
||||
const auto ti = std::type_index(typeid(value_type));
|
||||
auto obj = std::make_shared<typename CollectionType::value_type::value_type>();
|
||||
type_stack_.push(ti);
|
||||
access::process(*this, *obj);
|
||||
type_stack_.pop();
|
||||
auto ptr = typename CollectionType::value_type(std::make_shared<object::object_proxy<value_type>>(object_resolver, obj));
|
||||
const auto pk = ptr.primary_key();
|
||||
if (ptr.primary_key().is_valid()) {
|
||||
cont.push_back(ptr);
|
||||
}
|
||||
}
|
||||
// auto cp = std::make_shared<object::collection_proxy<typename CollectionType::value_type>>(resolver, objects);
|
||||
// cont.reset();
|
||||
}
|
||||
void on_has_many(const char * /*id*/, CollectionType &cont, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<object::is_object_ptr<typename CollectionType::value_type>::value> * = nullptr);
|
||||
|
||||
template<class CollectionType>
|
||||
void on_has_many(const char * /*id*/, CollectionType &, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<!object::is_object_ptr<typename CollectionType::value_type>::value> * = nullptr) {
|
||||
void on_has_many(const char * /*id*/, CollectionType &, const char * /*join_column*/, const utils::foreign_attributes &/*attr*/, std::enable_if_t<!object::is_object_ptr<typename CollectionType::value_type>::value> * = nullptr) {
|
||||
}
|
||||
|
||||
template<class Type>
|
||||
@@ -164,38 +143,8 @@ public:
|
||||
}
|
||||
|
||||
template<class Type>
|
||||
bool fetch(Type &obj) {
|
||||
bool first = true;
|
||||
do {
|
||||
if (auto fetched = reader_->fetch(); !fetched.is_ok() || !*fetched) {
|
||||
return !first;
|
||||
}
|
||||
last_pk_ = current_pk_;
|
||||
current_pk_ = discover_current_primary_key(obj);
|
||||
if (pk_has_changed()) {
|
||||
reader_->unshift();
|
||||
last_pk_.clear();
|
||||
current_pk_.clear();
|
||||
break;
|
||||
}
|
||||
first = false;
|
||||
type_stack_.emplace(typeid(Type));
|
||||
column_index_ = reader_->start_column_index();
|
||||
access::process(*this, obj);
|
||||
type_stack_.pop();
|
||||
} 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;
|
||||
}
|
||||
bool fetch(Type &obj);
|
||||
bool fetch(record &rec);
|
||||
|
||||
[[nodiscard]] const std::vector<object::attribute> &prototype() const;
|
||||
|
||||
@@ -234,8 +183,59 @@ protected:
|
||||
std::stack<std::type_index> type_stack_;
|
||||
utils::identifier current_pk_{};
|
||||
utils::identifier last_pk_{};
|
||||
std::unordered_set<object::collection_composite_key, object::collection_composite_key_hash> initialized_collections_;
|
||||
};
|
||||
|
||||
template<class CollectionType>
|
||||
void query_result_impl::on_has_many(const char *, CollectionType &cont, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<object::is_object_ptr<typename CollectionType::value_type>::value> *) {
|
||||
using value_type = typename CollectionType::value_type::value_type;
|
||||
auto object_resolver = resolver_->object_resolver<value_type>();
|
||||
auto resolver = resolver_->collection_resolver<typename CollectionType::value_type>(result_type_, join_column);
|
||||
|
||||
if (attr.fetch() == utils::fetch_type::Lazy) {
|
||||
cont.reset(std::make_shared<object::collection_proxy<typename CollectionType::value_type>>(resolver, current_pk_));
|
||||
} else {
|
||||
if (initialized_collections_.insert({result_type_, typeid(typename CollectionType::value_type), std::string{join_column}}).second) {
|
||||
cont.reset(std::make_shared<object::collection_proxy<typename CollectionType::value_type>>(resolver, std::vector<typename CollectionType::value_type>()));
|
||||
}
|
||||
|
||||
const auto ti = std::type_index(typeid(value_type));
|
||||
type_stack_.push(ti);
|
||||
auto obj = std::make_shared<typename CollectionType::value_type::value_type>();
|
||||
access::process(*this, *obj);
|
||||
type_stack_.pop();
|
||||
auto ptr = typename CollectionType::value_type(std::make_shared<object::object_proxy<value_type>>(object_resolver, obj));
|
||||
const auto pk = ptr.primary_key();
|
||||
if (ptr.primary_key().is_valid()) {
|
||||
cont.push_back(ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class Type>
|
||||
bool query_result_impl::fetch(Type &obj) {
|
||||
bool first = true;
|
||||
do {
|
||||
if (auto fetched = reader_->fetch(); !fetched.is_ok() || !*fetched) {
|
||||
return !first;
|
||||
}
|
||||
last_pk_ = current_pk_;
|
||||
current_pk_ = discover_current_primary_key(obj);
|
||||
if (pk_has_changed()) {
|
||||
reader_->unshift();
|
||||
last_pk_.clear();
|
||||
current_pk_.clear();
|
||||
break;
|
||||
}
|
||||
first = false;
|
||||
type_stack_.emplace(typeid(Type));
|
||||
column_index_ = reader_->start_column_index();
|
||||
access::process(*this, obj);
|
||||
type_stack_.pop();
|
||||
} while (last_pk_ == current_pk_);
|
||||
return true;
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
template<typename ValueType>
|
||||
void pk_reader::on_primary_key(const char *id, ValueType &value, const utils::primary_key_attribute& attr) {
|
||||
|
||||
Reference in New Issue
Block a user