has many fetch with join
This commit is contained in:
@@ -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_{};
|
||||
|
||||
Reference in New Issue
Block a user