83 lines
2.0 KiB
C++
83 lines
2.0 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/sql/data_type_traits.hpp"
|
|
|
|
#include "matador/utils/types.hpp"
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
namespace matador::sql {
|
|
|
|
class field
|
|
{
|
|
public:
|
|
explicit field(std::string name);
|
|
template<typename Type>
|
|
field(std::string name, Type value, size_t size = 0, int index = -1)
|
|
: name_(std::move(name))
|
|
, size_(size)
|
|
, index_(index)
|
|
, value_(value)
|
|
, type_(data_type_traits<Type>::builtin_type(size)) {}
|
|
field(std::string name, data_type_t data_type, size_t size = 0, int index = -1);
|
|
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_ = data_type_traits<Type>::builtin_type(-1);
|
|
return *this;
|
|
}
|
|
|
|
[[nodiscard]] const std::string& name() const;
|
|
[[nodiscard]] size_t size() 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_varchar() const;
|
|
[[nodiscard]] bool is_blob() const;
|
|
[[nodiscard]] bool is_null() const;
|
|
[[nodiscard]] bool is_unknown() const;
|
|
|
|
friend std::ostream& operator<<(std::ostream &out, const field &col);
|
|
|
|
private:
|
|
template<class Operator>
|
|
void process(Operator &op)
|
|
{
|
|
op.on_attribute(name_.c_str(), value_, type_, size_);
|
|
}
|
|
|
|
private:
|
|
friend class record;
|
|
|
|
std::string name_;
|
|
int index_{-1};
|
|
any_type value_;
|
|
size_t size_{};
|
|
data_type_t type_;
|
|
};
|
|
|
|
}
|
|
#endif //QUERY_FIELD_HPP
|