query/include/matador/orm/schema.hpp

68 lines
1.9 KiB
C++

#ifndef MATADOR_SCHEMA_HPP
#define MATADOR_SCHEMA_HPP
#include "matador/object/repository.hpp"
namespace matador::sql {
class connection_pool;
}
namespace matador::orm {
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_;
};
class schema final {
public:
explicit schema(sql::connection_pool &pool);
schema(sql::connection_pool &pool, const std::string &name);
template<typename Type>
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, const std::string &parent = "") {
return repo_.attach<Type>(name, parent);
}
template<typename Type, typename SuperType>
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name) {
return repo_.attach<Type, SuperType>(name);
}
[[nodiscard]] utils::result<void, utils::error> create() const;
[[nodiscard]] utils::result<void, utils::error> drop() const;
template<typename Type>
[[nodiscard]] utils::result<void, utils::error> drop_table();
[[nodiscard]] utils::result<void, utils::error> drop_table(const std::string &table_name) const;
[[nodiscard]] utils::result<std::vector<object::attribute>, utils::error> describe_table(const std::string &table_name) const;
[[nodiscard]] utils::result<bool, utils::error> table_exists(const std::string &table_name) const;
private:
object::repository repo_;
sql::connection_pool &pool_;
};
template<typename Type>
utils::result<void, utils::error> schema::drop_table() {
auto info = repo_.info<Type>();
if (info) {
return drop_table(info->get().name());
}
return utils::failure(info.err());
}
}
#endif //MATADOR_SCHEMA_HPP