collection_resolver progress
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
#ifndef MATADOR_ABSTRACT_COLLECTION_RESOLVER_HPP
|
||||
#define MATADOR_ABSTRACT_COLLECTION_RESOLVER_HPP
|
||||
|
||||
#include <string>
|
||||
#include <typeindex>
|
||||
|
||||
namespace matador::object {
|
||||
class abstract_collection_resolver {
|
||||
public:
|
||||
virtual ~abstract_collection_resolver() = default;
|
||||
|
||||
[[nodiscard]] const std::type_index& root_type() const;
|
||||
[[nodiscard]] const std::type_index& type() const;
|
||||
[[nodiscard]] const std::string& collection_name() const;
|
||||
|
||||
protected:
|
||||
explicit abstract_collection_resolver(const std::type_index& root_type, const std::type_index& type, std::string collection_name);
|
||||
|
||||
public:
|
||||
const std::type_index root_type_;
|
||||
const std::type_index type_;
|
||||
const std::string collection_name_;
|
||||
};
|
||||
}
|
||||
#endif // MATADOR_ABSTRACT_COLLECTION_RESOLVER_HPP
|
||||
@@ -1,30 +1,21 @@
|
||||
#ifndef MATADOR_COLLECTION_RESOLVER_HPP
|
||||
#define MATADOR_COLLECTION_RESOLVER_HPP
|
||||
|
||||
#include "matador/object/abstract_type_resolver.hpp"
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
#include "matador/object/abstract_collection_resolver.hpp"
|
||||
|
||||
#include <typeindex>
|
||||
#include <vector>
|
||||
|
||||
namespace matador::utils {
|
||||
class identifier;
|
||||
}
|
||||
|
||||
namespace matador::object {
|
||||
class abstract_collection_resolver {
|
||||
public:
|
||||
virtual ~abstract_collection_resolver() = default;
|
||||
|
||||
[[nodiscard]] const std::type_index& type() const { return type_; }
|
||||
|
||||
protected:
|
||||
explicit abstract_collection_resolver(const std::type_index& ti) : type_(ti) {}
|
||||
|
||||
public:
|
||||
const std::type_index type_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
class collection_resolver : public abstract_type_resolver {
|
||||
class collection_resolver : public abstract_collection_resolver {
|
||||
public:
|
||||
collection_resolver() : abstract_type_resolver(typeid(std::vector<Type>)) {}
|
||||
collection_resolver(const std::type_index& root_type, std::string collection_name)
|
||||
: abstract_collection_resolver(root_type, typeid(Type), std::move(collection_name)) {}
|
||||
|
||||
virtual std::vector<Type> resolve(const utils::identifier& id) = 0;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef MATADOR_CONTAINER_RESOLVER_FACTORY_HPP
|
||||
#define MATADOR_CONTAINER_RESOLVER_FACTORY_HPP
|
||||
|
||||
#include "matador/object/abstract_type_resolver_factory.hpp"
|
||||
#include "matador/object/collection_resolver.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
@@ -13,11 +12,11 @@ public:
|
||||
virtual void register_collection_resolver(std::shared_ptr<abstract_collection_resolver> &&resolver) = 0;
|
||||
};
|
||||
|
||||
class collection_resolver_factory : public abstract_type_resolver_factory {
|
||||
class collection_resolver_factory : public abstract_collection_resolver_factory {
|
||||
public:
|
||||
template<class Type>
|
||||
std::shared_ptr<collection_resolver<Type>> resolver() {
|
||||
const auto res = acquire_object_resolver(typeid(std::vector<object_ptr<Type>>));
|
||||
std::shared_ptr<collection_resolver<Type>> resolver(const std::type_index &root_type, const std::string &collection_name) {
|
||||
const auto res = acquire_collection_resolver(typeid(Type), root_type, collection_name);
|
||||
if (!res) {
|
||||
return std::dynamic_pointer_cast<collection_resolver<Type>>(res);
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
#ifndef MATADOR_CONTAINER_RESOLVER_HPP
|
||||
#define MATADOR_CONTAINER_RESOLVER_HPP
|
||||
|
||||
#include "matador/object/abstract_type_resolver.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace matador::utils {
|
||||
class identifier;
|
||||
}
|
||||
|
||||
namespace matador::object {
|
||||
template<typename Type>
|
||||
class container_resolver : public abstract_type_resolver {
|
||||
public:
|
||||
container_resolver() : abstract_type_resolver(typeid(Type)) {}
|
||||
|
||||
virtual std::shared_ptr<std::vector<Type>> resolve(const utils::identifier& id) = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MATADOR_CONTAINER_RESOLVER_HPP
|
||||
@@ -18,10 +18,5 @@ public:
|
||||
return std::dynamic_pointer_cast<object_resolver<Type>>(res);
|
||||
}
|
||||
};
|
||||
|
||||
class object_resolver_service : public object_resolver_factory, public collection_resolver_factory {
|
||||
public:
|
||||
};
|
||||
|
||||
}
|
||||
#endif //MATADOR_OBJECT_RESOLVER_FACTORY_HPP
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
#include "matador/sql/internal/collection_resolver_producer.hpp"
|
||||
#include "matador/sql/producer_resolver_factory.hpp"
|
||||
|
||||
#include "matador/query/table.hpp"
|
||||
@@ -55,10 +56,12 @@ public:
|
||||
protected:
|
||||
template<typename Type>
|
||||
friend class schema_observer;
|
||||
friend class producer_creator;
|
||||
|
||||
object::repository repo_;
|
||||
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<sql::composite_key, std::unique_ptr<sql::collection_resolver_producer>, sql::composite_key_hash> collection_resolver_producers_;
|
||||
};
|
||||
}
|
||||
#endif //MATADOR_BASIC_SCHEMA_HPP
|
||||
@@ -0,0 +1,63 @@
|
||||
#ifndef MATADOR_QUERY_CONTAINER_RESOLVER_HPP
|
||||
#define MATADOR_QUERY_CONTAINER_RESOLVER_HPP
|
||||
|
||||
#include "matador/object/collection_resolver.hpp"
|
||||
|
||||
#include "matador/sql/internal/identifier_statement_binder.hpp"
|
||||
#include "matador/sql/statement.hpp"
|
||||
|
||||
#include "matador/query/table.hpp"
|
||||
#include "matador/query/select_query_builder.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
class executor;
|
||||
}
|
||||
|
||||
namespace matador::query {
|
||||
template<typename Type>
|
||||
class query_collection_resolver : public object::collection_resolver<Type> {
|
||||
public:
|
||||
explicit query_collection_resolver(const basic_schema &repo, sql::executor& exec, const table &tab, std::string pk_name, const std::type_index& root_type, std::string join_column)
|
||||
: object::collection_resolver<Type>(root_type, join_column)
|
||||
, executor_(exec)
|
||||
, schema_(repo)
|
||||
, table_(tab)
|
||||
, pk_name_(std::move(pk_name))
|
||||
, join_column_(std::move(join_column))
|
||||
{}
|
||||
|
||||
std::vector<Type> resolve(const utils::identifier &id) override;
|
||||
protected:
|
||||
sql::executor& executor_;
|
||||
const basic_schema &schema_;
|
||||
const table &table_;
|
||||
std::string pk_name_;
|
||||
std::string join_column_;
|
||||
std::type_index index{typeid(Type)};
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
std::vector<Type> query_collection_resolver<Type>::resolve(const utils::identifier &id) {
|
||||
const auto *pk_column = table_[pk_name_];
|
||||
const auto *join_column = table_[join_column_];
|
||||
|
||||
auto stmt = query::select({*pk_column})
|
||||
.from(table_)
|
||||
.where(*join_column == id)
|
||||
.prepare(executor_);
|
||||
|
||||
if (!stmt) {
|
||||
return {};
|
||||
}
|
||||
|
||||
sql::identifier_statement_binder binder(*stmt);
|
||||
binder.bind(id);
|
||||
|
||||
auto result = stmt->template fetch_one_raw<Type>();
|
||||
if (!result) {
|
||||
return {};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
#endif //MATADOR_QUERY_CONTAINER_RESOLVER_HPP
|
||||
@@ -1,42 +0,0 @@
|
||||
#ifndef MATADOR_QUERY_CONTAINER_RESOLVER_HPP
|
||||
#define MATADOR_QUERY_CONTAINER_RESOLVER_HPP
|
||||
|
||||
#include "matador/object/container_resolver.hpp"
|
||||
|
||||
#include "matador/sql/internal/identifier_statement_binder.hpp"
|
||||
#include "matador/sql/statement.hpp"
|
||||
|
||||
#include "matador/query/table.hpp"
|
||||
#include "matador/query/select_query_builder.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
class executor;
|
||||
}
|
||||
|
||||
namespace matador::query {
|
||||
template<typename Type>
|
||||
class query_container_resolver : public object::container_resolver<Type> {
|
||||
public:
|
||||
explicit query_container_resolver(const basic_schema &repo, sql::executor& exec, const table &tab, std::string pk_name)
|
||||
: executor_(exec)
|
||||
, schema_(repo)
|
||||
, table_(tab)
|
||||
, pk_name_(std::move(pk_name)) {}
|
||||
|
||||
std::shared_ptr<Type> resolve(const utils::identifier &id) override;
|
||||
protected:
|
||||
sql::executor& executor_;
|
||||
const basic_schema &schema_;
|
||||
const table &table_;
|
||||
std::string pk_name_;
|
||||
std::type_index index{typeid(Type)};
|
||||
};
|
||||
|
||||
utils::result<sql::statement, utils::error> prepare_collection_statement(sql::executor& exec, entity_query_data &&data);
|
||||
|
||||
template<typename Type>
|
||||
std::shared_ptr<Type> query_container_resolver<Type>::resolve(const utils::identifier &id) {
|
||||
|
||||
}
|
||||
}
|
||||
#endif //MATADOR_QUERY_CONTAINER_RESOLVER_HPP
|
||||
@@ -5,7 +5,9 @@
|
||||
|
||||
#include "matador/sql/query_context.hpp"
|
||||
#include "matador/sql/internal/object_resolver_producer.hpp"
|
||||
#include "matador/sql/internal/collection_resolver_producer.hpp"
|
||||
|
||||
#include "matador/query/query_collection_resolver.hpp"
|
||||
#include "matador/query/query_object_resolver.hpp"
|
||||
#include "matador/query/basic_schema.hpp"
|
||||
|
||||
@@ -38,17 +40,18 @@ private:
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
class query_collection_resolver_producer : public sql::object_resolver_producer {
|
||||
class query_collection_resolver_producer : public sql::collection_resolver_producer {
|
||||
public:
|
||||
query_collection_resolver_producer() = default;
|
||||
query_collection_resolver_producer(const basic_schema& repo, const table& tab, std::string pk_name)
|
||||
: object_resolver_producer(typeid(Type))
|
||||
query_collection_resolver_producer(const basic_schema& repo, const table& tab, std::string pk_name, const std::type_index& root_type, std::string join_column)
|
||||
: collection_resolver_producer(root_type, typeid(Type), std::move(join_column))
|
||||
, repo_(repo)
|
||||
, table_(tab)
|
||||
, pk_name_(std::move(pk_name)) {}
|
||||
, 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_));
|
||||
std::shared_ptr<object::abstract_collection_resolver> produce(sql::executor &exec) override {
|
||||
return std::make_shared<query_collection_resolver<Type>>(repo_, exec, table_, std::move(pk_name_), root_type(), collection_name());
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -167,6 +170,11 @@ utils::result<void, utils::error> schema::drop_table(const sql::connection &conn
|
||||
|
||||
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>
|
||||
void on_primary_key(const char *id, ValueType &value, const utils::primary_key_attribute& attr = utils::default_pk_attributes) {
|
||||
|
||||
@@ -184,8 +192,24 @@ public:
|
||||
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) {
|
||||
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) {
|
||||
// SELECT id FROM books where author_id = ?;
|
||||
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::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::value_type), join_column};
|
||||
schema_.collection_resolver_producers_[key] = std::move(producer);
|
||||
}
|
||||
|
||||
template<class CollectionType>
|
||||
@@ -193,16 +217,26 @@ public:
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
basic_schema& schema_;
|
||||
const std::type_index root_type_;
|
||||
};
|
||||
|
||||
template <typename Type>
|
||||
void schema_observer<Type>::on_attach(const object::repository_node &node, const Type &/*prototype*/) const {
|
||||
const auto it = schema_.insert_table(typeid(Type), node);
|
||||
|
||||
if (it->second.node().info().has_primary_key()) {
|
||||
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);
|
||||
if (!it->second.node().info().has_primary_key()) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
producer_creator pc(schema_, typeid(Type));
|
||||
Type obj;
|
||||
access::process(pc, obj);
|
||||
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef MATADOR_COLLECTION_RESOLVER_PRODUCER_HPP
|
||||
#define MATADOR_COLLECTION_RESOLVER_PRODUCER_HPP
|
||||
|
||||
#include "matador/object/abstract_collection_resolver.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace matador::sql {
|
||||
class executor;
|
||||
class collection_resolver_producer {
|
||||
public:
|
||||
virtual ~collection_resolver_producer() = default;
|
||||
virtual std::shared_ptr<object::abstract_collection_resolver> produce(executor &exec) = 0;
|
||||
|
||||
[[nodiscard]] const std::type_index& root_type() const;
|
||||
[[nodiscard]] const std::type_index& type() const;
|
||||
[[nodiscard]] const std::string& collection_name() const;
|
||||
|
||||
protected:
|
||||
explicit collection_resolver_producer(const std::type_index &root_type, const std::type_index &type, std::string collection_name);
|
||||
|
||||
private:
|
||||
std::type_index root_type_;
|
||||
std::type_index type_;
|
||||
std::string collection_name_;
|
||||
};
|
||||
}
|
||||
#endif // MATADOR_COLLECTION_RESOLVER_PRODUCER_HPP
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef MATADOR_RESOLVER_PRODUCER_HPP
|
||||
#define MATADOR_RESOLVER_PRODUCER_HPP
|
||||
|
||||
#include "matador/object/object_resolver.hpp"
|
||||
#include "matador/object/abstract_type_resolver.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
||||
@@ -1,23 +1,60 @@
|
||||
#ifndef MATADOR_RESOLVER_FACTORY_HPP
|
||||
#define MATADOR_RESOLVER_FACTORY_HPP
|
||||
|
||||
#include "matador/object/object_resolver_factory.hpp"
|
||||
|
||||
#include "matador/sql/internal/object_resolver_producer.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "matador/object/abstract_collection_resolver.hpp"
|
||||
#include "matador/object/object_resolver_factory.hpp"
|
||||
#include "matador/sql/internal/object_resolver_producer.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
class executor;
|
||||
|
||||
class producer_resolver_factory : public object::object_resolver_factory {
|
||||
public:
|
||||
|
||||
std::shared_ptr<object::abstract_type_resolver> acquire_object_resolver(const std::type_index &type) override;
|
||||
void register_object_resolver(std::shared_ptr<object::abstract_type_resolver> &&resolver) override;
|
||||
|
||||
private:
|
||||
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 {
|
||||
public:
|
||||
std::shared_ptr<object::abstract_collection_resolver> acquire_collection_resolver(const std::type_index& root_type,
|
||||
const std::type_index& element_type,
|
||||
const std::string& collection_name) override;
|
||||
void register_collection_resolver(std::shared_ptr<object::abstract_collection_resolver>&& resolver) override;
|
||||
|
||||
private:
|
||||
std::unordered_map<composite_key, std::shared_ptr<object::abstract_collection_resolver>, composite_key_hash> resolvers_;
|
||||
};
|
||||
}
|
||||
#endif //MATADOR_RESOLVER_FACTORY_HPP
|
||||
Reference in New Issue
Block a user