query/include/matador/query/schema.hpp

151 lines
5.5 KiB
C++

#ifndef MATADOR_SCHEMA_HPP
#define MATADOR_SCHEMA_HPP
#include "matador/object/observer.hpp"
#include "matador/object/repository.hpp"
#include "matador/sql/query_context.hpp"
#include "matador/query/table.hpp"
namespace matador::sql {
class connection_pool;
class connection;
}
namespace matador::query {
class schema;
using schema_ref = std::reference_wrapper<schema>;
class schema_repository final {
public:
utils::result<void, utils::error> add(const std::string &name, const schema &schema);
utils::result<void, utils::error> remove(const std::string &name);
[[nodiscard]] utils::result<schema_ref, utils::error> get(const std::string &name);
private:
std::unordered_map<std::string, schema> schema_map_;
};
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_node final {
public:
schema_node(class table tab, const object::repository_node& node);
[[nodiscard]] const std::string& name() const;
[[nodiscard]] const class table& table() const;
[[nodiscard]] const object::repository_node& node() const;
private:
class table table_;
const object::repository_node& node_;
};
class schema final {
public:
using iterator = std::unordered_map<std::type_index, schema_node>::iterator;
using const_iterator = std::unordered_map<std::type_index, schema_node>::const_iterator;
schema() = default;
explicit schema(const std::string &name);
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;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
iterator find(const std::type_index& ti) { return schema_nodes_.find(ti); }
const_iterator find(const std::type_index& ti) const { return schema_nodes_.find(ti); }
iterator find(const std::string& name);
const_iterator find(const std::string& name) const;
const object::repository &repo() const { return repo_; }
object::repository &repo() { return repo_; }
private:
[[nodiscard]] sql::query_context build_add_constraint_context(const object::repository_node& node, const object::restriction& cons, const sql::connection &conn) const;
[[nodiscard]] sql::query_context build_drop_constraint_context(const object::repository_node& node, const object::restriction& cons, const sql::connection &conn) const;
void insert_table(const std::type_index& ti, const object::repository_node &node);
private:
template<typename Type>
friend class schema_observer;
object::repository repo_;
std::unordered_map<std::type_index, schema_node> schema_nodes_;
};
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 {
schema_.insert_table(typeid(Type), node);
}
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