moved session to namespace query and move basic_schema::initialize to session constructor
This commit is contained in:
parent
a34a3dc266
commit
3a82f4891a
|
|
@ -23,7 +23,7 @@
|
|||
|
||||
#include "matador/query/schema.hpp"
|
||||
|
||||
#include "matador/orm/session.hpp"
|
||||
#include "matador/query/session.hpp"
|
||||
|
||||
template <> struct matador::utils::data_type_traits<work::core::UserInfo, void> {
|
||||
static basic_type type(std::size_t /*size*/) { return basic_type::UInt64; }
|
||||
|
|
|
|||
|
|
@ -1,36 +0,0 @@
|
|||
#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,
|
||||
NoPrimaryKey,
|
||||
FailedToBuildQuery,
|
||||
FailedToFindObject,
|
||||
FailedToInsertObject,
|
||||
Failed
|
||||
};
|
||||
|
||||
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
|
||||
|
|
@ -89,12 +89,11 @@ public:
|
|||
|
||||
[[nodiscard]] bool contains(const std::type_index &index) const;
|
||||
|
||||
void initialize(sql::executor &exec) const;
|
||||
|
||||
protected:
|
||||
template<typename Type>
|
||||
friend class schema_observer;
|
||||
friend class producer_creator;
|
||||
friend class producer_accessor;
|
||||
|
||||
object::repository repo_;
|
||||
std::unordered_map<std::type_index, schema_node> schema_nodes_;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
namespace matador::query {
|
||||
enum class error_code {
|
||||
Success,
|
||||
Ok,
|
||||
Failure,
|
||||
InvalidQuery,
|
||||
InvalidSchema,
|
||||
|
|
@ -14,7 +14,17 @@ enum class error_code {
|
|||
InvalidColumn,
|
||||
InvalidConstraint,
|
||||
InvalidDataType,
|
||||
InvalidValue
|
||||
InvalidValue,
|
||||
InvalidRelationType,
|
||||
MissingPrimaryKey,
|
||||
UnknownType,
|
||||
FailedToAcquireConnection,
|
||||
FailedToFindPrimaryKey,
|
||||
FailedToBuildQuery,
|
||||
FailedToFindObject,
|
||||
FailedToInsertObject,
|
||||
FailedToAcquirePool,
|
||||
UnexpectedError
|
||||
};
|
||||
|
||||
class query_category_impl final : public std::error_category
|
||||
|
|
|
|||
|
|
@ -77,9 +77,9 @@ public:
|
|||
, contexts_by_type_{contexts_by_type}
|
||||
{}
|
||||
|
||||
utils::result<std::vector<insert_step>, query_build_error> build(const object::object_ptr<ObjectType> &ptr) {
|
||||
utils::result<std::vector<insert_step>, utils::error> build(const object::object_ptr<ObjectType> &ptr) {
|
||||
if (const auto it = schema_.find(typeid(ObjectType)); it == schema_.end()) {
|
||||
return utils::failure(query_build_error::UnknownType);
|
||||
return utils::failure(utils::error{error_code::UnknownType, "Unknown type for insert query"});
|
||||
}
|
||||
|
||||
steps_.clear();
|
||||
|
|
@ -148,18 +148,18 @@ public:
|
|||
|
||||
const auto it = schema_.find(std::string{id});
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception(query_build_error::UnknownType);
|
||||
throw query_builder_exception(error_code::UnknownType, "Unknown type");
|
||||
}
|
||||
|
||||
using relation_value_type = object::many_to_many_relation<ForeignType, ObjectType>;
|
||||
|
||||
if (std::type_index(typeid(relation_value_type)) != it->second.node().info().type_index()) {
|
||||
throw query_builder_exception(query_build_error::InvalidRelationType);
|
||||
throw query_builder_exception(error_code::InvalidRelationType, "Invalid relation type");
|
||||
}
|
||||
|
||||
const auto cit = contexts_by_type_.find(it->second.node().info().type_index());
|
||||
if (cit == contexts_by_type_.end()) {
|
||||
throw query_builder_exception(query_build_error::UnknownType);
|
||||
throw query_builder_exception(error_code::UnknownType, "Unknown type");
|
||||
}
|
||||
|
||||
const auto rel_it = processing_many_to_many_relations_.insert(id);
|
||||
|
|
@ -205,18 +205,18 @@ public:
|
|||
|
||||
const auto it = schema_.find(std::string{id});
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception(query_build_error::UnknownType);
|
||||
throw query_builder_exception(error_code::UnknownType, "Unknown type");
|
||||
}
|
||||
|
||||
using relation_value_type = object::many_to_many_relation<ForeignType, ObjectType>;
|
||||
|
||||
if (std::type_index(typeid(relation_value_type)) != it->second.node().info().type_index()) {
|
||||
throw query_builder_exception(query_build_error::InvalidRelationType);
|
||||
throw query_builder_exception(error_code::InvalidRelationType, "Invalid relation type");
|
||||
}
|
||||
|
||||
const auto cit = contexts_by_type_.find(it->second.node().info().type_index());
|
||||
if (cit == contexts_by_type_.end()) {
|
||||
throw query_builder_exception(query_build_error::UnknownType);
|
||||
throw query_builder_exception(error_code::UnknownType, "Unknown type");
|
||||
}
|
||||
|
||||
const auto rel_it = processing_many_to_many_relations_.insert(id);
|
||||
|
|
@ -276,7 +276,7 @@ private:
|
|||
|
||||
const auto it = schema_.find(typeid(EntityType));
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception(query_build_error::UnknownType);
|
||||
throw query_builder_exception(error_code::UnknownType, "Unknown type");
|
||||
}
|
||||
|
||||
// 1) Traverse relations first => dependencies will be inserted before this object
|
||||
|
|
@ -291,7 +291,7 @@ private:
|
|||
|
||||
const auto cit = contexts_by_type_.find(it->second.node().info().type_index());
|
||||
if (cit == contexts_by_type_.end()) {
|
||||
throw query_builder_exception(query_build_error::UnknownType);
|
||||
throw query_builder_exception(error_code::UnknownType, "Unknown type");
|
||||
}
|
||||
step.ctx = cit->second.insert;
|
||||
step.bind_object = [ptr](sql::statement &stmt) { stmt.bind(*ptr); };
|
||||
|
|
|
|||
|
|
@ -3,29 +3,19 @@
|
|||
|
||||
#include "matador/utils/error.hpp"
|
||||
|
||||
#include "matador/query/error_code.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
enum class query_build_error : std::uint8_t {
|
||||
Ok = 0,
|
||||
UnknownType,
|
||||
MissingPrimaryKey,
|
||||
MissingObject,
|
||||
UnexpectedError,
|
||||
QueryError,
|
||||
InvalidRelationType
|
||||
};
|
||||
|
||||
class query_builder_exception final : public std::exception {
|
||||
public:
|
||||
explicit query_builder_exception(query_build_error error, utils::error &&err = {});
|
||||
explicit query_builder_exception(error_code error, std::string &&msg);
|
||||
|
||||
[[nodiscard]] query_build_error error_type() const;
|
||||
[[nodiscard]] const utils::error &error() const;
|
||||
|
||||
private:
|
||||
const query_build_error error_type_;
|
||||
utils::error error_;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -30,21 +30,22 @@ public:
|
|||
, pk_name_(std::move(pk_name))
|
||||
{}
|
||||
|
||||
std::shared_ptr<object::abstract_collection_resolver> produce(sql::executor &exec) override {
|
||||
utils::result<sql::query_context, utils::error> build_query(const sql::dialect& d) override {
|
||||
const auto *pk_column = table_[pk_name_];
|
||||
const auto *join_column = table_[collection_name()];
|
||||
const auto object_resolver = exec.resolver()->object_resolver<typename Type::value_type>();
|
||||
|
||||
auto stmt = query::select({*pk_column})
|
||||
const auto stmt = select({*pk_column})
|
||||
.from(table_)
|
||||
.where(*join_column == utils::_)
|
||||
.prepare(exec);
|
||||
.compile(d);
|
||||
|
||||
if (!stmt) {
|
||||
return {};
|
||||
}
|
||||
return utils::ok(stmt);
|
||||
}
|
||||
|
||||
return std::make_shared<query_collection_resolver<Type>>(stmt.release(), root_type(), collection_name(), object_resolver);
|
||||
std::shared_ptr<object::abstract_collection_resolver> produce(sql::statement&& stmt, const sql::resolver_service& rs) override {
|
||||
const auto object_resolver = rs.object_resolver<typename Type::value_type>();
|
||||
|
||||
return std::make_shared<query_collection_resolver<Type>>(std::move(stmt), root_type(), collection_name(), object_resolver);
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
@ -76,10 +77,10 @@ public:
|
|||
void on_has_many(const char * /*id*/, CollectionType &/*cont*/, const char *join_column, const utils::foreign_attributes &/*attr*/, std::enable_if_t<object::is_object_ptr<typename CollectionType::value_type>::value> * = nullptr) {
|
||||
const auto it = schema_.find(typeid(typename CollectionType::value_type::value_type));
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
}
|
||||
if (!it->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
throw query_builder_exception{error_code::MissingPrimaryKey, "Missing primary key"};
|
||||
}
|
||||
|
||||
auto producer = std::make_unique<query_collection_resolver_producer<typename CollectionType::value_type>>(
|
||||
|
|
@ -101,7 +102,7 @@ public:
|
|||
void on_has_many_to_many(const char *id, CollectionType &, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) {
|
||||
const auto it = schema_.find(id);
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
}
|
||||
|
||||
auto producer = std::make_unique<query_collection_resolver_producer<typename CollectionType::value_type>>(
|
||||
|
|
@ -118,7 +119,7 @@ public:
|
|||
void on_has_many_to_many(const char *id, CollectionType &, const utils::foreign_attributes &/*attr*/) {
|
||||
const auto it = schema_.find(id);
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
}
|
||||
|
||||
object::join_columns_collector collector;
|
||||
|
|
@ -151,28 +152,23 @@ public:
|
|||
, table_(tab)
|
||||
, pk_name_(std::move(pk_name)) {}
|
||||
|
||||
std::shared_ptr<object::abstract_type_resolver> produce(sql::executor &exec) override {
|
||||
std::shared_ptr<object::abstract_type_resolver> produce(sql::statement&& stmt) override {
|
||||
return std::make_shared<query_object_resolver<Type>>(std::move(stmt));
|
||||
}
|
||||
|
||||
utils::result<sql::query_context, utils::error> build_query(const sql::dialect& d) override {
|
||||
producer_creator pc(repo_, typeid(Type));
|
||||
Type obj;
|
||||
access::process(pc, obj);
|
||||
|
||||
select_query_builder qb(repo_);
|
||||
const auto *pk_column = table_[pk_name_];
|
||||
auto builder_result = qb.build<Type>(*pk_column == utils::_);
|
||||
|
||||
if (!builder_result) {
|
||||
return nullptr;
|
||||
const auto result = qb.build<Type>(*pk_column == utils::_);
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
auto stmt = builder_result
|
||||
.release()
|
||||
.prepare(exec);
|
||||
|
||||
if (!stmt) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::make_shared<query_object_resolver<Type>>(stmt.release());
|
||||
return utils::ok(result->compile(d));
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "matador/query/query_builder_exception.hpp"
|
||||
|
||||
#include "matador/query/criteria.hpp"
|
||||
#include "matador/query/error_code.hpp"
|
||||
#include "matador/query/query.hpp"
|
||||
#include "matador/query/criteria/criteria_visitor.hpp"
|
||||
|
||||
|
|
@ -13,6 +14,7 @@
|
|||
#include "matador/object/repository.hpp"
|
||||
|
||||
#include "matador/utils/primary_key_attribute.hpp"
|
||||
#include "matador/utils/error.hpp"
|
||||
#include "matador/utils/result.hpp"
|
||||
#include "matador/utils/value.hpp"
|
||||
|
||||
|
|
@ -56,10 +58,10 @@ public:
|
|||
: schema_(scm){}
|
||||
|
||||
template<class EntityType>
|
||||
utils::result<fetchable_query, query_build_error> build(criteria_ptr clause = {}) {
|
||||
utils::result<fetchable_query, utils::error> build(criteria_ptr clause = {}) {
|
||||
const auto it = schema_.find(typeid(EntityType));
|
||||
if (it == schema_.end()) {
|
||||
return utils::failure(query_build_error::UnknownType);
|
||||
return utils::failure{utils::error(error_code::UnknownType, "Entity type not found in schema")};
|
||||
}
|
||||
table_info_stack_.push({it->second.node().info(), it->second.table().as(build_alias('t', ++table_index))});
|
||||
entity_query_data_ = { &table_info_stack_.top().table };
|
||||
|
|
@ -83,9 +85,9 @@ public:
|
|||
|
||||
return {utils::ok(std::move(q))};
|
||||
} catch (const query_builder_exception &ex) {
|
||||
return {utils::failure(ex.error_type())};
|
||||
return {utils::failure(ex.error())};
|
||||
} catch (...) {
|
||||
return {utils::failure(query_build_error::UnexpectedError)};
|
||||
return utils::failure{utils::error(error_code::UnexpectedError, "Unexpected error")};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -132,7 +134,7 @@ public:
|
|||
|
||||
const auto it = schema_.find(typeid(typename CollectionType::value_type::value_type));
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
}
|
||||
|
||||
auto next = processed_tables_.find(it->second.name());
|
||||
|
|
@ -148,7 +150,7 @@ public:
|
|||
table_info_stack_.pop();
|
||||
|
||||
if (!it->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
throw query_builder_exception{error_code::MissingPrimaryKey, "Missing primary key"};
|
||||
}
|
||||
|
||||
append_join(
|
||||
|
|
@ -168,7 +170,7 @@ public:
|
|||
}
|
||||
const auto result = schema_.find(typeid(typename ContainerType::value_type::value_type));
|
||||
if (result == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
}
|
||||
|
||||
auto next = processed_tables_.find(result->second.name());
|
||||
|
|
@ -181,7 +183,7 @@ public:
|
|||
if (relation == processed_tables_.end()) {
|
||||
const auto it = schema_.find(id);
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
}
|
||||
relation = processed_tables_.emplace(id, it->second.table().as(build_alias('t', ++table_index))).first;
|
||||
}
|
||||
|
|
@ -192,7 +194,7 @@ public:
|
|||
table_info_stack_.pop();
|
||||
|
||||
if (!result->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
throw query_builder_exception{error_code::MissingPrimaryKey, "Missing primary key"};
|
||||
}
|
||||
|
||||
append_join(
|
||||
|
|
@ -212,7 +214,7 @@ public:
|
|||
}
|
||||
const auto result = schema_.find(typeid(typename ContainerType::value_type::value_type));
|
||||
if (result == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
}
|
||||
|
||||
auto next = processed_tables_.find(result->second.name());
|
||||
|
|
@ -225,7 +227,7 @@ public:
|
|||
if (relation == processed_tables_.end()) {
|
||||
const auto it = schema_.find(id);
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
}
|
||||
const auto t = it->second.table().as(build_alias('t', ++table_index));
|
||||
relation = processed_tables_.insert({id, t}).first;
|
||||
|
|
@ -237,7 +239,7 @@ public:
|
|||
table_info_stack_.pop();
|
||||
|
||||
if (!result->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
throw query_builder_exception{error_code::MissingPrimaryKey, "Missing primary key"};
|
||||
}
|
||||
|
||||
const auto join_columns = join_columns_collector_.collect<typename ContainerType::value_type::value_type>();
|
||||
|
|
@ -281,10 +283,10 @@ template<class Pointer>
|
|||
void select_query_builder::on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr) {
|
||||
const auto it = schema_.find(typeid(typename Pointer::value_type));
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
}
|
||||
if (!it->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
throw query_builder_exception{error_code::MissingPrimaryKey, "Missing primary key"};
|
||||
}
|
||||
|
||||
const auto& info = it->second.node().info();
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
#ifndef QUERY_SESSION_HPP
|
||||
#define QUERY_SESSION_HPP
|
||||
|
||||
#include "matador/orm/error_code.hpp"
|
||||
|
||||
#include "matador/query/error_code.hpp"
|
||||
#include "matador/query/select_query_builder.hpp"
|
||||
#include "matador/query/criteria.hpp"
|
||||
#include "matador/query/insert_query_builder.hpp"
|
||||
|
|
@ -24,7 +23,7 @@
|
|||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
namespace matador::orm {
|
||||
namespace matador::query {
|
||||
utils::error make_error(error_code ec, const std::string &msg);
|
||||
|
||||
struct session_context {
|
||||
|
|
@ -41,7 +40,7 @@ struct session_context {
|
|||
std::shared_ptr<sql::resolver_service> resolver_service = std::make_shared<sql::resolver_service>();
|
||||
};
|
||||
|
||||
class session final : public sql::executor {
|
||||
class session final /*: public sql::executor*/ {
|
||||
public:
|
||||
session(session_context &&ctx, const query::schema &scm);
|
||||
|
||||
|
|
@ -65,12 +64,12 @@ public:
|
|||
template<typename Type>
|
||||
utils::result<sql::query_result<Type>, utils::error> find(query::criteria_ptr clause = {});
|
||||
|
||||
[[nodiscard]] utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::query_context &ctx) const override;
|
||||
[[nodiscard]] utils::result<sql::execute_result, utils::error> execute(const sql::query_context &ctx) const override;
|
||||
[[nodiscard]] utils::result<sql::statement, utils::error> prepare(const sql::query_context &ctx) override;
|
||||
[[nodiscard]] std::string str(const sql::query_context &ctx) const override;
|
||||
[[nodiscard]] const sql::dialect &dialect() const override;
|
||||
[[nodiscard]] std::shared_ptr<sql::resolver_service> resolver() const override;
|
||||
// [[nodiscard]] utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::query_context &ctx) const override;
|
||||
// [[nodiscard]] utils::result<sql::execute_result, utils::error> execute(const sql::query_context &ctx) const override;
|
||||
// [[nodiscard]] utils::result<sql::statement, utils::error> prepare(const sql::query_context &ctx) override;
|
||||
// [[nodiscard]] std::string str(const sql::query_context &ctx) const override;
|
||||
// [[nodiscard]] const sql::dialect &dialect() const override;
|
||||
// [[nodiscard]] std::shared_ptr<sql::resolver_service> resolver() const override;
|
||||
[[nodiscard]] const query::basic_schema &schema() const;
|
||||
|
||||
private:
|
||||
|
|
@ -105,7 +104,11 @@ utils::result<object::object_ptr<Type>, utils::error> session::insert(object::ob
|
|||
// Execute all steps; for Identity steps read RETURNING and write pk back into the object
|
||||
for (query::insert_step &step : *steps) {
|
||||
if (step.pk_generator == utils::generator_type::Sequence || step.pk_generator == utils::generator_type::Table) {
|
||||
auto result = it->second.pk_generator().next_id(*this);
|
||||
const auto conn = pool_.acquire();
|
||||
if (!conn.valid()) {
|
||||
return utils::failure(make_error(error_code::FailedToAcquirePool, "Failed to acquire connection pool for primary key generation."));
|
||||
}
|
||||
auto result = it->second.pk_generator().next_id(*conn);
|
||||
if (!result.is_ok()) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
|
@ -205,17 +208,17 @@ utils::result<object::object_ptr<Type>, utils::error> session::update(const obje
|
|||
using namespace matador::utils;
|
||||
using namespace matador::query;
|
||||
|
||||
const auto col = table_column(it->second.node().info().primary_key_attribute()->name());
|
||||
auto res = query::update(it->second.name())
|
||||
.set<Type>()
|
||||
.where(col == _)
|
||||
.prepare(*this);
|
||||
if (!res) {
|
||||
return utils::failure(res.err());
|
||||
const auto cit = contexts_by_type_.find(it->second.node().info().type_index());
|
||||
if (cit == contexts_by_type_.end()) {
|
||||
return failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||
}
|
||||
auto stmt = cache_.acquire(cit->second.update_one);
|
||||
if (!stmt.is_ok()) {
|
||||
return failure(stmt.err());
|
||||
}
|
||||
|
||||
res->bind(*obj);
|
||||
pk_object_binder binder(res.value(), res->bind_pos());
|
||||
stmt->bind(*obj);
|
||||
pk_object_binder binder(stmt.value(), stmt->bind_pos());
|
||||
if (const auto update_result = binder.bind(*obj).execute(); !update_result.is_ok()) {
|
||||
return utils::failure(update_result.err());
|
||||
}
|
||||
|
|
@ -232,15 +235,16 @@ utils::result<void, utils::error> session::remove(const object::object_ptr<Type>
|
|||
using namespace matador::query;
|
||||
|
||||
const auto col = table_column(it->second.node().info().primary_key_attribute()->name());
|
||||
auto res = query::remove()
|
||||
.from(it->second.name())
|
||||
.where(col == _)
|
||||
.prepare(*this);
|
||||
if (!res) {
|
||||
return utils::failure(res.err());
|
||||
const auto cit = contexts_by_type_.find(it->second.node().info().type_index());
|
||||
if (cit == contexts_by_type_.end()) {
|
||||
return failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||
}
|
||||
auto stmt = cache_.acquire(cit->second.delete_one);
|
||||
if (!stmt.is_ok()) {
|
||||
return failure(stmt.err());
|
||||
}
|
||||
|
||||
pk_object_binder binder(res.value(), res->bind_pos());
|
||||
pk_object_binder binder(*stmt, stmt->bind_pos());
|
||||
if (const auto update_result = binder.bind(*obj).execute(); !update_result.is_ok()) {
|
||||
return utils::failure(update_result.err());
|
||||
}
|
||||
|
|
@ -255,24 +259,19 @@ utils::result<object::object_ptr<Type>, utils::error> session::find(const Primar
|
|||
}
|
||||
const auto &info = it->second.node().info();
|
||||
if (!info.has_primary_key()) {
|
||||
return utils::failure(make_error(error_code::NoPrimaryKey, "Type hasn't primary key."));
|
||||
return utils::failure(make_error(error_code::FailedToFindPrimaryKey, "Type hasn't primary key."));
|
||||
}
|
||||
|
||||
query::select_query_builder eqb(schema_);
|
||||
const query::table_column c(&it->second.table(), info.primary_key_attribute()->name());
|
||||
using namespace matador::query;
|
||||
auto data = eqb.build<Type>(c == utils::_);
|
||||
if (!data.is_ok()) {
|
||||
return utils::failure(make_error(error_code::FailedToBuildQuery,
|
||||
"Failed to build query for type " + info.name() + "."));
|
||||
const auto cit = contexts_by_type_.find(it->second.node().info().type_index());
|
||||
if (cit == contexts_by_type_.end()) {
|
||||
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||
}
|
||||
auto stmt = cache_.acquire(cit->second.select_one);
|
||||
if (!stmt.is_ok()) {
|
||||
return utils::failure(stmt.err());
|
||||
}
|
||||
|
||||
auto res = data->prepare(*this);
|
||||
|
||||
if (!res) {
|
||||
return utils::failure(res.err());
|
||||
}
|
||||
auto stmt_result = res->bind(0, const_cast<PrimaryKeyType &>(pk))
|
||||
auto stmt_result = stmt->bind(0, const_cast<PrimaryKeyType &>(pk))
|
||||
.template fetch_one<Type>();
|
||||
if (!stmt_result) {
|
||||
return utils::failure(make_error(error_code::FailedToFindObject,
|
||||
|
|
@ -296,12 +295,13 @@ utils::result<sql::query_result<Type>, utils::error> session::find(query::criter
|
|||
"Failed to build query for type " + it->second.name() + "."));
|
||||
}
|
||||
|
||||
auto result = data->prepare(*this);
|
||||
if (!result.is_ok()) {
|
||||
return utils::failure(result.err());
|
||||
auto ctx = data->compile(dialect_);
|
||||
auto stmt = cache_.acquire(ctx);
|
||||
if (!stmt.is_ok()) {
|
||||
return utils::failure(stmt.err());
|
||||
}
|
||||
|
||||
return result->template fetch<Type>();
|
||||
return stmt->template fetch<Type>();
|
||||
}
|
||||
}
|
||||
#endif //QUERY_SESSION_HPP
|
||||
|
|
@ -3,14 +3,22 @@
|
|||
|
||||
#include "matador/object/abstract_collection_resolver.hpp"
|
||||
|
||||
#include "matador/sql/query_context.hpp"
|
||||
|
||||
#include "matador/utils/error.hpp"
|
||||
#include "matador/utils/result.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace matador::sql {
|
||||
class executor;
|
||||
class dialect;
|
||||
class statement;
|
||||
class resolver_service;
|
||||
class collection_resolver_producer {
|
||||
public:
|
||||
virtual ~collection_resolver_producer() = default;
|
||||
virtual std::shared_ptr<object::abstract_collection_resolver> produce(executor &exec) = 0;
|
||||
virtual utils::result<query_context, utils::error> build_query(const dialect& d) = 0;
|
||||
virtual std::shared_ptr<object::abstract_collection_resolver> produce(statement&& stmt, const resolver_service& rs) = 0;
|
||||
|
||||
[[nodiscard]] const std::type_index& root_type() const;
|
||||
[[nodiscard]] const std::type_index& type() const;
|
||||
|
|
|
|||
|
|
@ -3,14 +3,21 @@
|
|||
|
||||
#include "matador/object/abstract_type_resolver.hpp"
|
||||
|
||||
#include "matador/sql/query_context.hpp"
|
||||
|
||||
#include "matador/utils/error.hpp"
|
||||
#include "matador/utils/result.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace matador::sql {
|
||||
class executor;
|
||||
class dialect;
|
||||
class statement;
|
||||
class object_resolver_producer {
|
||||
public:
|
||||
virtual ~object_resolver_producer() = default;
|
||||
virtual std::shared_ptr<object::abstract_type_resolver> produce(executor &exec) = 0;
|
||||
virtual utils::result<query_context, utils::error> build_query(const dialect& d) = 0;
|
||||
virtual std::shared_ptr<object::abstract_type_resolver> produce(statement&& stmt) = 0;
|
||||
|
||||
[[nodiscard]] const std::type_index& type() const;
|
||||
|
||||
|
|
|
|||
|
|
@ -29,12 +29,6 @@ enum class sql_command {
|
|||
AlterSchema
|
||||
};
|
||||
|
||||
enum class return_mode {
|
||||
None,
|
||||
GeneratedKeys,
|
||||
Rows
|
||||
};
|
||||
|
||||
struct query_context {
|
||||
std::string sql;
|
||||
size_t sql_hash{};
|
||||
|
|
@ -48,14 +42,10 @@ struct query_context {
|
|||
// Data for resolving query result
|
||||
std::shared_ptr<resolver_service> resolver{};
|
||||
std::type_index result_type = typeid(void);
|
||||
return_mode mode = return_mode::None;
|
||||
|
||||
[[nodiscard]] bool is_ddl() const { return command == sql_command::Create || command == sql_command::Alter; }
|
||||
[[nodiscard]] bool is_dml() const { return command == sql_command::Insert || command == sql_command::Update || command == sql_command::Delete; }
|
||||
[[nodiscard]] bool is_query() const { return command == sql_command::Select; }
|
||||
|
||||
[[nodiscard]] bool expects_rows() const { return mode == return_mode::Rows; }
|
||||
[[nodiscard]] bool expects_generated_keys() const { return mode == return_mode::GeneratedKeys; }
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
add_library(matador-orm STATIC
|
||||
../../include/matador/orm/error_code.hpp
|
||||
../../include/matador/orm/session.hpp
|
||||
../../include/matador/query/session.hpp
|
||||
../../include/matador/query/abstract_pk_generator.hpp
|
||||
../../include/matador/query/attribute_string_writer.hpp
|
||||
../../include/matador/query/basic_schema.hpp
|
||||
|
|
@ -120,8 +119,7 @@ add_library(matador-orm STATIC
|
|||
../../include/matador/sql/sql_functions.hpp
|
||||
../../include/matador/sql/statement.hpp
|
||||
../../include/matador/sql/statement_cache.hpp
|
||||
orm/error_code.cpp
|
||||
orm/session.cpp
|
||||
query/session.cpp
|
||||
query/abstract_pk_generator.cpp
|
||||
query/attribute_string_writer.cpp
|
||||
query/basic_schema.cpp
|
||||
|
|
|
|||
|
|
@ -1,45 +0,0 @@
|
|||
#include "matador/orm/error_code.hpp"
|
||||
|
||||
namespace matador::orm {
|
||||
|
||||
const char * orm_category_impl::name() const noexcept {
|
||||
return "orm";
|
||||
}
|
||||
|
||||
std::string orm_category_impl::message(const int ev) const {
|
||||
switch (static_cast<error_code>(ev)) {
|
||||
case error_code::Ok:
|
||||
return "OK";
|
||||
case error_code::NoConnectionAvailable:
|
||||
return "No connection available";
|
||||
case error_code::UnknownType:
|
||||
return "Unknown type";
|
||||
case error_code::NoPrimaryKey:
|
||||
return "No primary key";
|
||||
case error_code::FailedToBuildQuery:
|
||||
return "Failed to build query";
|
||||
case error_code::FailedToFindObject:
|
||||
return "Failed to find object";
|
||||
case error_code::FailedToInsertObject:
|
||||
return "Failed to insert object";
|
||||
case error_code::Failed:
|
||||
return "Failed";
|
||||
default:
|
||||
return "Unknown error";
|
||||
}
|
||||
}
|
||||
|
||||
const std::error_category & orm_category() {
|
||||
static orm_category_impl instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
std::error_code make_error_code(error_code e) {
|
||||
return {static_cast<int>(e), orm_category()};
|
||||
}
|
||||
|
||||
std::error_condition make_error_condition(error_code e) {
|
||||
return {static_cast<int>(e), orm_category()};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,98 +0,0 @@
|
|||
#include "matador/orm/session.hpp"
|
||||
|
||||
#include "matador/sql/backend_provider.hpp"
|
||||
#include "matador/sql/dialect.hpp"
|
||||
|
||||
#include "matador/query/query.hpp"
|
||||
#include "matador/query/generator.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(session_context&& ctx, const query::schema &scm)
|
||||
: pool_(ctx.dns, ctx.connection_count, [ctx](const sql::connection_info& info) { return sql::connection(info, ctx.resolver_service); })
|
||||
, cache_(ctx.bus, pool_, ctx.cache_size)
|
||||
, dialect_(sql::backend_provider::instance().connection_dialect(pool_.info().type))
|
||||
, schema_(scm)
|
||||
, resolver_service_(ctx.resolver_service) {
|
||||
using namespace matador::utils;
|
||||
for (const auto &[type, node] : schema_) {
|
||||
query::query_contexts queries;
|
||||
|
||||
// SELECT all
|
||||
queries.select_all = query::select(node.table())
|
||||
.from(node.name())
|
||||
.compile(dialect_);
|
||||
if (node.table().has_primary_key()) {
|
||||
// SELECT one
|
||||
queries.select_one = query::select(node.table())
|
||||
.from(node.name())
|
||||
.where(*node.table().primary_key_column().value() == _)
|
||||
.compile(dialect_);
|
||||
// UPDATE one
|
||||
auto update_set = query::update(node.table());
|
||||
for (const auto &col: node.table().columns()) {
|
||||
update_set.set(col, _);
|
||||
}
|
||||
queries.update_one = update_set.where(*node.table().primary_key_column().value() == _)
|
||||
.compile(dialect_);
|
||||
// DELETE one
|
||||
queries.delete_one = query::remove()
|
||||
.from(node.name())
|
||||
.where(*node.table().primary_key_column().value() == _)
|
||||
.compile(dialect_);
|
||||
}
|
||||
// INSERT one
|
||||
queries.insert = query::insert()
|
||||
.into(node.name(), node.table())
|
||||
.values(query::generator::placeholders(node.table().columns().size()))
|
||||
.compile(dialect_);
|
||||
|
||||
contexts_by_type_[node.node().type_index()] = queries;
|
||||
}
|
||||
}
|
||||
|
||||
const class sql::dialect &session::dialect() const {
|
||||
return dialect_;
|
||||
}
|
||||
|
||||
std::shared_ptr<sql::resolver_service> session::resolver() const {
|
||||
return resolver_service_;
|
||||
}
|
||||
|
||||
const query::basic_schema & session::schema() const {
|
||||
return schema_;
|
||||
}
|
||||
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> session::fetch(const sql::query_context& ctx) const {
|
||||
if (const auto result = cache_.acquire(ctx); !result) {
|
||||
return utils::failure(result.err());
|
||||
} else if (auto fetch_result = result->fetch_internal(); !fetch_result) {
|
||||
return utils::failure(fetch_result.err());
|
||||
} else {
|
||||
return fetch_result;
|
||||
}
|
||||
}
|
||||
|
||||
utils::result<sql::execute_result, utils::error> session::execute(const sql::query_context& ctx) const {
|
||||
if (const auto result = cache_.acquire(ctx); !result) {
|
||||
return utils::failure(result.err());
|
||||
} else if (auto exec_result = result->execute(); !exec_result) {
|
||||
return utils::failure(exec_result.err());
|
||||
} else {
|
||||
return exec_result;
|
||||
}
|
||||
}
|
||||
|
||||
utils::result<sql::statement, utils::error> session::prepare(const sql::query_context& ctx) {
|
||||
return cache_.acquire(ctx);
|
||||
}
|
||||
|
||||
std::string session::str(const sql::query_context& ctx) const {
|
||||
return ctx.sql;
|
||||
}
|
||||
}
|
||||
|
|
@ -85,19 +85,4 @@ basic_schema::const_iterator basic_schema::find(const std::string &name) const {
|
|||
bool basic_schema::contains(const std::type_index &index) const {
|
||||
return schema_nodes_.count(index) == 1;
|
||||
}
|
||||
|
||||
void basic_schema::initialize(sql::executor &exec) const {
|
||||
auto factory = std::make_shared<sql::producer_resolver_factory>();
|
||||
for (const auto &[key, producer] : resolver_producers_) {
|
||||
auto resolver = producer->produce(exec);
|
||||
exec.resolver()->register_object_resolver(std::move(resolver));
|
||||
}
|
||||
|
||||
auto collection_factory = std::make_shared<sql::producer_collection_resolver_factory>();
|
||||
for (const auto &[key, producer] : collection_resolver_producers_) {
|
||||
auto resolver = producer->produce(exec);
|
||||
exec.resolver()->register_collection_resolver(std::move(resolver));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@ const char* query_category_impl::name() const noexcept {
|
|||
|
||||
std::string query_category_impl::message(int ev) const {
|
||||
switch (static_cast<error_code>(ev)) {
|
||||
case error_code::Success:
|
||||
case error_code::Ok:
|
||||
return "Success";
|
||||
case error_code::Failure:
|
||||
return "Failure";
|
||||
|
|
@ -25,6 +25,26 @@ std::string query_category_impl::message(int ev) const {
|
|||
return "Invalid data type";
|
||||
case error_code::InvalidValue:
|
||||
return "Invalid value";
|
||||
case error_code::InvalidRelationType:
|
||||
return "Invalid relation type";
|
||||
case error_code::MissingPrimaryKey:
|
||||
return "Missing primary key";
|
||||
case error_code::UnknownType:
|
||||
return "Unknown type";
|
||||
case error_code::FailedToAcquireConnection:
|
||||
return "Failed to acquire connection";
|
||||
case error_code::FailedToFindPrimaryKey:
|
||||
return "Failed to find primary key";
|
||||
case error_code::FailedToBuildQuery:
|
||||
return "Failed to build query";
|
||||
case error_code::FailedToFindObject:
|
||||
return "Failed to find object";
|
||||
case error_code::FailedToInsertObject:
|
||||
return "Failed to insert object";
|
||||
case error_code::FailedToAcquirePool:
|
||||
return "Failed to acquire pool";
|
||||
case error_code::UnexpectedError:
|
||||
return "Unexpected error";
|
||||
default:
|
||||
return "Unknown error";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -269,7 +269,6 @@ void query_builder::visit(internal::query_values_part& part) {
|
|||
}
|
||||
|
||||
void query_builder::visit(internal::query_returning_part& part) {
|
||||
query_.mode = sql::return_mode::Rows;
|
||||
query_.sql += " " + dialect_->returning() + " ";
|
||||
|
||||
build_fetchable_columns(query_, part.columns(), *dialect_);
|
||||
|
|
|
|||
|
|
@ -2,13 +2,9 @@
|
|||
|
||||
namespace matador::query {
|
||||
|
||||
query_builder_exception::query_builder_exception(const query_build_error error, utils::error&& err)
|
||||
: error_type_(error)
|
||||
, error_( std::move(err) ) {}
|
||||
|
||||
query_build_error query_builder_exception::error_type() const {
|
||||
return error_type_;
|
||||
}
|
||||
query_builder_exception::query_builder_exception(const error_code error, std::string&& msg)
|
||||
: error_(error, msg)
|
||||
{}
|
||||
|
||||
const utils::error& query_builder_exception::error() const {
|
||||
return error_;
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ void select_query_builder::on_revision(const char *id, uint64_t &/*rev*/) {
|
|||
void select_query_builder::push(const std::string &column_name) {
|
||||
const auto it = processed_tables_.find(table_info_stack_.top().info.name());
|
||||
if (it == processed_tables_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnexpectedError};
|
||||
throw query_builder_exception{error_code::UnexpectedError, "Unexpected error during column processing"};
|
||||
}
|
||||
entity_query_data_.columns.emplace_back(&it->second, column_name, build_alias('c', ++column_index));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,145 @@
|
|||
#include "matador/query/session.hpp"
|
||||
|
||||
#include "matador/sql/backend_provider.hpp"
|
||||
#include "matador/sql/dialect.hpp"
|
||||
|
||||
#include "matador/query/query.hpp"
|
||||
#include "matador/query/generator.hpp"
|
||||
#include "matador/query/basic_schema.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace matador::query {
|
||||
class producer_accessor {
|
||||
public:
|
||||
static const std::unordered_map<std::type_index, std::unique_ptr<sql::object_resolver_producer>>& resolver_producers(const basic_schema& scm) {
|
||||
return scm.resolver_producers_;
|
||||
}
|
||||
static const std::unordered_map<object::collection_composite_key, std::unique_ptr<sql::collection_resolver_producer>, object::collection_composite_key_hash>& collection_resolver_producers(const basic_schema& scm) {
|
||||
return scm.collection_resolver_producers_;
|
||||
}
|
||||
};
|
||||
|
||||
utils::error make_error(const error_code ec, const std::string &msg) {
|
||||
return utils::error(ec, msg);
|
||||
}
|
||||
|
||||
session::session(session_context&& ctx, const query::schema &scm)
|
||||
: pool_(ctx.dns, ctx.connection_count, [ctx](const sql::connection_info& info) { return sql::connection(info, ctx.resolver_service); })
|
||||
, cache_(ctx.bus, pool_, ctx.cache_size)
|
||||
, dialect_(sql::backend_provider::instance().connection_dialect(pool_.info().type))
|
||||
, schema_(scm)
|
||||
, resolver_service_(ctx.resolver_service) {
|
||||
using namespace matador::utils;
|
||||
for (const auto &[type, node] : schema_) {
|
||||
query::query_contexts queries;
|
||||
|
||||
// SELECT all
|
||||
queries.select_all = query::select(node.table())
|
||||
.from(node.name())
|
||||
.compile(dialect_);
|
||||
if (node.table().has_primary_key()) {
|
||||
// SELECT one
|
||||
queries.select_one = query::select(node.table())
|
||||
.from(node.name())
|
||||
.where(*node.table().primary_key_column().value() == _)
|
||||
.compile(dialect_);
|
||||
// UPDATE one
|
||||
auto update_set = query::update(node.table());
|
||||
for (const auto &col: node.table().columns()) {
|
||||
update_set.set(col, _);
|
||||
}
|
||||
queries.update_one = update_set.where(*node.table().primary_key_column().value() == _)
|
||||
.compile(dialect_);
|
||||
// DELETE one
|
||||
queries.delete_one = query::remove()
|
||||
.from(node.name())
|
||||
.where(*node.table().primary_key_column().value() == _)
|
||||
.compile(dialect_);
|
||||
}
|
||||
// INSERT one
|
||||
queries.insert = query::insert()
|
||||
.into(node.name(), node.table())
|
||||
.values(query::generator::placeholders(node.table().columns().size()))
|
||||
.compile(dialect_);
|
||||
|
||||
contexts_by_type_[node.node().type_index()] = queries;
|
||||
}
|
||||
|
||||
auto factory = std::make_shared<sql::producer_resolver_factory>();
|
||||
for (const auto &pair : producer_accessor::resolver_producers(schema_)) {
|
||||
auto res = pair.second->build_query(dialect_).and_then([this](sql::query_context&& query_ctx) -> result<sql::statement, error> {
|
||||
query_ctx.resolver = resolver_service_;
|
||||
return cache_.acquire(query_ctx);
|
||||
}).and_then([&pair, this](sql::statement&& stmt) -> result<void, error> {
|
||||
resolver_service_->register_object_resolver(pair.second->produce(std::move(stmt)));
|
||||
|
||||
return ok<void>();
|
||||
}).or_else([](const auto &err) {
|
||||
return failure(err);
|
||||
});
|
||||
|
||||
if (!res) {
|
||||
throw std::runtime_error(res.err().message());
|
||||
}
|
||||
}
|
||||
|
||||
auto collection_factory = std::make_shared<sql::producer_collection_resolver_factory>();
|
||||
for (const auto &pair : producer_accessor::collection_resolver_producers(schema_)) {
|
||||
auto res = pair.second->build_query(dialect_).and_then([this](sql::query_context&& query_ctx) -> result<sql::statement, error> {
|
||||
query_ctx.resolver = resolver_service_;
|
||||
return cache_.acquire(query_ctx);
|
||||
}).and_then([&pair, this](sql::statement&& stmt) -> result<void, error> {
|
||||
resolver_service_->register_collection_resolver(pair.second->produce(std::move(stmt), *resolver_service_));
|
||||
|
||||
return ok<void>();
|
||||
}).or_else([](const auto &err) {
|
||||
return failure(err);
|
||||
});
|
||||
|
||||
if (!res) {
|
||||
throw std::runtime_error(res.err().message());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// const class sql::dialect &session::dialect() const {
|
||||
// return dialect_;
|
||||
// }
|
||||
//
|
||||
// std::shared_ptr<sql::resolver_service> session::resolver() const {
|
||||
// return resolver_service_;
|
||||
// }
|
||||
|
||||
const query::basic_schema & session::schema() const {
|
||||
return schema_;
|
||||
}
|
||||
|
||||
// utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> session::fetch(const sql::query_context& ctx) const {
|
||||
// if (const auto result = cache_.acquire(ctx); !result) {
|
||||
// return utils::failure(result.err());
|
||||
// } else if (auto fetch_result = result->fetch_internal(); !fetch_result) {
|
||||
// return utils::failure(fetch_result.err());
|
||||
// } else {
|
||||
// return fetch_result;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// utils::result<sql::execute_result, utils::error> session::execute(const sql::query_context& ctx) const {
|
||||
// if (const auto result = cache_.acquire(ctx); !result) {
|
||||
// return utils::failure(result.err());
|
||||
// } else if (auto exec_result = result->execute(); !exec_result) {
|
||||
// return utils::failure(exec_result.err());
|
||||
// } else {
|
||||
// return exec_result;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// utils::result<sql::statement, utils::error> session::prepare(const sql::query_context& ctx) {
|
||||
// return cache_.acquire(ctx);
|
||||
// }
|
||||
//
|
||||
// std::string session::str(const sql::query_context& ctx) const {
|
||||
// return ctx.sql;
|
||||
// }
|
||||
}
|
||||
|
|
@ -311,7 +311,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
|
|||
.and_then([this] {return repo.create(db); });
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
repo.initialize(db);
|
||||
// repo.initialize(db);
|
||||
|
||||
REQUIRE(db.exists(AIRPLANE.table_name()));
|
||||
REQUIRE(db.exists(FLIGHT.table_name()));
|
||||
|
|
@ -538,7 +538,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
|
|||
.and_then([this] {return repo.create(db); });
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
repo.initialize(db);
|
||||
// repo.initialize(db);
|
||||
|
||||
const std::vector shipments {
|
||||
make_object<shipment>(1, "4711"),
|
||||
|
|
@ -636,7 +636,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy has many relation", "
|
|||
} );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
repo.initialize(db);
|
||||
// repo.initialize(db);
|
||||
|
||||
const std::vector authors {
|
||||
make_object<author>(1, "Michael", "Crichton", "23.10.1942", 1975, true),
|
||||
|
|
@ -718,7 +718,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation",
|
|||
.and_then([this] {return repo.create(db); });
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
repo.initialize(db);
|
||||
// repo.initialize(db);
|
||||
|
||||
const std::vector deps {
|
||||
make_object<department>(1, "Human Resources"),
|
||||
|
|
@ -864,7 +864,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many to many rel
|
|||
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
repo.initialize(db);
|
||||
// repo.initialize(db);
|
||||
|
||||
REQUIRE(db.exists(RECIPE.table_name()));
|
||||
REQUIRE(db.exists(INGREDIENT.table_name()));
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
#ifndef MATADOR_SESSION_FIXTURE_HPP
|
||||
#define MATADOR_SESSION_FIXTURE_HPP
|
||||
|
||||
#include "matador/orm/session.hpp"
|
||||
|
||||
#include "matador/utils/message_bus.hpp"
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
#include "matador/query/schema.hpp"
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
class SessionFixture {
|
||||
|
|
|
|||
|
|
@ -4,10 +4,13 @@
|
|||
|
||||
#include "connection.hpp"
|
||||
|
||||
#include "matador/query/session.hpp"
|
||||
|
||||
#include "models/author.hpp"
|
||||
#include "models/book.hpp"
|
||||
|
||||
using namespace matador;
|
||||
using namespace matador::query;
|
||||
using namespace matador::object;
|
||||
using namespace matador::test;
|
||||
|
||||
|
|
@ -58,8 +61,7 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[
|
|||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
schema.initialize(ses);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
auto s_king = make_object<author>(1, "Steven", "King", "21.9.1947", 1956, false);
|
||||
|
||||
|
|
@ -79,8 +81,7 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with
|
|||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
schema.initialize(ses);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
auto s_king = make_object<author_identity>("Steven King");
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
#include "connection.hpp"
|
||||
|
||||
#include "matador/query/session.hpp"
|
||||
|
||||
#include "models/recipe.hpp"
|
||||
#include "models/model_metas.hpp"
|
||||
|
||||
|
|
@ -17,8 +19,7 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many to many relat
|
|||
.and_then( [this] { return schema.attach<ingredient>("ingredients"); } )
|
||||
.and_then([this] { return schema.create(db); } );
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
schema.initialize(ses);
|
||||
query::session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
std::vector ingredients {
|
||||
make_object<ingredient>(1, "Apple"),
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
#include "connection.hpp"
|
||||
|
||||
#include "matador/query/session.hpp"
|
||||
|
||||
#include "models/airplane.hpp"
|
||||
#include "models/author.hpp"
|
||||
#include "models/book.hpp"
|
||||
|
|
@ -14,6 +16,7 @@
|
|||
#include <iostream>
|
||||
|
||||
using namespace matador;
|
||||
using namespace matador::query;
|
||||
using namespace matador::object;
|
||||
using namespace matador::test;
|
||||
|
||||
|
|
@ -22,7 +25,7 @@ TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") {
|
|||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
auto plane = ses.insert(make_object<airplane>(1, "Boeing", "A380"));
|
||||
REQUIRE(plane.is_ok());
|
||||
|
||||
|
|
@ -39,7 +42,7 @@ TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") {
|
|||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(res.is_ok());
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
auto result = ses.insert(make_object<airplane>(1, "Boeing", "A380"));
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
|
|
@ -70,9 +73,7 @@ TEST_CASE_METHOD(SessionFixture, "Session delete test", "[session][delete]") {
|
|||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(res.is_ok());
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
schema.initialize(ses);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
auto result = ses.insert(make_object<airplane>(1, "Boeing", "A380"));
|
||||
REQUIRE(result.is_ok());
|
||||
|
|
@ -87,7 +88,7 @@ TEST_CASE_METHOD(SessionFixture, "Session delete test", "[session][delete]") {
|
|||
|
||||
result = ses.find<airplane>(1);
|
||||
REQUIRE(result.is_error());
|
||||
REQUIRE(result.err().ec() == orm::error_code::FailedToFindObject);
|
||||
REQUIRE(result.err().ec() == query::error_code::FailedToFindObject);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
|
||||
|
|
@ -96,7 +97,7 @@ TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]")
|
|||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
auto plane = ses.insert(make_object<airplane>(1, "Boeing", "A380"));
|
||||
REQUIRE(plane.is_ok());
|
||||
auto f = ses.insert(make_object<flight>(2, *plane, "sully"));
|
||||
|
|
@ -118,13 +119,13 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session
|
|||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
auto a380 = ses.insert(make_object<airplane>(1, "Boeing", "A380"));
|
||||
REQUIRE(a380.is_ok());
|
||||
|
||||
auto find_result = ses.find<airplane>(2);
|
||||
REQUIRE(!find_result.is_ok());
|
||||
REQUIRE((find_result.err().ec() == orm::error_code::FailedToFindObject));
|
||||
REQUIRE((find_result.err().ec() == query::error_code::FailedToFindObject));
|
||||
|
||||
find_result = ses.find<airplane>(1);
|
||||
|
||||
|
|
@ -138,7 +139,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][f
|
|||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
std::vector planes {
|
||||
make_object<airplane>(1, "Airbus", "A380"),
|
||||
|
|
@ -175,8 +176,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
|
|||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
schema.initialize(ses);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
std::vector authors {
|
||||
make_object<author>(1, "Michael", "Crichton", "23.10.1942", 1975, true),
|
||||
make_object<author>( 2, "Steven", "King", "21.9.1947", 1956, false)
|
||||
|
|
@ -230,7 +230,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
|
|||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
std::vector departments {
|
||||
make_object<department>(1, "Insurance"),
|
||||
|
|
@ -289,7 +289,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with many-to-m
|
|||
return schema.create(db);
|
||||
} );
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
std::vector ingredients {
|
||||
make_object<ingredient>(1, "Apple"),
|
||||
|
|
@ -325,7 +325,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with many-to-m
|
|||
return schema.create(db);
|
||||
} );
|
||||
|
||||
orm::session ses({bus, connection::dns, 4}, schema);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
std::vector ingredients {
|
||||
make_object<ingredient>(1, "Apple"),
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include "matador/sql/connection_info.hpp"
|
||||
#include "matador/sql/connection_pool.hpp"
|
||||
#include "matador/orm/session.hpp"
|
||||
#include "matador/query/session.hpp"
|
||||
|
||||
// #include "matador/sql/statement_cache.hpp"
|
||||
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ public:
|
|||
StatementTestFixture() {
|
||||
REQUIRE(repo.attach<airplane>("airplanes")
|
||||
.and_then([this] {return repo.create(db); }));
|
||||
repo.initialize(db);
|
||||
// repo.initialize(db);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ add_executable(OrmTests
|
|||
backend/test_result_reader.hpp
|
||||
backend/test_statement.cpp
|
||||
backend/test_statement.hpp
|
||||
orm/SessionQueryBuilderTest.cpp
|
||||
query/ColumnExpressionTest.cpp
|
||||
query/ColumnGeneratorTest.cpp
|
||||
query/CriteriaTests.cpp
|
||||
query/GeneratorTests.cpp
|
||||
|
|
@ -23,6 +23,7 @@ add_executable(OrmTests
|
|||
query/QueryFixture.hpp
|
||||
query/QueryTest.cpp
|
||||
query/SchemaTest.cpp
|
||||
query/SessionQueryBuilderTest.cpp
|
||||
query/TableColumnTest.cpp
|
||||
sql/ColumnTest.cpp
|
||||
sql/ConnectionPoolFixture.hpp
|
||||
|
|
@ -31,7 +32,6 @@ add_executable(OrmTests
|
|||
sql/StatementCacheTest.cpp
|
||||
utils/auto_reset_event.cpp
|
||||
utils/auto_reset_event.hpp
|
||||
query/ColumnExpressionTest.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(OrmTests matador-orm matador-core Catch2::Catch2WithMain)
|
||||
|
|
|
|||
Loading…
Reference in New Issue