query/source/core/utils/value.cpp

170 lines
3.4 KiB
C++

#include "matador/utils/value.hpp"
#include <cstring>
namespace matador::utils {
namespace detail {
void initialize_by_basic_type(basic_type type, database_type &val);
size_t determine_size(const std::string &val)
{
return val.size();
}
size_t determine_size(const char *val)
{
return strlen(val);
}
size_t determine_size(const blob &val)
{
return val.size();
}
}
value::value(const basic_type data_type, const size_t size)
: size_(size)
, type_(data_type)
{
initialize_by_basic_type(type_, value_);
}
value::value(value &&x) noexcept
: value_(std::move(x.value_))
, type_(x.type_)
{
x.value_ = nullptr;
x.type_ = basic_type::type_null;
}
value &value::operator=(value &&x) noexcept
{
value_ = std::move(x.value_);
type_ = x.type_;
x.value_ = nullptr;
x.type_ = basic_type::type_null;
return *this;
}
std::string value::str() const
{
return as<std::string>().value_or("");
}
size_t value::size() const
{
return size_;
}
basic_type value::type() const {
return type_;
}
void value::type(const basic_type t) {
type_ = t;
detail::initialize_by_basic_type(type_, value_);
}
bool value::is_integer() const
{
return type_ >= basic_type::type_int8 && type_ <= basic_type::type_uint64;
}
bool value::is_floating_point() const
{
return type_ == basic_type::type_float || type_ == basic_type::type_double;
}
bool value::is_bool() const
{
return type_ == basic_type::type_bool;
}
bool value::is_string() const
{
return type_ == basic_type::type_text;
}
bool value::is_varchar() const
{
return type_ == basic_type::type_varchar;
}
bool value::is_date() const
{
return type_ == basic_type::type_date;
}
bool value::is_time() const
{
return type_ == basic_type::type_time;
}
bool value::is_blob() const
{
return type_ == basic_type::type_blob;
}
bool value::is_null() const
{
return type_ == basic_type::type_null;
}
namespace detail {
void initialize_by_basic_type(const basic_type type, database_type &val) {
switch (type) {
case basic_type::type_int8:
val.emplace<int8_t>();
break;
case basic_type::type_int16:
val.emplace<int16_t>();
break;
case basic_type::type_int32:
val.emplace<int32_t>();
break;
case basic_type::type_int64:
val.emplace<int64_t>();
break;
case basic_type::type_uint8:
val.emplace<uint8_t>();
break;
case basic_type::type_uint16:
val.emplace<uint16_t>();
break;
case basic_type::type_uint32:
val.emplace<uint32_t>();
break;
case basic_type::type_uint64:
val.emplace<uint64_t>();
break;
case basic_type::type_bool:
val.emplace<bool>();
break;
case basic_type::type_float:
val.emplace<float>();
break;
case basic_type::type_double:
val.emplace<double>();
break;
case basic_type::type_varchar:
case basic_type::type_text:
val.emplace<std::string>();
break;
// case basic_type::type_date:
// val.emplace<date>();
// break;
// case basic_type::type_time:
// val.emplace<time>();
// break;
case basic_type::type_blob:
val.emplace<utils::blob>();
break;
default:
val.emplace<nullptr_t>();
}
}
}
}