added assignment from value, conversion to database_type and appropriate tests
This commit is contained in:
@@ -10,6 +10,8 @@ std::string utils_category_impl::message(const int ev) const {
|
||||
switch (static_cast<utils_error>(ev)) {
|
||||
case (utils_error::InvalidVersionString):
|
||||
return "Invalid version string";
|
||||
case utils_error::IdentifierTypeMismatch:
|
||||
return "Identifier type mismatch";
|
||||
default:
|
||||
return "Unknown error";
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "matador/utils/identifier.hpp"
|
||||
|
||||
#include "matador/utils/value.hpp"
|
||||
#include "matador/utils/errors.hpp"
|
||||
|
||||
#include <ostream>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace matador::utils {
|
||||
@@ -121,6 +123,73 @@ identifier & identifier::operator=(const char *value) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
result<identifier, error> identifier::from_value(const value &val) {
|
||||
identifier id;
|
||||
if (const auto result = id.assign(val); result.is_error()) {
|
||||
return failure(result.err());
|
||||
}
|
||||
|
||||
return ok(id);
|
||||
}
|
||||
|
||||
database_type identifier::to_database_type() const {
|
||||
return std::visit([](const auto &v) -> database_type {
|
||||
using T = std::decay_t<decltype(v)>;
|
||||
|
||||
if constexpr (std::is_same_v<T, std::monostate>) {
|
||||
return nullptr;
|
||||
} else if constexpr (std::is_same_v<T, std::string>) {
|
||||
return v;
|
||||
} else {
|
||||
return static_cast<T>(v);
|
||||
}
|
||||
}, value_);
|
||||
}
|
||||
|
||||
result<void, error> identifier::assign(const value &val) {
|
||||
switch (val.type()) {
|
||||
case basic_type::Null:
|
||||
value_ = std::monostate{};
|
||||
return ok<void>{};
|
||||
|
||||
case basic_type::Int8:
|
||||
if (auto v = val.as<int8_t>()) { value_ = *v; return ok<void>{}; }
|
||||
break;
|
||||
case basic_type::Int16:
|
||||
if (auto v = val.as<int16_t>()) { value_ = *v; return ok<void>{}; }
|
||||
break;
|
||||
case basic_type::Int32:
|
||||
if (auto v = val.as<int32_t>()) { value_ = *v; return ok<void>{}; }
|
||||
break;
|
||||
case basic_type::Int64:
|
||||
if (auto v = val.as<int64_t>()) { value_ = *v; return ok<void>{}; }
|
||||
break;
|
||||
|
||||
case basic_type::UInt8:
|
||||
if (auto v = val.as<uint8_t>()) { value_ = *v; return ok<void>{}; }
|
||||
break;
|
||||
case basic_type::UInt16:
|
||||
if (auto v = val.as<uint16_t>()) { value_ = *v; return ok<void>{}; }
|
||||
break;
|
||||
case basic_type::UInt32:
|
||||
if (auto v = val.as<uint32_t>()) { value_ = *v; return ok<void>{}; }
|
||||
break;
|
||||
case basic_type::UInt64:
|
||||
if (auto v = val.as<uint64_t>()) { value_ = *v; return ok<void>{}; }
|
||||
break;
|
||||
|
||||
case basic_type::Text:
|
||||
case basic_type::Varchar:
|
||||
if (auto v = val.as<std::string>()) { value_ = *v; return ok<void>{}; }
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return failure(error{utils_error::IdentifierTypeMismatch});
|
||||
}
|
||||
|
||||
bool identifier::operator==(const identifier &x) const {
|
||||
return value_ == x.value_;
|
||||
}
|
||||
|
||||
@@ -62,6 +62,10 @@ int field::index() const {
|
||||
return index_;
|
||||
}
|
||||
|
||||
utils::value field::value() const {
|
||||
return value_;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const field &col) {
|
||||
out << col.str();
|
||||
return out;
|
||||
|
||||
Reference in New Issue
Block a user