Skip to content

Commit

Permalink
Make bytea as an alias type via string typedef
Browse files Browse the repository at this point in the history
It conflicts with array of char which exists in nature.
Complete describe types which was not described with their
arrays.
  • Loading branch information
thed636 committed Sep 19, 2018
1 parent 2649db9 commit 79075e6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
8 changes: 8 additions & 0 deletions include/ozo/binary_deserialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ struct recv_impl<pg::name> {
}
};

template <>
struct recv_impl<pg::bytea> {
template <typename M>
static istream& apply(istream& in, int32_t size, const oid_map_t<M>& oid_map, pg::bytea& out) {
return recv_impl<std::vector<char>>::apply(in, size, oid_map, out);
}
};

template <typename T, typename M, typename Out>
void recv(const value<T>& in, const oid_map_t<M>& oids, Out& out) {
if (recv_null(in.is_null(), out)) {
Expand Down
9 changes: 9 additions & 0 deletions include/ozo/binary_serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,13 @@ struct send_impl<pg::name> {
}
};


template <>
struct send_impl<pg::bytea> {
template <typename M>
static ostream& apply(ostream& out, const oid_map_t<M>& map, const pg::name& in) {
return send_impl<std::vector<char>>::apply(out, map, in);
}
};

} // namespace ozo
7 changes: 4 additions & 3 deletions include/ozo/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>, bytea)

} // namespace pg
} // namespace ozo
Expand Down Expand Up @@ -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<char>, "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>)

Expand Down
7 changes: 4 additions & 3 deletions tests/binary_deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> 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<char>& underlying = got;
EXPECT_EQ("test", std::string_view(std::data(underlying), std::size(underlying)));
}

TEST_F(recv, should_convert_TEXTOID_to_std_string) {
Expand Down

0 comments on commit 79075e6

Please sign in to comment.