object generating progress
This commit is contained in:
@@ -108,6 +108,8 @@ add_library(matador-core STATIC
|
||||
utils/value.cpp
|
||||
utils/version.cpp
|
||||
object/object.cpp
|
||||
../../include/matador/object/object_generator.hpp
|
||||
object/object_generator.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(matador-core ${CMAKE_DL_LIBS})
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "matador/object/object_generator.hpp"
|
||||
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
object_generator::object_generator( const repository& repo, std::unique_ptr<object>&& object )
|
||||
: repo_(repo)
|
||||
, object_(std::move(object)) {}
|
||||
|
||||
void object_generator::on_revision(const char* id, uint64_t& rev) {
|
||||
on_attribute(id, rev);
|
||||
}
|
||||
|
||||
void object_generator::create_pk_constraint(const std::string& name) const {
|
||||
class constraint pk_constraint("PK_" + object_->name());
|
||||
pk_constraint.options_ |= utils::constraints::PrimaryKey;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
|
||||
pk_constraint.attr_ = &*pk_attr;
|
||||
}
|
||||
pk_constraint.owner_ = object_.get();
|
||||
object_->constraints_.emplace_back(std::move(pk_constraint));
|
||||
}
|
||||
|
||||
void object_generator::create_fk_constraint(const std::type_index& ti, const std::string& name) const {
|
||||
const auto result = repo_.basic_info(ti);
|
||||
if (!result) {
|
||||
return;
|
||||
}
|
||||
const auto *pk_attribute = result.value().get().primary_key_attribute();
|
||||
class constraint pk_constraint("FK_" + object_->name() + "_" + name);
|
||||
pk_constraint.options_ |= utils::constraints::ForeignKey;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
|
||||
pk_constraint.attr_ = &*pk_attr;
|
||||
}
|
||||
pk_constraint.owner_ = object_.get();
|
||||
pk_constraint.ref_column_name_ = pk_attribute->name();
|
||||
pk_constraint.ref_table_name_ = pk_attribute->owner() ? pk_attribute->owner()->name() : "";
|
||||
object_->constraints_.emplace_back(std::move(pk_constraint));
|
||||
}
|
||||
|
||||
void object_generator::create_unique_constraint(const std::string& name) const {
|
||||
class constraint pk_constraint("UK_" + object_->name() + "_" + name);
|
||||
pk_constraint.options_ |= utils::constraints::Unique;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
|
||||
pk_constraint.attr_ = &*pk_attr;
|
||||
}
|
||||
pk_constraint.owner_ = object_.get();
|
||||
}
|
||||
|
||||
std::vector<attribute>::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(attribute& ref, utils::identifier &&pk) const {
|
||||
object_->pk_attribute_ = &ref;
|
||||
object_->pk_identifier_ = std::move(pk);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user