query/include/matador/sql/entity_query_builder.hpp

105 lines
2.9 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)
: pk_(pk) {}
// determine pk
// collect eager relations for joins
template<class EntityType>
std::optional<query_context> build(connection &db, const schema &scm) {
EntityType obj;
matador::utils::access::process(*this, obj);
const auto info = scm.info<EntityType>();
if (!info) {
return std::nullopt;
}
columns_.clear();
table_name_ = info.value().name;
query q(db, scm);
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) {
// collect join information
}
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) {
// 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_;
std::string table_name_;
};
}
#endif //QUERY_ENTITY_QUERY_BUILDER_HPP