112 lines
3.4 KiB
C++
112 lines
3.4 KiB
C++
#ifndef MATADOR_GENERATOR_HPP
|
|
#define MATADOR_GENERATOR_HPP
|
|
|
|
#include <matador/object/repository.hpp>
|
|
|
|
#include <matador/utils/placeholder.hpp>
|
|
|
|
#include <vector>
|
|
|
|
namespace matador::sql::generator {
|
|
template < class Type >
|
|
static std::vector<utils::placeholder> placeholder(const object::repository &repo);
|
|
template < class Type >
|
|
static std::vector<utils::placeholder> columns(const object::repository &repo);
|
|
template < class Type >
|
|
static std::vector<utils::placeholder> key_values(const object::repository &repo);
|
|
|
|
class placeholder_generator {
|
|
public:
|
|
|
|
template < class Type >
|
|
static std::vector<utils::placeholder> generate(const object::repository &repo, const bool force_lazy = false) {
|
|
const auto info = repo.info<Type>();
|
|
if (!info) {
|
|
return {};
|
|
}
|
|
std::vector<utils::placeholder> columns;
|
|
placeholder_generator gen(columns, repo, info.value().get().name(), force_lazy);
|
|
Type obj;
|
|
access::process(gen, obj);
|
|
return columns;
|
|
}
|
|
|
|
template < class Type >
|
|
static std::vector<utils::placeholder> 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<utils::placeholder> columns;
|
|
placeholder_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:
|
|
|
|
};
|
|
}
|
|
#endif //MATADOR_GENERATOR_HPP
|