Skip to content

Commit a92d624

Browse files
committed
mapping::ResultMapper. Map KeyValue, map Object.
1 parent 6ed7282 commit a92d624

File tree

5 files changed

+98
-12
lines changed

5 files changed

+98
-12
lines changed

src/oatpp-postgresql/mapping/Deserializer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void Deserializer::setDeserializerMethod(const data::mapping::type::ClassId& cla
7878
}
7979
}
8080

81-
oatpp::Void Deserializer::deserialize(const InData& data, Type* type) const {
81+
oatpp::Void Deserializer::deserialize(const InData& data, const Type* type) const {
8282

8383
auto id = type->classId.id;
8484
auto& method = m_methods[id];
@@ -131,7 +131,7 @@ v_int64 Deserializer::deInt(const InData& data) {
131131
throw std::runtime_error("[oatpp::postgresql::mapping::Deserializer::deInt()]: Error. Unknown OID.");
132132
}
133133

134-
oatpp::Void Deserializer::deserializeString(const InData& data, Type* type) {
134+
oatpp::Void Deserializer::deserializeString(const InData& data, const Type* type) {
135135
(void) type;
136136
switch(data.oid) {
137137
case TEXTOID: return oatpp::String(data.data, data.size, true);

src/oatpp-postgresql/mapping/Deserializer.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Deserializer {
4141
v_buff_size size;
4242
};
4343
public:
44-
typedef oatpp::Void (*DeserializerMethod)(const InData& data, Type* type);
44+
typedef oatpp::Void (*DeserializerMethod)(const InData& data, const Type* type);
4545
private:
4646
static v_int16 deInt2(const InData& data);
4747
static v_int32 deInt4(const InData& data);
@@ -55,14 +55,14 @@ class Deserializer {
5555

5656
void setDeserializerMethod(const data::mapping::type::ClassId& classId, DeserializerMethod method);
5757

58-
oatpp::Void deserialize(const InData& data, Type* type) const;
58+
oatpp::Void deserialize(const InData& data, const Type* type) const;
5959

6060
public:
6161

62-
static oatpp::Void deserializeString(const InData& data, Type* type);
62+
static oatpp::Void deserializeString(const InData& data, const Type* type);
6363

6464
template<class IntWrapper>
65-
static oatpp::Void deserializeInt(const InData& data, Type* type) {
65+
static oatpp::Void deserializeInt(const InData& data, const Type* type) {
6666
(void) type;
6767
auto value = deInt(data);
6868
return IntWrapper((typename IntWrapper::UnderlyingType) value);

src/oatpp-postgresql/mapping/ResultMapper.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ ResultMapper::ResultMapper() {
4949
{
5050
m_readOneRowMethods.resize(data::mapping::type::ClassId::getClassCount(), nullptr);
5151

52-
setReadOneRowMethod(data::mapping::type::__class::AbstractObject::CLASS_ID, nullptr);
52+
setReadOneRowMethod(data::mapping::type::__class::AbstractObject::CLASS_ID, &ResultMapper::readRowAsObject);
5353

5454
setReadOneRowMethod(data::mapping::type::__class::AbstractVector::CLASS_ID, &ResultMapper::readRowAsList<oatpp::AbstractVector>);
5555
setReadOneRowMethod(data::mapping::type::__class::AbstractList::CLASS_ID, &ResultMapper::readRowAsList<oatpp::AbstractList>);
5656
setReadOneRowMethod(data::mapping::type::__class::AbstractUnorderedSet::CLASS_ID, &ResultMapper::readRowAsList<oatpp::AbstractUnorderedSet>);
5757

58-
setReadOneRowMethod(data::mapping::type::__class::AbstractPairList::CLASS_ID, nullptr);
59-
setReadOneRowMethod(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, nullptr);
58+
setReadOneRowMethod(data::mapping::type::__class::AbstractPairList::CLASS_ID, &ResultMapper::readRowAsKeyValue<oatpp::AbstractFields>);
59+
setReadOneRowMethod(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, &ResultMapper::readRowAsKeyValue<oatpp::AbstractUnorderedFields>);
6060
}
6161

6262
{
@@ -88,6 +88,38 @@ void ResultMapper::setReadRowsMethod(const data::mapping::type::ClassId& classId
8888
}
8989
}
9090

91+
oatpp::Void ResultMapper::readRowAsObject(ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 rowIndex) {
92+
93+
auto object = type->creator();
94+
const auto& fieldsMap = type->propertiesGetter()->getMap();
95+
96+
for(auto& f : fieldsMap) {
97+
98+
const std::string& fname = f.first;
99+
auto field = f.second;
100+
101+
oatpp::String key((const char*) fname.data(), fname.size(), false);
102+
auto colIt = dbData->colIndices.find(key);
103+
104+
if(colIt != dbData->colIndices.end()) {
105+
106+
auto i = colIt->second;
107+
108+
mapping::Deserializer::InData inData;
109+
inData.oid = PQftype(dbData->dbResult, i);
110+
inData.size = PQfsize(dbData->dbResult, i);
111+
inData.data = PQgetvalue(dbData->dbResult, rowIndex, i);
112+
113+
field->set(object.get(), _this->m_deserializer.deserialize(inData, field->type));
114+
115+
}
116+
117+
}
118+
119+
return object;
120+
121+
}
122+
91123
oatpp::Void ResultMapper::readOneRow(ResultData* dbData, const Type* type, v_int64 rowIndex) {
92124

93125
auto id = type->classId.id;
@@ -99,7 +131,13 @@ oatpp::Void ResultMapper::readOneRow(ResultData* dbData, const Type* type, v_int
99131

100132
throw std::runtime_error("[oatpp::postgresql::mapping::ResultMapper::readOneRow()]: "
101133
"Error. Invalid result container type. "
102-
"Allowed types are oatpp::Vector, oatpp::List, oatpp::UnorderedSet");
134+
"Allowed types are "
135+
"oatpp::Vector, "
136+
"oatpp::List, "
137+
"oatpp::UnorderedSet, "
138+
"oatpp::Fields, "
139+
"oatpp::UnorderedFields, "
140+
"oatpp::Object");
103141

104142
}
105143

src/oatpp-postgresql/mapping/ResultMapper.hpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,35 @@ class ResultMapper {
8181

8282
template<class Collection>
8383
static oatpp::Void readRowAsKeyValue(ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 rowIndex) {
84-
return nullptr;
84+
85+
auto mapWrapper = type->creator();
86+
auto polymorphicDispatcher = static_cast<const typename Collection::Class::AbstractPolymorphicDispatcher*>(type->polymorphicDispatcher);
87+
const auto& map = mapWrapper.template staticCast<Collection>();
88+
89+
auto it = type->params.begin();
90+
Type* keyType = *it ++;
91+
if(keyType->classId.id != oatpp::data::mapping::type::__class::String::CLASS_ID.id){
92+
throw std::runtime_error("[oatpp::postgresql::mapping::ResultMapper::readRowAsKeyValue()]: Invalid map key. Key should be String");
93+
}
94+
Type* valueType = *it;
95+
96+
for(v_int32 i = 0; i < dbData->colCount; i ++) {
97+
98+
mapping::Deserializer::InData inData;
99+
100+
inData.oid = PQftype(dbData->dbResult, i);
101+
inData.size = PQfsize(dbData->dbResult, i);
102+
inData.data = PQgetvalue(dbData->dbResult, rowIndex, i);
103+
104+
polymorphicDispatcher->addPolymorphicItem(mapWrapper, dbData->colNames[i], _this->m_deserializer.deserialize(inData, valueType));
105+
106+
}
107+
108+
return mapWrapper;
85109
}
86110

111+
static oatpp::Void readRowAsObject(ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 rowIndex);
112+
87113
template<class Collection>
88114
static oatpp::Void readRowsAsList(ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 count) {
89115

test/oatpp-postgresql/tests.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@
1515

1616
namespace {
1717

18+
#include OATPP_CODEGEN_BEGIN(DTO)
19+
20+
class Ints : public oatpp::DTO {
21+
22+
DTO_INIT(Ints, DTO)
23+
24+
DTO_FIELD(Int8, f_int8);
25+
DTO_FIELD(UInt8, f_uint8);
26+
27+
DTO_FIELD(Int16, f_int16);
28+
DTO_FIELD(UInt16, f_uint16);
29+
30+
DTO_FIELD(Int32, f_int32);
31+
DTO_FIELD(UInt32, f_uint32);
32+
33+
DTO_FIELD(Int64, f_int64);
34+
DTO_FIELD(UInt64, f_uint64);
35+
36+
};
37+
38+
#include OATPP_CODEGEN_END(DTO)
39+
1840
#include OATPP_CODEGEN_BEGIN(DbClient)
1941

2042
class MyClient : public oatpp::orm::DbClient {
@@ -85,7 +107,7 @@ class Test : public oatpp::test::UnitTest {
85107
OATPP_LOGD(TAG, "OK=%d, count=%d", res->isSuccess(), res->count());
86108

87109
{
88-
oatpp::Vector<oatpp::Vector<oatpp::Int64>> resObjType;
110+
oatpp::Vector<oatpp::Object<Ints>> resObjType;
89111
oatpp::Void polymorph(resObjType.valueType);
90112

91113
res->fetch(polymorph, res->count());

0 commit comments

Comments
 (0)