157 lines
4.6 KiB
C++
157 lines
4.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/utils/primary_key_attribute.hpp"
|
|
|
|
#include "matador/sql/column.hpp"
|
|
|
|
#include "matador/object/repository.hpp"
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <stack>
|
|
|
|
namespace matador::sql {
|
|
|
|
class column_generator
|
|
{
|
|
private:
|
|
column_generator(std::vector<column> &column_infos,
|
|
const object::repository &ts,
|
|
const std::string &table_name,
|
|
bool force_lazy,
|
|
bool has_many_to_many = false);
|
|
|
|
public:
|
|
template < class Type >
|
|
static std::vector<column> generate(const object::repository &scm, const 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;
|
|
access::process(gen, obj);
|
|
return columns;
|
|
}
|
|
|
|
static std::vector<column> generate(const object::repository &scm, const std::string &name, bool force_lazy = false);
|
|
|
|
template < class Type >
|
|
static std::vector<column> generate_has_many_to_many(const object::repository &scm, const 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;
|
|
access::process(gen, obj);
|
|
return columns;
|
|
}
|
|
|
|
template < class V >
|
|
void on_primary_key(const char *id, V &, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {
|
|
if (has_many_to_many_) {
|
|
return;
|
|
}
|
|
push(id);
|
|
}
|
|
void on_revision(const char *id, uint64_t &/*rev*/);
|
|
|
|
template<typename Type>
|
|
void on_attribute(const char *id, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) {
|
|
if (has_many_to_many_) {
|
|
return;
|
|
}
|
|
push(id);
|
|
}
|
|
|
|
template<class Pointer>
|
|
void on_belongs_to(const char *id, Pointer &x, const utils::foreign_attributes &attr) {
|
|
on_foreign_key(id, x, attr);
|
|
}
|
|
template<class Pointer>
|
|
void on_has_one(const char *id, Pointer &x, const utils::foreign_attributes &attr) {
|
|
on_foreign_key(id, x, attr);
|
|
}
|
|
|
|
template<class ContainerType>
|
|
void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &attr) {
|
|
if (has_many_to_many_) {
|
|
return;
|
|
}
|
|
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;
|
|
}
|
|
|
|
if (seen_tables.count(info->get().name()) == 0) {
|
|
auto it = seen_tables.insert(info->get().name()).first;
|
|
table_name_stack_.push(info.value().get().name());
|
|
typename ContainerType::value_type::value_type obj;
|
|
access::process(*this, obj);
|
|
table_name_stack_.pop();
|
|
seen_tables.erase(it);
|
|
}
|
|
}
|
|
|
|
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 (!has_many_to_many_) {
|
|
return;
|
|
}
|
|
push(join_column);
|
|
push(inverse_join_column);
|
|
}
|
|
|
|
template<class ContainerType>
|
|
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
|
|
|
|
private:
|
|
template<class Pointer>
|
|
void on_foreign_key(const char *id, Pointer &, const utils::foreign_attributes &attr) {
|
|
if (has_many_to_many_) {
|
|
return;
|
|
}
|
|
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;
|
|
}
|
|
if (seen_tables.count(info->get().name()) == 0) {
|
|
auto it = seen_tables.insert(info->get().name()).first;
|
|
table_name_stack_.push(info.value().get().name());
|
|
typename Pointer::value_type obj;
|
|
access::process(*this, obj);
|
|
table_name_stack_.pop();
|
|
seen_tables.erase(it);
|
|
}
|
|
}
|
|
}
|
|
|
|
void push(const std::string &column_name);
|
|
|
|
private:
|
|
std::stack<std::string> table_name_stack_;
|
|
std::vector<column> &column_infos_;
|
|
std::unordered_set<std::string> seen_tables;
|
|
const object::repository &table_schema_;
|
|
int column_index{0};
|
|
bool force_lazy_{false};
|
|
bool has_many_to_many_{false};
|
|
};
|
|
|
|
}
|
|
|
|
#endif //QUERY_COLUMN_GENERATOR_HPP
|