object generating progress

This commit is contained in:
Sascha Kühl
2025-11-25 16:27:25 +01:00
parent ab7a2db869
commit f47b3bb87f
16 changed files with 198 additions and 242 deletions
+59 -90
View File
@@ -1,49 +1,22 @@
#include "matador/object/attribute.hpp"
#include "matador/object/object.hpp"
#include <ostream>
#include <utility>
namespace matador::object {
attribute::attribute(const char *name)
: attribute(name, utils::basic_type::type_null, "", {utils::null_attributes}) {
}
attribute::attribute(std::string name)
: attribute(std::move(name), utils::basic_type::type_null, "", {utils::null_attributes}) {
: attribute(std::move(name), utils::basic_type::type_null, {}, null_option_type::NOT_NULL) {
}
attribute::attribute(std::string name,
const utils::basic_type type,
const utils::field_attributes &attr,
const null_option_type null_opt,
const int index)
: attribute(std::move(name), type, "", {attr, null_opt, index}) {
}
attribute::attribute(std::string name,
const utils::basic_type type,
const int index,
const std::shared_ptr<attribute> &ref_column,
const utils::field_attributes &attr,
const null_option_type null_opt)
: attribute(std::move(name), type, "", {attr, null_opt, index}, ref_column) {
}
attribute::attribute( std::string name, const utils::basic_type type, const std::shared_ptr<attribute>& ref_column )
: attribute(std::move(name), type, {}, {}, ref_column) {
}
attribute::attribute(std::string name,
const utils::basic_type type,
std::string table_name,
const attribute_options& options,
const std::shared_ptr<attribute>& ref_column)
const utils::basic_type type,
const utils::field_attributes &attr,
const null_option_type null_opt)
: name_(std::move(name))
, table_name_(std::move(table_name))
, options_( options )
, type_( type )
, reference_column_( ref_column ) {
}
, options_{attr, null_opt}
, type_(type)
{}
const std::string &attribute::name() const {
return name_;
@@ -54,7 +27,7 @@ void attribute::name( const std::string& n ) {
}
std::string attribute::full_name() const {
return !table_name_.empty() ? table_name_ + "." + name_ : name_;
return owner_ ? owner_->name() + "." + name_ : name_;
}
int attribute::index() const {
@@ -77,27 +50,23 @@ utils::basic_type attribute::type() const {
return type_;
}
const std::string& attribute::table_name() const {
return table_name_;
object* attribute::owner() const {
return owner_;
}
void attribute::table_name( const std::string& name ) {
table_name_ = name;
}
// const std::string& attribute::table_name() const {
// return owner_ ? owner_->name() : "";
// }
//
// void attribute::table_name( const std::string& name ) {
// table_name_ = name;
// }
void attribute::change_type(const utils::basic_type type, const utils::field_attributes& attr) {
options_.attributes = attr;
type_ = type;
}
std::shared_ptr<attribute> attribute::reference_column() const {
return reference_column_;
}
bool attribute::is_foreign_reference() const {
return reference_column_ != nullptr;
}
bool attribute::is_integer() const {
return type_ >= utils::basic_type::type_int8 && type_ <= utils::basic_type::type_uint64;
}
@@ -134,45 +103,45 @@ bool attribute::is_null() const {
return type_ == utils::basic_type::type_null;
}
attribute make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr,
null_option_type null_opt) {
return {name, type, attr, null_opt};
}
template<>
attribute make_column<std::string>(const std::string &name, utils::field_attributes attr,
null_option_type null_opt) {
return make_column(name, utils::data_type_traits<std::string>::type(attr.size()), attr, null_opt);
}
template<>
attribute make_pk_column<std::string>(const std::string &name, size_t size) {
return make_column<std::string>(name, {size, utils::constraints::PrimaryKey});
}
template<>
attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute> &ref_column) {
return {
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
{size, utils::constraints::ForeignKey}, null_option_type::NOT_NULL
};
}
template<>
attribute make_fk_column<std::string>( const std::string& name, const std::string& ref_table_name, const std::string& ref_column_name ) {
return {
name, utils::basic_type::type_varchar, 0,
std::make_shared<attribute>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::ForeignKey}),
{ 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL
};
}
template<>
attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) {
const auto ref_column = std::make_shared<attribute>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::ForeignKey});
return {
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
{size, utils::constraints::ForeignKey}, null_option_type::NOT_NULL
};
}
// attribute make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr,
// null_option_type null_opt) {
// return {name, type, attr, null_opt};
// }
//
// template<>
// attribute make_column<std::string>(const std::string &name, utils::field_attributes attr,
// null_option_type null_opt) {
// return make_column(name, utils::data_type_traits<std::string>::type(attr.size()), attr, null_opt);
// }
//
// template<>
// attribute make_pk_column<std::string>(const std::string &name, size_t size) {
// return make_column<std::string>(name, {size, utils::constraints::PrimaryKey});
// }
//
// template<>
// attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute> &ref_column) {
// return {
// name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
// {size, utils::constraints::ForeignKey}, null_option_type::NOT_NULL
// };
// }
//
// template<>
// attribute make_fk_column<std::string>( const std::string& name, const std::string& ref_table_name, const std::string& ref_column_name ) {
// return {
// name, utils::basic_type::type_varchar, 0,
// std::make_shared<attribute>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::ForeignKey}),
// { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL
// };
// }
//
// template<>
// attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) {
// const auto ref_column = std::make_shared<attribute>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::ForeignKey});
// return {
// name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
// {size, utils::constraints::ForeignKey}, null_option_type::NOT_NULL
// };
// }
}
+3 -3
View File
@@ -14,11 +14,11 @@ void attribute_generator::on_revision(const char *id, uint64_t &rev) {
on_attribute(id, rev);
}
utils::result<std::shared_ptr<attribute>, utils::error> attribute_generator::determine_foreign_ref(const std::type_index &ti) const {
return repo_.reference_column(ti);
utils::result<attribute*, utils::error> attribute_generator::determine_foreign_ref(const std::type_index &ti) const {
return repo_.primary_key_attribute(ti);
}
void attribute_generator::insert_missing_reference_column(const std::type_index& ti, const std::shared_ptr<attribute>& ref_column) const {
void attribute_generator::insert_missing_reference_column(const std::type_index& ti, attribute* ref_column) const {
const_cast<repository&>(repo_).missing_references_.insert({ti, ref_column});
}
+27 -19
View File
@@ -5,20 +5,24 @@
#include <algorithm>
namespace matador::object {
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
const std::vector<attribute> &attributes,
utils::identifier &&pk,
const std::shared_ptr<attribute> &pk_as_fk_column)
: node_(std::move(node))
, attributes_(attributes)
, identifier_(std::move(pk))
, pk_as_fk_column_(pk_as_fk_column) {
}
// basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
// const std::vector<attribute> &attributes,
// utils::identifier &&pk,
// const std::shared_ptr<attribute> &pk_as_fk_column)
// : node_(std::move(node))
// , attributes_(attributes)
// , identifier_(std::move(pk))
// , pk_as_fk_column_(pk_as_fk_column) {
// }
//
// basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
// const std::vector<attribute> &attributes)
// : node_(std::move(node))
// , attributes_(attributes) {}
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
const std::vector<attribute> &attributes)
: node_(std::move(node))
, attributes_(attributes) {}
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node, std::unique_ptr<object>&& obj)
: object_(std::move(obj))
, node_(std::move(node)) {}
std::type_index basic_object_info::type_index() const {
return node_->type_index();
@@ -29,19 +33,23 @@ std::string basic_object_info::name() const {
}
const std::vector<attribute>& basic_object_info::attributes() const {
return attributes_;
return object_->attributes();
}
std::shared_ptr<attribute> basic_object_info::reference_column() const {
return pk_as_fk_column_;
}
// std::shared_ptr<attribute> basic_object_info::reference_column() const {
// return pk_as_fk_column_;
// }
bool basic_object_info::has_primary_key() const {
return identifier_.has_value();
return object_->has_primary_key();
}
const utils::identifier& basic_object_info::primary_key() const {
return identifier_.value();
return object_->primary_key();
}
attribute* basic_object_info::primary_key_attribute() const {
return object_->primary_key_attribute();
}
basic_object_info::endpoint_iterator basic_object_info::register_relation_endpoint(const std::type_index &type, const std::shared_ptr<relation_endpoint> &endpoint) {
+2 -2
View File
@@ -19,7 +19,7 @@ void constraints_generator::create_pk_constraint(const std::string& name) const
}
void constraints_generator::create_fk_constraint(const std::string& name, const basic_object_info& info) const {
const auto pk_attribute = info.reference_column();
const auto *pk_attribute = info.primary_key_attribute();
class constraint pk_constraint("FK_" + obj_.name() + "_" + name);
pk_constraint.options_ |= utils::constraints::ForeignKey;
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) {
@@ -27,7 +27,7 @@ void constraints_generator::create_fk_constraint(const std::string& name, const
}
pk_constraint.owner_ = &obj_;
pk_constraint.ref_column_name_ = pk_attribute->name();
pk_constraint.ref_table_name_ = pk_attribute->table_name();
pk_constraint.ref_table_name_ = pk_attribute->owner() ? pk_attribute->owner()->name() : "";
constraints_.emplace_back(std::move(pk_constraint));
}
+2
View File
@@ -14,6 +14,8 @@ std::string object_category_impl::message(const int ev) const {
return "Node not found";
case error_code::NodeAlreadyExists:
return "Node already exists";
case error_code::NoPrimaryKey:
return "No primary key";
case error_code::Failure:
return "Failure";
default:
+5 -1
View File
@@ -21,7 +21,7 @@ void object::add_constraint( class constraint c ) {
ref.owner_ = this;
}
const attribute* object::primary_key_attribute() const {
attribute* object::primary_key_attribute() const {
return pk_attribute_;
}
@@ -41,6 +41,10 @@ const std::string& object::alias() const {
return alias_;
}
void object::update_name(const std::string& name) {
name_ = name;
}
bool object::has_attributes() const {
return attributes_.empty();
}
+7 -4
View File
@@ -62,13 +62,16 @@ utils::result<basic_object_info_ref, utils::error> repository::basic_info( const
return utils::ok(basic_object_info_ref{result.value()->info()});
}
utils::result<std::shared_ptr<attribute>, utils::error> repository::reference_column(const std::type_index &type_index) const {
utils::result<attribute*, utils::error> repository::primary_key_attribute(const std::type_index &type_index) const {
const auto result = find_node(type_index);
if (result) {
return utils::ok((*result)->info().reference_column());
if (!result) {
return utils::failure(result.err());
}
return utils::failure(result.err());
if (!result.value()->info().has_primary_key()) {
return utils::failure(make_error(error_code::NodeAlreadyExists, "Object '" + result.value()->name() + "' does not have a primary key."));
}
return utils::ok((*result)->info().primary_key_attribute());
}
void repository::dump(std::ostream &os) const {
+9 -9
View File
@@ -45,9 +45,9 @@ const basic_object_info &repository_node::info() const {
void repository_node::update_name(const std::string& name) {
name_ = name;
if (info_->reference_column()) {
info_->reference_column()->table_name(name);
}
// if (info_->reference_column()) {
// info_->reference_column()->table_name(name);
// }
}
const repository& repository_node::schema() const {
@@ -104,19 +104,19 @@ utils::result<repository_node::node_ptr, utils::error> repository_node::make_and
return repo.attach_node(node, "");
}
std::shared_ptr<attribute> repository_node::determine_reference_column(const std::type_index& ti,
const std::string& table_name,
const primary_key_info& pk_info,
repository& repo) {
attribute* repository_node::determine_reference_column(const std::type_index& ti,
const std::string& table_name,
const primary_key_info& pk_info,
repository& repo) {
const auto it = repo.missing_references_.find(ti);
if (it == repo.missing_references_.end()) {
return std::make_shared<attribute>(pk_info.pk_column_name, pk_info.type, table_name, attribute_options{utils::constraints::ForeignKey});
return new attribute(pk_info.pk_column_name, pk_info.type, {utils::constraints::ForeignKey}, null_option_type::NOT_NULL);
}
auto ref_column = it->second;
repo.missing_references_.erase(it);
ref_column->name(pk_info.pk_column_name);
ref_column->table_name(table_name);
ref_column->owner()->update_name(table_name);
ref_column->change_type(pk_info.type);
ref_column->attributes() = utils::constraints::ForeignKey;