added conversion from/to blob

This commit is contained in:
Sascha Kuehl 2024-03-10 16:55:55 +01:00
parent dd94ed1020
commit 6bbc870362
3 changed files with 30 additions and 2 deletions

View File

@ -55,6 +55,12 @@ void convert(std::string &dest, SourceType source, typename std::enable_if<std::
} }
} }
template < typename SourceType >
void convert(utils::blob &dest, SourceType source, typename std::enable_if<!std::is_same<utils::blob, SourceType>::value>::type* = nullptr)
{
throw std::logic_error("couldn't convert value to matador::utils::blob");
}
void convert(std::string &dest, const char* source); void convert(std::string &dest, const char* source);
unsigned long long to_unsigned_long_long(const char *source); unsigned long long to_unsigned_long_long(const char *source);
@ -108,8 +114,11 @@ void convert(DestType &dest, bool source, typename std::enable_if<std::is_floati
template < typename DestType > template < typename DestType >
void convert(DestType &dest, const utils::blob &data) void convert(DestType &dest, const utils::blob &data)
{ {
throw std::logic_error("couldn't convert matador::utils::blob into destination type");
} }
void convert(utils::blob &dest, const utils::blob &data);
} }
#endif //QUERY_CONVERT_HPP #endif //QUERY_CONVERT_HPP

View File

@ -59,4 +59,9 @@ long double to_double(const char *source)
return result; return result;
} }
void convert(utils::blob &dest, const utils::blob &data)
{
dest = data;
}
} }

View File

@ -2,10 +2,10 @@
#include "matador/sql/field.hpp" #include "matador/sql/field.hpp"
using namespace matador::sql; using namespace matador;
TEST_CASE("Field test", "[field]") { TEST_CASE("Field test", "[field]") {
field f("name"); sql::field f("name");
REQUIRE(f.name() == "name"); REQUIRE(f.name() == "name");
REQUIRE(f.is_null()); REQUIRE(f.is_null());
@ -35,4 +35,18 @@ TEST_CASE("Field test", "[field]") {
auto bool_val = f.as<bool>(); auto bool_val = f.as<bool>();
REQUIRE(bool_val.has_value()); REQUIRE(bool_val.has_value());
REQUIRE(bool_val.value()); REQUIRE(bool_val.value());
f = utils::blob{ 7,8,6,5,4,3 };
REQUIRE(!f.is_null());
REQUIRE(!f.is_integer());
REQUIRE(!f.is_floating_point());
REQUIRE(f.is_blob());
REQUIRE(!f.is_bool());
REQUIRE(!f.is_string());
auto blob_val = f.as<utils::blob>();
REQUIRE(blob_val.has_value());
REQUIRE(blob_val.value() == utils::blob{ 7,8,6,5,4,3 });
REQUIRE_THROWS_AS(f.as<std::string>(), std::logic_error);
} }