Skip to content

Commit f1de170

Browse files
committed
mapping. Add postgresql::Uuid Ser/De.
1 parent 5513b71 commit f1de170

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

src/oatpp-postgresql/mapping/Deserializer.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "Deserializer.hpp"
2626

2727
#include "Oid.hpp"
28+
#include "oatpp-postgresql/Types.hpp"
2829

2930
#if defined(WIN32) || defined(_WIN32)
3031
#include <WinSock2.h>
@@ -74,6 +75,10 @@ Deserializer::Deserializer() {
7475
setDeserializerMethod(data::mapping::type::__class::AbstractPairList::CLASS_ID, nullptr);
7576
setDeserializerMethod(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, nullptr);
7677

78+
////
79+
80+
setDeserializerMethod(postgresql::mapping::type::__class::Uuid::CLASS_ID, &Deserializer::deserializeUuid);
81+
7782
}
7883

7984
void Deserializer::setDeserializerMethod(const data::mapping::type::ClassId& classId, DeserializerMethod method) {
@@ -151,6 +156,10 @@ oatpp::Void Deserializer::deserializeString(const Deserializer* _this, const InD
151156
switch(data.oid) {
152157
case TEXTOID:
153158
case VARCHAROID: return oatpp::String(data.data, data.size, true);
159+
case UUIDOID: {
160+
postgresql::mapping::type::Uuid uuid((p_char8)data.data);
161+
return uuid.toString();
162+
}
154163
}
155164

156165
throw std::runtime_error("[oatpp::postgresql::mapping::Deserializer::deserializeString()]: Error. Unknown OID.");
@@ -197,8 +206,16 @@ oatpp::Void Deserializer::deserializeAny(const Deserializer* _this, const InData
197206
}
198207

199208
oatpp::Void Deserializer::deserializeUuid(const Deserializer* _this, const InData& data, const Type* type) {
209+
210+
(void) _this;
200211
(void) type;
201-
return oatpp::String("<uuid>");
212+
213+
if(data.isNull) {
214+
return oatpp::postgresql::Uuid();
215+
}
216+
217+
return postgresql::Uuid((p_char8)data.data);
218+
202219
}
203220

204221
}}}

src/oatpp-postgresql/mapping/Serializer.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#include "Serializer.hpp"
2626

27+
#include "oatpp-postgresql/Types.hpp"
28+
2729
#if defined(WIN32) || defined(_WIN32)
2830
#include <WinSock2.h>
2931
#else
@@ -65,6 +67,10 @@ Serializer::Serializer() {
6567
setSerializerMethod(data::mapping::type::__class::AbstractPairList::CLASS_ID, nullptr);
6668
setSerializerMethod(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, nullptr);
6769

70+
////
71+
72+
setSerializerMethod(postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::serializeUuid);
73+
6874
}
6975

7076
void Serializer::setSerializerMethod(const data::mapping::type::ClassId& classId, SerializerMethod method) {
@@ -225,4 +231,15 @@ void Serializer::serializeFloat64(OutputData& outData, const oatpp::Void& polymo
225231
}
226232
}
227233

234+
void Serializer::serializeUuid(OutputData& outData, const oatpp::Void& polymorph) {
235+
if(polymorph) {
236+
auto v = polymorph.staticCast<postgresql::Uuid>();
237+
outData.data = (const char*) v->getData();
238+
outData.dataSize = v->getSize();
239+
outData.dataFormat = 1;
240+
} else{
241+
serNull(outData);
242+
}
243+
}
244+
228245
}}}

src/oatpp-postgresql/mapping/Serializer.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ class Serializer {
137137
*/
138138
static void serializeFloat64(OutputData& outData, const oatpp::Void& polymorph);
139139

140+
/**
141+
* OID used - UUIDOID
142+
* @param outData
143+
* @param polymorph
144+
*/
145+
static void serializeUuid(OutputData& outData, const oatpp::Void& polymorph);
146+
140147
};
141148

142149
}}}

src/oatpp-postgresql/mapping/TypeMapper.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ TypeMapper::TypeMapper() {
6262

6363
setTypeOid(data::mapping::type::__class::AbstractPairList::CLASS_ID, 0);
6464
setTypeOid(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, 0);
65+
66+
////
67+
68+
setTypeOid(postgresql::mapping::type::__class::Uuid::CLASS_ID, UUIDOID);
69+
6570
}
6671

6772
{
@@ -77,12 +82,12 @@ TypeMapper::TypeMapper() {
7782

7883
setOidType(BOOLOID, oatpp::Boolean::Class::getType());
7984

85+
////
86+
8087
setOidType(TIMESTAMPOID, oatpp::UInt64::Class::getType());
8188

8289
setOidType(UUIDOID, oatpp::postgresql::Uuid::Class::getType());
8390

84-
85-
8691
}
8792

8893
}

test/oatpp-postgresql/tests.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,18 @@ class MyClient : public oatpp::orm::DbClient {
6262
PARAM(oatpp::String, email),
6363
PREPARE(true))
6464

65-
QUERY(selectUsers, "SELECT * FROM EXAMPLE_USER", PREPARE(true))
65+
QUERY(createUserUuid,
66+
"INSERT INTO EXAMPLE_USER "
67+
"(userId, login, password, email) VALUES "
68+
"(:userId, :login, :password, :email) "
69+
"RETURNING *;",
70+
PARAM(oatpp::postgresql::Uuid, userId),
71+
PARAM(oatpp::String, login),
72+
PARAM(oatpp::String, password),
73+
PARAM(oatpp::String, email),
74+
PREPARE(true))
75+
76+
QUERY(selectUsers, "SELECT userId FROM EXAMPLE_USER", PREPARE(true))
6677

6778
QUERY(insertStrs,
6879
"INSERT INTO test_strs "
@@ -127,11 +138,19 @@ class Test : public oatpp::test::UnitTest {
127138
void onRun() override {
128139

129140
{
130-
v_char8 data[16] = "012345670123456";
131-
oatpp::postgresql::mapping::type::Uuid uuid(data);
141+
v_char8 data1[16] = "012345670123456";
142+
v_char8 data2[16] = "012345670123457";
143+
oatpp::postgresql::Uuid uuid1(data1);
144+
oatpp::postgresql::Uuid uuid2(data2);
132145

133-
auto text = uuid.toString();
146+
auto text = uuid1->toString();
134147
OATPP_LOGD(TAG, "uuid='%s'", text->c_str());
148+
149+
if(uuid1 == uuid2) {
150+
OATPP_LOGD(TAG, "eq");
151+
} else {
152+
OATPP_LOGD(TAG, "!eq");
153+
}
135154
}
136155

137156
oatpp::String connStr = "postgresql://postgres:db-pass@localhost:5432/postgres";

0 commit comments

Comments
 (0)