From 79075e615a765fb0851cd68b59def703f8127df7 Mon Sep 17 00:00:00 2001 From: "Sergei L. Khandrikov" Date: Sun, 16 Sep 2018 22:17:05 +0300 Subject: [PATCH] Make bytea as an alias type via string typedef It conflicts with array of char which exists in nature. Complete describe types which was not described with their arrays. --- include/ozo/binary_deserialization.h | 8 ++++++++ include/ozo/binary_serialization.h | 9 +++++++++ include/ozo/type_traits.h | 7 ++++--- tests/binary_deserialization.cpp | 7 ++++--- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/include/ozo/binary_deserialization.h b/include/ozo/binary_deserialization.h index 67d70f59c..a1ca41256 100644 --- a/include/ozo/binary_deserialization.h +++ b/include/ozo/binary_deserialization.h @@ -134,6 +134,14 @@ struct recv_impl { } }; +template <> +struct recv_impl { + template + static istream& apply(istream& in, int32_t size, const oid_map_t& oid_map, pg::bytea& out) { + return recv_impl>::apply(in, size, oid_map, out); + } +}; + template void recv(const value& in, const oid_map_t& oids, Out& out) { if (recv_null(in.is_null(), out)) { diff --git a/include/ozo/binary_serialization.h b/include/ozo/binary_serialization.h index 4bdb231ea..18e290062 100644 --- a/include/ozo/binary_serialization.h +++ b/include/ozo/binary_serialization.h @@ -48,4 +48,13 @@ struct send_impl { } }; + +template <> +struct send_impl { + template + static ostream& apply(ostream& out, const oid_map_t& map, const pg::name& in) { + return send_impl>::apply(out, map, in); + } +}; + } // namespace ozo diff --git a/include/ozo/type_traits.h b/include/ozo/type_traits.h index 0811c39cb..7b4428ec2 100644 --- a/include/ozo/type_traits.h +++ b/include/ozo/type_traits.h @@ -351,6 +351,7 @@ constexpr auto size_of(const T& v) noexcept -> typename std::enable_if< namespace pg { BOOST_STRONG_TYPEDEF(std::string, name) +BOOST_STRONG_TYPEDEF(std::vector, bytea) } // namespace pg } // namespace ozo @@ -456,9 +457,9 @@ OZO_PG_DEFINE_TYPE_AND_ARRAY(smtp::message, "code.message", null_oid, null_oid, >{};\ } -OZO_PG_DEFINE_TYPE(bool, "bool", BOOLOID, bytes<1>) -OZO_PG_DEFINE_TYPE(char, "char", CHAROID, bytes<1>) -OZO_PG_DEFINE_TYPE(std::vector, "bytea", BYTEAOID, dynamic_size) +OZO_PG_DEFINE_TYPE_AND_ARRAY(bool, "bool", BOOLOID, 1000, bytes<1>) +OZO_PG_DEFINE_TYPE_AND_ARRAY(char, "char", CHAROID, 1002, bytes<1>) +OZO_PG_DEFINE_TYPE_AND_ARRAY(ozo::pg::bytea, "bytea", BYTEAOID, 1001, dynamic_size) OZO_PG_DEFINE_TYPE_AND_ARRAY(boost::uuids::uuid, "uuid", UUIDOID, 2951, bytes<16>) diff --git a/tests/binary_deserialization.cpp b/tests/binary_deserialization.cpp index 382a349f8..9758867ec 100644 --- a/tests/binary_deserialization.cpp +++ b/tests/binary_deserialization.cpp @@ -116,16 +116,17 @@ TEST_F(recv, should_convert_INT8OID_to_int64_t) { EXPECT_EQ(7, got); } -TEST_F(recv, should_convert_BYTEAOID_to_std_vector_of_char) { +TEST_F(recv, should_convert_BYTEAOID_to_pg_bytea) { const char* bytes = "test"; EXPECT_CALL(mock, field_type(_)).WillRepeatedly(Return(BYTEAOID)); EXPECT_CALL(mock, get_value(_, _)).WillRepeatedly(Return(bytes)); EXPECT_CALL(mock, get_length(_, _)).WillRepeatedly(Return(4)); EXPECT_CALL(mock, get_isnull(_, _)).WillRepeatedly(Return(false)); - std::vector got; + ozo::pg::bytea got; ozo::recv(value, oid_map, got); - EXPECT_EQ("test", std::string_view(std::data(got), std::size(got))); + std::vector& underlying = got; + EXPECT_EQ("test", std::string_view(std::data(underlying), std::size(underlying))); } TEST_F(recv, should_convert_TEXTOID_to_std_string) {