implemented lazy loading for object_ptr and belongs_to
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
#ifndef MATADOR_BASIC_SCHEMA_HPP
|
||||
#define MATADOR_BASIC_SCHEMA_HPP
|
||||
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
#include "matador/sql/producer_resolver_factory.hpp"
|
||||
|
||||
#include "matador/query/table.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <typeindex>
|
||||
|
||||
namespace matador::query {
|
||||
class schema_node final {
|
||||
public:
|
||||
schema_node(class table tab, const object::repository_node& node);
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] const class table& table() const;
|
||||
[[nodiscard]] const object::repository_node& node() const;
|
||||
|
||||
private:
|
||||
class table table_;
|
||||
const object::repository_node& node_;
|
||||
};
|
||||
|
||||
class basic_schema {
|
||||
public:
|
||||
using schema_node_map = std::unordered_map<std::type_index, schema_node>;
|
||||
using iterator = std::unordered_map<std::type_index, schema_node>::iterator;
|
||||
using const_iterator = std::unordered_map<std::type_index, schema_node>::const_iterator;
|
||||
|
||||
basic_schema();
|
||||
explicit basic_schema(const std::string &name);
|
||||
|
||||
iterator begin();
|
||||
iterator end();
|
||||
[[nodiscard]] const_iterator begin() const;
|
||||
[[nodiscard]] const_iterator end() const;
|
||||
|
||||
[[nodiscard]] size_t size() const;
|
||||
[[nodiscard]] bool empty() const;
|
||||
|
||||
iterator find(const std::type_index& ti);
|
||||
iterator find(const std::string& name);
|
||||
|
||||
[[nodiscard]] const_iterator find(const std::type_index& ti) const;
|
||||
[[nodiscard]] const_iterator find(const std::string& name) const;
|
||||
|
||||
[[nodiscard]] bool contains(const std::type_index &index) const;
|
||||
|
||||
void initialize_executor(sql::executor &exec) const;
|
||||
|
||||
protected:
|
||||
template<typename Type>
|
||||
friend class schema_observer;
|
||||
|
||||
object::repository repo_;
|
||||
std::unordered_map<std::type_index, schema_node> schema_nodes_;
|
||||
std::unordered_map<std::type_index, std::unique_ptr<sql::resolver_producer>> resolver_producers_;
|
||||
};
|
||||
}
|
||||
#endif //MATADOR_BASIC_SCHEMA_HPP
|
||||
@@ -7,6 +7,9 @@
|
||||
#include "matador/utils/placeholder.hpp"
|
||||
#include "matador/utils/value.hpp"
|
||||
|
||||
namespace matador::utils {
|
||||
class identifier;
|
||||
}
|
||||
namespace matador::sql {
|
||||
struct query_context;
|
||||
}
|
||||
@@ -57,6 +60,9 @@ criteria_ptr operator>=(const table_column &col, utils::placeholder p);
|
||||
criteria_ptr operator<(const table_column &col, utils::placeholder p);
|
||||
criteria_ptr operator<=(const table_column &col, utils::placeholder p);
|
||||
|
||||
criteria_ptr operator==(const table_column &col, const utils::identifier &id);
|
||||
criteria_ptr operator!=(const table_column &col, const utils::identifier &id);
|
||||
|
||||
criteria_ptr operator&&(criteria_ptr left, criteria_ptr right);
|
||||
|
||||
criteria_ptr operator||(criteria_ptr left, criteria_ptr right);
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#include "matador/query/internal/column_value_pair.hpp"
|
||||
|
||||
#include "matador/query/table.hpp"
|
||||
#include "matador/query/schema.hpp"
|
||||
#include "matador/query/basic_schema.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
@@ -45,7 +45,7 @@ constexpr auto default_column_generator_options = column_generator_options::Forc
|
||||
|
||||
class column_generator {
|
||||
public:
|
||||
explicit column_generator(const schema &repo,
|
||||
explicit column_generator(const basic_schema &repo,
|
||||
const table* tab,
|
||||
column_generator_options options = default_column_generator_options);
|
||||
|
||||
@@ -141,7 +141,7 @@ private:
|
||||
void push(const std::string &column_name);
|
||||
|
||||
private:
|
||||
const schema &repo_;
|
||||
const basic_schema &repo_;
|
||||
std::vector<table_column> result_;
|
||||
std::stack<const table*> table_stack_;
|
||||
std::unordered_set<std::string> seen_tables;
|
||||
@@ -289,7 +289,7 @@ std::vector<internal::column_value_pair> column_value_pairs() {
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
std::vector<table_column> columns(const schema &repo,
|
||||
std::vector<table_column> columns(const basic_schema &repo,
|
||||
const table &tab,
|
||||
const column_generator_options options = default_column_generator_options) {
|
||||
column_generator generator(repo, &tab, options);
|
||||
@@ -297,7 +297,7 @@ std::vector<table_column> columns(const schema &repo,
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
std::vector<table_column> columns(const schema &repo,
|
||||
std::vector<table_column> columns(const basic_schema &repo,
|
||||
const column_generator_options options = default_column_generator_options) {
|
||||
const auto it = repo.find(typeid(Type));
|
||||
if (it == repo.end()) {
|
||||
@@ -309,7 +309,7 @@ std::vector<table_column> columns(const schema &repo,
|
||||
|
||||
template<typename Type>
|
||||
std::vector<table_column> columns(const Type &obj,
|
||||
const schema &repo,
|
||||
const basic_schema &repo,
|
||||
const column_generator_options options = default_column_generator_options) {
|
||||
const auto it = repo.find(typeid(Type));
|
||||
if (it == repo.end()) {
|
||||
|
||||
@@ -3,13 +3,15 @@
|
||||
|
||||
#include "query_intermediate.hpp"
|
||||
|
||||
#include "../query_compiler.hpp"
|
||||
#include "matador/query/query_compiler.hpp"
|
||||
#include "matador/sql/executor.hpp"
|
||||
|
||||
#include "../../sql/query_result.hpp"
|
||||
#include "../../sql/record.hpp"
|
||||
#include "matador/sql/query_result.hpp"
|
||||
#include "matador/sql/query_record_result.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include "../../utils/error.hpp"
|
||||
#include "../../utils/result.hpp"
|
||||
#include "matador/utils/error.hpp"
|
||||
#include "matador/utils/result.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
class executor;
|
||||
@@ -32,14 +34,15 @@ public:
|
||||
}
|
||||
|
||||
const auto prototype = result.value()->prototype();
|
||||
return utils::ok(sql::query_result<Type>(result.release(), [prototype] {
|
||||
auto resolver = exec.resolver_factory()->resolver<Type>();
|
||||
return utils::ok(sql::query_result<Type>(result.release(), resolver, [prototype] {
|
||||
return sql::detail::create_prototype<Type>(prototype);
|
||||
}));
|
||||
}
|
||||
[[nodiscard]] utils::result<sql::query_result<sql::record>, utils::error> fetch_all(const sql::executor &exec) const;
|
||||
|
||||
template < class Type >
|
||||
utils::result<std::unique_ptr<Type>, utils::error> fetch_one(const sql::executor &exec)
|
||||
utils::result<object::object_ptr<Type>, utils::error> fetch_one(sql::executor &exec)
|
||||
{
|
||||
auto result = fetch(exec);
|
||||
if (!result.is_ok()) {
|
||||
@@ -47,15 +50,16 @@ public:
|
||||
}
|
||||
|
||||
const auto prototype = result.value()->prototype();
|
||||
auto objects = sql::query_result<Type>(result.release(), [prototype] {
|
||||
auto resolver = exec.resolver_factory()->resolver<Type>();
|
||||
auto objects = sql::query_result<Type>(result.release(), resolver, [prototype] {
|
||||
return sql::detail::create_prototype<Type>(prototype);
|
||||
});
|
||||
auto first = objects.begin();
|
||||
if (first == objects.end()) {
|
||||
return utils::ok(std::unique_ptr<Type>{nullptr});
|
||||
return utils::ok(object::object_ptr<Type>{});
|
||||
}
|
||||
|
||||
return utils::ok(std::unique_ptr<Type>{first.release()});
|
||||
return utils::ok(first.optr());
|
||||
}
|
||||
|
||||
[[nodiscard]] utils::result<std::optional<sql::record>, utils::error> fetch_one(const sql::executor &exec) const;
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
|
||||
#include "matador/query/intermediates/query_intermediate.hpp"
|
||||
#include "matador/query/intermediates/query_into_intermediate.hpp"
|
||||
#include "matador/query/schema.hpp"
|
||||
#include "matador/query/generator.hpp"
|
||||
|
||||
namespace matador::query {
|
||||
class schema;
|
||||
|
||||
class query_insert_intermediate : public query_intermediate {
|
||||
public:
|
||||
|
||||
@@ -4,13 +4,12 @@
|
||||
#include "matador/query/query_intermediates.hpp"
|
||||
|
||||
#include "matador/query/generator.hpp"
|
||||
#include "matador/query/schema.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
class connection;
|
||||
}
|
||||
namespace matador::query {
|
||||
|
||||
class schema;
|
||||
table_column alias(const std::string &column, const std::string &as);
|
||||
table_column alias(table_column &&col, const std::string &as);
|
||||
table_column count(const std::string &column);
|
||||
|
||||
@@ -0,0 +1,337 @@
|
||||
#ifndef QUERY_ENTITY_QUERY_BUILDER_HPP
|
||||
#define QUERY_ENTITY_QUERY_BUILDER_HPP
|
||||
|
||||
#include "matador/query/query_builder_exception.hpp"
|
||||
|
||||
#include "matador/query/criteria.hpp"
|
||||
#include "matador/query/query.hpp"
|
||||
|
||||
#include "matador/sql/statement.hpp"
|
||||
|
||||
#include "matador/object/join_columns_collector.hpp"
|
||||
#include "matador/object/repository.hpp"
|
||||
#include "matador/query/criteria/criteria_visitor.hpp"
|
||||
|
||||
#include "matador/utils/primary_key_attribute.hpp"
|
||||
#include "matador/utils/result.hpp"
|
||||
#include "matador/utils/value.hpp"
|
||||
|
||||
#include <stack>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
struct entity_query_data {
|
||||
const table* root_table{nullptr};
|
||||
std::string pk_column_name{};
|
||||
std::vector<table_column> columns{};
|
||||
std::unordered_map<std::string, sql::statement> lazy_loading_statements{};
|
||||
std::vector<join_data> joins{};
|
||||
criteria_ptr where_clause{};
|
||||
};
|
||||
|
||||
class criteria_transformer final : public criteria_visitor {
|
||||
public:
|
||||
criteria_transformer(const basic_schema &repo, const std::unordered_map<std::string, table>& tables_by_name);
|
||||
void visit( const between_criteria& node ) override;
|
||||
void visit( const binary_criteria& node ) override;
|
||||
void visit( const binary_column_criteria& node ) override;
|
||||
void visit( const collection_criteria& node ) override;
|
||||
void visit( const collection_query_criteria& node ) override;
|
||||
void visit( const like_criteria& node ) override;
|
||||
void visit( const logical_criteria& node ) override;
|
||||
void visit( const not_criteria& node ) override;
|
||||
|
||||
private:
|
||||
void update_criteria_column(const abstract_column_criteria& node) const;
|
||||
|
||||
private:
|
||||
const basic_schema &repo_;
|
||||
const std::unordered_map<std::string, table>& tables_by_name_;
|
||||
};
|
||||
|
||||
class query_builder final {
|
||||
public:
|
||||
query_builder(const basic_schema &scm, sql::executor &exec)
|
||||
: schema_(scm)
|
||||
, executor_(exec){}
|
||||
|
||||
template<class EntityType>
|
||||
utils::result<entity_query_data, query_build_error> build(criteria_ptr clause = {}) {
|
||||
const auto it = schema_.find(typeid(EntityType));
|
||||
if (it == schema_.end()) {
|
||||
return utils::failure(query_build_error::UnknownType);
|
||||
}
|
||||
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 };
|
||||
processed_tables_.insert({it->second.name(), *entity_query_data_.root_table});
|
||||
try {
|
||||
EntityType obj;
|
||||
access::process(*this, obj);
|
||||
|
||||
if (clause) {
|
||||
criteria_transformer transformer{schema_, processed_tables_};
|
||||
clause->accept(transformer);
|
||||
entity_query_data_.where_clause = std::move(clause);
|
||||
}
|
||||
return {utils::ok(std::move(entity_query_data_))};
|
||||
} catch (const query_builder_exception &ex) {
|
||||
return {utils::failure(ex.error_type())};
|
||||
} catch (...) {
|
||||
return {utils::failure(query_build_error::UnexpectedError)};
|
||||
}
|
||||
}
|
||||
|
||||
template < class V >
|
||||
void on_primary_key(const char *id, V &, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {
|
||||
push(id);
|
||||
if (!is_root_entity()) {
|
||||
return;
|
||||
}
|
||||
entity_query_data_.pk_column_name = id;
|
||||
}
|
||||
|
||||
void on_revision(const char *id, uint64_t &/*rev*/);
|
||||
|
||||
template<typename Type>
|
||||
void on_attribute(const char *id, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes)
|
||||
{
|
||||
push(id);
|
||||
}
|
||||
|
||||
template<class Pointer>
|
||||
void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
||||
on_foreign_object(id, obj, attr);
|
||||
}
|
||||
|
||||
template<class Pointer>
|
||||
void on_has_one(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
||||
on_foreign_object(id, obj, attr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
struct NoopDeleter {
|
||||
void operator()(const T*) const {}
|
||||
};
|
||||
|
||||
template<class ContainerType>
|
||||
void on_has_many(const char * /*id*/, ContainerType &, const char *join_column, const utils::foreign_attributes &attr) {
|
||||
if (attr.fetch() != utils::fetch_type::Eager) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto it = schema_.find(typeid(typename ContainerType::value_type::value_type));
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
}
|
||||
|
||||
auto next = processed_tables_.find(it->second.name());
|
||||
if (next != processed_tables_.end()) {
|
||||
// node already processed
|
||||
return;
|
||||
}
|
||||
|
||||
table_info_stack_.push({it->second.node().info(), it->second.table().as(build_alias('t', ++table_index))});
|
||||
next = processed_tables_.insert({it->second.name(), table_info_stack_.top().table}).first;
|
||||
typename ContainerType::value_type::value_type obj;
|
||||
access::process(*this , obj);
|
||||
table_info_stack_.pop();
|
||||
|
||||
if (!it->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
}
|
||||
|
||||
append_join(
|
||||
table_column{&table_info_stack_.top().table, table_info_stack_.top().info.primary_key_attribute()->name()},
|
||||
table_column{&next->second, join_column}
|
||||
);
|
||||
|
||||
|
||||
// const auto result = schema_.repo().basic_info(typeid(typename ContainerType::value_type::value_type));
|
||||
// if (!result) {
|
||||
// throw query_builder_exception{query_build_error::UnknownType};
|
||||
// }
|
||||
//
|
||||
// const auto &info = result.value().get();
|
||||
// auto next = processed_tables_.find(info.name());
|
||||
// if (next != processed_tables_.end()) {
|
||||
// return;
|
||||
// }
|
||||
// table_info_stack_.push({*result, std::make_shared<table>(info.name(), build_alias('t', ++table_index))});
|
||||
// next = processed_tables_.insert({info.name(), table_info_stack_.top().table}).first;
|
||||
// typename ContainerType::value_type::value_type obj;
|
||||
// access::process(*this , obj);
|
||||
// table_info_stack_.pop();
|
||||
//
|
||||
// if (!info.has_primary_key()) {
|
||||
// throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
// }
|
||||
//
|
||||
// append_join(
|
||||
// column{table_info_stack_.top().table.get(), table_info_stack_.top().info.get().primary_key_attribute()->name()},
|
||||
// column{next->second.get(), join_column}
|
||||
// );
|
||||
}
|
||||
|
||||
template<class ContainerType>
|
||||
void on_has_many_to_many(const char *id, ContainerType &/*cont*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &attr) {
|
||||
if (attr.fetch() != utils::fetch_type::Eager) {
|
||||
return;
|
||||
}
|
||||
const auto result = schema_.find(typeid(typename ContainerType::value_type::value_type));
|
||||
if (result == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
}
|
||||
|
||||
auto next = processed_tables_.find(result->second.name());
|
||||
if (next != processed_tables_.end()) {
|
||||
// attribute was already processed
|
||||
return;
|
||||
}
|
||||
|
||||
auto relation = processed_tables_.find(id);
|
||||
if (relation == processed_tables_.end()) {
|
||||
const auto it = schema_.find(id);
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
}
|
||||
relation = processed_tables_.emplace(id, it->second.table().as(build_alias('t', ++table_index))).first;
|
||||
}
|
||||
table_info_stack_.push({result->second.node().info(), result->second.table().as(build_alias('t', ++table_index))});
|
||||
next = processed_tables_.insert({result->second.name(), table_info_stack_.top().table}).first;
|
||||
typename ContainerType::value_type::value_type obj;
|
||||
access::process(*this , obj);
|
||||
table_info_stack_.pop();
|
||||
|
||||
if (!result->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
}
|
||||
|
||||
append_join(
|
||||
table_column{&table_info_stack_.top().table, table_info_stack_.top().info.primary_key_attribute()->name()},
|
||||
table_column{&relation->second, join_column}
|
||||
);
|
||||
append_join(
|
||||
table_column{&relation->second, inverse_join_column},
|
||||
table_column{&next->second, result->second.node().info().primary_key_attribute()->name()}
|
||||
);
|
||||
}
|
||||
|
||||
template<class ContainerType>
|
||||
void on_has_many_to_many(const char *id, ContainerType &/*cont*/, const utils::foreign_attributes &attr) {
|
||||
if (attr.fetch() != utils::fetch_type::Eager) {
|
||||
return;
|
||||
}
|
||||
const auto result = schema_.find(typeid(typename ContainerType::value_type::value_type));
|
||||
if (result == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
}
|
||||
|
||||
auto next = processed_tables_.find(result->second.name());
|
||||
if (next != processed_tables_.end()) {
|
||||
// attribute was already processed
|
||||
return;
|
||||
}
|
||||
|
||||
auto relation = processed_tables_.find(id);
|
||||
if (relation == processed_tables_.end()) {
|
||||
const auto it = schema_.find(id);
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
}
|
||||
const auto t = it->second.table().as(build_alias('t', ++table_index));
|
||||
relation = processed_tables_.insert({id, t}).first;
|
||||
}
|
||||
table_info_stack_.push({result->second.node().info(), result->second.table().as(build_alias('t', ++table_index))});
|
||||
next = processed_tables_.insert({result->second.name(), table_info_stack_.top().table}).first;
|
||||
typename ContainerType::value_type::value_type obj;
|
||||
access::process(*this , obj);
|
||||
table_info_stack_.pop();
|
||||
|
||||
if (!result->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
}
|
||||
|
||||
const auto join_columns = join_columns_collector_.collect<typename ContainerType::value_type::value_type>();
|
||||
|
||||
append_join(
|
||||
table_column{&table_info_stack_.top().table, table_info_stack_.top().info.primary_key_attribute()->name()},
|
||||
table_column{&relation->second, join_columns.inverse_join_column}
|
||||
);
|
||||
append_join(
|
||||
table_column{&relation->second, join_columns.join_column},
|
||||
table_column{&next->second, result->second.node().info().primary_key_attribute()->name()}
|
||||
);
|
||||
}
|
||||
|
||||
private:
|
||||
template<class Pointer>
|
||||
void on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr);
|
||||
void push(const std::string &column_name);
|
||||
static std::string build_alias(char prefix, unsigned int count);
|
||||
[[nodiscard]] bool is_root_entity() const;
|
||||
void append_join(const table_column &left, const table_column &right);
|
||||
|
||||
private:
|
||||
struct table_info {
|
||||
const object::basic_object_info &info;
|
||||
class table table;
|
||||
};
|
||||
|
||||
std::stack<table_info> table_info_stack_{};
|
||||
std::unordered_map<std::string, table> processed_tables_{};
|
||||
const basic_schema &schema_;
|
||||
entity_query_data entity_query_data_{};
|
||||
unsigned int column_index{0};
|
||||
unsigned int table_index{0};
|
||||
object::join_columns_collector join_columns_collector_{};
|
||||
sql::executor &executor_;
|
||||
};
|
||||
|
||||
template<class Pointer>
|
||||
void 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};
|
||||
}
|
||||
if (!it->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
}
|
||||
|
||||
const auto& info = it->second.node().info();
|
||||
auto foreign_table = it->second.table().as(build_alias('t', ++table_index));
|
||||
if (attr.fetch() == utils::fetch_type::Eager) {
|
||||
|
||||
auto next = processed_tables_.find(info.name());
|
||||
if (next != processed_tables_.end()) {
|
||||
return;
|
||||
}
|
||||
table_info_stack_.push({info, std::move(foreign_table)});
|
||||
next = processed_tables_.insert({info.name(), table_info_stack_.top().table}).first;
|
||||
typename Pointer::value_type obj;
|
||||
access::process(*this, obj);
|
||||
table_info_stack_.pop();
|
||||
|
||||
append_join(
|
||||
table_column{&table_info_stack_.top().table, id},
|
||||
table_column{&next->second, info.primary_key_attribute()->name()}
|
||||
);
|
||||
} else {
|
||||
push(id);
|
||||
using namespace matador::utils;
|
||||
using namespace matador::query;
|
||||
// create select query
|
||||
auto result = query::query::select(generator::columns<typename Pointer::value_type>(schema_, foreign_table, generator::column_generator_options::ForceLazy))
|
||||
.from(foreign_table)
|
||||
.where(table_column(&foreign_table, info.primary_key_attribute()->name(), "") == _)
|
||||
.prepare(executor_);
|
||||
if (!result) {
|
||||
throw query_builder_exception(query_build_error::QueryError, result.release_error());
|
||||
}
|
||||
|
||||
entity_query_data_.lazy_loading_statements.emplace(id, std::move(result.release()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
#endif //QUERY_ENTITY_QUERY_BUILDER_HPP
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef QUERY_BUILDER_EXCEPTION_HPP
|
||||
#define QUERY_BUILDER_EXCEPTION_HPP
|
||||
|
||||
#include "matador/utils/error.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
enum class query_build_error : std::uint8_t {
|
||||
Ok = 0,
|
||||
UnknownType,
|
||||
MissingPrimaryKey,
|
||||
UnexpectedError,
|
||||
QueryError
|
||||
};
|
||||
|
||||
class query_builder_exception final : public std::exception {
|
||||
public:
|
||||
explicit query_builder_exception(query_build_error error, utils::error &&err = {});
|
||||
|
||||
[[nodiscard]] query_build_error error_type() const;
|
||||
[[nodiscard]] const utils::error &error() const;
|
||||
|
||||
private:
|
||||
const query_build_error error_type_;
|
||||
utils::error error_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_BUILDER_EXCEPTION_HPP
|
||||
@@ -0,0 +1,62 @@
|
||||
#ifndef MATADOR_QUERY_OBJECT_LOADER_HPP
|
||||
#define MATADOR_QUERY_OBJECT_LOADER_HPP
|
||||
|
||||
#include "matador/sql/internal/identifier_statement_binder.hpp"
|
||||
#include "matador/sql/statement.hpp"
|
||||
|
||||
#include "matador/object/object_resolver.hpp"
|
||||
|
||||
#include "matador/query/table.hpp"
|
||||
#include "matador/query/query_builder.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
class executor;
|
||||
}
|
||||
|
||||
namespace matador::query {
|
||||
template<typename Type>
|
||||
class query_object_resolver : public object::object_resolver<Type> {
|
||||
public:
|
||||
explicit query_object_resolver(const basic_schema &repo, sql::executor& exec, const table &tab, std::string pk_name)
|
||||
: executor_(exec)
|
||||
, schema_(repo)
|
||||
, table_(tab)
|
||||
, pk_name_(std::move(pk_name)) {}
|
||||
|
||||
std::shared_ptr<Type> resolve(const utils::identifier &id) override;
|
||||
protected:
|
||||
sql::executor& executor_;
|
||||
const basic_schema &schema_;
|
||||
const table &table_;
|
||||
std::string pk_name_;
|
||||
std::type_index index{typeid(Type)};
|
||||
};
|
||||
|
||||
utils::result<sql::statement, utils::error> prepare_statement(sql::executor& exec, entity_query_data &&data);
|
||||
|
||||
template<typename Type>
|
||||
std::shared_ptr<Type> query_object_resolver<Type>::resolve(const utils::identifier &id) {
|
||||
query_builder qb(schema_, executor_);
|
||||
const auto *pk_column = table_[pk_name_];
|
||||
auto builder_result = qb.build<Type>(*pk_column == utils::_);
|
||||
|
||||
if (!builder_result) {
|
||||
return nullptr;
|
||||
}
|
||||
auto stmt = prepare_statement(executor_, builder_result.release());
|
||||
if (!stmt) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sql::identifier_statement_binder binder(*stmt);
|
||||
binder.bind(id);
|
||||
|
||||
auto result = stmt->template fetch_one_raw<Type>();
|
||||
if (!result) {
|
||||
return nullptr;
|
||||
}
|
||||
return *result;
|
||||
}
|
||||
|
||||
}
|
||||
#endif //MATADOR_QUERY_OBJECT_LOADER_HPP
|
||||
@@ -2,19 +2,41 @@
|
||||
#define MATADOR_SCHEMA_HPP
|
||||
|
||||
#include "matador/object/observer.hpp"
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
#include "matador/sql/query_context.hpp"
|
||||
#include "matador/sql/internal/resolver_producer.hpp"
|
||||
|
||||
#include "matador/query/table.hpp"
|
||||
#include "matador/query/query_object_resolver.hpp"
|
||||
#include "matador/query/basic_schema.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
class connection_pool;
|
||||
class connection;
|
||||
class executor;
|
||||
}
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
template<typename Type>
|
||||
class query_resolver_producer : public sql::resolver_producer {
|
||||
public:
|
||||
query_resolver_producer() = default;
|
||||
query_resolver_producer(const basic_schema& repo, const table& tab, std::string pk_name)
|
||||
: resolver_producer(typeid(Type))
|
||||
, repo_(repo)
|
||||
, table_(tab)
|
||||
, pk_name_(std::move(pk_name)) {}
|
||||
|
||||
std::shared_ptr<object::abstract_object_resolver> produce(sql::executor &exec) override {
|
||||
return std::make_shared<query_object_resolver<Type>>(repo_, exec, table_, std::move(pk_name_));
|
||||
}
|
||||
|
||||
private:
|
||||
const basic_schema& repo_;
|
||||
const table& table_;
|
||||
std::string pk_name_;
|
||||
};
|
||||
|
||||
class schema;
|
||||
|
||||
using schema_ref = std::reference_wrapper<schema>;
|
||||
@@ -52,25 +74,14 @@ private:
|
||||
schema& schema_;
|
||||
};
|
||||
|
||||
class schema_node final {
|
||||
class schema final : public basic_schema {
|
||||
public:
|
||||
schema_node(class table tab, const object::repository_node& node);
|
||||
using basic_schema::basic_schema;
|
||||
using basic_schema::iterator;
|
||||
using basic_schema::const_iterator;
|
||||
using basic_schema::contains;
|
||||
using basic_schema::find;
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] const class table& table() const;
|
||||
[[nodiscard]] const object::repository_node& node() const;
|
||||
|
||||
private:
|
||||
class table table_;
|
||||
const object::repository_node& node_;
|
||||
};
|
||||
class schema final {
|
||||
public:
|
||||
using iterator = std::unordered_map<std::type_index, schema_node>::iterator;
|
||||
using const_iterator = std::unordered_map<std::type_index, schema_node>::const_iterator;
|
||||
|
||||
schema() = default;
|
||||
explicit schema(const std::string &name);
|
||||
|
||||
template<typename Type, typename... Observers>
|
||||
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, Observers&&... observers) {
|
||||
@@ -97,28 +108,15 @@ public:
|
||||
[[nodiscard]] static utils::result<std::vector<object::attribute>, utils::error> describe_table(const std::string &table_name, const sql::connection &conn) ;
|
||||
[[nodiscard]] utils::result<bool, utils::error> table_exists(const std::string &table_name, const sql::connection &conn) const;
|
||||
|
||||
iterator begin();
|
||||
iterator end();
|
||||
const_iterator begin() const;
|
||||
const_iterator end() const;
|
||||
|
||||
[[nodiscard]] size_t size() const;
|
||||
[[nodiscard]] bool empty() const;
|
||||
template<typename Type>
|
||||
iterator find() { return basic_schema::find(typeid(Type)); }
|
||||
|
||||
template<typename Type>
|
||||
iterator find() { return find(typeid(Type)); }
|
||||
iterator find(const std::type_index& ti);
|
||||
iterator find(const std::string& name);
|
||||
const_iterator find() const { return basic_schema::find(typeid(Type)); }
|
||||
|
||||
template<typename Type>
|
||||
const_iterator find() const { return find(typeid(Type)); }
|
||||
const_iterator find(const std::type_index& ti) const;
|
||||
const_iterator find(const std::string& name) const;
|
||||
|
||||
[[nodiscard]] bool contains(const std::type_index &index) const;
|
||||
template < typename Type >
|
||||
[[nodiscard]] bool contains() const {
|
||||
return contains(std::type_index(typeid(Type)));
|
||||
return basic_schema::contains(std::type_index(typeid(Type)));
|
||||
}
|
||||
|
||||
const object::repository &repo() const { return repo_; }
|
||||
@@ -130,14 +128,11 @@ private:
|
||||
[[nodiscard]] static sql::query_context build_add_constraint_context(const object::repository_node& node, const object::restriction& cons, const sql::connection &conn) ;
|
||||
[[nodiscard]] static sql::query_context build_drop_constraint_context(const object::repository_node& node, const object::restriction& cons, const sql::connection &conn) ;
|
||||
|
||||
void insert_table(const std::type_index& ti, const object::repository_node &node);
|
||||
iterator insert_table(const std::type_index& ti, const object::repository_node &node);
|
||||
|
||||
private:
|
||||
template<typename Type>
|
||||
friend class schema_observer;
|
||||
|
||||
object::repository repo_;
|
||||
std::unordered_map<std::type_index, schema_node> schema_nodes_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
@@ -151,7 +146,12 @@ utils::result<void, utils::error> schema::drop_table(const sql::connection &conn
|
||||
}
|
||||
template <typename Type>
|
||||
void schema_observer<Type>::on_attach(const object::repository_node &node, const Type &/*prototype*/) const {
|
||||
schema_.insert_table(typeid(Type), node);
|
||||
const auto it = schema_.insert_table(typeid(Type), node);
|
||||
|
||||
if (it->second.node().info().has_primary_key()) {
|
||||
auto producer = std::make_unique<query_resolver_producer<Type>>(schema_, it->second.table(), it->second.node().info().primary_key_attribute()->name());
|
||||
schema_.resolver_producers_[typeid(Type)] = std::move(producer);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
|
||||
@@ -6,9 +6,7 @@
|
||||
#include "matador/utils/basic_types.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
namespace matador::query {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user