session topological sorted table creation (progress)
This commit is contained in:
parent
e7376ef269
commit
dd737623f4
|
|
@ -31,6 +31,8 @@ public:
|
|||
[[nodiscard]] std::string type_name() const;
|
||||
[[nodiscard]] const schema_node& node() const;
|
||||
|
||||
std::shared_ptr<schema_node> node_ptr() const;
|
||||
|
||||
[[nodiscard]] bool is_has_one() const;
|
||||
[[nodiscard]] bool is_has_many() const;
|
||||
[[nodiscard]] bool is_belongs_to() const;
|
||||
|
|
|
|||
|
|
@ -25,6 +25,10 @@ const schema_node &relation_endpoint::node() const {
|
|||
return *node_;
|
||||
}
|
||||
|
||||
std::shared_ptr<schema_node> relation_endpoint::node_ptr() const {
|
||||
return node_;
|
||||
}
|
||||
|
||||
bool relation_endpoint::is_has_one() const {
|
||||
return type_ == relation_type::HAS_ONE;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,23 +19,6 @@ session::session(sql::connection_pool<sql::connection> &pool)
|
|||
}
|
||||
|
||||
utils::result<void, utils::error> session::create_schema() const {
|
||||
std::vector<std::shared_ptr<object::schema_node> > relations_nodes;
|
||||
auto c = pool_.acquire();
|
||||
for (const auto &node: *schema_) {
|
||||
// if (!node->info().has_primary_key()) {
|
||||
// relations_nodes.push_back(node);
|
||||
// continue;
|
||||
// }
|
||||
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>();
|
||||
|
||||
|
||||
// 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;
|
||||
|
|
@ -44,14 +27,14 @@ 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());
|
||||
in_degree[it->second->node().name()]++;
|
||||
in_degree[it->second->node().name()] = std::make_pair(0, it->second->node_ptr());
|
||||
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()] = 0;
|
||||
in_degree[node->name()].first = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -60,8 +43,8 @@ utils::result<void, utils::error> session::create_schema() const {
|
|||
std::vector<object::schema::node_ptr> sorted_order;
|
||||
|
||||
for (const auto &[table, degree]: in_degree) {
|
||||
if (degree == 0) {
|
||||
zero_in_degree.push(table);
|
||||
if (degree.first == 0) {
|
||||
zero_in_degree.push(degree.second);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -70,11 +53,11 @@ utils::result<void, utils::error> session::create_schema() const {
|
|||
zero_in_degree.pop();
|
||||
sorted_order.push_back(current);
|
||||
|
||||
for (const auto &neighbor: dependency_graph[current]) {
|
||||
in_degree[neighbor]--;
|
||||
for (const auto &neighbor: dependency_graph[current->name()]) {
|
||||
in_degree[neighbor].first--;
|
||||
|
||||
if (in_degree[neighbor] == 0) {
|
||||
zero_in_degree.push(neighbor);
|
||||
if (in_degree[neighbor].first == 0) {
|
||||
zero_in_degree.push(in_degree[neighbor].second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -87,7 +70,8 @@ utils::result<void, utils::error> session::create_schema() const {
|
|||
|
||||
// Step 4: Create tables in the sorted order
|
||||
|
||||
for (const auto &table_name : sorted_order) {
|
||||
for (const auto &node : sorted_order) {
|
||||
std::cout << "Creating table " << node->name() << std::endl;
|
||||
// schema_.
|
||||
// auto result = query::query::create()
|
||||
//
|
||||
|
|
@ -101,6 +85,19 @@ 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());
|
||||
}
|
||||
}
|
||||
|
||||
return utils::ok<void>();
|
||||
|
||||
}
|
||||
|
||||
utils::result<void, utils::error> session::drop_table(const std::string &table_name) const {
|
||||
|
|
|
|||
Loading…
Reference in New Issue