140 lines
6.1 KiB
C++
140 lines
6.1 KiB
C++
#ifndef MATADOR_OBJECT_GENERATOR_HPP
|
|
#define MATADOR_OBJECT_GENERATOR_HPP
|
|
|
|
#include "matador/object/object.hpp"
|
|
|
|
#include "matador/utils/access.hpp"
|
|
#include "matador/utils/primary_key_attribute.hpp"
|
|
|
|
#include <memory>
|
|
|
|
namespace matador::object {
|
|
class repository;
|
|
|
|
class pk_type_determinator {
|
|
private:
|
|
pk_type_determinator() = default;
|
|
|
|
public:
|
|
template<class Type>
|
|
static utils::basic_type determine() {
|
|
pk_type_determinator determinator;
|
|
Type t;
|
|
|
|
access::process(determinator, t);
|
|
return determinator.type_;
|
|
}
|
|
|
|
template<typename ValueType>
|
|
void on_primary_key(const char *, ValueType &/*pk*/, const utils::primary_key_attribute& attr = utils::default_pk_attributes) {
|
|
type_ = utils::data_type_traits<ValueType>::type(attr.size());
|
|
}
|
|
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
|
|
template < class Type >
|
|
static void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
|
static void on_attribute(const char * /*id*/, char * /*x*/, 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(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
|
template<class ContainerType>
|
|
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, 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 * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
|
|
|
|
private:
|
|
utils::basic_type type_{};
|
|
};
|
|
|
|
class object_generator {
|
|
private:
|
|
explicit object_generator(const repository& repo, std::unique_ptr<object> &&object);
|
|
|
|
public:
|
|
template < class Type >
|
|
static std::unique_ptr<object> generate(const repository &repo, const std::string &name, const std::string &alias = "") {
|
|
return generate(std::make_unique<Type>(), repo, name, alias);
|
|
}
|
|
|
|
template < class Type >
|
|
static std::unique_ptr<object> generate(std::unique_ptr<Type>&& t, const repository &repo, const std::string &name, const std::string &alias = "") {
|
|
object_generator gen(repo, std::make_unique<object>(name, alias));
|
|
access::process(gen, *t);
|
|
return gen.move();
|
|
}
|
|
|
|
[[nodiscard]] std::unique_ptr<object> move() { return std::move(object_); }
|
|
|
|
template < class Type >
|
|
void on_primary_key(const char *, Type &x, const utils::primary_key_attribute& attr = utils::default_pk_attributes);
|
|
void on_revision(const char *id, uint64_t &rev);
|
|
|
|
template<typename Type>
|
|
void on_attribute(const char *id, Type &x, const utils::field_attributes &attr = utils::null_attributes);
|
|
|
|
template<typename Type>
|
|
void on_attribute(const char *id, std::optional<Type> &x, const utils::field_attributes &attr = utils::null_attributes);
|
|
|
|
template<class Pointer>
|
|
void on_belongs_to(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/) {
|
|
on_foreign_key(id, x);
|
|
create_fk_constraint( typeid(typename Pointer::value_type), id);
|
|
}
|
|
template<class Pointer>
|
|
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
|
|
|
template <class Pointer>
|
|
void on_foreign_key(const char *id, Pointer &/*x*/) {
|
|
const auto type = pk_type_determinator::determine<typename Pointer::value_type>();
|
|
auto &ref = object_->attributes_.emplace_back(id, type, utils::constraints::ForeignKey, null_option_type::NOT_NULL);
|
|
ref.owner_ = object_.get();
|
|
}
|
|
template<class ContainerType>
|
|
static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
|
template<class ContainerType>
|
|
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, 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 * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
|
|
|
|
private:
|
|
template<typename ValueType>
|
|
attribute &emplace_attribute(const char *id, const utils::field_attributes& attr, null_option_type null_option) {
|
|
auto &ref = object_->attributes_.emplace_back(id, utils::data_type_traits<ValueType>::type(attr.size()), attr, null_option);
|
|
ref.owner_ = object_.get();
|
|
return ref;
|
|
}
|
|
|
|
void create_pk_constraint(const std::string& name) const;
|
|
void create_fk_constraint(const std::type_index& ti, const std::string& name) const;
|
|
void create_unique_constraint(const std::string& name) const;
|
|
|
|
[[nodiscard]] std::list<attribute>::iterator find_attribute_by_name(const std::string &name) const;
|
|
|
|
void prepare_primary_key(attribute &ref, utils::identifier &&pk) const;
|
|
|
|
private:
|
|
const repository &repo_;
|
|
std::unique_ptr<object> object_;
|
|
};
|
|
|
|
template<typename ValueType>
|
|
void object_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) {
|
|
auto &ref = emplace_attribute<ValueType>(id, { attr.size(), utils::constraints::PrimaryKey }, null_option_type::NOT_NULL);
|
|
prepare_primary_key(ref, utils::identifier(x));
|
|
create_pk_constraint(id);
|
|
}
|
|
|
|
template<typename Type>
|
|
void object_generator::on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr) {
|
|
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NOT_NULL);
|
|
}
|
|
|
|
template<typename Type>
|
|
void object_generator::on_attribute(const char *id, std::optional<Type> & /*x*/, const utils::field_attributes &attr) {
|
|
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NULLABLE);
|
|
}
|
|
|
|
}
|
|
#endif //MATADOR_OBJECT_GENERATOR_HPP
|