Skip to content

Commit 15c79fe

Browse files
committed
added support of bytea postgresql type and bytearray mapped oatpp type
1 parent c175890 commit 15c79fe

17 files changed

+873
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ build/
4545

4646
**/.DS_Store
4747

48+
# vscode
49+
.vscode/

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
add_library(${OATPP_THIS_MODULE_NAME}
33
oatpp-postgresql/mapping/type/Uuid.cpp
44
oatpp-postgresql/mapping/type/Uuid.hpp
5+
oatpp-postgresql/mapping/type/ByteArray.cpp
6+
oatpp-postgresql/mapping/type/ByteArray.hpp
57
oatpp-postgresql/mapping/Deserializer.cpp
68
oatpp-postgresql/mapping/Deserializer.hpp
79
oatpp-postgresql/mapping/Oid.hpp

src/oatpp-postgresql/Executor.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,16 @@ Executor::Executor(const std::shared_ptr<provider::Provider<Connection>>& connec
138138
, m_resultMapper(std::make_shared<mapping::ResultMapper>())
139139
{
140140
m_defaultTypeResolver->addKnownClasses({
141-
Uuid::Class::CLASS_ID
141+
Uuid::Class::CLASS_ID,
142+
ByteArray::Class::CLASS_ID
142143
});
143144
}
144145

145146
std::shared_ptr<data::mapping::TypeResolver> Executor::createTypeResolver() {
146147
auto typeResolver = std::make_shared<data::mapping::TypeResolver>();
147148
typeResolver->addKnownClasses({
148-
Uuid::Class::CLASS_ID
149+
Uuid::Class::CLASS_ID,
150+
ByteArray::Class::CLASS_ID
149151
});
150152
return typeResolver;
151153
}

src/oatpp-postgresql/Types.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define oatpp_postgresql_Types_hpp
2727

2828
#include "mapping/type/Uuid.hpp"
29+
#include "mapping/type/ByteArray.hpp"
2930

3031
namespace oatpp { namespace postgresql {
3132

@@ -34,6 +35,11 @@ namespace oatpp { namespace postgresql {
3435
*/
3536
typedef oatpp::data::mapping::type::Primitive<mapping::type::UuidObject, mapping::type::__class::Uuid> Uuid;
3637

38+
/**
39+
* ByteArray as oatpp-postgresql type.
40+
*/
41+
typedef oatpp::postgresql::mapping::type::ByteArray ByteArray;
42+
3743
}}
3844

3945
#endif // oatpp_postgresql_Types_hpp

src/oatpp-postgresql/mapping/Deserializer.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Deserializer::Deserializer() {
7575
////
7676

7777
setDeserializerMethod(postgresql::mapping::type::__class::Uuid::CLASS_ID, &Deserializer::deserializeUuid);
78-
78+
setDeserializerMethod(postgresql::mapping::type::__class::ByteArray::CLASS_ID, &Deserializer::deserializeByteArray);
7979
}
8080

8181
void Deserializer::setDeserializerMethod(const data::mapping::type::ClassId& classId, DeserializerMethod method) {
@@ -268,6 +268,7 @@ const oatpp::Type* Deserializer::guessAnyType(const InData& data) {
268268
case INT2OID: return oatpp::Int16::Class::getType();
269269
case INT4OID: return oatpp::Int32::Class::getType();
270270
case INT8OID: return oatpp::Int64::Class::getType();
271+
case BYTEAOID: return oatpp::postgresql::ByteArray::Class::getType();
271272

272273
case FLOAT4OID: return oatpp::Float32::Class::getType();
273274
case FLOAT8OID: return oatpp::Float64::Class::getType();
@@ -286,6 +287,8 @@ const oatpp::Type* Deserializer::guessAnyType(const InData& data) {
286287
case INT2ARRAYOID: return generateMultidimensionalArrayType<oatpp::Int16>(data);
287288
case INT4ARRAYOID: return generateMultidimensionalArrayType<oatpp::Int32>(data);
288289
case INT8ARRAYOID: return generateMultidimensionalArrayType<oatpp::Int64>(data);
290+
case BYTEAARRAYOID:
291+
return generateMultidimensionalArrayType<oatpp::postgresql::ByteArray>(data);
289292

290293
case FLOAT4ARRAYOID: return generateMultidimensionalArrayType<oatpp::Float32>(data);
291294
case FLOAT8ARRAYOID: return generateMultidimensionalArrayType<oatpp::Float64>(data);
@@ -333,6 +336,15 @@ oatpp::Void Deserializer::deserializeUuid(const Deserializer* _this, const InDat
333336

334337
}
335338

339+
oatpp::Void Deserializer::deserializeByteArray(const Deserializer*, const InData &data, const Type*) {
340+
341+
if (data.isNull || data.size == 0) {
342+
return postgresql::ByteArray();
343+
}
344+
345+
return postgresql::ByteArray((p_uint8)data.data, data.size);
346+
}
347+
336348
oatpp::Void Deserializer::deserializeSubArray(const Type* type,
337349
ArrayDeserializationMeta& meta,
338350
v_int32 dimension)

src/oatpp-postgresql/mapping/Deserializer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ class Deserializer {
108108

109109
static oatpp::Void deserializeUuid(const Deserializer* _this, const InData& data, const Type* type);
110110

111+
static oatpp::Void deserializeByteArray(const Deserializer *_this, const InData &data, const Type *type);
112+
111113
template<typename T>
112114
static const oatpp::Type* generateMultidimensionalArrayType(const InData& data) {
113115

src/oatpp-postgresql/mapping/Serializer.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void Serializer::setSerializerMethods() {
7373
////
7474

7575
setSerializerMethod(postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::serializeUuid);
76-
76+
setSerializerMethod(postgresql::mapping::type::__class::ByteArray::CLASS_ID, &Serializer::serializeByteArray);
7777
}
7878

7979
void Serializer::setTypeOidMethods() {
@@ -126,6 +126,8 @@ void Serializer::setTypeOidMethods() {
126126
setTypeOidMethod(postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::getTypeOid<UUIDOID>);
127127
setArrayTypeOidMethod(postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::getTypeOid<UUIDARRAYOID>);
128128

129+
setTypeOidMethod(postgresql::mapping::type::__class::ByteArray::CLASS_ID, &Serializer::getTypeOid<BYTEAOID>);
130+
setArrayTypeOidMethod(postgresql::mapping::type::__class::ByteArray::CLASS_ID, &Serializer::getTypeOid<BYTEAARRAYOID>);
129131
}
130132

131133
void Serializer::setSerializerMethod(const data::mapping::type::ClassId& classId, SerializerMethod method) {
@@ -462,6 +464,21 @@ void Serializer::serializeUuid(const Serializer* _this, OutputData& outData, con
462464
}
463465
}
464466

467+
void Serializer::serializeByteArray(const Serializer *_this, OutputData &outData, const oatpp::Void &polymorph) {
468+
469+
(void)_this;
470+
471+
if (polymorph) {
472+
auto v = polymorph.cast<oatpp::postgresql::ByteArray>();
473+
outData.data = (char*)(v->data());
474+
outData.dataSize = static_cast<int>(v->size());
475+
outData.dataFormat = 1;
476+
outData.oid = BYTEAARRAYOID;
477+
} else {
478+
serNull(outData);
479+
}
480+
}
481+
465482
const oatpp::Type* Serializer::getArrayItemTypeAndDimensions(const oatpp::Void& polymorph, std::vector<v_int32>& dimensions) {
466483

467484
oatpp::Void curr = polymorph;

src/oatpp-postgresql/mapping/Serializer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ class Serializer {
111111

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

114+
static void serializeByteArray(const Serializer *_this, OutputData &outData, const oatpp::Void &polymorph);
115+
114116
struct ArraySerializationMeta {
115117

116118
const Serializer* _this;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/***************************************************************************
2+
*
3+
* Project _____ __ ____ _ _
4+
* ( _ ) /__\ (_ _)_| |_ _| |_
5+
* )(_)( /(__)\ )( (_ _)(_ _)
6+
* (_____)(__)(__)(__) |_| |_|
7+
*
8+
*
9+
* Copyright 2018-present
10+
*
11+
* Licensed under the Apache License, Version 2.0 (the "License");
12+
* you may not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing, software
18+
* distributed under the License is distributed on an "AS IS" BASIS,
19+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
* See the License for the specific language governing permissions and
21+
* limitations under the License.
22+
*
23+
***************************************************************************/
24+
25+
#include "ByteArray.hpp"
26+
27+
#include "oatpp/core/data/stream/BufferStream.hpp"
28+
#include "oatpp/encoding/Base64.hpp"
29+
#include "oatpp/encoding/Hex.hpp"
30+
31+
namespace oatpp {
32+
namespace postgresql {
33+
namespace mapping {
34+
namespace type {
35+
36+
namespace __class {
37+
const ClassId ByteArray::CLASS_ID("oatpp::postgresql::ByteArray");
38+
}
39+
40+
ByteArray::ByteArray(const std::shared_ptr<std::vector<v_uint8>> &ptr, const _oatpp_Type *const valueType)
41+
: oatpp::data::mapping::type::ObjectWrapper<std::vector<v_uint8>, __class::ByteArray>(ptr) {
42+
if (type::__class::ByteArray::getType() != valueType) {
43+
throw std::runtime_error("Value type does not match");
44+
}
45+
}
46+
47+
ByteArray ByteArray::fromHexEncodedString(const String &hexEncodedString) {
48+
const v_buff_usize expected_bytes_number = hexEncodedString->size() / 2;
49+
50+
data::stream::BufferOutputStream stream(default_buffer_size);
51+
encoding::Hex::decode(&stream, hexEncodedString->data(), hexEncodedString->size(), true);
52+
if (stream.getCurrentPosition() != expected_bytes_number) {
53+
throw std::invalid_argument("[oatpp::postgresql::mapping::type::ByteArray::fromHexEncodedString(String)]:"
54+
"Error. Invalid string.");
55+
}
56+
57+
return ByteArray((const v_uint8 *)stream.getData(), expected_bytes_number);
58+
}
59+
60+
ByteArray ByteArray::fromBase64EncodedString(const String &base64EncodedString) {
61+
const auto tb = encoding::Base64::decode(base64EncodedString);
62+
return ByteArray(reinterpret_cast<const v_uint8 *>(tb->data()), tb->size());
63+
}
64+
65+
String toBase64EncodedString(const ByteArray &arr) {
66+
67+
if (arr->empty()) {
68+
return String();
69+
}
70+
return encoding::Base64::encode(arr->data(), arr->size());
71+
}
72+
73+
String toHexEncodedString(const ByteArray &arr) {
74+
75+
if (arr->empty()) {
76+
return String();
77+
}
78+
79+
oatpp::data::stream::BufferOutputStream stream(ByteArray::default_buffer_size);
80+
encoding::Hex::encode(&stream, arr->data(), arr->size());
81+
82+
return stream.toString();
83+
}
84+
} // namespace type
85+
} // namespace mapping
86+
} // namespace postgresql
87+
} // namespace oatpp

0 commit comments

Comments
 (0)