82 lines
1.9 KiB
C++
82 lines
1.9 KiB
C++
#ifndef QUERY_VALUE_HPP
|
|
#define QUERY_VALUE_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>
|
|
|
|
namespace matador::sql {
|
|
|
|
namespace detail {
|
|
template<typename Type>
|
|
size_t determine_size(const Type &/*val*/)
|
|
{
|
|
return 0;
|
|
}
|
|
size_t determine_size(const std::string &val);
|
|
size_t determine_size(const char *val);
|
|
size_t determine_size(const utils::blob &val);
|
|
|
|
}
|
|
class value
|
|
{
|
|
public:
|
|
value() = default;
|
|
template<typename Type>
|
|
explicit value(Type value, size_t size = 0)
|
|
: size_(size)
|
|
, value_(value)
|
|
, type_(data_type_traits<Type>::builtin_type(size)) {}
|
|
explicit value(data_type_t data_type, size_t size = 0);
|
|
value(const value &x) = default;
|
|
value& operator=(const value &x) = default;
|
|
template<typename Type>
|
|
value& operator=(Type val)
|
|
{
|
|
value_ = val;
|
|
size_ = detail::determine_size(val);
|
|
type_ = data_type_traits<Type>::builtin_type(size_);
|
|
return *this;
|
|
}
|
|
value(value &&x) noexcept;
|
|
value& operator=(value &&x) noexcept;
|
|
|
|
template<class Type>
|
|
std::optional<Type> as() const
|
|
{
|
|
if (std::holds_alternative<Type>(value_)) {
|
|
return std::get<Type>(value_);
|
|
} else {
|
|
any_type_to_visitor<Type> visitor;
|
|
std::visit(visitor, const_cast<any_type &>(value_));
|
|
return visitor.result;
|
|
}
|
|
}
|
|
[[nodiscard]] std::string str() const;
|
|
|
|
[[nodiscard]] size_t size() const;
|
|
[[nodiscard]] data_type_t type() 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;
|
|
|
|
private:
|
|
any_type value_;
|
|
size_t size_{};
|
|
data_type_t type_{data_type_t::type_unknown};
|
|
|
|
};
|
|
|
|
}
|
|
#endif //QUERY_VALUE_HPP
|