orm schema class progress

This commit is contained in:
Sascha Kühl 2025-10-30 15:29:31 +01:00
parent 270c2922ff
commit 5632746eb6
2 changed files with 71 additions and 0 deletions

View File

@ -3,6 +3,10 @@
#include "matador/object/repository.hpp"
namespace matador::sql {
class connection_pool;
}
namespace matador::orm {
class schema {
@ -19,6 +23,9 @@ public:
return repo_.attach<Type, SuperType>(name);
}
utils::result<void, utils::error> create(sql::connection_pool &pool) const;
utils::result<void, utils::error> drop() const;
private:
object::repository repo_;
};

View File

@ -1,6 +1,70 @@
#include "matador/orm/schema.hpp"
#include "matador/query/query.hpp"
#include "matador/sql/connection_pool.hpp"
namespace matador::orm {
schema::schema( const std::string& name )
: repo_(name){}
}
matador::utils::result<void, matador::utils::error> matador::orm::schema::create(sql::connection_pool &pool) const {
// 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::repository::node_ptr>> in_degree;
//
// for (const auto &node: repo_) {
// for (auto it = node->info().endpoint_begin(); it != node->info().endpoint_end(); ++it) {
// dependency_graph[node->name()].push_back(it->second->node().name());
//
// 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()] = 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;
// }
std::vector<std::string> fk_sql_commands;
auto c = pool.acquire();
for (const auto &node: repo_) {
auto ctx = query::query::create()
.table(node->name(), node->info().definition().columns())
.compile(*c);
for ( const auto& [sql, command] : ctx.additional_commands ) {
fk_sql_commands.push_back( sql );
}
std::cout << ctx.sql << std::endl;
if (auto result = c->execute(ctx.sql); !result) {
return utils::failure(result.err());
}
}
// execute additional commands (e.g. ALTER TABLE ADD FK)
for (const auto &sql: fk_sql_commands) {
std::cout << sql << std::endl;
if (auto result = c->execute(sql); !result) {
return utils::failure(result.err());
}
}
return utils::ok<void>();
}
matador::utils::result<void, matador::utils::error> matador::orm::schema::drop() const {
return utils::ok<void>();
}