|
|
|
@@ -18,27 +18,6 @@ class executor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
class query_collection_resolver_producer : public sql::collection_resolver_producer {
|
|
|
|
|
public:
|
|
|
|
@@ -53,6 +32,7 @@ public:
|
|
|
|
|
std::shared_ptr<object::abstract_collection_resolver> produce(sql::executor &exec) override {
|
|
|
|
|
const auto *pk_column = table_[pk_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})
|
|
|
|
|
.from(table_)
|
|
|
|
@@ -63,7 +43,7 @@ public:
|
|
|
|
|
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:
|
|
|
|
@@ -72,6 +52,82 @@ private:
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
void schema_observer<Type>::on_attach(const object::repository_node &node, const Type &/*prototype*/) const {
|
|
|
|
|
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());
|
|
|
|
|
schema_.resolver_producers_[typeid(Type)] = std::move(producer);
|
|
|
|
|
|
|
|
|
|
producer_creator pc(schema_, typeid(Type));
|
|
|
|
|
Type obj;
|
|
|
|
|
access::process(pc, obj);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
|