query/include/matador/sql/value_extractor.hpp

73 lines
2.2 KiB
C++

#ifndef QUERY_VALUE_EXTRACTOR_HPP
#define QUERY_VALUE_EXTRACTOR_HPP
#include "matador/sql/fk_value_extractor.hpp"
#include "matador/sql/types.hpp"
#include <vector>
namespace matador::sql {
class value_extractor
{
private:
explicit value_extractor(std::vector<any_type> &values);
public:
~value_extractor() = default;
template < class Type >
static std::vector<any_type> extract(const Type &type)
{
std::vector<any_type> values;
value_extractor gen(values);
matador::utils::access::process(gen, type);
return std::move(values);
}
template<typename ValueType>
void on_primary_key(const char *, ValueType &x, typename std::enable_if<std::is_integral<ValueType>::value && !std::is_same<bool, ValueType>::value>::type* = 0)
{
append(x);
}
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 *, Type &x, const utils::field_attributes &/*attr*/ = utils::null_attributes)
{
append(x);
}
void on_attribute(const char *id, char *x, const utils::field_attributes &/*attr*/ = utils::null_attributes);
void on_attribute(const char *id, std::string &x, const utils::field_attributes &/*attr*/ = utils::null_attributes);
template<class Type, template < class ... > class Pointer>
void on_belongs_to(const char * /*id*/, Pointer<Type> &x, utils::cascade_type)
{
values_.emplace_back(fk_value_extractor_.extract(*x));
}
template<class Type, template < class ... > class Pointer>
void on_has_one(const char * /*id*/, Pointer<Type> &x, utils::cascade_type)
{
values_.emplace_back(fk_value_extractor_.extract(*x));
}
template<class ContainerType>
void on_has_many(const char *, ContainerType &, const char *, const char *, utils::cascade_type) {}
template<class ContainerType>
void on_has_many(const char *, ContainerType &, utils::cascade_type) {}
private:
template<typename Type>
void append(Type &value)
{
values_.emplace_back(data_type_traits<Type>::create_value(value));
}
private:
detail::fk_value_extractor fk_value_extractor_;
std::vector<any_type> &values_;
};
}
#endif //QUERY_VALUE_EXTRACTOR_HPP