session topological sorted table creation (progress)

This commit is contained in:
Sascha Kühl 2025-07-14 22:39:15 +02:00
parent 0d93b9b1ed
commit e7376ef269
1 changed files with 98 additions and 36 deletions

View File

@ -4,10 +4,10 @@
#include "matador/query/query.hpp" #include "matador/query/query.hpp"
#include <queue>
#include <stdexcept> #include <stdexcept>
namespace matador::orm { namespace matador::orm {
utils::error make_error(const error_code ec, const std::string &msg) { utils::error make_error(const error_code ec, const std::string &msg) {
return utils::error(ec, msg); return utils::error(ec, msg);
} }
@ -15,7 +15,8 @@ utils::error make_error( const error_code ec, const std::string& msg ) {
session::session(sql::connection_pool<sql::connection> &pool) session::session(sql::connection_pool<sql::connection> &pool)
: pool_(pool) : pool_(pool)
, dialect_(sql::backend_provider::instance().connection_dialect(pool_.info().type)) , dialect_(sql::backend_provider::instance().connection_dialect(pool_.info().type))
, schema_(std::make_unique<object::schema>(dialect_.default_schema_name())){} , schema_(std::make_unique<object::schema>(dialect_.default_schema_name())) {
}
utils::result<void, utils::error> session::create_schema() const { utils::result<void, utils::error> session::create_schema() const {
std::vector<std::shared_ptr<object::schema_node> > relations_nodes; std::vector<std::shared_ptr<object::schema_node> > relations_nodes;
@ -33,6 +34,73 @@ utils::result<void, utils::error> session::create_schema() const {
} }
} }
return utils::ok<void>(); return utils::ok<void>();
// Step 1: Build dependency graph
std::unordered_map<std::string, std::vector<std::string> > dependency_graph;
std::unordered_map<std::string, std::pair<int,object::schema::node_ptr>> in_degree;
for (const auto &node: *schema_) {
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());
in_degree[it->second->node().name()]++;
}
// Ensure the current node exists in the graph representation
if (in_degree.find(node->name()) == in_degree.end()) {
in_degree[node->name()] = 0;
}
}
// 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;
for (const auto &[table, degree]: in_degree) {
if (degree == 0) {
zero_in_degree.push(table);
}
}
while (!zero_in_degree.empty()) {
auto current = zero_in_degree.front();
zero_in_degree.pop();
sorted_order.push_back(current);
for (const auto &neighbor: dependency_graph[current]) {
in_degree[neighbor]--;
if (in_degree[neighbor] == 0) {
zero_in_degree.push(neighbor);
}
}
}
// Step 3: Check for cycles
if (sorted_order.size() != in_degree.size()) {
throw std::logic_error("Cycle detected in table dependencies");
}
// Step 4: Create tables in the sorted order
for (const auto &table_name : sorted_order) {
// schema_.
// auto result = query::query::create()
//
// .table(table_name, /* Pass table definition here */)
//
// .execute(*c);
//
// if (!result) {
//
// return utils::failure(result.err());
//
// }
}
} }
utils::result<void, utils::error> session::drop_table(const std::string &table_name) const { utils::result<void, utils::error> session::drop_table(const std::string &table_name) const {
@ -51,8 +119,7 @@ utils::result<void, utils::error> session::drop_table(const std::string &table_n
return utils::ok<void>(); return utils::ok<void>();
} }
utils::result<sql::query_result<sql::record>, utils::error> session::fetch(const sql::query_context &q) const utils::result<sql::query_result<sql::record>, utils::error> session::fetch(const sql::query_context &q) const {
{
auto c = pool_.acquire(); auto c = pool_.acquire();
if (!c.valid()) { if (!c.valid()) {
throw std::logic_error("no database connection available"); throw std::logic_error("no database connection available");
@ -83,8 +150,7 @@ size_t session::execute(const std::string &sql) const {
return c->execute(sql); return c->execute(sql);
} }
sql::statement session::prepare(const sql::query_context& q) const sql::statement session::prepare(const sql::query_context &q) const {
{
auto c = pool_.acquire(); auto c = pool_.acquire();
if (!c.valid()) { if (!c.valid()) {
throw std::logic_error("no database connection available"); throw std::logic_error("no database connection available");
@ -92,8 +158,7 @@ sql::statement session::prepare(const sql::query_context& q) const
return c->prepare(q).release(); return c->prepare(q).release();
} }
std::vector<object::attribute_definition> session::describe_table(const std::string &table_name) const std::vector<object::attribute_definition> session::describe_table(const std::string &table_name) const {
{
auto c = pool_.acquire(); auto c = pool_.acquire();
if (!c.valid()) { if (!c.valid()) {
throw std::logic_error("no database connection available"); throw std::logic_error("no database connection available");
@ -101,8 +166,7 @@ std::vector<object::attribute_definition> session::describe_table(const std::str
return c->describe(table_name).release(); return c->describe(table_name).release();
} }
bool session::table_exists(const std::string &table_name) const bool session::table_exists(const std::string &table_name) const {
{
auto c = pool_.acquire(); auto c = pool_.acquire();
if (!c.valid()) { if (!c.valid()) {
throw std::logic_error("no database connection available"); throw std::logic_error("no database connection available");
@ -110,8 +174,7 @@ bool session::table_exists(const std::string &table_name) const
return c->exists(dialect_.default_schema_name(), table_name); return c->exists(dialect_.default_schema_name(), table_name);
} }
const class sql::dialect &session::dialect() const const class sql::dialect &session::dialect() const {
{
return dialect_; return dialect_;
} }
@ -127,5 +190,4 @@ query::fetchable_query session::build_select_query(entity_query_data &&data) {
.order_by(sql::column{data.root_table, data.pk_column_name}) .order_by(sql::column{data.root_table, data.pk_column_name})
.asc(); .asc();
} }
} }