added assignment from value, conversion to database_type and appropriate tests

This commit is contained in:
2026-04-05 12:48:13 +02:00
parent 9e4660c4ee
commit 1806e2670c
7 changed files with 170 additions and 5 deletions
+2
View File
@@ -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";
}
+70 -1
View File
@@ -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_;
}