Skip to content

Commit 78387ad

Browse files
committed
Start working on array serialization
1 parent 7b3d20f commit 78387ad

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_library(${OATPP_THIS_MODULE_NAME}
44
oatpp-postgresql/mapping/type/Uuid.hpp
55
oatpp-postgresql/mapping/Deserializer.cpp
66
oatpp-postgresql/mapping/Deserializer.hpp
7+
oatpp-postgresql/mapping/PgArray.hpp
78
oatpp-postgresql/mapping/Oid.hpp
89
oatpp-postgresql/mapping/ResultMapper.cpp
910
oatpp-postgresql/mapping/ResultMapper.hpp

src/oatpp-postgresql/mapping/Deserializer.cpp

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

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

3031
#if defined(WIN32) || defined(_WIN32)
@@ -279,7 +280,7 @@ const oatpp::Type* Deserializer::guessAnyType(Oid oid) {
279280

280281
case TIMESTAMPOID: return oatpp::UInt64::Class::getType();
281282

282-
case FLOAT4ARRAYOID: return oatpp::Vector<Float32>::Class::getType();
283+
case FLOAT8ARRAYOID: return oatpp::Vector<Float64>::Class::getType();
283284

284285
case UUIDOID: return oatpp::postgresql::Uuid::Class::getType();
285286

@@ -316,18 +317,6 @@ oatpp::Void Deserializer::deserializeUuid(const Deserializer* _this, const InDat
316317

317318
}
318319

319-
// after https://stackoverflow.com/questions/4016412/postgresqls-libpq-encoding-for-binary-transport-of-array-data
320-
struct PgArray {
321-
int32_t ndim; // Number of dimensions
322-
int32_t _ign; // offset for data, removed by libpq
323-
Oid oid; // type of element in the array
324-
325-
// Start of array (1st dimension)
326-
int32_t size; // Number of elements
327-
int32_t index; // Index of first element
328-
int32_t elem; // Beginning of (size, value) elements
329-
};
330-
331320
oatpp::Void Deserializer::deserializeArray(const Deserializer* _this, const InData& data, const Type* type) {
332321

333322
(void) _this;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Created by dsmyth on 1/1/21.
3+
//
4+
5+
#ifndef oatpp_postgresql_mapping_PgArray_hpp
6+
#define oatpp_postgresql_mapping_PgArray_hpp
7+
8+
// after https://stackoverflow.com/questions/4016412/postgresqls-libpq-encoding-for-binary-transport-of-array-data
9+
struct PgArray {
10+
int32_t ndim; // Number of dimensions
11+
int32_t _ign; // offset for data, removed by libpq
12+
Oid oid; // type of element in the array
13+
14+
// Start of array (1st dimension)
15+
int32_t size; // Number of elements
16+
int32_t index; // Index of first element
17+
int32_t elem; // Beginning of (size, value) elements
18+
};
19+
20+
#endif // oatpp_postgresql_mapping_PgArray_hpp

src/oatpp-postgresql/mapping/Serializer.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ void Serializer::setSerializerMethods() {
6767
////
6868

6969
setSerializerMethod(postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::serializeUuid);
70+
setSerializerMethod(data::mapping::type::__class::AbstractVector::CLASS_ID, &Serializer::serializeArray);
7071

7172
}
7273

@@ -91,11 +92,12 @@ void Serializer::setTypeOidMethods() {
9192
setTypeOidMethod(data::mapping::type::__class::Float64::CLASS_ID, &Serializer::getTypeOid<FLOAT8OID>);
9293
setTypeOidMethod(data::mapping::type::__class::Boolean::CLASS_ID, &Serializer::getTypeOid<BOOLOID>);
9394

94-
setTypeOidMethod(data::mapping::type::__class::AbstractEnum::CLASS_ID, &Serializer::getEnumTypeOid);
95+
setTypeOidMethod(data::mapping::type::__class::AbstractVector::CLASS_ID, &Serializer::getTypeOid<FLOAT8ARRAYOID>);
9596

9697
////
9798

9899
setTypeOidMethod(postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::getTypeOid<UUIDOID>);
100+
setTypeOidMethod(data::mapping::type::__class::AbstractEnum::CLASS_ID, &Serializer::getEnumTypeOid);
99101

100102
}
101103

@@ -389,4 +391,19 @@ void Serializer::serializeUuid(const Serializer* _this, OutputData& outData, con
389391
}
390392
}
391393

394+
void Serializer::serializeArray(const Serializer* _this, OutputData& outData, const oatpp::Void& polymorph) {
395+
396+
(void) _this;
397+
398+
if(polymorph) {
399+
auto v = polymorph.staticCast<oatpp::Vector<Float64>>();
400+
outData.data = nullptr;
401+
outData.dataSize = 0;
402+
outData.dataFormat = 1;
403+
outData.oid = FLOAT8ARRAYOID;
404+
} else{
405+
serNull(outData);
406+
}
407+
}
408+
392409
}}}

src/oatpp-postgresql/mapping/Serializer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class Serializer {
9999

100100
static void serializeUuid(const Serializer* _this, OutputData& outData, const oatpp::Void& polymorph);
101101

102+
static void serializeArray(const Serializer* _this, OutputData& outData, const oatpp::Void& polymorph);
102103
private:
103104

104105
template<Oid OID>

0 commit comments

Comments
 (0)