303 lines
11 KiB
C++
303 lines
11 KiB
C++
#ifndef MATADOR_SCHEMA_HPP
|
|
#define MATADOR_SCHEMA_HPP
|
|
|
|
#include "matador/object/observer.hpp"
|
|
|
|
#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/select_query_builder.hpp"
|
|
#include "matador/query/basic_schema.hpp"
|
|
|
|
namespace matador::sql {
|
|
class connection_pool;
|
|
class connection;
|
|
class executor;
|
|
}
|
|
|
|
namespace matador::query {
|
|
template<typename Type>
|
|
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, 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))
|
|
{}
|
|
|
|
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_)
|
|
.where(*join_column == utils::_)
|
|
.prepare(exec);
|
|
|
|
if (!stmt) {
|
|
return {};
|
|
}
|
|
|
|
return std::make_shared<query_collection_resolver<Type>>(stmt.release(), root_type(), collection_name(), object_resolver);
|
|
}
|
|
|
|
private:
|
|
const basic_schema& repo_;
|
|
const table& table_;
|
|
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 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) {
|
|
|
|
}
|
|
|
|
template<class CollectionType>
|
|
void on_has_many_to_many(const char *id, CollectionType &, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) {
|
|
const auto it = schema_.find(id);
|
|
if (it == schema_.end()) {
|
|
throw query_builder_exception{query_build_error::UnknownType};
|
|
}
|
|
|
|
auto producer = std::make_unique<query_collection_resolver_producer<typename CollectionType::value_type>>(
|
|
schema_,
|
|
it->second.table(),
|
|
inverse_join_column,
|
|
root_type_,
|
|
join_column);
|
|
const object::collection_composite_key key{root_type_, typeid(typename CollectionType::value_type), inverse_join_column};
|
|
schema_.collection_resolver_producers_[key] = std::move(producer);
|
|
}
|
|
|
|
template<class CollectionType>
|
|
void on_has_many_to_many(const char *id, CollectionType &, const utils::foreign_attributes &/*attr*/) {
|
|
const auto it = schema_.find(id);
|
|
if (it == schema_.end()) {
|
|
throw query_builder_exception{query_build_error::UnknownType};
|
|
}
|
|
|
|
object::join_columns_collector collector;
|
|
const auto jc = collector.collect<typename CollectionType::value_type::value_type>();
|
|
|
|
auto producer = std::make_unique<query_collection_resolver_producer<typename CollectionType::value_type>>(
|
|
schema_,
|
|
it->second.table(),
|
|
jc.join_column,
|
|
root_type_,
|
|
jc.inverse_join_column);
|
|
const object::collection_composite_key key{root_type_, typeid(typename CollectionType::value_type), jc.join_column};
|
|
schema_.collection_resolver_producers_[key] = std::move(producer);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
select_query_builder qb(repo_);
|
|
const auto *pk_column = table_[pk_name_];
|
|
auto builder_result = qb.build<Type>(*pk_column == utils::_);
|
|
|
|
if (!builder_result) {
|
|
return nullptr;
|
|
}
|
|
|
|
auto stmt = builder_result
|
|
.release()
|
|
.prepare(exec);
|
|
|
|
if (!stmt) {
|
|
return nullptr;
|
|
}
|
|
|
|
return std::make_shared<query_object_resolver<Type>>(stmt.release());
|
|
}
|
|
|
|
private:
|
|
basic_schema& repo_;
|
|
const table& table_;
|
|
std::string pk_name_;
|
|
};
|
|
|
|
class schema;
|
|
|
|
using schema_ref = std::reference_wrapper<schema>;
|
|
|
|
template<typename Type>
|
|
class schema_observer final : public object::observer<Type> {
|
|
public:
|
|
explicit schema_observer(schema& s)
|
|
: schema_(s) {}
|
|
template<typename OtherType>
|
|
explicit schema_observer(const schema_observer<OtherType> &x)
|
|
: schema_(x.schema_)
|
|
{}
|
|
void on_attach(const object::repository_node &node, const Type &prototype) const override;
|
|
void on_detach(const object::repository_node &node, const Type &prototype) const override;
|
|
void on_insert(const Type &obj) override;
|
|
void on_update(const Type &obj) override;
|
|
void on_delete(const Type &obj) override;
|
|
|
|
private:
|
|
template < class OtherType >
|
|
friend class schema_observer;
|
|
|
|
schema& schema_;
|
|
};
|
|
|
|
class schema final : public basic_schema {
|
|
public:
|
|
using basic_schema::basic_schema;
|
|
using basic_schema::iterator;
|
|
using basic_schema::const_iterator;
|
|
using basic_schema::contains;
|
|
using basic_schema::find;
|
|
|
|
|
|
template<typename Type, typename... Observers>
|
|
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, Observers&&... observers) {
|
|
return repo_.attach<Type>(name, schema_observer<Type>{*this}, std::forward<Observers>(observers)...);
|
|
}
|
|
|
|
template<typename Type, typename... Observers>
|
|
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, const std::string &parent, Observers&&... observers) {
|
|
return repo_.attach<Type>(name, parent, schema_observer<Type>{*this}, std::forward<Observers>(observers)...);
|
|
}
|
|
|
|
template<typename Type, typename SuperType, typename... Observers>
|
|
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, Observers&&... observers) {
|
|
return repo_.attach<Type, SuperType>(name, schema_observer<Type>{*this}, std::forward<Observers>(observers)...);
|
|
}
|
|
|
|
[[nodiscard]] utils::result<void, utils::error> create(const sql::connection &conn) const;
|
|
[[nodiscard]] utils::result<void, utils::error> drop(const sql::connection &conn) const;
|
|
|
|
template<typename Type>
|
|
[[nodiscard]] utils::result<void, utils::error> drop_table(const sql::connection &conn);
|
|
[[nodiscard]] static utils::result<void, utils::error> drop_table(const std::string &table_name, const sql::connection &conn);
|
|
|
|
[[nodiscard]] static utils::result<std::vector<object::attribute>, utils::error> describe_table(const std::string &table_name, const sql::connection &conn) ;
|
|
[[nodiscard]] utils::result<bool, utils::error> table_exists(const std::string &table_name, const sql::connection &conn) const;
|
|
|
|
template<typename Type>
|
|
iterator find() { return basic_schema::find(typeid(Type)); }
|
|
|
|
template<typename Type>
|
|
const_iterator find() const { return basic_schema::find(typeid(Type)); }
|
|
|
|
template < typename Type >
|
|
[[nodiscard]] bool contains() const {
|
|
return basic_schema::contains(std::type_index(typeid(Type)));
|
|
}
|
|
|
|
const object::repository &repo() const { return repo_; }
|
|
object::repository &repo() { return repo_; }
|
|
|
|
void dump(std::ostream &os) const;
|
|
|
|
private:
|
|
[[nodiscard]] static sql::query_context build_add_constraint_context(const object::repository_node& node, const object::restriction& cons, const sql::connection &conn) ;
|
|
[[nodiscard]] static sql::query_context build_drop_constraint_context(const object::repository_node& node, const object::restriction& cons, const sql::connection &conn) ;
|
|
|
|
iterator insert_table(const std::type_index& ti, const object::repository_node &node, utils::generator_type generator_type);
|
|
|
|
private:
|
|
template<typename Type>
|
|
friend class schema_observer;
|
|
};
|
|
|
|
template<typename Type>
|
|
utils::result<void, utils::error> schema::drop_table(const sql::connection &conn) {
|
|
auto info = repo_.basic_info<Type>();
|
|
if (info) {
|
|
return drop_table(info->get().name(), conn);
|
|
}
|
|
|
|
return utils::failure(info.err());
|
|
}
|
|
|
|
template <typename Type>
|
|
void schema_observer<Type>::on_attach(const object::repository_node &node, const Type &/*prototype*/) const {
|
|
primary_key_generator_finder finder;
|
|
const auto generator_type = finder.find(node.info<Type>().get());
|
|
|
|
const auto it = schema_.insert_table(typeid(Type), node, generator_type);
|
|
|
|
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);
|
|
}
|
|
|
|
template <typename Type>
|
|
void schema_observer<Type>::on_detach(const object::repository_node &/*node*/, const Type &/*prototype*/) const {}
|
|
template <typename Type>
|
|
void schema_observer<Type>::on_insert(const Type &/*obj*/) {}
|
|
template <typename Type>
|
|
void schema_observer<Type>::on_update(const Type &/*obj*/) {}
|
|
template <typename Type>
|
|
void schema_observer<Type>::on_delete(const Type &/*obj*/) {}
|
|
} // namespace matador::query
|
|
#endif //MATADOR_SCHEMA_HPP
|