collection resolver progress

This commit is contained in:
Sascha Kühl 2026-02-01 13:17:40 +01:00
parent c838e9dd7b
commit f9333158e2
21 changed files with 247 additions and 192 deletions

View File

@ -129,8 +129,7 @@ std::string postgres_connection::generate_statement_name(const sql::query_contex
return name.str(); return name.str();
} }
utils::result<std::unique_ptr<sql::statement_impl>, utils::error> postgres_connection::prepare( utils::result<std::unique_ptr<sql::statement_impl>, utils::error> postgres_connection::prepare(const sql::query_context &context) {
const sql::query_context &context) {
auto statement_name = generate_statement_name(context); auto statement_name = generate_statement_name(context);
PGresult *result = PQprepare(conn_, statement_name.c_str(), context.sql.c_str(), static_cast<int>(context.bind_vars.size()), nullptr); PGresult *result = PQprepare(conn_, statement_name.c_str(), context.sql.c_str(), static_cast<int>(context.bind_vars.size()), nullptr);

View File

@ -24,7 +24,7 @@ public:
// Eager // Eager
collection_proxy(std::weak_ptr<collection_resolver<Type>> resolver, std::vector<Type> items) collection_proxy(std::weak_ptr<collection_resolver<Type>> resolver, std::vector<Type> items)
: items_(std::move(items)), resolver_(std::move(resolver)) {} : loaded_{true}, items_(std::move(items)), resolver_(std::move(resolver)) {}
// Transient // Transient
explicit collection_proxy(std::vector<Type> items) explicit collection_proxy(std::vector<Type> items)

View File

@ -0,0 +1,20 @@
#ifndef MATADOR_COLLECTION_UTILS_HPP
#define MATADOR_COLLECTION_UTILS_HPP
#include <string>
#include <typeindex>
namespace matador::object {
struct collection_composite_key {
std::type_index root_type;
std::type_index type;
std::string name;
bool operator==(const collection_composite_key& other) const;
};
struct collection_composite_key_hash {
std::size_t operator()(const collection_composite_key& k) const noexcept;
};
}
#endif //MATADOR_COLLECTION_UTILS_HPP

View File

@ -62,7 +62,7 @@ protected:
object::repository repo_; object::repository repo_;
std::unordered_map<std::type_index, schema_node> schema_nodes_; std::unordered_map<std::type_index, schema_node> schema_nodes_;
std::unordered_map<std::type_index, std::unique_ptr<sql::object_resolver_producer>> resolver_producers_; std::unordered_map<std::type_index, std::unique_ptr<sql::object_resolver_producer>> resolver_producers_;
std::unordered_map<sql::composite_key, std::unique_ptr<sql::collection_resolver_producer>, sql::composite_key_hash> collection_resolver_producers_; std::unordered_map<object::collection_composite_key, std::unique_ptr<sql::collection_resolver_producer>, object::collection_composite_key_hash> collection_resolver_producers_;
}; };
} }
#endif //MATADOR_BASIC_SCHEMA_HPP #endif //MATADOR_BASIC_SCHEMA_HPP

View File

@ -15,15 +15,20 @@ namespace matador::query {
template<typename Type> template<typename Type>
class query_collection_resolver : public object::collection_resolver<Type> { class query_collection_resolver : public object::collection_resolver<Type> {
public: public:
explicit query_collection_resolver(sql::statement &&stmt, const std::type_index& root_type, std::string join_column) explicit query_collection_resolver(sql::statement &&stmt,
const std::type_index& root_type,
std::string join_column,
const std::shared_ptr<object::object_resolver<typename Type::value_type>> &resolver)
: object::collection_resolver<Type>(root_type, join_column) : object::collection_resolver<Type>(root_type, join_column)
, stmt_(std::move(stmt)) , stmt_(std::move(stmt))
, resolver_(resolver)
{} {}
std::vector<Type> resolve(const utils::identifier &id) override; std::vector<Type> resolve(const utils::identifier &id) override;
protected: protected:
sql::statement stmt_; sql::statement stmt_;
std::type_index index{typeid(Type)}; std::type_index index{typeid(Type)};
std::shared_ptr<object::object_resolver<typename Type::value_type>> resolver_;
}; };
struct value_to_identifier{ struct value_to_identifier{
@ -76,7 +81,7 @@ std::vector<Type> query_collection_resolver<Type>::resolve(const utils::identifi
} }
identifier_creator creator; identifier_creator creator;
r.at(0).process(creator); r.at(0).process(creator);
const auto op = std::make_shared<object::object_proxy<typename Type::value_type>>(std::shared_ptr<object::object_resolver<typename Type::value_type>>{}, creator.visitor.id_); const auto op = std::make_shared<object::object_proxy<typename Type::value_type>>(resolver_, creator.visitor.id_);
out.emplace_back(op); out.emplace_back(op);
} }
return out; return out;

