130 lines
3.6 KiB
C++
130 lines
3.6 KiB
C++
#ifndef QUERY_COLUMN_GENERATOR_HPP
|
|
#define QUERY_COLUMN_GENERATOR_HPP
|
|
|
|
#include "matador/utils/access.hpp"
|
|
#include "matador/utils/field_attributes.hpp"
|
|
#include "matador/utils/foreign_attributes.hpp"
|
|
|
|
#include "matador/sql/column.hpp"
|
|
|
|
#include "matador/object/schema.hpp"
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <stack>
|
|
|
|
namespace matador::sql {
|
|
|
|
class column_generator
|
|
{
|
|
private:
|
|
column_generator(std::vector<column> &column_infos,
|
|
const object::schema &ts,
|
|
const std::string &table_name,
|
|
bool force_lazy);
|
|
|
|
public:
|
|
~column_generator() = default;
|
|
|
|
template < class Type >
|
|
static std::vector<column> generate(const object::schema &scm, bool force_lazy = false)
|
|
{
|
|
const auto info = scm.info<Type>();
|
|
if (!info) {
|
|
return {};
|
|
}
|
|
std::vector<column> columns;
|
|
column_generator gen(columns, scm, info.value().get().name(), force_lazy);
|
|
Type obj;
|
|
matador::access::process(gen, obj);
|
|
return std::move(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)
|
|
{
|
|
push(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 &, const utils::foreign_attributes &attr)
|
|
{
|
|
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
|
|
push(id);
|
|
} else {
|
|
const auto info = table_schema_.info<typename Pointer::value_type>();
|
|
if (!info) {
|
|
return;
|
|
}
|
|
table_name_stack_.push(info.value().get().name());
|
|
typename Pointer::value_type obj;
|
|
matador::access::process(*this, obj);
|
|
table_name_stack_.pop();
|
|
}
|
|
}
|
|
template<class Pointer>
|
|
void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
|
{
|
|
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
|
|
push(id);
|
|
} else {
|
|
const auto info = table_schema_.info<typename Pointer::value_type>();
|
|
if (!info) {
|
|
return;
|
|
}
|
|
table_name_stack_.push(info.value().get().name());
|
|
typename Pointer::value_type obj;
|
|
matador::access::process(*this, obj);
|
|
table_name_stack_.pop();
|
|
}
|
|
}
|
|
template<class ContainerType>
|
|
void on_has_many(ContainerType &, const char *, const utils::foreign_attributes &attr)
|
|
{
|
|
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
|
|
return;
|
|
}
|
|
const auto info = table_schema_.info<typename ContainerType::value_type::value_type>();
|
|
if (!info) {
|
|
return;
|
|
}
|
|
|
|
table_name_stack_.push(info.value().get().name());
|
|
typename ContainerType::value_type::value_type obj;
|
|
matador::access::process(*this, obj);
|
|
table_name_stack_.pop();
|
|
}
|
|
|
|
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)
|
|
{
|
|
}
|
|
|
|
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);
|
|
|
|
private:
|
|
std::stack<std::string> table_name_stack_;
|
|
std::vector<column> &column_infos_;
|
|
const object::schema &table_schema_;
|
|
int column_index{0};
|
|
bool force_lazy_{false};
|
|
};
|
|
|
|
}
|
|
|
|
#endif //QUERY_COLUMN_GENERATOR_HPP
|