Skip to content

Commit f71a962

Browse files
committed
Merge branch 'AddFloatArrays' of github.com:dsmyth/oatpp-postgresql into AddFloatArrays
2 parents 4591a5e + 9a51260 commit f71a962

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/oatpp-postgresql/mapping/PgArray.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
struct PgElem {
1313
v_int32 size; // size of each element value (bytes)
14-
v_uint8 value[1]; // Beginning of value array -- dynamically sized
14+
v_uint8 value[]; // Beginning of value array -- dynamically sized
1515
};
1616

1717
// after https://stackoverflow.com/questions/4016412/postgresqls-libpq-encoding-for-binary-transport-of-array-data
@@ -29,7 +29,7 @@ struct PgArrayHeader {
2929
// Layout of Postgres array in memory
3030
struct PgArray {
3131
PgArrayHeader header;
32-
PgElem elem[1]; // Beginning of (size, value) elements
32+
PgElem elem[]; // Beginning of (size, value) elements
3333
};
3434

3535
template<typename T, int dim>

src/oatpp-postgresql/mapping/Serializer.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ void Serializer::serializeArray(const Serializer* _this, OutputData& outData, co
400400
auto v = polymorph.staticCast<oatpp::Vector<Float64>>();
401401

402402
// get size of header + vector size * sizeof each element (size + data)
403-
auto dataSize = sizeof(PgArrayHeader) + v->size() * sizeof(PgElem);
403+
auto dataSize = sizeof(PgArrayHeader) + v->size() * (sizeof(PgElem) + sizeof(v_float64));
404404
outData.dataBuffer.reset(new char[dataSize]);
405405
outData.dataSize = dataSize;
406406
outData.dataFormat = 1;
@@ -418,12 +418,16 @@ void Serializer::serializeArray(const Serializer* _this, OutputData& outData, co
418418
pgArray->header.index = 0;
419419

420420
// stuff in the elements in network order
421+
auto *elemBuff = reinterpret_cast<p_uint8>(pgArray->elem);
421422
for (int i=0; i < v->size(); i++) {
422-
pgArray->elem[i].size = htonl(sizeof(v_float64));
423+
*reinterpret_cast<p_uint32>(elemBuff) = htonl(sizeof(v_float64));
424+
elemBuff += sizeof(v_int32);
423425
v_float64 fValue = v->at(i);
424426
auto pVal = reinterpret_cast<p_int64>(&fValue);
425-
pgArray->elem[i].value[0] = htonl(*pVal >> 32);
426-
pgArray->elem[i].value[1] = htonl(*pVal & 0xFFFFFFFF);
427+
*reinterpret_cast<p_uint32>(elemBuff) = htonl(*pVal >> 32);
428+
elemBuff += sizeof(v_int32);
429+
*reinterpret_cast<p_uint32>(elemBuff) = htonl(*pVal & 0xFFFFFFFF);
430+
elemBuff += sizeof(v_int32);
427431
}
428432
} else{
429433
serNull(outData);

0 commit comments

Comments
 (0)