query/include/matador/sql/entity_query_builder.hpp

205 lines
5.8 KiB
C++

#ifndef QUERY_ENTITY_QUERY_BUILDER_HPP
#define QUERY_ENTITY_QUERY_BUILDER_HPP
#include "matador/sql/connection.hpp"
#include "matador/sql/condition.hpp"
#include "matador/sql/query_context.hpp"
#include "matador/sql/query.hpp"
#include "matador/sql/query_intermediates.hpp"
#include <iostream>
namespace matador::sql {
struct join_data
{
table join_table;
std::unique_ptr<basic_condition> condition;
};
struct entity_query_data {
std::string root_table_name;
std::vector<column> columns;
std::vector<join_data> joins;
std::unique_ptr<basic_condition> where_clause;
};
template < typename PrimaryKeyType >
class entity_query_builder
{
public:
explicit entity_query_builder(const PrimaryKeyType &pk, const schema &scm)
: pk_(pk)
, schema_(scm) {}
template<class EntityType>
std::optional<entity_query_data> build() {
const auto info = schema_.info<EntityType>();
if (!info) {
return std::nullopt;
}
table_info_stack_.push(info.value());
entity_query_data_ = { info->name };
EntityType obj;
matador::utils::access::process(*this, obj);
return std::move(entity_query_data_);
}
template < class V >
void on_primary_key(const char *id, V &, typename std::enable_if<std::is_integral<V>::value && !std::is_same<bool, V>::value>::type* = 0)
{
push(id);
if (is_root_entity() && std::is_integral_v<PrimaryKeyType>) {
entity_query_data_.where_clause = make_condition(column{table_info_stack_.top().name, id, ""} == pk_);
}
}
void on_primary_key(const char *id, std::string &, size_t)
{
push(id);
if (!is_root_entity()) {
auto b = std::is_same_v<PrimaryKeyType, std::string>;
std::cout << "is matching primary key: " << std::boolalpha << b << "\n";
}
}
void on_revision(const char *id, unsigned long long &/*rev*/)
{
push(id);
}
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 &, const utils::foreign_attributes &attr)
{
if (attr.fetch() == utils::fetch_type::EAGER) {
const auto info = schema_.info<typename Pointer::value_type>();
if (!info) {
return;
}
table_info_stack_.push(info.value());
typename Pointer::value_type obj;
matador::utils::access::process(*this , obj);
table_info_stack_.pop();
auto pk = info->prototype.primary_key();
if (!pk) {
// error, prototype doesn't has a pk
return;
}
append_join({table_info_stack_.top().name, id}, {info->name, pk->name()});
} else {
push(id);
}
}
template<class Pointer>
void on_has_one(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) {
return;
}
table_info_stack_.push(info.value());
typename Pointer::value_type obj;
matador::utils::access::process(*this, obj);
table_info_stack_.pop();
auto pk = info->prototype.primary_key();
if (!pk) {
// error, prototype doesn't has a pk
return;
}
append_join({table_info_stack_.top().name, id}, {info->name, pk->name()});
} else {
push(id);
}
}
template<class ContainerType>
void on_has_many(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) {
return;
}
table_info_stack_.push(info.value());
typename ContainerType::value_type::value_type obj;
matador::utils::access::process(*this , obj);
table_info_stack_.pop();
auto pk = info->prototype.primary_key();
if (!pk) {
// error, prototype doesn't has a pk
return;
}
append_join({table_info_stack_.top().name, table_info_stack_.top().prototype.primary_key()->name()}, {info->name, join_column});
}
}
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)
{
if (attr.fetch() == utils::fetch_type::EAGER) {
const auto info = schema_.info<typename ContainerType::value_type::value_type>();
if (!info) {
return;
}
table_info_stack_.push(info.value());
typename ContainerType::value_type::value_type obj;
matador::utils::access::process(*this , obj);
table_info_stack_.pop();
auto pk = info->prototype.primary_key();
if (!pk) {
// error, prototype doesn't has a pk
return;
}
append_join({table_info_stack_.top().name, table_info_stack_.top().prototype.primary_key()->name()}, {id, join_column});
append_join({info->name, pk->name()}, {id, inverse_join_column});
}
}
template<class ContainerType>
void on_has_many_to_many(const char *id, ContainerType &c, const utils::foreign_attributes &/*attr*/) {}
private:
void push(const std::string &column_name)
{
char str[4];
snprintf(str, 4, "c%02d", ++column_index);
entity_query_data_.columns.emplace_back(table_info_stack_.top().name, column_name, str);
}
[[nodiscard]] bool is_root_entity() const {
return table_info_stack_.size() == 1;
}
void append_join(const column &left, const column &right)
{
entity_query_data_.joins.push_back({
{ left.table },
make_condition(left == right)
});
}
private:
PrimaryKeyType pk_;
std::stack<table_info> table_info_stack_;
const schema &schema_;
entity_query_data entity_query_data_;
int column_index{0};
};
}
#endif //QUERY_ENTITY_QUERY_BUILDER_HPP