167 lines
3.2 KiB
C++
167 lines
3.2 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::Null;
|
|
}
|
|
|
|
value &value::operator=(value &&x) noexcept
|
|
{
|
|
value_ = std::move(x.value_);
|
|
type_ = x.type_;
|
|
x.value_ = nullptr;
|
|
x.type_ = basic_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::Int8 && type_ <= basic_type::UInt64;
|
|
}
|
|
|
|
bool value::is_floating_point() const
|
|
{
|
|
return type_ == basic_type::Float || type_ == basic_type::Double;
|
|
}
|
|
|
|
bool value::is_bool() const
|
|
{
|
|
return type_ == basic_type::Boolean;
|
|
}
|
|
|
|
bool value::is_string() const
|
|
{
|
|
return type_ == basic_type::Text;
|
|
}
|
|
|
|
bool value::is_varchar() const
|
|
{
|
|
return type_ == basic_type::Varchar;
|
|
}
|
|
|
|
bool value::is_date() const
|
|
{
|
|
return type_ == basic_type::Date;
|
|
}
|
|
|
|
bool value::is_time() const
|
|
{
|
|
return type_ == basic_type::Time;
|
|
}
|
|
|
|
bool value::is_blob() const
|
|
{
|
|
return type_ == basic_type::Blob;
|
|
}
|
|
|
|
bool value::is_null() const
|
|
{
|
|
return type_ == basic_type::Null;
|
|
}
|
|
|
|
namespace detail {
|
|
void initialize_by_basic_type(const basic_type type, database_type &val) {
|
|
switch (type) {
|
|
case basic_type::Int8:
|
|
val.emplace<int8_t>();
|
|
break;
|
|
case basic_type::Int16:
|
|
val.emplace<int16_t>();
|
|
break;
|
|
case basic_type::Int32:
|
|
val.emplace<int32_t>();
|
|
break;
|
|
case basic_type::Int64:
|
|
val.emplace<int64_t>();
|
|
break;
|
|
case basic_type::UInt8:
|
|
val.emplace<uint8_t>();
|
|
break;
|
|
case basic_type::UInt16:
|
|
val.emplace<uint16_t>();
|
|
break;
|
|
case basic_type::UInt32:
|
|
val.emplace<uint32_t>();
|
|
break;
|
|
case basic_type::UInt64:
|
|
val.emplace<uint64_t>();
|
|
break;
|
|
case basic_type::Boolean:
|
|
val.emplace<bool>();
|
|
break;
|
|
case basic_type::Float:
|
|
val.emplace<float>();
|
|
break;
|
|
case basic_type::Double:
|
|
val.emplace<double>();
|
|
break;
|
|
case basic_type::Varchar:
|
|
case basic_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::Blob:
|
|
val.emplace<utils::blob>();
|
|
break;
|
|
default:
|
|
val.emplace<nullptr_t>();
|
|
}
|
|
|
|
}
|
|
}
|
|
} |