collection_resolver progress
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user