View File

@ -18,27 +18,6 @@ class executor;
} }
namespace matador::query { namespace matador::query {
template<typename Type>
class query_object_resolver_producer : public sql::object_resolver_producer {
public:
query_object_resolver_producer() = default;
query_object_resolver_producer(const basic_schema& repo, const table& tab, std::string pk_name)
: object_resolver_producer(typeid(Type))
, repo_(repo)
, table_(tab)
, pk_name_(std::move(pk_name)) {}
std::shared_ptr<object::abstract_type_resolver> produce(sql::executor &exec) override {
return std::make_shared<query_object_resolver<Type>>(repo_, exec, table_, std::move(pk_name_));
}
private:
const basic_schema& repo_;
const table& table_;
std::string pk_name_;
};
template<typename Type> template<typename Type>
class query_collection_resolver_producer : public sql::collection_resolver_producer { class query_collection_resolver_producer : public sql::collection_resolver_producer {
public: public:
@ -53,6 +32,7 @@ public:
std::shared_ptr<object::abstract_collection_resolver> produce(sql::executor &exec) override { std::shared_ptr<object::abstract_collection_resolver> produce(sql::executor &exec) override {
const auto *pk_column = table_[pk_name_]; const auto *pk_column = table_[pk_name_];
const auto *join_column = table_[collection_name()]; const auto *join_column = table_[collection_name()];
const auto object_resolver = exec.resolver()->object_resolver<typename Type::value_type>();
auto stmt = query::select({*pk_column}) auto stmt = query::select({*pk_column})
.from(table_) .from(table_)
@ -63,7 +43,7 @@ public:
return {}; return {};
} }
return std::make_shared<query_collection_resolver<Type>>(stmt.release(), root_type(), collection_name()); return std::make_shared<query_collection_resolver<Type>>(stmt.release(), root_type(), collection_name(), object_resolver);
} }
private: private:
@ -72,6 +52,82 @@ private:
std::string pk_name_; std::string pk_name_;
}; };
class producer_creator final {
public:
producer_creator(basic_schema& schema, const std::type_index& root_type)
: schema_(schema)
, root_type_(root_type)
{}
template<typename ValueType>
static void on_primary_key(const char * /*id*/, ValueType &/*value*/, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {}
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
template<class Type>
static void on_attribute(const char * /*id*/, Type &/*value*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template<class Pointer>
static void on_belongs_to(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) {}
template<class Pointer>
static void on_has_one(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) {}
template<class ContainerType>
static void on_has_many_to_many(const char *, ContainerType &, 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 *, ContainerType &, const utils::foreign_attributes &/*attr*/) {}
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) {
const auto it = schema_.find(typeid(typename CollectionType::value_type::value_type));
if (it == schema_.end()) {
throw query_builder_exception{query_build_error::UnknownType};
}
if (!it->second.node().info().has_primary_key()) {
throw query_builder_exception{query_build_error::MissingPrimaryKey};
}
auto producer = std::make_unique<query_collection_resolver_producer<typename CollectionType::value_type>>(
schema_,
it->second.table(),
it->second.node().info().primary_key_attribute()->name(),
root_type_,
join_column);
const object::collection_composite_key key{root_type_, typeid(typename CollectionType::value_type), join_column};
schema_.collection_resolver_producers_[key] = std::move(producer);
}
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) {
}
private:
basic_schema& schema_;
const std::type_index root_type_;
};
template<typename Type>
class query_object_resolver_producer : public sql::object_resolver_producer {
public:
query_object_resolver_producer() = default;
query_object_resolver_producer(basic_schema& repo, const table& tab, std::string pk_name)
: object_resolver_producer(typeid(Type))
, repo_(repo)
, table_(tab)
, pk_name_(std::move(pk_name)) {}
std::shared_ptr<object::abstract_type_resolver> produce(sql::executor &exec) override {
producer_creator pc(repo_, typeid(Type));
Type obj;
access::process(pc, obj);
return std::make_shared<query_object_resolver<Type>>(repo_, exec, table_, std::move(pk_name_));
}
private:
basic_schema& repo_;
const table& table_;
std::string pk_name_;
};
class schema; class schema;
using schema_ref = std::reference_wrapper<schema>; using schema_ref = std::reference_wrapper<schema>;
@ -180,57 +236,6 @@ utils::result<void, utils::error> schema::drop_table(const sql::connection &conn
return utils::failure(info.err()); return utils::failure(info.err());
} }
class producer_creator final {
public:
producer_creator(basic_schema& schema, const std::type_index& root_type)
: schema_(schema)
, root_type_(root_type)
{}
template<typename ValueType>
static void on_primary_key(const char * /*id*/, ValueType &/*value*/, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {}
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
template<class Type>
static void on_attribute(const char * /*id*/, Type &/*value*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template<class Pointer>
static void on_belongs_to(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) {}
template<class Pointer>
static void on_has_one(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) {}
template<class ContainerType>
static void on_has_many_to_many(const char *, ContainerType &, 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 *, ContainerType &, const utils::foreign_attributes &/*attr*/) {}
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) {
const auto it = schema_.find(typeid(typename CollectionType::value_type::value_type));
if (it == schema_.end()) {
throw query_builder_exception{query_build_error::UnknownType};
}
if (!it->second.node().info().has_primary_key()) {
throw query_builder_exception{query_build_error::MissingPrimaryKey};
}
auto producer = std::make_unique<query_collection_resolver_producer<typename CollectionType::value_type>>(
schema_,
it->second.table(),
it->second.node().info().primary_key_attribute()->name(),
root_type_,
join_column);
const sql::composite_key key{root_type_, typeid(typename CollectionType::value_type), join_column};
schema_.collection_resolver_producers_[key] = std::move(producer);
}
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) {
}
private:
basic_schema& schema_;
const std::type_index root_type_;
};
template <typename Type> template <typename Type>
void schema_observer<Type>::on_attach(const object::repository_node &node, const Type &/*prototype*/) const { void schema_observer<Type>::on_attach(const object::repository_node &node, const Type &/*prototype*/) const {
const auto it = schema_.insert_table(typeid(Type), node); const auto it = schema_.insert_table(typeid(Type), node);
@ -241,11 +246,6 @@ void schema_observer<Type>::on_attach(const object::repository_node &node, const
auto producer = std::make_unique<query_object_resolver_producer<Type>>(schema_, it->second.table(), it->second.node().info().primary_key_attribute()->name()); auto producer = std::make_unique<query_object_resolver_producer<Type>>(schema_, it->second.table(), it->second.node().info().primary_key_attribute()->name());
schema_.resolver_producers_[typeid(Type)] = std::move(producer); schema_.resolver_producers_[typeid(Type)] = std::move(producer);
producer_creator pc(schema_, typeid(Type));
Type obj;
access::process(pc, obj);
} }
template <typename Type> template <typename Type>

View File

@ -149,7 +149,7 @@ public:
} }
template<class CollectionType> 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 ContainerType> template<class ContainerType>

