mapping. Add postgresql::Uuid Ser/De.

This commit is contained in:
lganzzzo 2020-08-19 02:46:58 +03:00
parent 5513b717a8
commit f1de170881
5 changed files with 72 additions and 7 deletions

View File

@ -25,6 +25,7 @@
#include "Deserializer.hpp"
#include "Oid.hpp"
#include "oatpp-postgresql/Types.hpp"
#if defined(WIN32) || defined(_WIN32)
#include <WinSock2.h>
@ -74,6 +75,10 @@ Deserializer::Deserializer() {
setDeserializerMethod(data::mapping::type::__class::AbstractPairList::CLASS_ID, nullptr);
setDeserializerMethod(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, nullptr);
////
setDeserializerMethod(postgresql::mapping::type::__class::Uuid::CLASS_ID, &Deserializer::deserializeUuid);
}
void Deserializer::setDeserializerMethod(const data::mapping::type::ClassId& classId, DeserializerMethod method) {
@ -151,6 +156,10 @@ oatpp::Void Deserializer::deserializeString(const Deserializer* _this, const InD
switch(data.oid) {
case TEXTOID:
case VARCHAROID: return oatpp::String(data.data, data.size, true);
case UUIDOID: {
postgresql::mapping::type::Uuid uuid((p_char8)data.data);
return uuid.toString();
}
}
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
}
oatpp::Void Deserializer::deserializeUuid(const Deserializer* _this, const InData& data, const Type* type) {
(void) _this;
(void) type;
return oatpp::String("<uuid>");
if(data.isNull) {
return oatpp::postgresql::Uuid();
}
return postgresql::Uuid((p_char8)data.data);
}
}}}

View File

@ -24,6 +24,8 @@
#include "Serializer.hpp"
#include "oatpp-postgresql/Types.hpp"
#if defined(WIN32) || defined(_WIN32)
#include <WinSock2.h>
#else
@ -65,6 +67,10 @@ Serializer::Serializer() {
setSerializerMethod(data::mapping::type::__class::AbstractPairList::CLASS_ID, nullptr);
setSerializerMethod(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, nullptr);
////
setSerializerMethod(postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::serializeUuid);
}
void Serializer::setSerializerMethod(const data::mapping::type::ClassId& classId, SerializerMethod method) {
@ -225,4 +231,15 @@ void Serializer::serializeFloat64(OutputData& outData, const oatpp::Void& polymo
}
}
void Serializer::serializeUuid(OutputData& outData, const oatpp::Void& polymorph) {
if(polymorph) {
auto v = polymorph.staticCast<postgresql::Uuid>();
outData.data = (const char*) v->getData();
outData.dataSize = v->getSize();
outData.dataFormat = 1;
} else{
serNull(outData);
}
}
}}}

View File

@ -137,6 +137,13 @@ public:
*/
static void serializeFloat64(OutputData& outData, const oatpp::Void& polymorph);
/**
* OID used - UUIDOID
* @param outData
* @param polymorph
*/
static void serializeUuid(OutputData& outData, const oatpp::Void& polymorph);
};
}}}

View File

@ -62,6 +62,11 @@ TypeMapper::TypeMapper() {
setTypeOid(data::mapping::type::__class::AbstractPairList::CLASS_ID, 0);
setTypeOid(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, 0);
////
setTypeOid(postgresql::mapping::type::__class::Uuid::CLASS_ID, UUIDOID);
}
{
@ -77,12 +82,12 @@ TypeMapper::TypeMapper() {
setOidType(BOOLOID, oatpp::Boolean::Class::getType());
////
setOidType(TIMESTAMPOID, oatpp::UInt64::Class::getType());
setOidType(UUIDOID, oatpp::postgresql::Uuid::Class::getType());
}
}

View File

@ -62,7 +62,18 @@ public:
PARAM(oatpp::String, email),
PREPARE(true))
QUERY(selectUsers, "SELECT * FROM EXAMPLE_USER", PREPARE(true))
QUERY(createUserUuid,
"INSERT INTO EXAMPLE_USER "
"(userId, login, password, email) VALUES "
"(:userId, :login, :password, :email) "
"RETURNING *;",
PARAM(oatpp::postgresql::Uuid, userId),
PARAM(oatpp::String, login),
PARAM(oatpp::String, password),
PARAM(oatpp::String, email),
PREPARE(true))
QUERY(selectUsers, "SELECT userId FROM EXAMPLE_USER", PREPARE(true))
QUERY(insertStrs,
"INSERT INTO test_strs "
@ -127,11 +138,19 @@ public:
void onRun() override {
{
v_char8 data[16] = "012345670123456";
oatpp::postgresql::mapping::type::Uuid uuid(data);
v_char8 data1[16] = "012345670123456";
v_char8 data2[16] = "012345670123457";
oatpp::postgresql::Uuid uuid1(data1);
oatpp::postgresql::Uuid uuid2(data2);
auto text = uuid.toString();
auto text = uuid1->toString();
OATPP_LOGD(TAG, "uuid='%s'", text->c_str());
if(uuid1 == uuid2) {
OATPP_LOGD(TAG, "eq");
} else {
OATPP_LOGD(TAG, "!eq");
}
}
oatpp::String connStr = "postgresql://postgres:db-pass@localhost:5432/postgres";