progress on relation completer

This commit is contained in:
Sascha Kühl 2025-07-04 16:03:04 +02:00
parent 9c7628b6cb
commit 8f2d07518f
10 changed files with 152 additions and 63 deletions

View File

@ -18,7 +18,6 @@ struct author {
bool distinguished{false};
matador::object::collection<matador::object::object_ptr<book>> books;
template<typename Operator>
void process( Operator& op ) {
namespace field = matador::access;
@ -28,7 +27,7 @@ struct author {
field::attribute( op, "date_of_birth", date_of_birth, 31 );
field::attribute( op, "year_of_birth", year_of_birth );
field::attribute( op, "distinguished", distinguished );
field::has_many( op, "books", books, "author_id" );
field::has_many( op, "books", books, "author_id", matador::utils::default_foreign_attributes );
}
};
}

View File

@ -18,7 +18,7 @@ struct book {
namespace field = matador::access;
field::primary_key( op, "id", id );
field::attribute( op, "title", title, 511 );
field::has_one( op, "author_id", book_author, matador::utils::default_foreign_attributes );
field::belongs_to( op, "author_id", book_author, matador::utils::default_foreign_attributes );
field::attribute( op, "published_in", published_in );
}
};

View File

@ -5,6 +5,48 @@
#include "author.hpp"
#include "book.hpp"
/*
* node author
*
* relation_endpoints
* 1. - field_name "books"
* - type_index <book>
* - type "has_many"
* - node <author>
* - foreign endpoint (1) "author"
*
* node book
* 2. - field_name "author_id"
* - type_index <author>
* - type "belongs_to"
* - node <book>
* - foreign endpoint (2) "books"
*
* Attach process:
*
* attach<author>
* - relation completer detects "has_many<book>"
* - has_many<book> doesn't find <book> (not yet attached)
* - create endpoint (1) without foreign endpoint
* - create node many_to_many_relation<author, book>("author_id", "id")
* - 3. create endpoint
* - field name "author_id"
* - type_index <author>
* - type "belongs_to"
* - node <many_to_many_relation<author, book>>
* - foreign endpoint (1) "author
* - set foreign endpoint of (1) to endpoint (3)
* - attach (internal) node<many_to_many_relation<author, book>>
*
* attach<book>
* - relation completer detects "belongs_to<author>"
* - belongs_to<author> finds <book>
* - check relation endpoints...
*
*
*
*/
using namespace demo;
using namespace matador;
int main() {
@ -15,5 +57,4 @@ int main() {
auto result = schema.attach<author>("authors")
.and_then([&schema] { return schema.attach<book>("books"); });
}

View File

@ -14,13 +14,15 @@ public:
object_info(const std::shared_ptr<schema_node>& node,
std::shared_ptr<attribute_definition> &&ref_column)
: basic_object_info(node, typeid(Type), {}, std::move(ref_column), {}) {
: basic_object_info(node, typeid(Type), {}, std::move(ref_column), {})
, creator_([]{return std::make_unique<Type>(); }){
}
object_info(const std::shared_ptr<schema_node>& node,
utils::identifier &&pk,
std::shared_ptr<attribute_definition> &&ref_column,
object_definition &&definition)
: basic_object_info(node, typeid(Type), std::move(pk), std::move(ref_column), std::move(definition)) {
: basic_object_info(node, typeid(Type), std::move(pk), std::move(ref_column), std::move(definition))
, creator_([]{return std::make_unique<Type>(); }){
}
object_info(const std::shared_ptr<schema_node>& node,
utils::identifier &&pk,
@ -30,6 +32,12 @@ public:
: basic_object_info(node, typeid(Type), std::move(pk), std::move(ref_column), std::move(definition))
, creator_(std::move(creator)){
}
object_info(const std::shared_ptr<schema_node>& node,
object_definition &&definition,
create_func&& creator)
: basic_object_info(node, typeid(Type), std::move(definition))
, creator_(std::move(creator)){
}
const Type &prototype() const { return prototype_; }
std::unique_ptr<Type> create() const { return creator_(); }

View File

@ -61,7 +61,7 @@ class relation_completer final {
public:
using value_type = Type;
static void prepare_expected_nodes(schema_node &node) {
static void complete(const std::shared_ptr<schema_node> &node) {
relation_completer completer(node);
Type obj;
@ -95,13 +95,13 @@ public:
void on_has_many_to_many(const char *id, ContainerType &collection, const utils::foreign_attributes &attr);
private:
explicit relation_completer(schema_node& node)
explicit relation_completer(const std::shared_ptr<schema_node>& node)
: node_(node)
, schema_(node.schema_)
, schema_(node->schema_)
, log_(logger::create_logger("relation_completer")) {}
private:
schema_node &node_;
std::shared_ptr<schema_node> node_;
schema& schema_;
logger::logger log_;
};
@ -118,25 +118,16 @@ public:
template <typename Type>
[[nodiscard]] utils::result<void, utils::error> attach(const std::string& name, const std::string &parent = "") {
if (has_node(name)) {
return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + name + "' already exists"));
}
if (const auto it = expected_node_map_.find(typeid(Type)); it != expected_node_map_.end()) {
const auto node = it->second;
expected_node_map_.erase(it);
node->update_name(name);
node_map_.insert({node->name(), node})/*.first*/;
type_index_node_map_.insert({node->type_index(), node});
} else {
// analyze node (collect unknown types by type index)
const auto node = schema_node::make_node<Type>(*this, name);
relation_completer<Type>::prepare_expected_nodes(*node);
// if (has_node(name)) {
// return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + name + "' already exists"));
// }
auto node = acquire_node<Type>(name);
log_.info("attach node %s", name.c_str());
relation_completer<Type>::complete(node);
if (auto result = attach_node(node, parent); !result) {
return utils::failure(result.err());
}
}
return utils::ok<void>();
}
@ -204,7 +195,7 @@ public:
return utils::failure(result.err());
}
return utils::ok(basic_object_info_ref{result.value()->basic_info()});
return utils::ok(basic_object_info_ref{result.value()->info()});
}
[[nodiscard]] utils::result<std::shared_ptr<attribute_definition>, utils::error> reference(const std::type_index &type_index) const;
@ -221,6 +212,20 @@ private:
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> find_node(const std::string &name) const;
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> find_node(const std::type_index &type_index) const;
template <typename Type>
std::shared_ptr<schema_node> acquire_node(const std::string& name) {
if (const auto it = expected_node_map_.find(typeid(Type)); it != expected_node_map_.end()) {
const auto node = it->second;
expected_node_map_.erase(it);
node->update_name(name);
return node;
}
return schema_node::make_node<Type>(*this, name);
}
[[nodiscard]] bool has_node(const std::string &name) const;
[[nodiscard]] bool has_node(const std::type_index& index) const;
[[nodiscard]] bool has_node(const std::type_index& index, const std::string &name) const;
@ -234,22 +239,40 @@ private:
std::string name_;
std::shared_ptr<schema_node> root_;
t_node_map node_map_;
t_type_index_node_map type_index_node_map_;
t_node_map nodes_by_name_;
t_type_index_node_map nodes_by_type_;
t_type_index_node_map expected_node_map_;
logger::logger log_;
};
template<typename Type>
template<class CollectionType>
void relation_completer<Type>::on_has_many( const char *id, CollectionType&, const char *join_column, const utils::foreign_attributes&, std::enable_if_t<is_object_ptr<typename CollectionType::value_type>::value>* /*unused*/ ) {
using value_type = typename CollectionType::value_type;
void relation_completer<Type>::on_has_many( const char *id, CollectionType&,
const char *join_column,
const utils::foreign_attributes&,
std::enable_if_t<is_object_ptr<typename CollectionType::value_type>::value>* /*unused*/ ) {
using value_type = typename CollectionType::value_type::value_type;
// Check if the object_ptr type is already inserted in the schema (by id)
if (auto result = schema_.find_node(id); !result) {
const std::type_index ti = typeid(many_to_many_relation<value_type, Type>);
const auto endpoint = node_->info().find_relation_endpoint(ti);
if (endpoint == node_->info().endpoint_end()) {
log_.debug("node '%s' has has many foreign keys '%s' mapped by '%s'", node_->name().c_str(), id, join_column);
const auto node = schema_node::make_relation_node<many_to_many_relation<value_type, Type>>(schema_, id, [join_column] {
return new many_to_many_relation<value_type, Type>(join_column, "id");
return std::make_unique<many_to_many_relation<value_type, Type>>(join_column, "id");
});
// schema_.attach_node(node, typeid(many_to_many_relation<value_type, Type>));
log_.debug("node '%s' many to many type: %s", node_->name().c_str(), typeid(many_to_many_relation<value_type, Type>).name());
result = schema_.attach_node(node, "");
if (!result) {
// Todo: throw internal error
return;
}
node_->info_->register_relation_endpoint(node->type_index(), relation_endpoint(id, relation_type::BELONGS_TO, node));
node->info_->register_relation_endpoint(node_->type_index(), relation_endpoint(id, relation_type::HAS_MANY, node_));
}
} else {
}
}
template<typename Type>
@ -307,7 +330,7 @@ void relation_completer<Type>::on_has_one(const char * id, ForeignPointerType &/
schema_.expected_node_map_.insert({ti, schema_node::make_node<typename ForeignPointerType::value_type>(schema_, ti.name())});
} else {
const auto& foreign_node = result.value();
if (const auto rit = foreign_node->basic_info().find_relation_endpoint(ti); rit != foreign_node->basic_info().endpoint_end()) {
if (const auto rit = foreign_node->info().find_relation_endpoint(ti); rit != foreign_node->info().endpoint_end()) {
if (rit->second.is_has_many()) {
} else if (rit->second.is_has_one()) {
@ -322,8 +345,24 @@ void relation_completer<Type>::on_has_one(const char * id, ForeignPointerType &/
template<typename Type>
template<class ForeignPointerType>
void relation_completer<Type>::on_belongs_to( const char* id, ForeignPointerType& obj, const utils::foreign_attributes& attr ) {
void relation_completer<Type>::on_belongs_to(const char* id, ForeignPointerType& /*obj*/, const utils::foreign_attributes& /*attr*/) {
auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
if (auto result = schema_.find_node(ti); !result) {
log_.debug("node '%s' has foreign key '%s' belongs to '%s'", node_->name().c_str(), id, typeid(typename ForeignPointerType::value_type).name());
} else {
const auto foreign_node = result.value();
auto it = foreign_node->info().find_relation_endpoint(node_->type_index());
if (it != foreign_node->info().endpoint_end()) {
auto endpoint = it->second;
}
using TP = many_to_many_relation<typename ForeignPointerType::value_type, Type>;
log_.debug("node '%s', many to many type: %s", node_->name().c_str(), typeid(TP).name());
log_.debug("foreign node '%s', many to many type: %s", foreign_node->name().c_str(), ti.name());
if (foreign_node->type_index() == ti) {
log_.debug("node '%s' has foreign key '%s' belongs to '%s': already inserted", node_->name().c_str(), id, result.value()->name().c_str());
} else {
}
}
}
}

View File

@ -26,7 +26,8 @@ public:
node,
std::move(pk_info.pk),
std::make_shared<attribute_definition>(pk_info.pk_column_name, name, pk_info.type, utils::constraints::FOREIGN_KEY),
object_definition{attribute_definition_generator::generate<Type>(tree)}
object_definition{attribute_definition_generator::generate<Type>(tree)},
[]{ return std::make_unique<Type>(); }
);
node->info_ = std::move(info);
@ -37,11 +38,12 @@ public:
static std::shared_ptr<schema_node> make_relation_node(object::schema& tree, const std::string& name, CreatorFunc &&creator) {
auto node = std::shared_ptr<schema_node>(new schema_node(tree, name));
// auto info = std::make_unique<object_info<Type>>(
// node,
// object_definition{attribute_definition_generator::generate<Type>(tree)}
// );
// node->info_ = std::move(info);
auto info = std::make_unique<object_info<Type>>(
node,
object_definition{attribute_definition_generator::generate<Type>(tree)},
std::move(creator)
);
node->info_ = std::move(info);
return node;
}
@ -60,7 +62,7 @@ public:
[[nodiscard]] node_ptr next() const;
[[nodiscard]] node_ptr prev() const;
[[nodiscard]] const basic_object_info& basic_info() const;
[[nodiscard]] const basic_object_info& info() const;
void update_name(const std::string& name);

View File

@ -3,7 +3,7 @@
#include "matador/object/schema_node.hpp"
namespace matador::object {
relation_endpoint::relation_endpoint(std::string field_name, relation_type type, const std::shared_ptr<schema_node> &node)
relation_endpoint::relation_endpoint(std::string field_name, const relation_type type, const std::shared_ptr<schema_node> &node)
: field_name_(std::move(field_name))
, type_(type)
, node_(node) {

View File

@ -40,11 +40,11 @@ std::string schema::name() const {
utils::result<std::shared_ptr<attribute_definition>, utils::error> schema::reference(const std::type_index &type_index) const {
const auto result = find_node(type_index);
if (result) {
return utils::ok((*result)->basic_info().reference_column());
return utils::ok((*result)->info().reference_column());
}
if (const auto it = expected_node_map_.find(type_index); it != expected_node_map_.end()) {
return utils::ok(it->second->basic_info().reference_column());
return utils::ok(it->second->info().reference_column());
}
return utils::failure(result.err());
@ -69,8 +69,8 @@ utils::result<std::shared_ptr<schema_node>, utils::error> schema::attach_node(co
push_back_child(parent_node, node);
// Todo: check return value
node_map_.insert({node->name(), node})/*.first*/;
type_index_node_map_.insert({node->type_index(), node});
nodes_by_name_.insert({node->name(), node})/*.first*/;
nodes_by_type_.insert({node->type_index(), node});
return utils::ok(node);
}
@ -88,24 +88,24 @@ utils::result<std::shared_ptr<schema_node>, utils::error> schema::attach_node(co
push_back_child(*result, node);
// Todo: check return value
node_map_.insert({node->name(), node})/*.first*/;
type_index_node_map_.insert({node->type_index(), node});
nodes_by_name_.insert({node->name(), node})/*.first*/;
nodes_by_type_.insert({node->type_index(), node});
return utils::ok(node);
}
utils::result<std::shared_ptr<schema_node>, utils::error> schema::find_node(const std::string &name) const {
// first search in the prototype map
const auto i = node_map_.find(name);
if (i == node_map_.end()) {
const auto i = nodes_by_name_.find(name);
if (i == nodes_by_name_.end()) {
return utils::failure(make_error(error_code::NodeNotFound, "Couldn't find node by name '" + name + "'"));
}
return utils::ok(i->second);
}
utils::result<std::shared_ptr<schema_node>, utils::error> schema::find_node(const std::type_index &type_index) const {
const auto i = type_index_node_map_.find(type_index);
if (i == type_index_node_map_.end()) {
const auto i = nodes_by_type_.find(type_index);
if (i == nodes_by_type_.end()) {
return utils::failure(make_error(error_code::NodeNotFound,
"Couldn't find node by type '" + std::string(type_index.name()) + "'"));
}
@ -129,14 +129,14 @@ void schema::push_back_child(const node_ptr &parent, const node_ptr &child) {
}
bool schema::has_node(const std::string &name) const {
return node_map_.count(name) > 0;
return nodes_by_name_.count(name) > 0;
}
bool schema::has_node(const std::type_index &index) const {
return type_index_node_map_.count(index) > 0;
return nodes_by_type_.count(index) > 0;
}
bool schema::has_node(const std::type_index &index, const std::string &name) const {
return node_map_.count(name) > 0 || type_index_node_map_.count(index) > 0;
return nodes_by_name_.count(name) > 0 || nodes_by_type_.count(index) > 0;
}
} // namespace matador::object

View File

@ -31,7 +31,7 @@ std::type_index schema_node::type_index() const {
return info_->type_index();
}
const basic_object_info &schema_node::basic_info() const {
const basic_object_info &schema_node::info() const {
return *info_;
}

View File

@ -21,7 +21,7 @@ utils::result<void, utils::error> session::create_schema() const {
auto c = pool_.acquire();
for (const auto &t : *schema_) {
auto result = query::query::create()
.table(t.name(), t.basic_info().definition().columns())
.table(t.name(), t.info().definition().columns())
.execute(*c);
if ( !result ) {
return utils::failure(result.err());