View File

@ -23,6 +23,7 @@
#include <stack> #include <stack>
#include <string> #include <string>
#include <typeindex> #include <typeindex>
#include <unordered_set>
namespace matador::utils { namespace matador::utils {
class value; class value;
@ -63,9 +64,9 @@ public:
template<class ContainerType> template<class ContainerType>
static void on_has_many(const char * /*id*/, ContainerType &, const char * /*join_column*/, const utils::foreign_attributes &/*attr*/) {} static void on_has_many(const char * /*id*/, ContainerType &, const char * /*join_column*/, const utils::foreign_attributes &/*attr*/) {}
template<class ContainerType> 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> 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: private:
size_t column_index_{}; size_t column_index_{};
@ -126,32 +127,10 @@ public:
} }
template<class CollectionType> 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) { 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();
}
template<class CollectionType> 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> template<class Type>
@ -164,38 +143,8 @@ public:
} }
template<class Type> template<class Type>
bool fetch(Type &obj) { bool fetch(Type &obj);
bool first = true; bool fetch(record &rec);
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;
}
[[nodiscard]] const std::vector<object::attribute> &prototype() const; [[nodiscard]] const std::vector<object::attribute> &prototype() const;
@ -234,8 +183,59 @@ protected:
std::stack<std::type_index> type_stack_; std::stack<std::type_index> type_stack_;
utils::identifier current_pk_{}; utils::identifier current_pk_{};
utils::identifier last_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 { namespace detail {
template<typename ValueType> template<typename ValueType>
void pk_reader::on_primary_key(const char *id, ValueType &value, const utils::primary_key_attribute& attr) { void pk_reader::on_primary_key(const char *id, ValueType &value, const utils::primary_key_attribute& attr) {

View File

@ -6,6 +6,7 @@
#include "matador/object/abstract_collection_resolver.hpp" #include "matador/object/abstract_collection_resolver.hpp"
#include "matador/object/object_resolver_factory.hpp" #include "matador/object/object_resolver_factory.hpp"
#include "matador/object/collection_resolver_factory.hpp" #include "matador/object/collection_resolver_factory.hpp"
#include "matador/object/collection_utils.hpp"
namespace matador::sql { namespace matador::sql {
class executor; class executor;
@ -19,33 +20,6 @@ private:
std::unordered_map<std::type_index, std::shared_ptr<object::abstract_type_resolver>> resolvers_; std::unordered_map<std::type_index, std::shared_ptr<object::abstract_type_resolver>> resolvers_;
}; };
struct composite_key {
std::type_index root_type;
std::type_index type;
std::string name;
bool operator==(const composite_key& other) const {
return root_type == other.root_type &&
type == other.type &&
name == other.name;
}
};
struct composite_key_hash {
std::size_t operator()(const composite_key& k) const noexcept {
const std::size_t h1 = std::hash<std::type_index>{}(k.root_type);
const std::size_t h2 = std::hash<std::type_index>{}(k.type);
const std::size_t h3 = std::hash<std::string>{}(k.name);
// Klassische Hash-Kombination (Boost-Style)
std::size_t seed = h1;
seed ^= h2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);
seed ^= h3 + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
}
};
class producer_collection_resolver_factory : public object::collection_resolver_factory { class producer_collection_resolver_factory : public object::collection_resolver_factory {
public: public:
[[nodiscard]] std::shared_ptr<object::abstract_collection_resolver> acquire_collection_resolver(const std::type_index& root_type, [[nodiscard]] std::shared_ptr<object::abstract_collection_resolver> acquire_collection_resolver(const std::type_index& root_type,
@ -54,7 +28,7 @@ public:
void register_collection_resolver(std::shared_ptr<object::abstract_collection_resolver>&& resolver) override; void register_collection_resolver(std::shared_ptr<object::abstract_collection_resolver>&& resolver) override;
private: private:
std::unordered_map<composite_key, std::shared_ptr<object::abstract_collection_resolver>, composite_key_hash> resolvers_; std::unordered_map<object::collection_composite_key, std::shared_ptr<object::abstract_collection_resolver>, object::collection_composite_key_hash> resolvers_;
}; };
} }
#endif //MATADOR_RESOLVER_FACTORY_HPP #endif //MATADOR_RESOLVER_FACTORY_HPP

View File

@ -120,6 +120,8 @@ add_library(matador-core STATIC
utils/uuid.cpp utils/uuid.cpp
utils/value.cpp utils/value.cpp
utils/version.cpp utils/version.cpp
../../include/matador/object/collection_utils.hpp
object/collection_utils.cpp
) )
target_link_libraries(matador-core ${CMAKE_DL_LIBS}) target_link_libraries(matador-core ${CMAKE_DL_LIBS})

View File

@ -0,0 +1,22 @@
#include "matador/object/collection_utils.hpp"
namespace matador::object {
bool collection_composite_key::operator==(const collection_composite_key &other) const {
return root_type == other.root_type &&
type == other.type &&
name == other.name;
}
std::size_t collection_composite_key_hash::operator()(const collection_composite_key &k) const noexcept {
const std::size_t h1 = std::hash<std::type_index>{}(k.root_type);
const std::size_t h2 = std::hash<std::type_index>{}(k.type);
const std::size_t h3 = std::hash<std::string>{}(k.name);
// Klassische Hash-Kombination (Boost-Style)
std::size_t seed = h1;
seed ^= h2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);
seed ^= h3 + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
}
}

View File

@ -39,6 +39,16 @@ query_result_impl::on_attribute(const char *id, utils::value &val, const utils::
reader_->read_value(id, column_index_++, val, attr.size()); reader_->read_value(id, column_index_++, val, attr.size());
} }
bool query_result_impl::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;
}
const std::vector<object::attribute> &query_result_impl::prototype() const { const std::vector<object::attribute> &query_result_impl::prototype() const {
return prototype_; return prototype_;
} }

