#ifndef QUERY_COLUMN_GENERATOR_HPP #define QUERY_COLUMN_GENERATOR_HPP #include "matador/sql/column.hpp" #include "matador/sql/types.hpp" #include "matador/utils/access.hpp" #include "matador/utils/field_attributes.hpp" #include #include namespace matador::sql { class table_repository; class fk_column_generator { public: fk_column_generator() = default; template column generate(const char *id, Type &x, const std::string &ref_table, const std::string &ref_column) { utils::access::process(*this, x); return column{id, type_, 0, ref_table, ref_column, { utils::constraints::FOREIGN_KEY }}; } template void on_primary_key(const char *, ValueType &/*pk*/, typename std::enable_if::value && !std::is_same::value>::type* = 0) { type_ = data_type_traits::builtin_type(0); } void on_primary_key(const char * /*id*/, std::string &/*pk*/, size_t size); void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {} template < class Type > void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {} void on_attribute(const char * /*id*/, char * /*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {} template void on_belongs_to(const char * /*id*/, Pointer &/*x*/, utils::cascade_type) {} template void on_has_one(const char * /*id*/, Pointer &/*x*/, utils::cascade_type) {} template void on_has_many(const char *, ContainerType &, const char *, const char *, utils::cascade_type) {} template void on_has_many(const char *, ContainerType &, utils::cascade_type) {} private: data_type_t type_{}; }; class column_generator { private: column_generator(std::vector &columns, const table_repository &repo); public: ~column_generator() = default; template < class Type > static std::vector generate(const table_repository &repo) { std::vector columns; column_generator gen(columns, repo); Type obj; matador::utils::access::process(gen, obj); return std::move(columns); } template < class V > void on_primary_key(const char *, V &x, typename std::enable_if::value && !std::is_same::value>::type* = 0); void on_primary_key(const char *id, std::string &pk, size_t size); void on_revision(const char *id, unsigned long long &rev); template void on_attribute(const char *id, Type &x, const utils::field_attributes &attr = utils::not_null_attributes); template void on_attribute(const char *id, std::optional &x, const utils::field_attributes &attr = utils::null_attributes); template void on_belongs_to(const char *id, Pointer &x, utils::cascade_type) { const auto [ref_table, ref_column] = determine_foreign_ref(std::type_index(typeid(typename Pointer::value_type))); columns_.push_back(fk_column_generator_.generate(id, *x, ref_table, ref_column)); } template void on_has_one(const char *id, Pointer &x, utils::cascade_type) { const auto [ref_table, ref_column] = determine_foreign_ref(std::type_index(typeid(typename Pointer::value_type))); columns_.push_back(fk_column_generator_.generate(id, *x, ref_table, ref_column)); } template void on_has_many(const char *, ContainerType &, const char *, const char *, utils::cascade_type) {} template void on_has_many(const char *, ContainerType &, utils::cascade_type) {} private: std::pair determine_foreign_ref(const std::type_index &ti); private: size_t index_ = 0; std::vector &columns_; const table_repository &repo_; fk_column_generator fk_column_generator_; }; template void column_generator::on_primary_key(const char *id, V &x, typename std::enable_if::value && !std::is_same::value>::type*) { on_attribute(id, x, { utils::constraints::PRIMARY_KEY | utils::constraints::NOT_NULL }); } template void column_generator::on_attribute(const char *id, Type &x, const utils::field_attributes &attr) { if (attr.options() == utils::constraints::NONE) { columns_.push_back(column{id, x, { attr.size(), utils::constraints::NOT_NULL}}); } else { columns_.emplace_back(id, x, attr); } } template void column_generator::on_attribute(const char *id, std::optional &x, const utils::field_attributes &attr) { columns_.emplace_back(id, data_type_traits::builtin_type(attr.size()), attr); } } #endif //QUERY_COLUMN_GENERATOR_HPP