172 lines
5.6 KiB
C++
172 lines
5.6 KiB
C++
#ifndef QUERY_SESSION_HPP
|
|
#define QUERY_SESSION_HPP
|
|
|
|
#include "matador/orm/error_code.hpp"
|
|
#include "matador/orm/session_query_builder.hpp"
|
|
|
|
#include "matador/query/query.hpp"
|
|
|
|
#include "matador/sql/column_generator.hpp"
|
|
#include "matador/sql/connection.hpp"
|
|
#include "matador/sql/connection_pool.hpp"
|
|
#include "matador/sql/statement.hpp"
|
|
|
|
#include "matador/object/object_ptr.hpp"
|
|
#include "matador/object/object_definition.hpp"
|
|
#include "matador/object/schema.hpp"
|
|
|
|
#include <unordered_map>
|
|
|
|
namespace matador::orm {
|
|
|
|
utils::error make_error(error_code ec, const std::string &msg);
|
|
|
|
class session final {
|
|
public:
|
|
explicit session(sql::connection_pool<sql::connection> &pool);
|
|
|
|
template<typename Type>
|
|
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &table_name) const;
|
|
|
|
utils::result<void, utils::error> create_schema() const;
|
|
|
|
template<typename Type>
|
|
utils::result<object::object_ptr<Type>, utils::error> insert(Type *obj);
|
|
|
|
template< class Type, typename... Args >
|
|
utils::result<object::object_ptr<Type>, utils::error> insert(Args&&... args) {
|
|
return insert(new Type(std::forward<Args>(args)...));
|
|
}
|
|
|
|
template<typename Type, typename PrimaryKeyType>
|
|
utils::result<object::object_ptr<Type>, utils::error> find(const PrimaryKeyType &pk) {
|
|
auto c = pool_.acquire();
|
|
if (!c.valid()) {
|
|
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
|
|
}
|
|
auto info = schema_->info<Type>();
|
|
if (!info) {
|
|
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
|
}
|
|
|
|
session_query_builder eqb(*schema_);
|
|
auto data = eqb.build<Type>(pk);
|
|
if (!data.is_ok()) {
|
|
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
|
}
|
|
|
|
auto obj = build_select_query(data.release()).template fetch_one<Type>(*c);
|
|
|
|
if (!obj) {
|
|
return utils::failure(obj.err());
|
|
}
|
|
if (!obj->get()) {
|
|
return utils::failure(make_error(error_code::FailedToFindObject, "Failed to find object of type " + info->get().name() + " with primary key " + std::to_string(pk) + "."));
|
|
}
|
|
return utils::ok(object::object_ptr<Type>{ obj.release() });
|
|
}
|
|
|
|
template<typename Type>
|
|
utils::result<sql::query_result<Type>, utils::error> find() {
|
|
auto c = pool_.acquire();
|
|
if (!c.valid()) {
|
|
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
|
|
}
|
|
auto info = schema_->info<Type>();
|
|
if (!info) {
|
|
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
|
}
|
|
|
|
session_query_builder eqb(*schema_);
|
|
auto data = eqb.build<Type>();
|
|
if (!data.is_ok()) {
|
|
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
|
}
|
|
|
|
return build_select_query(data.release()).template fetch_all<Type>(*c);
|
|
}
|
|
|
|
template<typename Type>
|
|
utils::result<query::query_from_intermediate, utils::error> select() {
|
|
auto c = pool_.acquire();
|
|
if (!c.valid()) {
|
|
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
|
|
}
|
|
auto info = schema_->info<Type>();
|
|
if (!info) {
|
|
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
|
}
|
|
|
|
session_query_builder eqb(*schema_);
|
|
auto data = eqb.build<Type>();
|
|
if (!data.is_ok()) {
|
|
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
|
}
|
|
|
|
return utils::ok(build_select_query(data.release()).template fetch_all<Type>(*c));
|
|
}
|
|
|
|
template<typename Type>
|
|
utils::result<void, utils::error> drop_table();
|
|
utils::result<void, utils::error> drop_table(const std::string &table_name) const;
|
|
|
|
[[nodiscard]] utils::result<sql::query_result<sql::record>, utils::error> fetch(const sql::query_context &q) const;
|
|
[[nodiscard]] size_t execute(const std::string &sql) const;
|
|
[[nodiscard]] sql::statement prepare(const sql::query_context& q) const;
|
|
|
|
[[nodiscard]] std::vector<object::attribute_definition> describe_table(const std::string &table_name) const;
|
|
[[nodiscard]] bool table_exists(const std::string &table_name) const;
|
|
|
|
[[nodiscard]] const sql::dialect& dialect() const;
|
|
|
|
private:
|
|
friend class query_select;
|
|
|
|
static query::fetchable_query build_select_query(entity_query_data &&data);
|
|
|
|
private:
|
|
sql::connection_pool<sql::connection> &pool_;
|
|
const sql::dialect &dialect_;
|
|
|
|
std::unique_ptr<object::schema> schema_;
|
|
mutable std::unordered_map<std::string, object::object_definition> prototypes_;
|
|
};
|
|
|
|
template<typename Type>
|
|
[[nodiscard]] utils::result<void, utils::error> session::attach(const std::string &table_name) const {
|
|
return schema_->attach<Type>(table_name);
|
|
}
|
|
|
|
template<typename Type>
|
|
utils::result<object::object_ptr<Type>, utils::error> session::insert(Type *obj)
|
|
{
|
|
auto c = pool_.acquire();
|
|
auto info = schema_->info<Type>();
|
|
if (!info) {
|
|
return utils::failure(info.err());
|
|
}
|
|
|
|
auto res = query::query::insert()
|
|
.into(info->get().name(), sql::column_generator::generate<Type>(*schema_, true))
|
|
.values(*obj)
|
|
.execute(*c);
|
|
if (!res) {
|
|
return utils::failure(res.err());
|
|
}
|
|
|
|
return utils::ok(object::object_ptr{obj});
|
|
}
|
|
|
|
template<typename Type>
|
|
utils::result<void, utils::error> session::drop_table() {
|
|
auto info = schema_->info<Type>();
|
|
if (info) {
|
|
return drop_table(info->get().name());
|
|
}
|
|
|
|
return utils::failure(info.err());
|
|
}
|
|
|
|
}
|
|
#endif //QUERY_SESSION_HPP
|