attaching schema nodes progress

This commit is contained in:
Sascha Kühl 2025-07-15 16:04:43 +02:00
parent dd737623f4
commit a0bfa3261b
11 changed files with 147 additions and 100 deletions

View File

@ -104,8 +104,8 @@ using namespace demo;
using namespace matador;
int main() {
logger::default_min_log_level(logger::log_level::LVL_DEBUG);
logger::add_log_sink(logger::create_stdout_sink());
// logger::default_min_log_level(logger::log_level::LVL_DEBUG);
// logger::add_log_sink(logger::create_stdout_sink());
{
// has_many with builtin-type

View File

@ -97,7 +97,7 @@ int main() {
.and_then([&ses] { return ses.attach<jobs::IdListPayload, jobs::Payload>("id_payloads"); })
.and_then([&ses] { return ses.attach<jobs::Task>("tasks"); })
;
// ses.dump_schema(std::cout);
ses.dump_schema(std::cout);
if (!result) {
std::cout << "error: " << result.err().message() << std::endl;

View File

@ -30,7 +30,7 @@ public:
[[nodiscard]] bool has_primary_key() const;
[[nodiscard]] const utils::identifier& primary_key() const;
void register_relation_endpoint(const std::type_index &type, const std::shared_ptr<relation_endpoint> &endpoint);
endpoint_iterator register_relation_endpoint(const std::type_index &type, const std::shared_ptr<relation_endpoint> &endpoint);
void unregister_relation_endpoint(const std::type_index &type);
[[nodiscard]] const_endpoint_iterator find_relation_endpoint(const std::type_index &type) const;

View File

@ -44,7 +44,7 @@ public:
template<class ForeignPointerType>
void on_belongs_to(const char *id, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/);
template<class ForeignPointerType>
static void on_has_one(const char * /*id*/, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/) {}
void on_has_one(const char * /*id*/, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/);
template<class CollectionType>
void on_has_many(const char *id, CollectionType &, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<is_object_ptr<typename CollectionType::value_type>::value> * = nullptr);
@ -91,6 +91,11 @@ void foreign_node_completer::on_belongs_to( const char* /*id*/, ForeignPointerTy
attach_node<typename ForeignPointerType::value_type>();
}
template<class ForeignPointerType>
void foreign_node_completer::on_has_one( const char*, ForeignPointerType&, const utils::foreign_attributes& ) {
attach_node<typename ForeignPointerType::value_type>();
}
template<class CollectionType>
void foreign_node_completer::on_has_many( const char* /*id*/, CollectionType&, const char* /*join_column*/, const utils::foreign_attributes& /*attr*/, std::enable_if_t<is_object_ptr<typename CollectionType::value_type>::value>* ) {
attach_node<typename CollectionType::value_type::value_type>();

View File

@ -20,9 +20,10 @@ private:
public:
explicit shadow_schema(object::schema& s);
object::schema& schema() const;
[[nodiscard]] object::schema& schema() const;
[[nodiscard]] bool schema_contains(const std::type_index& ti) const;
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::type_index &type_index) const;
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::string &name) const;
[[nodiscard]] utils::result<node_ptr, utils::error> attach_node(const std::shared_ptr<schema_node> &node) const;
[[nodiscard]] utils::result<void, utils::error> detach_node(const std::shared_ptr<schema_node> &node) const;

View File

@ -126,6 +126,19 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
// Todo: throw internal error or attach node
return;
}
auto local_it = nodes_.top()->info().find_relation_endpoint(typeid(value_type));
if (local_it == nodes_.top()->info().endpoint_end()) {
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, result.value());
local_it = nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
}
auto foreign_it = result.value()->info().find_relation_endpoint(typeid(value_type));
if (foreign_it == result.value()->info().endpoint_end()) {
const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, nodes_.top());
foreign_it = result.value()->info_->register_relation_endpoint(typeid(Type), foreign_endpoint);
}
link_relation_endpoints(local_it->second, foreign_it->second);
/*
if (const auto endpoint = nodes_.top()->info().find_relation_endpoint(typeid(relation_value_type)); endpoint == nodes_.top()->info().endpoint_end()) {
// Endpoint was not found
log_.debug("node '%s' has has many foreign keys '%s' mapped by '%s'", nodes_.top()->name().c_str(), id, join_column);
@ -159,6 +172,7 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
}
}
*/
}
template<typename Type>
@ -179,10 +193,13 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &, con
// Todo: throw internal exception
}
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, nodes_.top());
const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, result.value());
// const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, nodes_.top());
// const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, result.value());
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, result.value());
const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, nodes_.top());
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
foreign_endpoint->node_->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
result.value()->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
link_relation_endpoints(local_endpoint, foreign_endpoint);
}
@ -194,20 +211,18 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
const char *inverse_join_column,
const utils::foreign_attributes &/*attr*/) {
using relation_value_type = many_to_many_relation<typename CollectionType::value_type::value_type, Type>;
auto result = schema_.find_node(typeid(relation_value_type));
if (result) {
// Found relation node.
// Complete relation links
const auto &foreign_node = result.value();
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, nodes_.top());
nodes_.top()->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
const auto it = foreign_node->info_->find_relation_endpoint(nodes_.top()->type_index());
if (it == foreign_node->info().endpoint_end()) {
// Todo: Throw error
using value_type = typename CollectionType::value_type::value_type;
// Check if the object_ptr type is already inserted in the schema (by id)
auto result = schema_.find_node(typeid(value_type));
if (!result) {
// Todo: throw internal error or attach node
return;
}
link_relation_endpoints(local_endpoint, it->second);
} else {
const auto foreign_node = result.value();
result = schema_.find_node(id);
if (!result) {
// Relation not found.
auto creator = [join_column, inverse_join_column] {
return std::make_unique<relation_value_type>(join_column, inverse_join_column);
@ -220,15 +235,21 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
}
auto& node = result.value();
auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, nodes_.top());
auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, node);
auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BELONGS_TO,
node);
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node);
const auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, nodes_.top());
const auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BELONGS_TO, foreign_node);
const auto foreign_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node);
// register relation endpoint in local node
nodes_.top()->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
// register relation endpoint in foreign node
foreign_node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
// register endpoints in relation node
node->info_->register_relation_endpoint(nodes_.top()->type_index(), join_endpoint);
node->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), inverse_join_endpoint);
// link endpoints
link_relation_endpoints(local_endpoint, join_endpoint);
node->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type),
inverse_join_endpoint);
link_relation_endpoints(foreign_endpoint, inverse_join_endpoint);
}
}
@ -251,28 +272,24 @@ template<class ForeignPointerType>
void relation_completer<Type>::on_has_one(const char *id,
ForeignPointerType &/*obj*/,
const utils::foreign_attributes &/*attr*/) {
const auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
if (const auto result = schema_.find_node(ti); !result) {
using value_type = typename ForeignPointerType::value_type;
const auto ti = std::type_index(typeid(value_type));
const auto result = schema_.find_node(ti);
if (!result) {
// Node was not found
// Create node without the foreign relation endpoint
log_.debug("node '%s' has foreign key '%s' has one '%s'", nodes_.top()->name().c_str(), id, ti.name());
nodes_.top()->info_->
register_relation_endpoint(ti, std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, nodes_.top()));
} else {
const auto &foreign_node = result.value();
if (const auto it = foreign_node->info().find_relation_endpoint(nodes_.top()->type_index());
it != foreign_node->info().endpoint_end()) {
if (it->second->is_belongs_to()) {
it->second->node_ = foreign_node;
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, nodes_.top());
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
link_relation_endpoints(endpoint, it->second);
} else {
// Todo: Throw internal error
return;
}
auto local_it = nodes_.top()->info().find_relation_endpoint(typeid(value_type));
if (local_it == nodes_.top()->info().endpoint_end()) {
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, result.value());
local_it = nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
}
if (const auto foreign_it = result.value()->info().find_relation_endpoint(typeid(Type)); foreign_it != result.value()->info().endpoint_end()) {
link_relation_endpoints(local_it->second, foreign_it->second);
}
}
template<typename Type>
@ -282,13 +299,12 @@ void relation_completer<Type>::on_belongs_to(const char *id,
const utils::foreign_attributes & /*attr*/) {
using value_type = typename ForeignPointerType::value_type;
const auto ti = std::type_index(typeid(value_type));
if (auto result = schema_.find_node(ti); !result) {
auto result = schema_.find_node(ti);
if (!result) {
// Type was not found
// Create node without the foreign relation endpoint
log_.debug("node '%s' has foreign key '%s' belongs to '%s'", nodes_.top()->name().c_str(), id, ti.name());
nodes_.top()->info_->register_relation_endpoint(
ti, std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, node_ptr{}));
} else {
// Todo: Throw internal error
return;
}
// Type was found
const auto &foreign_node = result.value();
// Check foreign node and relation endpoint
@ -296,7 +312,7 @@ void relation_completer<Type>::on_belongs_to(const char *id,
it != foreign_node->info().endpoint_end()) {
// Found corresponding relation endpoint in the foreign node
if (it->second->is_has_one()) {
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, nodes_.top());
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, foreign_node);
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
link_relation_endpoints(endpoint, it->second);
} else if (it->second->foreign_endpoint()->node().type_index() == typeid(many_to_many_relation<Type, value_type>)) {
@ -306,18 +322,18 @@ void relation_completer<Type>::on_belongs_to(const char *id,
// (detach), and the endpoints must be adjusted
const auto foreign_endpoint = it->second->foreign_endpoint();
const auto detach_result = schema_.detach_node(foreign_endpoint->node_);
foreign_endpoint->node_ = nodes_.top();
foreign_endpoint->node_ = foreign_node;
// foreign_endpoint->node_ = nodes_.top();
nodes_.top()->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
} else {
// check type
}
} else {
// Relation node was not found, create only endpoint.
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, nodes_.top());
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, foreign_node);
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
}
}
}
template<typename Type>
void relation_completer<Type>::register_relation_endpoints(const endpoint_ptr &endpoint,

View File

@ -48,8 +48,10 @@ public:
} else if (!has_node(name)) {
it->second->update_name(name);
nodes_by_name_[name] = it->second;
relation_completer<Type>::complete(it->second);
log_.info("attach: update node name to '%s' (type: %s)", it->second->name().c_str(), it->second->type_index().name());
} else {
// remove_node( )
return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + name + "' already exists"));
}

