#ifndef MATADOR_OBJECT_GENERATOR_HPP #define MATADOR_OBJECT_GENERATOR_HPP #include "matador/object/basic_repository.hpp" #include "matador/object/object.hpp" #include "matador/utils/access.hpp" #include "matador/utils/primary_key_attribute.hpp" #include namespace matador::object { class pk_type_determinator { private: pk_type_determinator() = default; public: template static utils::basic_type determine() { pk_type_determinator determinator; Type t; access::process(determinator, t); return determinator.type_; } template void on_primary_key(const char *, ValueType &/*pk*/, const utils::primary_key_attribute& attr = utils::default_pk_attributes) { type_ = utils::data_type_traits::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 static void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {} template static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {} template static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {} template 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 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(basic_repository& repo, const std::shared_ptr &object); public: template < class Type > static std::shared_ptr generate(basic_repository &repo, const std::string &name) { return generate(std::make_unique(), repo, name); } template < class Type > static std::shared_ptr generate(std::unique_ptr&& t, basic_repository &repo, const std::string &name) { const std::type_index ti(typeid(Type)); if (repo.has_object_for_type(ti)) { auto obj = repo.object_for_type(ti); repo.remove_object_for_type(ti); obj->update_name(name); return obj; } auto obj = std::make_shared(name); std::ignore = repo.provide_object_in_advance(ti, obj); object_generator gen(repo, obj); access::process(gen, *t); return obj; } 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 void on_attribute(const char *id, Type &x, const utils::field_attributes &attr = utils::null_attributes); template void on_attribute(const char *id, std::optional &x, const utils::field_attributes &attr = utils::null_attributes); template void on_belongs_to(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/) { on_foreign_key(id, x); create_fk_constraint(id); } template static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {} template void on_foreign_key(const char *id, Pointer &/*x*/) { const auto type = pk_type_determinator::determine(); auto &ref = object_->attributes_.emplace_back(id, type, utils::constraints::ForeignKey, null_option_type::NotNull); ref.owner_ = object_; } template static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {} template 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 static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {} private: template 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::type(attr.size()), attr, null_option); ref.owner_ = object_; return ref; } void create_pk_constraint(const std::string& name) const; template void create_fk_constraint(const std::string& name) const; void create_unique_constraint(const std::string& name) const; [[nodiscard]] std::list::iterator find_attribute_by_name(const std::string &name) const; void prepare_primary_key(attribute &ref, utils::identifier &&pk) const; template [[nodiscard]] std::shared_ptr fk_object() const; static std::shared_ptr acquire_object(basic_repository &repo, const std::type_index &ti, const std::string& name); private: basic_repository &repo_; std::shared_ptr object_; }; template void object_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) { auto &ref = emplace_attribute(id, { attr.size(), utils::constraints::PrimaryKey }, null_option_type::NotNull); prepare_primary_key(ref, utils::identifier(x)); } template void object_generator::on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr) { std::ignore = emplace_attribute(id, attr, null_option_type::NotNull); } template void object_generator::on_attribute(const char *id, std::optional & /*x*/, const utils::field_attributes &attr) { std::ignore = emplace_attribute(id, attr, null_option_type::Nullable); } template void object_generator::create_fk_constraint(const std::string& name) const { const auto pk_attr = find_attribute_by_name(name); if (pk_attr == std::end(object_->attributes_)) { return; } const auto obj = fk_object(); restriction pk_constraint(*pk_attr); pk_constraint.options_ |= utils::constraints::ForeignKey; pk_constraint.owner_ = object_; pk_constraint.reference_ = obj; object_->constraints_.emplace_back(std::move(pk_constraint)); } template std::shared_ptr object_generator::fk_object() const { const auto ti = std::type_index(typeid(Type)); if (const auto result = repo_.basic_info(ti)) { return result->get().object(); } if (repo_.has_object_for_type(ti)) { return repo_.object_for_type(ti); } const auto obj = repo_.provide_object_in_advance(ti, std::make_shared("")); object_generator gen(repo_, obj); Type t; access::process(gen, t); return obj; } } #endif //MATADOR_OBJECT_GENERATOR_HPP