115 lines
2.8 KiB
C++
115 lines
2.8 KiB
C++
#ifndef QUERY_FIELD_HPP
|
|
#define QUERY_FIELD_HPP
|
|
|
|
#include "matador/sql/any_type.hpp"
|
|
#include "matador/sql/any_type_to_visitor.hpp"
|
|
|
|
#include "matador/utils/types.hpp"
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
namespace matador::sql {
|
|
|
|
enum class field_type {
|
|
Integer,
|
|
FloatingPoint,
|
|
String,
|
|
Boolean,
|
|
Blob,
|
|
Null
|
|
};
|
|
|
|
template < typename Type, typename Enable = void >
|
|
struct field_traits;
|
|
|
|
template<typename Type>
|
|
struct field_traits<Type, typename std::enable_if<std::is_integral<Type>::value && !std::is_same<Type, bool>::value>::type>
|
|
{
|
|
static field_type type() { return field_type::Integer; }
|
|
};
|
|
|
|
template<typename Type>
|
|
struct field_traits<Type, typename std::enable_if<std::is_floating_point<Type>::value>::type>
|
|
{
|
|
static field_type type() { return field_type::FloatingPoint; }
|
|
};
|
|
|
|
template<typename Type>
|
|
struct field_traits<Type, typename std::enable_if<std::is_same<Type, std::string>::value>::type>
|
|
{
|
|
static field_type type() { return field_type::String; }
|
|
};
|
|
|
|
template<typename Type>
|
|
struct field_traits<Type, typename std::enable_if<std::is_same<Type, bool>::value>::type>
|
|
{
|
|
static field_type type() { return field_type::Boolean; }
|
|
};
|
|
|
|
template<typename Type>
|
|
struct field_traits<Type, typename std::enable_if<std::is_same<Type, nullptr_t>::value>::type>
|
|
{
|
|
static field_type type() { return field_type::Null; }
|
|
};
|
|
|
|
template<typename Type>
|
|
struct field_traits<Type, typename std::enable_if<std::is_same<Type, utils::blob>::value>::type>
|
|
{
|
|
static field_type type() { return field_type::Blob; }
|
|
};
|
|
|
|
class field
|
|
{
|
|
public:
|
|
explicit field(std::string name);
|
|
template<typename Type>
|
|
field(std::string name, Type value, int index = -1)
|
|
: name_(std::move(name))
|
|
, index_(index)
|
|
, value_(value)
|
|
, type_(field_traits<Type>::type()) {}
|
|
field(const field &x) = default;
|
|
field& operator=(const field &x) = default;
|
|
field(field &&x) noexcept;
|
|
field& operator=(field &&x) noexcept;
|
|
|
|
template<typename Type>
|
|
field& operator=(Type value) {
|
|
value_ = std::move(value);
|
|
type_ = field_traits<Type>::type();
|
|
return *this;
|
|
}
|
|
|
|
[[nodiscard]] const std::string& name() const;
|
|
[[nodiscard]] int index() const;
|
|
|
|
template<class Type>
|
|
std::optional<Type> as() const
|
|
{
|
|
any_type_to_visitor<Type> visitor;
|
|
std::visit(visitor, const_cast<any_type&>(value_));
|
|
return visitor.result;
|
|
}
|
|
|
|
[[nodiscard]] std::string str() const;
|
|
|
|
[[nodiscard]] bool is_integer() const;
|
|
[[nodiscard]] bool is_floating_point() const;
|
|
[[nodiscard]] bool is_bool() const;
|
|
[[nodiscard]] bool is_string() const;
|
|
[[nodiscard]] bool is_blob() const;
|
|
[[nodiscard]] bool is_null() const;
|
|
|
|
friend std::ostream& operator<<(std::ostream &out, const field &col);
|
|
|
|
private:
|
|
std::string name_;
|
|
int index_{-1};
|
|
any_type value_;
|
|
field_type type_;
|
|
};
|
|
|
|
}
|
|
#endif //QUERY_FIELD_HPP
|