Skip to content

added support of bytea postgresql type #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ build/

**/.DS_Store

# vscode
.vscode/
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
add_library(${OATPP_THIS_MODULE_NAME}
oatpp-postgresql/mapping/type/Uuid.cpp
oatpp-postgresql/mapping/type/Uuid.hpp
oatpp-postgresql/mapping/type/ByteArray.cpp
oatpp-postgresql/mapping/type/ByteArray.hpp
oatpp-postgresql/mapping/Deserializer.cpp
oatpp-postgresql/mapping/Deserializer.hpp
oatpp-postgresql/mapping/Oid.hpp
Expand Down
6 changes: 4 additions & 2 deletions src/oatpp-postgresql/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,16 @@ Executor::Executor(const std::shared_ptr<provider::Provider<Connection>>& connec
, m_resultMapper(std::make_shared<mapping::ResultMapper>())
{
m_defaultTypeResolver->addKnownClasses({
Uuid::Class::CLASS_ID
Uuid::Class::CLASS_ID,
ByteArray::Class::CLASS_ID
});
}

std::shared_ptr<data::mapping::TypeResolver> Executor::createTypeResolver() {
auto typeResolver = std::make_shared<data::mapping::TypeResolver>();
typeResolver->addKnownClasses({
Uuid::Class::CLASS_ID
Uuid::Class::CLASS_ID,
ByteArray::Class::CLASS_ID
});
return typeResolver;
}
Expand Down
6 changes: 6 additions & 0 deletions src/oatpp-postgresql/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define oatpp_postgresql_Types_hpp

#include "mapping/type/Uuid.hpp"
#include "mapping/type/ByteArray.hpp"

namespace oatpp { namespace postgresql {

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

/**
* ByteArray as oatpp-postgresql type.
*/
typedef oatpp::postgresql::mapping::type::ByteArray ByteArray;

}}

#endif // oatpp_postgresql_Types_hpp
14 changes: 13 additions & 1 deletion src/oatpp-postgresql/mapping/Deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Deserializer::Deserializer() {
////

setDeserializerMethod(postgresql::mapping::type::__class::Uuid::CLASS_ID, &Deserializer::deserializeUuid);

setDeserializerMethod(postgresql::mapping::type::__class::ByteArray::CLASS_ID, &Deserializer::deserializeByteArray);
}

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

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

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

}

oatpp::Void Deserializer::deserializeByteArray(const Deserializer*, const InData &data, const Type*) {

if (data.isNull || data.size == 0) {
return postgresql::ByteArray();
}

return postgresql::ByteArray((p_uint8)data.data, data.size);
}

oatpp::Void Deserializer::deserializeSubArray(const Type* type,
ArrayDeserializationMeta& meta,
v_int32 dimension)
Expand Down
2 changes: 2 additions & 0 deletions src/oatpp-postgresql/mapping/Deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class Deserializer {

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

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

template<typename T>
static const oatpp::Type* generateMultidimensionalArrayType(const InData& data) {

Expand Down
19 changes: 18 additions & 1 deletion src/oatpp-postgresql/mapping/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void Serializer::setSerializerMethods() {
////

setSerializerMethod(postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::serializeUuid);

setSerializerMethod(postgresql::mapping::type::__class::ByteArray::CLASS_ID, &Serializer::serializeByteArray);
}

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

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

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

void Serializer::serializeByteArray(const Serializer *_this, OutputData &outData, const oatpp::Void &polymorph) {

(void)_this;

if (polymorph) {
auto v = polymorph.cast<oatpp::postgresql::ByteArray>();
outData.data = (char*)(v->data());
outData.dataSize = static_cast<int>(v->size());
outData.dataFormat = 1;
outData.oid = BYTEAARRAYOID;
} else {
serNull(outData);
}
}

const oatpp::Type* Serializer::getArrayItemTypeAndDimensions(const oatpp::Void& polymorph, std::vector<v_int32>& dimensions) {

oatpp::Void curr = polymorph;
Expand Down
2 changes: 2 additions & 0 deletions src/oatpp-postgresql/mapping/Serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class Serializer {

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

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

struct ArraySerializationMeta {

const Serializer* _this;
Expand Down
87 changes: 87 additions & 0 deletions src/oatpp-postgresql/mapping/type/ByteArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/

#include "ByteArray.hpp"

#include "oatpp/core/data/stream/BufferStream.hpp"
#include "oatpp/encoding/Base64.hpp"
#include "oatpp/encoding/Hex.hpp"

namespace oatpp {
namespace postgresql {
namespace mapping {
namespace type {

namespace __class {
const ClassId ByteArray::CLASS_ID("oatpp::postgresql::ByteArray");
}

ByteArray::ByteArray(const std::shared_ptr<std::vector<v_uint8>> &ptr, const _oatpp_Type *const valueType)
: oatpp::data::mapping::type::ObjectWrapper<std::vector<v_uint8>, __class::ByteArray>(ptr) {
if (type::__class::ByteArray::getType() != valueType) {
throw std::runtime_error("Value type does not match");
}
}

ByteArray ByteArray::fromHexEncodedString(const String &hexEncodedString) {
const v_buff_usize expected_bytes_number = hexEncodedString->size() / 2;

data::stream::BufferOutputStream stream(default_buffer_size);
encoding::Hex::decode(&stream, hexEncodedString->data(), hexEncodedString->size(), true);
if (stream.getCurrentPosition() != expected_bytes_number) {
throw std::invalid_argument("[oatpp::postgresql::mapping::type::ByteArray::fromHexEncodedString(String)]:"
"Error. Invalid string.");
}

return ByteArray((const v_uint8 *)stream.getData(), expected_bytes_number);
}

ByteArray ByteArray::fromBase64EncodedString(const String &base64EncodedString) {
const auto tb = encoding::Base64::decode(base64EncodedString);
return ByteArray(reinterpret_cast<const v_uint8 *>(tb->data()), tb->size());
}

String toBase64EncodedString(const ByteArray &arr) {

if (arr->empty()) {
return String();
}
return encoding::Base64::encode(arr->data(), arr->size());
}

String toHexEncodedString(const ByteArray &arr) {

if (arr->empty()) {
return String();
}

oatpp::data::stream::BufferOutputStream stream(ByteArray::default_buffer_size);
encoding::Hex::encode(&stream, arr->data(), arr->size());

return stream.toString();
}
} // namespace type
} // namespace mapping
} // namespace postgresql
} // namespace oatpp
Loading