78 lines
2.9 KiB
C++
78 lines
2.9 KiB
C++
#ifndef MATADOR_CONSTRAINTS_GENERATOR_HPP
|
|
#define MATADOR_CONSTRAINTS_GENERATOR_HPP
|
|
|
|
#include "matador/object/constraint.hpp"
|
|
|
|
#include "matador/utils/access.hpp"
|
|
#include "matador/utils/field_attributes.hpp"
|
|
#include "matador/utils/primary_key_attribute.hpp"
|
|
|
|
#include <list>
|
|
#include <typeindex>
|
|
|
|
namespace matador::object {
|
|
class object;
|
|
class basic_object_info;
|
|
class repository;
|
|
|
|
class constraints_generator final {
|
|
private:
|
|
constraints_generator(std::list<class constraint> &constraints, const repository &repo, object &obj);
|
|
|
|
public:
|
|
constraints_generator() = delete;
|
|
|
|
template < typename Type >
|
|
static std::list<class constraint> generate(const repository &repo, object &obj) {
|
|
Type t;
|
|
return generate(t, repo, obj);
|
|
}
|
|
|
|
template < typename Type >
|
|
static std::list<class constraint> generate(const Type& t, const repository &repo, object &obj) {
|
|
std::list<class constraint> constraints;
|
|
constraints_generator gen(constraints, repo, obj);
|
|
access::process(gen, t);
|
|
return constraints;
|
|
}
|
|
|
|
template < class Type >
|
|
void on_primary_key(const char *id, Type &/*x*/, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {
|
|
create_pk_constraint(id);
|
|
}
|
|
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
|
|
template<typename Type>
|
|
void on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr = utils::null_attributes) {
|
|
if (utils::is_constraint_set(attr.options() ,utils::constraints::Unique)) {
|
|
create_unique_constraint(id);
|
|
}
|
|
}
|
|
|
|
template<class Pointer>
|
|
void on_belongs_to(const char *id, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {
|
|
create_fk_constraint( typeid(typename Pointer::value_type), id);
|
|
}
|
|
template<class Pointer>
|
|
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
|
template<class ContainerType>
|
|
static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
|
template<class ContainerType>
|
|
static 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*/) {}
|
|
template<class ContainerType>
|
|
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
|
|
|
|
private:
|
|
void create_pk_constraint(const std::string& name) const;
|
|
void create_fk_constraint(const std::type_index& ti, const std::string& name) const;
|
|
void create_unique_constraint(const std::string& name) const;
|
|
|
|
[[nodiscard]] std::list<attribute>::iterator find_attribute_by_name(const std::string &name) const;
|
|
|
|
private:
|
|
std::list<class constraint> &constraints_;
|
|
const repository &repo_;
|
|
object &obj_;
|
|
};
|
|
}
|
|
|
|
#endif //MATADOR_CONSTRAINTS_GENERATOR_HPP
|