114 lines
3.2 KiB
C++
114 lines
3.2 KiB
C++
#ifndef QUERY_ENTITY_QUERY_BUILDER_HPP
|
|
#define QUERY_ENTITY_QUERY_BUILDER_HPP
|
|
|
|
#include "matador/sql/connection.hpp"
|
|
#include "matador/sql/query_context.hpp"
|
|
#include "matador/sql/query.hpp"
|
|
#include "matador/sql/query_intermediates.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
namespace matador::sql {
|
|
|
|
template < typename PrimaryKeyType >
|
|
class entity_query_builder
|
|
{
|
|
public:
|
|
explicit entity_query_builder(const PrimaryKeyType &pk, const schema &scm)
|
|
: pk_(pk)
|
|
, schema_(scm) {}
|
|
|
|
// determine pk
|
|
// collect eager relations for joins
|
|
template<class EntityType>
|
|
std::optional<query_context> build(connection &db) {
|
|
EntityType obj;
|
|
matador::utils::access::process(*this, obj);
|
|
|
|
const auto info = schema_.info<EntityType>();
|
|
if (!info) {
|
|
return std::nullopt;
|
|
}
|
|
columns_.clear();
|
|
table_name_ = info.value().name;
|
|
query q(db, schema_);
|
|
return q.select<EntityType>().from({table_name_}).build();
|
|
// auto from_intermediate = q.select<EntityType>().from({"t"});
|
|
// return {};
|
|
}
|
|
|
|
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)
|
|
{
|
|
auto b = std::is_integral_v<PrimaryKeyType>;
|
|
std::cout << "is matching primary key: " << std::boolalpha << b << "\n";
|
|
columns_.emplace_back(table_name_, id, "");
|
|
}
|
|
|
|
void on_primary_key(const char *id, std::string &, size_t)
|
|
{
|
|
auto b = std::is_same_v<PrimaryKeyType, std::string>;
|
|
std::cout << "is matching primary key: " << std::boolalpha << b << "\n";
|
|
columns_.emplace_back(table_name_, id, "");
|
|
}
|
|
|
|
void on_revision(const char *id, unsigned long long &/*rev*/)
|
|
{
|
|
columns_.emplace_back(table_name_, id, "");
|
|
}
|
|
|
|
template<typename Type>
|
|
void on_attribute(const char *id, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes)
|
|
{
|
|
columns_.emplace_back(table_name_, id, "");
|
|
}
|
|
|
|
template<class Pointer>
|
|
void on_belongs_to(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
|
{
|
|
if (attr.fetch() == utils::fetch_type::EAGER) {
|
|
column col{id};
|
|
|
|
// collect join information (determine primary key of foreign entity)
|
|
}
|
|
columns_.emplace_back(table_name_, id, "");
|
|
}
|
|
|
|
template<class Pointer>
|
|
void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
|
{
|
|
if (attr.fetch() == utils::fetch_type::EAGER) {
|
|
auto info = schema_.info<typename Pointer::value_type>();
|
|
if (info) {
|
|
column lcol(table_name_, id, "");
|
|
// column rcol(info.value())
|
|
}
|
|
// collect join information
|
|
}
|
|
columns_.emplace_back(table_name_, id, "");
|
|
}
|
|
|
|
template<class ContainerType>
|
|
void on_has_many(const char *, ContainerType &, const char *, const char *, const utils::foreign_attributes &attr)
|
|
{
|
|
if (attr.fetch() == utils::fetch_type::EAGER) {
|
|
// collect join information
|
|
}
|
|
}
|
|
|
|
template<class ContainerType>
|
|
void on_has_many(const char *id, ContainerType &c, const utils::foreign_attributes &attr)
|
|
{
|
|
on_has_many(id, c, "", "", attr);
|
|
}
|
|
|
|
private:
|
|
PrimaryKeyType pk_;
|
|
std::vector<column> columns_;
|
|
const schema &schema_;
|
|
std::string table_name_;
|
|
};
|
|
|
|
}
|
|
#endif //QUERY_ENTITY_QUERY_BUILDER_HPP
|