#ifndef QUERY_RESULT_PK_RESOLVER_HPP #define QUERY_RESULT_PK_RESOLVER_HPP #include "matador/utils/access.hpp" #include "matador/utils/field_attributes.hpp" #include "matador/utils/foreign_attributes.hpp" #include "matador/utils/identifier.hpp" #include "matador/utils/primary_key_attribute.hpp" #include "matador/sql/interface/query_result_reader.hpp" #include #include namespace matador::sql::internal { class query_result_pk_resolver final { public: explicit query_result_pk_resolver(query_result_reader &reader) : reader_(reader) {} template utils::identifier discover(Type &obj) { column_index_ = reader_.start_column_index(); pk_.clear(); access::process(*this, obj); return pk_; } template void on_primary_key(const char *id, ValueType &/*value*/, const utils::primary_key_attribute& attr = utils::default_pk_attributes) { if (!type_stack_.empty()) { return; } ValueType value; utils::data_type_traits::read_value(reader_, id, column_index_++, value, attr.size()); pk_ = value; } void on_revision(const char * /*id*/, uint64_t &/*rev*/) { ++column_index_; } template < class Type > void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) { ++column_index_; } void on_attribute(const char *id, const utils::value &x, const utils::field_attributes &attr = utils::null_attributes); template < class Pointer > void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &attr) { on_foreign_key(attr.fetch() ); } template < class Pointer > void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &attr) { on_foreign_key(attr.fetch() ); } template static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {} template static 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 static void on_has_many_to_many(const char *id, ContainerType &c, const utils::foreign_attributes &/*attr*/) {} private: template void on_foreign_key(const utils::fetch_type fetch) { if (fetch == utils::fetch_type::LAZY) { ++column_index_; } else { const Type obj; type_stack_.push(typeid(Type)); access::process(*this, obj); type_stack_.pop(); } } private: size_t column_index_{}; utils::identifier pk_; query_result_reader &reader_; std::stack type_stack_; }; } #endif //QUERY_RESULT_PK_RESOLVER_HPP