fixed backend test compilation
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#ifndef ERROR_CODE_HPP
|
||||
#define ERROR_CODE_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <system_error>
|
||||
|
||||
namespace matador::orm {
|
||||
|
||||
enum class error_code : uint8_t {
|
||||
Ok = 0,
|
||||
NoConnectionAvailable,
|
||||
UnknownType,
|
||||
FailedToBuildQuery,
|
||||
FailedToFindObject
|
||||
};
|
||||
|
||||
class orm_category_impl final : public std::error_category
|
||||
{
|
||||
public:
|
||||
[[nodiscard]] const char* name() const noexcept override;
|
||||
[[nodiscard]] std::string message(int ev) const override;
|
||||
};
|
||||
|
||||
const std::error_category& orm_category();
|
||||
std::error_code make_error_code(error_code e);
|
||||
std::error_condition make_error_condition(error_code e);
|
||||
|
||||
}
|
||||
|
||||
template <>
|
||||
struct std::is_error_code_enum<matador::orm::error_code> : true_type {};
|
||||
|
||||
#endif //ERROR_CODE_HPP
|
||||
@@ -1,6 +1,7 @@
|
||||
#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"
|
||||
@@ -18,16 +19,9 @@
|
||||
|
||||
namespace matador::orm {
|
||||
|
||||
enum class session_error {
|
||||
Ok = 0,
|
||||
NoConnectionAvailable,
|
||||
UnknownType,
|
||||
FailedToBuildQuery,
|
||||
FailedToFindObject
|
||||
};
|
||||
utils::error make_error(error_code ec, const std::string &msg);
|
||||
|
||||
class session
|
||||
{
|
||||
class session final {
|
||||
public:
|
||||
explicit session(sql::connection_pool<sql::connection> &pool);
|
||||
|
||||
@@ -37,81 +31,81 @@ public:
|
||||
utils::result<void, utils::error> create_schema() const;
|
||||
|
||||
template<typename Type>
|
||||
object::object_ptr<Type> insert(Type *obj);
|
||||
utils::result<object::object_ptr<Type>, utils::error> insert(Type *obj);
|
||||
|
||||
template< class Type, typename... Args >
|
||||
object::object_ptr<Type> insert(Args&&... 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>, session_error> find(const PrimaryKeyType &pk) {
|
||||
utils::result<object::object_ptr<Type>, utils::error> find(const PrimaryKeyType &pk) {
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
return utils::failure(session_error::NoConnectionAvailable);
|
||||
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
|
||||
}
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return utils::failure(session_error::UnknownType);
|
||||
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(session_error::FailedToBuildQuery);
|
||||
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
||||
}
|
||||
|
||||
auto obj = build_select_query(c, data.release()).template fetch_one<Type>();
|
||||
auto obj = build_select_query(data.release()).template fetch_one<Type>(*c);
|
||||
|
||||
if (!obj) {
|
||||
return utils::failure(session_error::FailedToFindObject);
|
||||
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>, session_error> find() {
|
||||
utils::result<sql::query_result<Type>, utils::error> find() {
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
return utils::failure(session_error::NoConnectionAvailable);
|
||||
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
|
||||
}
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return utils::failure(session_error::UnknownType);
|
||||
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(session_error::FailedToBuildQuery);
|
||||
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
||||
}
|
||||
|
||||
return utils::ok(build_select_query(c, data.release()).template fetch_all<Type>());
|
||||
return build_select_query(data.release()).template fetch_all<Type>(*c);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
utils::result<query::query_from_intermediate, session_error> select() {
|
||||
utils::result<query::query_from_intermediate, utils::error> select() {
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
return utils::failure(session_error::NoConnectionAvailable);
|
||||
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
|
||||
}
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return utils::failure(session_error::UnknownType);
|
||||
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(session_error::FailedToBuildQuery);
|
||||
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
||||
}
|
||||
|
||||
return utils::ok(build_select_query(c, data.release()).template fetch_all<Type>());
|
||||
return utils::ok(build_select_query(data.release()).template fetch_all<Type>(*c));
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void drop_table();
|
||||
void drop_table(const std::string &table_name);
|
||||
void 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]] query_result<record> fetch(const std::string &sql) const;
|
||||
@@ -128,7 +122,7 @@ private:
|
||||
|
||||
// [[nodiscard]] std::unique_ptr<sql::query_result_impl> fetch(const std::string &sql) const;
|
||||
|
||||
static query::fetchable_query build_select_query(sql::connection_ptr<sql::connection> &conn, entity_query_data &&data);
|
||||
static query::fetchable_query build_select_query(entity_query_data &&data);
|
||||
|
||||
private:
|
||||
sql::connection_pool<sql::connection> &pool_;
|
||||
@@ -145,20 +139,23 @@ template<typename Type>
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
object::object_ptr<Type> session::insert(Type *obj)
|
||||
utils::result<object::object_ptr<Type>, utils::error> session::insert(Type *obj)
|
||||
{
|
||||
auto c = pool_.acquire();
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return {};
|
||||
return utils::failure(info.err());
|
||||
}
|
||||
|
||||
query::query::insert()
|
||||
.into(info->name, sql::column_generator::generate<Type>(*schema_, true))
|
||||
auto res = query::query::insert()
|
||||
.into(info->get().name(), sql::column_generator::generate<Type>(*schema_, true))
|
||||
.values(*obj)
|
||||
.execute();
|
||||
.execute(*c);
|
||||
if (!res) {
|
||||
return utils::failure(res.err());
|
||||
}
|
||||
|
||||
return object::object_ptr{obj};
|
||||
return utils::ok(object::object_ptr{obj});
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
|
||||
@@ -121,9 +121,9 @@ public:
|
||||
}
|
||||
pk_ = nullptr;
|
||||
table_info_stack_.push(info.value());
|
||||
entity_query_data_ = { info->name() };
|
||||
entity_query_data_ = { info->get().name() };
|
||||
try {
|
||||
access::process(*this, info->prototype());
|
||||
access::process(*this, info->get().prototype());
|
||||
|
||||
return {utils::ok(std::move(entity_query_data_))};
|
||||
} catch (const query_builder_exception &ex) {
|
||||
|
||||
Reference in New Issue
Block a user