210 lines
6.1 KiB
C++
210 lines
6.1 KiB
C++
#include "matador/orm/session.hpp"
|
|
|
|
#include "matador/sql/backend_provider.hpp"
|
|
|
|
#include "matador/query/query.hpp"
|
|
|
|
#include <queue>
|
|
#include <stdexcept>
|
|
|
|
namespace matador::orm {
|
|
utils::error make_error(const error_code ec, const std::string &msg) {
|
|
return utils::error(ec, msg);
|
|
}
|
|
|
|
session::session(sql::connection_pool<sql::connection> &pool)
|
|
: pool_(pool)
|
|
, dialect_(sql::backend_provider::instance().connection_dialect(pool_.info().type))
|
|
, schema_(std::make_unique<object::schema>(dialect_.default_schema_name())) {
|
|
}
|
|
|
|
utils::result<void, utils::error> session::create_schema() 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::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());
|
|
|
|
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()] = 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;
|
|
|
|
for (const auto &[table, degree]: in_degree) {
|
|
if (degree.first == 0) {
|
|
zero_in_degree.push(degree.second);
|
|
}
|
|
}
|
|
|
|
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();
|
|
sorted_order.push_back(current);
|
|
|
|
for (const auto &neighbor: dependency_graph[current->name()]) {
|
|
in_degree[neighbor].first--;
|
|
|
|
if (in_degree[neighbor].first == 0) {
|
|
zero_in_degree.push(in_degree[neighbor].second);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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 &node : sorted_order) {
|
|
std::cout << "Creating table " << node->name() << std::endl;
|
|
// schema_.
|
|
// auto result = query::query::create()
|
|
//
|
|
// .table(table_name, /* Pass table definition here */)
|
|
//
|
|
// .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>();
|
|
|
|
}
|
|
|
|
utils::result<void, utils::error> session::drop_table(const std::string &table_name) const {
|
|
auto c = pool_.acquire();
|
|
if (!c.valid()) {
|
|
throw std::logic_error("no database connection available");
|
|
}
|
|
|
|
auto result = query::query::drop()
|
|
.table(table_name)
|
|
.execute(*c);
|
|
if (result.is_error()) {
|
|
return utils::failure(result.err());
|
|
}
|
|
|
|
return utils::ok<void>();
|
|
}
|
|
|
|
utils::result<sql::query_result<sql::record>, utils::error> session::fetch(const sql::query_context &q) const {
|
|
auto c = pool_.acquire();
|
|
if (!c.valid()) {
|
|
throw std::logic_error("no database connection available");
|
|
}
|
|
auto it = prototypes_.find(q.table.name);
|
|
if (it == prototypes_.end()) {
|
|
auto result = c->describe(q.table.name);
|
|
if (!result) {
|
|
return utils::failure(result.err());
|
|
}
|
|
it = prototypes_.emplace(q.table.name, *result).first;
|
|
}
|
|
// adjust columns from given query
|
|
for (auto &col: q.prototype) {
|
|
if (const auto rit = it->second.find(col.name()); rit != it->second.end()) {
|
|
const_cast<object::attribute_definition &>(col).type(rit->type());
|
|
}
|
|
}
|
|
auto res = c->fetch(q);
|
|
return utils::ok(sql::query_result<sql::record>{std::move(*res)});
|
|
}
|
|
|
|
size_t session::execute(const std::string &sql) const {
|
|
auto c = pool_.acquire();
|
|
if (!c.valid()) {
|
|
throw std::logic_error("no database connection available");
|
|
}
|
|
return c->execute(sql);
|
|
}
|
|
|
|
sql::statement session::prepare(const sql::query_context &q) const {
|
|
auto c = pool_.acquire();
|
|
if (!c.valid()) {
|
|
throw std::logic_error("no database connection available");
|
|
}
|
|
return c->prepare(q).release();
|
|
}
|
|
|
|
std::vector<object::attribute_definition> session::describe_table(const std::string &table_name) const {
|
|
auto c = pool_.acquire();
|
|
if (!c.valid()) {
|
|
throw std::logic_error("no database connection available");
|
|
}
|
|
return c->describe(table_name).release();
|
|
}
|
|
|
|
bool session::table_exists(const std::string &table_name) const {
|
|
auto c = pool_.acquire();
|
|
if (!c.valid()) {
|
|
throw std::logic_error("no database connection available");
|
|
}
|
|
return c->exists(dialect_.default_schema_name(), table_name);
|
|
}
|
|
|
|
const class sql::dialect &session::dialect() const {
|
|
return dialect_;
|
|
}
|
|
|
|
void session::dump_schema(std::ostream &os) const {
|
|
schema_->dump(os);
|
|
}
|
|
|
|
query::fetchable_query session::build_select_query(entity_query_data &&data) {
|
|
return query::query::select(data.columns)
|
|
.from(*data.root_table)
|
|
.join_left(data.joins)
|
|
.where(std::move(data.where_clause))
|
|
.order_by(sql::column{data.root_table, data.pk_column_name})
|
|
.asc();
|
|
}
|
|
}
|