View File

@ -55,8 +55,8 @@ const utils::identifier& basic_object_info::primary_key() const {
return identifier_.value();
}
void basic_object_info::register_relation_endpoint(const std::type_index &type, const std::shared_ptr<relation_endpoint> &endpoint) {
relation_endpoints_.insert(std::make_pair(type, endpoint));
basic_object_info::endpoint_iterator basic_object_info::register_relation_endpoint(const std::type_index &type, const std::shared_ptr<relation_endpoint> &endpoint) {
return relation_endpoints_.insert(std::make_pair(type, endpoint)).first;
}
void basic_object_info::unregister_relation_endpoint(const std::type_index &type) {

View File

@ -19,6 +19,10 @@ utils::result<shadow_schema::node_ptr, utils::error> shadow_schema::find_node( c
return schema_.find_node(type_index);
}
utils::result<shadow_schema::node_ptr, utils::error> shadow_schema::find_node( const std::string& name ) const {
return schema_.find_node(name);
}
utils::result<shadow_schema::node_ptr, utils::error> shadow_schema::attach_node( const std::shared_ptr<schema_node>& node ) const {
return schema_.attach_node(node, "");
}

View File

@ -64,11 +64,11 @@ utils::result<std::shared_ptr<attribute_definition>, utils::error> schema::refer
void schema::dump(std::ostream &os) const {
for (const auto &node : *this) {
os << "node [" << node->name() << "] (" << node->type_index().name() << ")\n";
os << "node [" << node->name() << "] (" /*<< node->type_index().name()*/ << ")\n";
for (auto it = node->info().endpoint_begin(); it != node->info().endpoint_end(); ++it) {
os << " " << /*node.name() << "::" <<*/ it->second->field_name() << " (" << it->second->type_name() << ")";
os << " " /*<< node->name() << "::"*/ << it->second->field_name() << " (" << it->second->type_name() << ")";
if (it->second->foreign_endpoint()) {
os << " <---> " << it->second->foreign_endpoint()->node().name() << "::" << it->second->foreign_endpoint()->field_name() << " (" << it->second->foreign_endpoint()->type_name() << ")\n";
os << " <---> " << it->second->node().name() << "::" << it->second->foreign_endpoint()->field_name() << " (" << it->second->foreign_endpoint()->type_name() << ")\n";
} else {
os << "\n";
}

View File

@ -27,17 +27,31 @@ utils::result<void, utils::error> session::create_schema() const {
for (auto it = node->info().endpoint_begin(); it != node->info().endpoint_end(); ++it) {
dependency_graph[node->name()].push_back(it->second->node().name());
in_degree[it->second->node().name()] = std::make_pair(0, it->second->node_ptr());
auto n = it->second->node_ptr();
auto nn = node->name();
if (it->second->is_has_one()) {
continue;
}
if (const auto dit = in_degree.find(it->second->node().name()); dit == in_degree.end()) {
in_degree[it->second->node().name()] = std::make_pair(1, it->second->node_ptr());
} else {
in_degree[it->second->node().name()].first++;
}
}
// Ensure the current node exists in the graph representation
if (in_degree.find(node->name()) == in_degree.end()) {
in_degree[node->name()].first = 0;
in_degree[node->name()] = std::make_pair(0, node);
}
}
for (const auto &it : dependency_graph) {
std::cout << "Dependency graph " << it.first << std::endl;
for (const auto &neighbor: it.second) {
std::cout << " " << neighbor << std::endl;
}
std::cout << std::endl;
}
// Step 2: Perform topological sorting (Kahn's Algorithm)
std::queue<object::schema::node_ptr> zero_in_degree;
std::vector<object::schema::node_ptr> sorted_order;
@ -48,6 +62,11 @@ utils::result<void, utils::error> session::create_schema() const {
}
}
for (const auto &it : in_degree) {
std::cout << "In degree table " << it.second.second->name() << " (" << it.second.first << ")" << std::endl;
}
while (!zero_in_degree.empty()) {
auto current = zero_in_degree.front();
zero_in_degree.pop();
@ -65,7 +84,7 @@ utils::result<void, utils::error> session::create_schema() const {
// Step 3: Check for cycles
if (sorted_order.size() != in_degree.size()) {
throw std::logic_error("Cycle detected in table dependencies");
// throw std::logic_error("Cycle detected in table dependencies");
}
// Step 4: Create tables in the sorted order
@ -86,15 +105,15 @@ utils::result<void, utils::error> session::create_schema() const {
// }
}
auto c = pool_.acquire();
for (const auto &node: *schema_) {
auto result = query::query::create()
.table(node->name(), node->info().definition().columns())
.execute(*c);
if (!result) {
return utils::failure(result.err());
}
}
// auto c = pool_.acquire();
// for (const auto &node: *schema_) {
// auto result = query::query::create()
// .table(node->name(), node->info().definition().columns())
// .execute(*c);
// if (!result) {
// return utils::failure(result.err());
// }
// }
return utils::ok<void>();