has many fetch with join
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
#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/sql/interface/query_result_reader.hpp"
|
||||
|
||||
#include <stack>
|
||||
#include <string>
|
||||
|
||||
namespace matador::sql::internal {
|
||||
|
||||
class query_result_pk_resolver final {
|
||||
public:
|
||||
explicit query_result_pk_resolver(query_result_reader &reader) : reader_(reader) {}
|
||||
|
||||
template<class Type>
|
||||
utils::identifier discover(Type &obj) {
|
||||
column_index_ = reader_.start_column_index();
|
||||
pk_.clear();
|
||||
access::process(*this, obj);
|
||||
|
||||
return pk_;
|
||||
}
|
||||
|
||||
template<typename ValueType>
|
||||
void on_primary_key(const char *id, ValueType &/*value*/, std::enable_if_t<std::is_integral_v<ValueType> && !std::is_same_v<bool, ValueType>>* = nullptr) {
|
||||
if (!type_stack_.empty()) {
|
||||
return;
|
||||
}
|
||||
ValueType value;
|
||||
utils::data_type_traits<ValueType>::read_value(reader_, id, column_index_++, value);
|
||||
pk_ = value;
|
||||
}
|
||||
void on_primary_key(const char *id, std::string &value, size_t size);
|
||||
void on_revision(const char * /*id*/, unsigned long long &/*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<typename Pointer::value_type>(attr.fetch() ); }
|
||||
template < class Pointer >
|
||||
void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &attr) { on_foreign_key<typename Pointer::value_type>(attr.fetch() ); }
|
||||
|
||||
template<class ContainerType>
|
||||
void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class ContainerType>
|
||||
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<class ContainerType>
|
||||
void on_has_many_to_many(const char *id, ContainerType &c, const utils::foreign_attributes &/*attr*/) {}
|
||||
|
||||
private:
|
||||
template<class Type>
|
||||
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<std::type_index> type_stack_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_RESULT_PK_RESOLVER_HPP
|
||||
Reference in New Issue
Block a user