126 lines
3.5 KiB
C++
126 lines
3.5 KiB
C++
#include "matador/orm/session.hpp"
|
|
|
|
#include "matador/sql/backend_provider.hpp"
|
|
|
|
#include "matador/query/query.hpp"
|
|
|
|
#include <stdexcept>
|
|
|
|
namespace matador::orm {
|
|
|
|
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 {
|
|
auto c = pool_.acquire();
|
|
for (const auto &t : *schema_) {
|
|
auto result = query::query::create()
|
|
.table(t.name(), t.basic_info().definition().columns())
|
|
.execute(*c);
|
|
if ( !result ) {
|
|
return utils::failure(result.err());
|
|
}
|
|
}
|
|
return utils::ok<void>();
|
|
}
|
|
|
|
void session::drop_table(const std::string &table_name)
|
|
{
|
|
auto c = pool_.acquire();
|
|
if (!c.valid()) {
|
|
throw std::logic_error("no database connection available");
|
|
}
|
|
|
|
// c->query(*schema_).drop().table(table_name).execute();
|
|
}
|
|
|
|
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()); /*col.type() == utils::basic_type::type_unknown && */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)/*, q.prototype*/});
|
|
}
|
|
|
|
//query_result<record> session::fetch(const std::string &sql) const
|
|
//{
|
|
// return query_result<record>(std::unique_ptr());
|
|
//}
|
|
|
|
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_;
|
|
}
|
|
|
|
// std::unique_ptr<sql::query_result_impl> session::fetch(const std::string &sql) const
|
|
// {
|
|
// auto c = pool_.acquire();
|
|
// if (!c.valid()) {
|
|
// throw std::logic_error("no database connection available");
|
|
// }
|
|
// return c->fetch(sql);
|
|
// }
|
|
|
|
query::fetchable_query session::build_select_query(sql::connection_ptr<sql::connection> &conn, entity_query_data &&data) {
|
|
return query::query::select(data.columns)
|
|
.from(data.root_table_name)
|
|
.join_left(data.joins)
|
|
.where(std::move(data.where_clause))
|
|
.order_by(sql::column{data.root_table_name, data.pk_column_})
|
|
.asc();
|
|
}
|
|
|
|
} |