View File

@ -244,7 +244,12 @@ utils::result<std::unique_ptr<statement_impl>, utils::error> connection::perform
const auto rit = std::find_if( const auto rit = std::find_if(
std::begin(*result), std::begin(*result),
std::end(*result), std::end(*result),
[&col](const auto &value) { return value.name() == col.name(); } [&col, &ctx](const auto &value) {
if (ctx.table_name.empty()) {
return value.name() == col.name();
}
return value.name() == col.name() || ctx.table_name + "." + value.name() == col.name();
}
); );
if (col.type() == utils::basic_type::Null && rit != result->end()) { if (col.type() == utils::basic_type::Null && rit != result->end()) {
const_cast<object::attribute&>(col).change_type(rit->type()); const_cast<object::attribute&>(col).change_type(rit->type());

View File

@ -4,12 +4,15 @@ namespace matador::sql {
const std::type_index& collection_resolver_producer::root_type() const { const std::type_index& collection_resolver_producer::root_type() const {
return root_type_; return root_type_;
} }
const std::type_index& collection_resolver_producer::type() const { const std::type_index& collection_resolver_producer::type() const {
return type_; return type_;
} }
const std::string& collection_resolver_producer::collection_name() const { const std::string& collection_resolver_producer::collection_name() const {
return collection_name_; return collection_name_;
} }
collection_resolver_producer::collection_resolver_producer(const std::type_index& root_type, collection_resolver_producer::collection_resolver_producer(const std::type_index& root_type,
const std::type_index& type, const std::type_index& type,
std::string collection_name) std::string collection_name)

View File

@ -15,14 +15,14 @@ std::shared_ptr<object::abstract_collection_resolver>
producer_collection_resolver_factory::acquire_collection_resolver(const std::type_index& root_type, producer_collection_resolver_factory::acquire_collection_resolver(const std::type_index& root_type,
const std::type_index& element_type, const std::type_index& element_type,
const std::string& collection_name) const { const std::string& collection_name) const {
const composite_key key{root_type, element_type, collection_name}; const object::collection_composite_key key{root_type, element_type, collection_name};
if (const auto it = resolvers_.find(key); it != resolvers_.end()) { if (const auto it = resolvers_.find(key); it != resolvers_.end()) {
return it->second; return it->second;
} }
return nullptr; return nullptr;
} }
void producer_collection_resolver_factory::register_collection_resolver(std::shared_ptr<object::abstract_collection_resolver>&& resolver) { void producer_collection_resolver_factory::register_collection_resolver(std::shared_ptr<object::abstract_collection_resolver>&& resolver) {
const composite_key key{resolver->root_type(), resolver->type(), resolver->collection_name()}; const object::collection_composite_key key{resolver->root_type(), resolver->type(), resolver->collection_name()};
resolvers_[key] = std::move(resolver); resolvers_[key] = std::move(resolver);
} }
} // namespace matador::sql } // namespace matador::sql

View File

@ -588,10 +588,8 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
} }
TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy has many relation", "[query][has_many][lazy]") { TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy has many relation", "[query][has_many][lazy]") {
// auto result = repo.attach<author>("authors") auto result = repo.attach<author>("authors")
// .and_then( [this] { return repo.attach<book>("books"); } ) .and_then( [this] { return repo.attach<book>("books"); } )
auto result = repo.attach<book>("books")
.and_then( [this] { return repo.attach<author>("authors"); } )
.and_then([this] { .and_then([this] {
return repo.create(db); return repo.create(db);
} ); } );
@ -662,6 +660,13 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy has many relation", "
REQUIRE(a->year_of_birth == authors.at(index)->year_of_birth); REQUIRE(a->year_of_birth == authors.at(index)->year_of_birth);
REQUIRE(a->distinguished == authors.at(index)->distinguished); REQUIRE(a->distinguished == authors.at(index)->distinguished);
REQUIRE(!a->books.empty()); REQUIRE(!a->books.empty());
for (const auto& b : a->books) {
const auto title = b->title;
// REQUIRE(b->id == books.at(index)->id);
// REQUIRE(b->title == books.at(index)->title);
// REQUIRE(b->author_id == books.at(index)->author_id);
// REQUIRE(b->published_in == books.at(index)->published_in);
}
++index; ++index;
} }
} }
@ -735,11 +740,12 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation",
} }
TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation", "[query][belongs_to][eager]") { TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation", "[query][belongs_to][eager]") {
auto result = repo.attach<author>("authors") // auto result = repo.attach<author>("authors")
.and_then( [this] { return repo.attach<book>("books"); } ) // .and_then( [this] { return repo.attach<book>("books"); } )
.and_then([this] { auto result = repo.attach<book>("books")
return repo.create(db); .and_then( [this] { return repo.attach<author>("authors"); })
} ); .and_then([this] { return repo.create(db); });
REQUIRE(result.is_ok());
const std::vector authors { const std::vector authors {
object_ptr{std::make_shared<author>(1, "Michael", "Crichton", "23.10.1942", 1975, true)}, object_ptr{std::make_shared<author>(1, "Michael", "Crichton", "23.10.1942", 1975, true)},

View File

@ -17,8 +17,10 @@ TEST_CASE_METHOD(SchemaFixture, "Test schema one-two-many", "[schema][one-to-man
using namespace matador::test; using namespace matador::test;
query::schema repo; query::schema repo;
auto result = repo.attach<department>("departments") // auto result = repo.attach<department>("departments")
.and_then([&repo] { return repo.attach<employee>("employees"); }); // .and_then([&repo] { return repo.attach<employee>("employees"); });
auto result = repo.attach<employee>("employees")
.and_then([&repo] { return repo.attach<department>("departments"); });
REQUIRE(result); REQUIRE(result);
auto conn = pool.acquire(); auto conn = pool.acquire();

View File

@ -165,9 +165,12 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][f
} }
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many lazy relation", "[session][find][one-to-many][eager]") { TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many lazy relation", "[session][find][one-to-many][eager]") {
auto result = schema.attach<author>("authors") // auto result = schema.attach<author>("authors")
.and_then( [this] { return schema.attach<book>("books"); } ) // .and_then( [this] { return schema.attach<book>("books"); } )
auto result = schema.attach<book>("books")
.and_then( [this] { return schema.attach<author>("authors"); } )
.and_then([this] { return schema.create(db); } ); .and_then([this] { return schema.create(db); } );
REQUIRE(result.is_ok());
schema.initialize_executor(ses); schema.initialize_executor(ses);
std::vector authors { std::vector authors {
@ -217,11 +220,14 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
} }
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many eager relation", "[session][find][one-to-many][eager]") { TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many eager relation", "[session][find][one-to-many][eager]") {
auto result = schema.attach<department>("departments") // auto result = repo.attach<department>("departments")
.and_then( [this] { return schema.attach<employee>("employees"); } ) // .and_then([this] { return repo.attach<employee>("employees"); })
auto result = schema.attach<employee>("employees")
.and_then([this] { return schema.attach<department>("departments"); })
.and_then([this] { .and_then([this] {
return schema.create(db); return schema.create(db);
} ); } );
REQUIRE(result.is_ok());
std::vector<std::unique_ptr<department>> departments; std::vector<std::unique_ptr<department>> departments;
departments.emplace_back(new department{1, "Insurance"}); departments.emplace_back(new department{1, "Insurance"});

View File

@ -6,8 +6,7 @@
#include "matador/utils/access.hpp" #include "matador/utils/access.hpp"
#include "matador/object/object_ptr.hpp" #include "matador/object/object_ptr.hpp"
#include "matador/object/collection.hpp"
#include <vector>
namespace matador::test { namespace matador::test {
struct order { struct order {
@ -23,7 +22,7 @@ struct order {
std::string ship_region; std::string ship_region;
std::string ship_postal_code; std::string ship_postal_code;
std::string ship_country; std::string ship_country;
std::vector<object::object_ptr<order_details> > order_details_; object::collection<object::object_ptr<order_details> > order_details_;
template<class Operator> template<class Operator>
void process(Operator &op) { void process(Operator &op) {

View File

@ -52,6 +52,7 @@ utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> test_connec
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<test_result_reader>(), return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<test_result_reader>(),
context.prototype, context.prototype,
context.resolver, context.resolver,
context.result_type,
context.prototype.size())); context.prototype.size()));
} }

View File

@ -22,6 +22,7 @@ utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> test_statem
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<test_result_reader>(), return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<test_result_reader>(),
query_.prototype, query_.prototype,
query_.resolver, query_.resolver,
query_.result_type,
query_.prototype.size())); query_.prototype.size()));
} }