fixed backend test compilation
This commit is contained in:
@@ -1,16 +1,38 @@
|
||||
#ifndef OBJECT_PTR_HPP
|
||||
#define OBJECT_PTR_HPP
|
||||
|
||||
#include "matador/utils/identifier.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace matador::object {
|
||||
template <typename Type>
|
||||
class object_ptr {
|
||||
public:
|
||||
using value_type = Type;
|
||||
Type* operator->() { return ptr; };
|
||||
Type& operator*() { return *ptr; };
|
||||
object_ptr() = default;
|
||||
object_ptr(Type *obj) : ptr_(obj) {}
|
||||
explicit object_ptr(std::shared_ptr<Type> obj) : ptr_(obj) {}
|
||||
object_ptr(const object_ptr &other) : ptr_(other.ptr_) {}
|
||||
object_ptr(object_ptr &&other) noexcept : ptr_(std::move(other.ptr_)) {}
|
||||
object_ptr &operator=(const object_ptr &other) = default;
|
||||
object_ptr &operator=(object_ptr &&other) = default;
|
||||
|
||||
using value_type = Type;
|
||||
Type* operator->() { return ptr_.get(); }
|
||||
Type& operator*() const { return *ptr_; }
|
||||
|
||||
[[nodiscard]] bool empty() const { return ptr_ == nullptr; }
|
||||
|
||||
Type* get() { return ptr_.get(); }
|
||||
const Type* get() const { return ptr_.get(); }
|
||||
operator bool() { return valid(); }
|
||||
bool valid() { return ptr_ != nullptr; }
|
||||
|
||||
[[nodiscard]] const utils::identifier& primary_key() const { return pk_; }
|
||||
void primary_key(const utils::identifier &pk) { pk_ = pk; }
|
||||
private:
|
||||
Type* ptr{nullptr};
|
||||
std::shared_ptr<Type> ptr_{};
|
||||
utils::identifier pk_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -25,11 +25,10 @@ protected:
|
||||
|
||||
public:
|
||||
template < class Type >
|
||||
utils::result<sql::query_result<Type>, utils::error> fetch_all(sql::executor &exec)
|
||||
{
|
||||
utils::result<sql::query_result<Type>, utils::error> fetch_all(sql::executor &exec) {
|
||||
auto result = fetch(exec);
|
||||
if (!result.is_ok()) {
|
||||
return utils::error(result.err());
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok(sql::query_result<Type>(result.release()));
|
||||
@@ -41,7 +40,7 @@ public:
|
||||
{
|
||||
auto result = fetch(exec);
|
||||
if (!result.is_ok()) {
|
||||
return utils::error(result.err());
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
auto objects = sql::query_result<Type>(result.release());
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
|
||||
#include "matador/query/intermediates/executable_query.hpp"
|
||||
|
||||
#include "matador/object/attribute_definition_generator.hpp"
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
class query_create_intermediate : public query_intermediate
|
||||
@@ -14,11 +16,10 @@ public:
|
||||
|
||||
executable_query table(const sql::table &table, std::initializer_list<object::attribute_definition> columns);
|
||||
executable_query table(const sql::table &table, const std::vector<object::attribute_definition> &columns);
|
||||
// template<class Type>
|
||||
// executable_query table(const sql::table &table, const sql::schema &schema)
|
||||
// {
|
||||
// return this->table(table, column_definition_generator::generate<Type>(schema));
|
||||
// }
|
||||
template<class Type>
|
||||
executable_query table(const sql::table &table, const object::schema &schema) {
|
||||
return this->table(table, object::attribute_definition_generator::generate<Type>(schema));
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -2,20 +2,21 @@
|
||||
#define QUERY_INSERT_INTERMEDIATE_HPP
|
||||
|
||||
#include "matador/query/intermediates/query_intermediate.hpp"
|
||||
#include "matador/query/intermediates/query_into_intermediate.hpp"
|
||||
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
class query_into_intermediate;
|
||||
|
||||
class query_insert_intermediate : public query_intermediate
|
||||
{
|
||||
public:
|
||||
query_insert_intermediate();
|
||||
|
||||
// template<class Type>
|
||||
// query_into_intermediate into(const sql::table &table, const sql::schema &schema) {
|
||||
// return into(table, column_generator::generate<Type>(schema));
|
||||
// }
|
||||
template<class Type>
|
||||
query_into_intermediate into(const sql::table &table, const object::schema &schema) {
|
||||
return into(table, sql::column_generator::generate<Type>(schema));
|
||||
}
|
||||
query_into_intermediate into(const sql::table &table, std::initializer_list<sql::column> columns);
|
||||
query_into_intermediate into(const sql::table &table, std::vector<sql::column> &&columns);
|
||||
query_into_intermediate into(const sql::table &table, const std::vector<std::string> &column_names);
|
||||
|
||||
@@ -9,15 +9,6 @@
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
// template < class Type >
|
||||
// std::vector<utils::any_type> as_placeholder(const Type &obj)
|
||||
// {
|
||||
// placeholder_generator generator;
|
||||
// access::process(generator, obj);
|
||||
|
||||
// return generator.placeholder_values;
|
||||
// \}
|
||||
|
||||
class query_into_intermediate : public query_intermediate
|
||||
{
|
||||
public:
|
||||
@@ -25,16 +16,15 @@ public:
|
||||
|
||||
executable_query values(std::initializer_list<std::variant<utils::placeholder, utils::database_type>> values);
|
||||
executable_query values(std::vector<std::variant<utils::placeholder, utils::database_type>> &&values);
|
||||
// template<class Type>
|
||||
// executable_query values()
|
||||
// {
|
||||
// Type obj;
|
||||
// return values(std::move(as_placeholder(obj)));
|
||||
// }
|
||||
executable_query values(std::vector<utils::database_type> &&values);
|
||||
template<class Type>
|
||||
executable_query values(const Type &obj)
|
||||
{
|
||||
return values(std::move(value_extractor::extract(obj)));
|
||||
executable_query values() {
|
||||
Type obj;
|
||||
return values(obj);
|
||||
}
|
||||
template<class Type>
|
||||
executable_query values(const Type &obj) {
|
||||
return values(value_extractor::extract(obj));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -11,31 +11,19 @@
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
// template < class Type >
|
||||
// std::vector<internal::key_value_pair> as_key_value_placeholder(const Type &obj)
|
||||
// {
|
||||
// placeholder_key_value_generator generator;
|
||||
// access::process(generator, obj);
|
||||
//
|
||||
// return generator.placeholder_values;
|
||||
// }
|
||||
|
||||
class query_update_intermediate : public query_intermediate
|
||||
{
|
||||
class query_update_intermediate : public query_intermediate {
|
||||
public:
|
||||
explicit query_update_intermediate(const sql::table& table);
|
||||
|
||||
query_set_intermediate set(std::initializer_list<internal::key_value_pair> columns);
|
||||
query_set_intermediate set(std::vector<internal::key_value_pair> &&columns);
|
||||
// template<class Type>
|
||||
// query_set_intermediate set()
|
||||
// {
|
||||
// Type obj;
|
||||
// return set(std::move(as_key_value_placeholder(obj)));
|
||||
// }
|
||||
template<class Type>
|
||||
query_set_intermediate set(const Type &obj)
|
||||
{
|
||||
query_set_intermediate set() {
|
||||
Type obj;
|
||||
return set(obj);
|
||||
}
|
||||
template<class Type>
|
||||
query_set_intermediate set(const Type &obj) {
|
||||
return set(key_value_generator::generate(obj));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
#include "matador/query/query_intermediates.hpp"
|
||||
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
class connection;
|
||||
}
|
||||
@@ -22,10 +24,10 @@ public:
|
||||
[[nodiscard]] static query_select_intermediate select(const std::vector<sql::column>& columns);
|
||||
[[nodiscard]] static query_select_intermediate select(const std::vector<std::string> &column_names);
|
||||
[[nodiscard]] static query_select_intermediate select(std::vector<sql::column> columns, std::initializer_list<sql::column> additional_columns);
|
||||
// template<class Type>
|
||||
// [[nodiscard]] static query_select_intermediate select(const sql::schema &schema) {
|
||||
// return select(sql::column_generator::generate<Type>(schema));
|
||||
// }
|
||||
template<class Type>
|
||||
[[nodiscard]] static query_select_intermediate select(const object::schema &schema) {
|
||||
return select(sql::column_generator::generate<Type>(schema));
|
||||
}
|
||||
[[nodiscard]] static query_insert_intermediate insert();
|
||||
[[nodiscard]] static query_update_intermediate update(const sql::table &table);
|
||||
[[nodiscard]] static query_delete_intermediate remove();
|
||||
|
||||
@@ -17,8 +17,7 @@ private:
|
||||
|
||||
public:
|
||||
template < class Type >
|
||||
static std::vector<utils::database_type> extract(const Type &type)
|
||||
{
|
||||
static std::vector<utils::database_type> extract(const Type &type) {
|
||||
std::vector<utils::database_type> values;
|
||||
value_extractor gen(values);
|
||||
access::process(gen, type);
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#ifndef QUERY_CONNECTION_HPP
|
||||
#define QUERY_CONNECTION_HPP
|
||||
|
||||
#include "matador/sql/abstract_sql_logger.hpp"
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include "matador/sql/abstract_sql_logger.hpp"
|
||||
#include "matador/sql/connection_info.hpp"
|
||||
#include "matador/sql/executor.hpp"
|
||||
#include "matador/sql/statement.hpp"
|
||||
@@ -51,7 +52,7 @@ public:
|
||||
*
|
||||
* @param x The connection to copy move
|
||||
*/
|
||||
connection(connection &&x) noexcept = default;
|
||||
connection(connection &&x) noexcept;
|
||||
/**
|
||||
* Assigns moves from the given connection
|
||||
*
|
||||
|
||||
@@ -29,8 +29,7 @@ struct connection_info {
|
||||
std::string driver{}; /**< Driver to use. This is used by th mssql/odbc backends. */
|
||||
|
||||
template < class Operator >
|
||||
void process(Operator &op)
|
||||
{
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::attribute(op, "type", type);
|
||||
field::attribute(op, "user", user);
|
||||
|
||||
@@ -81,7 +81,8 @@ public:
|
||||
connection_repo_.emplace_back(count, info_);
|
||||
auto &conn = connection_repo_.back();
|
||||
idle_connections_.emplace(conn.first, &conn);
|
||||
conn.second.open();
|
||||
// Todo: handle result
|
||||
std::ignore = conn.second.open();
|
||||
--count;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,8 +33,7 @@ public:
|
||||
}
|
||||
|
||||
template < class Type >
|
||||
void bind(const size_t pos, Type &val)
|
||||
{
|
||||
void bind(const size_t pos, Type &val) {
|
||||
utils::data_type_traits<Type>::bind_value(binder(), adjust_index(pos), val);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user