335 lines
12 KiB
C++
335 lines
12 KiB
C++
#ifndef QUERY_ENTITY_QUERY_BUILDER_HPP
|
|
#define QUERY_ENTITY_QUERY_BUILDER_HPP
|
|
|
|
#include "matador/query/condition.hpp"
|
|
#include "matador/query/query_intermediates.hpp"
|
|
|
|
#include "matador/sql/connection.hpp"
|
|
|
|
#include "matador/object/schema.hpp"
|
|
|
|
#include "matador/utils/result.hpp"
|
|
#include "matador/utils/value.hpp"
|
|
|
|
#include <stack>
|
|
#include <unordered_set>
|
|
|
|
namespace matador::orm {
|
|
|
|
struct join_columns
|
|
{
|
|
std::string join_column;
|
|
std::string inverse_join_column;
|
|
};
|
|
|
|
class join_column_collector
|
|
{
|
|
public:
|
|
template<class Type>
|
|
join_columns collect() {
|
|
join_columns_ = {};
|
|
Type obj;
|
|
|
|
access::process(*this, obj);
|
|
|
|
return join_columns_;
|
|
}
|
|
template < class V >
|
|
void on_primary_key(const char * /*id*/, V &, std::enable_if_t<std::is_integral_v<V> && !std::is_same_v<bool, V>>* = nullptr) {}
|
|
static void on_primary_key(const char * /*id*/, std::string &, size_t) {}
|
|
static void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
|
|
template<typename Type>
|
|
static void on_attribute(const char * /*id*/, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
|
template<class Pointer>
|
|
static void on_belongs_to(const char * /*id*/, Pointer &obj, const utils::foreign_attributes &attr) {}
|
|
template<class Pointer>
|
|
static void on_has_one(const char * /*id*/, Pointer &obj, const utils::foreign_attributes &attr) {}
|
|
template<class ContainerType>
|
|
static void on_has_many(ContainerType &, const char *join_column, const utils::foreign_attributes &attr) {}
|
|
template<class ContainerType>
|
|
void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) {
|
|
join_columns_.join_column = join_column;
|
|
join_columns_.inverse_join_column = inverse_join_column;
|
|
}
|
|
template<class ContainerType>
|
|
static void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const utils::foreign_attributes &/*attr*/) {}
|
|
|
|
private:
|
|
join_columns join_columns_;
|
|
};
|
|
|
|
struct entity_query_data {
|
|
std::shared_ptr<sql::table> root_table;
|
|
std::string pk_column_name{};
|
|
std::vector<sql::column> columns{};
|
|
std::vector<query::join_data> joins{};
|
|
std::unique_ptr<query::basic_condition> where_clause{};
|
|
};
|
|
|
|
enum class query_build_error : std::uint8_t {
|
|
Ok = 0,
|
|
UnknownType,
|
|
MissingPrimaryKey,
|
|
UnexpectedError
|
|
};
|
|
|
|
class query_builder_exception final : public std::exception
|
|
{
|
|
public:
|
|
explicit query_builder_exception(const query_build_error error) : error_(error) {}
|
|
|
|
[[nodiscard]] query_build_error error() const { return error_; }
|
|
|
|
private:
|
|
const query_build_error error_;
|
|
};
|
|
|
|
class session_query_builder final
|
|
{
|
|
public:
|
|
explicit session_query_builder(const object::schema &scm)
|
|
: schema_(scm) {}
|
|
|
|
template<class EntityType, typename PrimaryKeyType>
|
|
utils::result<entity_query_data, query_build_error> build(const PrimaryKeyType &pk) {
|
|
auto info = schema_.info<EntityType>();
|
|
if (!info) {
|
|
return utils::failure(query_build_error::UnknownType);
|
|
}
|
|
pk_ = pk;
|
|
table_info_stack_.push({info.value(), std::make_shared<sql::table>(info.value().get().name(), build_alias('t', ++table_index))});
|
|
entity_query_data_ = { table_info_stack_.top().table };
|
|
processed_tables_.insert({info->get().name(), entity_query_data_.root_table});
|
|
try {
|
|
access::process(*this, info->get().prototype());
|
|
|
|
return {utils::ok(std::move(entity_query_data_))};
|
|
} catch (const query_builder_exception &ex) {
|
|
return {utils::failure(ex.error())};
|
|
} catch (...) {
|
|
return {utils::failure(query_build_error::UnexpectedError)};
|
|
}
|
|
}
|
|
|
|
template<class EntityType>
|
|
utils::result<entity_query_data, query_build_error> build() {
|
|
const auto info = schema_.info<EntityType>();
|
|
if (!info) {
|
|
return utils::failure(query_build_error::UnknownType);
|
|
}
|
|
pk_ = nullptr;
|
|
table_info_stack_.push({info.value(), std::make_shared<sql::table>(info.value().get().name(), build_alias('t', ++table_index))});
|
|
entity_query_data_ = { table_info_stack_.top().table };
|
|
processed_tables_.insert({info->get().name(), entity_query_data_.root_table});
|
|
try {
|
|
access::process(*this, info->get().prototype());
|
|
|
|
return {utils::ok(std::move(entity_query_data_))};
|
|
} catch (const query_builder_exception &ex) {
|
|
return {utils::failure(ex.error())};
|
|
} catch (...) {
|
|
return {utils::failure(query_build_error::UnexpectedError)};
|
|
}
|
|
}
|
|
|
|
template < class V >
|
|
void on_primary_key(const char *id, V &, std::enable_if_t<std::is_integral_v<V> && !std::is_same_v<bool, V>>* = nullptr)
|
|
{
|
|
push(id);
|
|
if (!is_root_entity()) {
|
|
return;
|
|
}
|
|
if (pk_.is_null()) {
|
|
entity_query_data_.pk_column_name = id;
|
|
} else if (pk_.is_integer()) {
|
|
const auto t = std::make_shared<sql::table>(table_info_stack_.top().info.get().name());
|
|
auto v = *pk_.as<V>();
|
|
auto c = sql::column{t, id, ""};
|
|
auto co = std::make_unique<query::condition<sql::column, V>>(c, query::basic_condition::operand_type::EQUAL, v);
|
|
entity_query_data_.where_clause = std::move(co);
|
|
entity_query_data_.pk_column_name = id;
|
|
}
|
|
}
|
|
|
|
void on_primary_key(const char *id, std::string &, size_t);
|
|
void on_revision(const char *id, unsigned long long &/*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<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) {
|
|
const auto info = schema_.info<typename ContainerType::value_type::value_type>();
|
|
if (!info) {
|
|
throw query_builder_exception{query_build_error::UnknownType};
|
|
}
|
|
|
|
auto next = processed_tables_.find(info->get().name());
|
|
if (next != processed_tables_.end()) {
|
|
return;
|
|
}
|
|
table_info_stack_.push({info.value(), std::make_shared<sql::table>(info->get().name(), build_alias('t', ++table_index))});
|
|
next = processed_tables_.insert({info->get().name(), table_info_stack_.top().table}).first;
|
|
typename ContainerType::value_type::value_type obj;
|
|
access::process(*this , obj);
|
|
table_info_stack_.pop();
|
|
|
|
auto pk = info->get().definition().primary_key();
|
|
if (!pk) {
|
|
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
|
}
|
|
|
|
append_join(
|
|
sql::column{table_info_stack_.top().table, table_info_stack_.top().info.get().definition().primary_key()->name()},
|
|
sql::column{next->second, 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 info = schema_.info<typename ContainerType::value_type::value_type>();
|
|
if (!info) {
|
|
throw query_builder_exception{query_build_error::UnknownType};
|
|
}
|
|
|
|
auto next = processed_tables_.find(info->get().name());
|
|
if (next != processed_tables_.end()) {
|
|
return;
|
|
}
|
|
table_info_stack_.push({info.value(), std::make_shared<sql::table>(info->get().name(), build_alias('t', ++table_index))});
|
|
next = processed_tables_.insert({info->get().name(), table_info_stack_.top().table}).first;
|
|
typename ContainerType::value_type::value_type obj;
|
|
access::process(*this , obj);
|
|
table_info_stack_.pop();
|
|
|
|
auto pk = info->get().definition().primary_key();
|
|
if (!pk) {
|
|
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
|
}
|
|
|
|
append_join(
|
|
sql::column{table_info_stack_.top().table, table_info_stack_.top().info.get().definition().primary_key()->name()},
|
|
sql::column{std::make_shared<sql::table>(id), join_column}
|
|
);
|
|
append_join(
|
|
sql::column{next->second, inverse_join_column},
|
|
sql::column{std::make_shared<sql::table>(info->get().name()), pk->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 info = schema_.info<typename ContainerType::value_type::value_type>();
|
|
if (!info) {
|
|
throw query_builder_exception{query_build_error::UnknownType};
|
|
}
|
|
|
|
auto next = processed_tables_.find(info->get().name());
|
|
if (next != processed_tables_.end()) {
|
|
return;
|
|
}
|
|
table_info_stack_.push({info.value(), std::make_shared<sql::table>(info->get().name(), build_alias('t', ++table_index))});
|
|
typename ContainerType::value_type::value_type obj;
|
|
access::process(*this , obj);
|
|
table_info_stack_.pop();
|
|
|
|
auto pk = info->get().definition().primary_key();
|
|
if (!pk) {
|
|
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
|
}
|
|
|
|
const auto join_columns = join_column_collector_.collect<typename ContainerType::value_type::value_type>();
|
|
|
|
append_join(
|
|
sql::column{table_info_stack_.top().table, table_info_stack_.top().info.get().definition().primary_key()->name()},
|
|
sql::column{std::make_shared<sql::table>(id), join_columns.inverse_join_column}
|
|
);
|
|
append_join(
|
|
sql::column{std::make_shared<sql::table>(id), join_columns.join_column},
|
|
sql::column{std::make_shared<sql::table>(info->get().name()), pk->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 sql::column &left, const sql::column &right);
|
|
|
|
private:
|
|
utils::value pk_;
|
|
struct table_info {
|
|
std::reference_wrapper<const object::basic_object_info> info;
|
|
std::shared_ptr<sql::table> table;
|
|
};
|
|
|
|
std::stack<table_info> table_info_stack_;
|
|
std::unordered_map<std::string, std::shared_ptr<sql::table>> processed_tables_;
|
|
const object::schema &schema_;
|
|
entity_query_data entity_query_data_;
|
|
unsigned int column_index{0};
|
|
unsigned int table_index{0};
|
|
join_column_collector join_column_collector_;
|
|
};
|
|
|
|
template<class Pointer>
|
|
void session_query_builder::on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr) {
|
|
if (attr.fetch() == utils::fetch_type::EAGER) {
|
|
const auto info = schema_.info<typename Pointer::value_type>();
|
|
if (!info) {
|
|
throw query_builder_exception{query_build_error::UnknownType};
|
|
}
|
|
|
|
auto next = processed_tables_.find(info->get().name());
|
|
if (next != processed_tables_.end()) {
|
|
return;
|
|
}
|
|
table_info_stack_.push({info.value(), std::make_shared<sql::table>(info->get().name(), build_alias('t', ++table_index))});
|
|
next = processed_tables_.insert({info->get().name(), table_info_stack_.top().table}).first;
|
|
typename Pointer::value_type obj;
|
|
access::process(*this, obj);
|
|
table_info_stack_.pop();
|
|
|
|
auto pk = info->get().definition().primary_key();
|
|
if (!pk) {
|
|
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
|
}
|
|
append_join(
|
|
sql::column{table_info_stack_.top().table, id},
|
|
sql::column{next->second, pk->name()}
|
|
);
|
|
} else {
|
|
push(id);
|
|
}
|
|
}
|
|
|
|
}
|
|
#endif //QUERY_ENTITY_QUERY_BUILDER_HPP
|