#include "matador/object/object_generator.hpp" #include "matador/object/repository.hpp" #include namespace matador::object { object_generator::object_generator(basic_repository &repo, const std::shared_ptr &object) : repo_(repo) , object_(object) { } std::shared_ptr object_generator::acquire_object(basic_repository &repo, const std::type_index &ti, const std::string &name) { 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; } return repo.provide_object_in_advance(ti, std::make_shared(name)); } void object_generator::on_revision(const char *id, uint64_t &rev) { access::attribute(*this, id, rev); } void object_generator::create_pk_constraint(const std::string &name) const { const auto pk_attr = find_attribute_by_name(name); if (pk_attr == std::end(object_->attributes_)) { return; } restriction pk_constraint(pk_attr->index()); pk_constraint.options_ |= utils::constraints::PrimaryKey; pk_constraint.owner_ = object_; object_->constraints_.emplace_back(std::move(pk_constraint)); } void object_generator::create_unique_constraint(const std::string &name) const { const auto pk_attr = find_attribute_by_name(name); if (pk_attr == std::end(object_->attributes_)) { return; } restriction pk_constraint(pk_attr->index()); pk_constraint.options_ |= utils::constraints::Unique; pk_constraint.owner_ = object_; } std::vector::iterator object_generator::find_attribute_by_name(const std::string &name) const { return std::find_if(std::begin(object_->attributes_), std::end(object_->attributes_), [&name](const attribute &elem) { return elem.name() == name; }); } void object_generator::prepare_primary_key(const attribute &ref, utils::identifier &&pk) const { object_->pk_column_index_ = static_cast(ref.index_); object_->pk_identifier_ = std::move(pk); } void object_generator::prepare_relation_table(const std::string &join_column, const std::string &inverse_join_column) const { auto it = find_attribute_by_name(join_column); if (it == std::end(object_->attributes_)) { return; } object_->join_column_index_ = static_cast(it->index_); it = find_attribute_by_name(inverse_join_column); if (it == std::end(object_->attributes_)) { return; } object_->inverse_join_column_index_ = static_cast(it->index_); } }