131 lines
3.6 KiB
C++
131 lines
3.6 KiB
C++
#include "matador/orm/session.hpp"
|
|
|
|
#include "matador/sql/backend_provider.hpp"
|
|
|
|
#include "matador/query/query.hpp"
|
|
|
|
#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 {
|
|
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>();
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
} |