-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathResultMapper.cpp
More file actions
227 lines (169 loc) · 8.36 KB
/
ResultMapper.cpp
File metadata and controls
227 lines (169 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* 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 "ResultMapper.hpp"
#include "oatpp/base/Log.hpp"
#include "oatpp/utils/parser/Caret.hpp"
namespace oatpp { namespace postgresql { namespace mapping {
ResultMapper::ResultData::ResultData(PGresult* pDbResult, const std::shared_ptr<const data::mapping::TypeResolver>& pTypeResolver)
: dbResult(pDbResult)
, typeResolver(pTypeResolver)
{
rowIndex = 0;
rowCount = PQntuples(dbResult);
{
colCount = PQnfields(dbResult);
for (v_int32 i = 0; i < colCount; i++) {
oatpp::String colName = (const char*) PQfname(dbResult, i);
colNames.push_back(colName);
colIndices.insert({colName, i});
}
}
char* rowsAffectedStr = PQcmdTuples(dbResult); // will be freed when the result is freed
affectedRowCount = oatpp::utils::parser::Caret(rowsAffectedStr).parseUnsignedInt();
}
ResultMapper::ResultMapper() {
{
m_readOneRowMethods.resize(data::type::ClassId::getClassCount(), nullptr);
setReadOneRowMethod(data::type::__class::AbstractObject::CLASS_ID, &ResultMapper::readOneRowAsObject);
setReadOneRowMethod(data::type::__class::AbstractVector::CLASS_ID, &ResultMapper::readOneRowAsCollection);
setReadOneRowMethod(data::type::__class::AbstractList::CLASS_ID, &ResultMapper::readOneRowAsCollection);
setReadOneRowMethod(data::type::__class::AbstractUnorderedSet::CLASS_ID, &ResultMapper::readOneRowAsCollection);
setReadOneRowMethod(data::type::__class::AbstractPairList::CLASS_ID, &ResultMapper::readOneRowAsMap);
setReadOneRowMethod(data::type::__class::AbstractUnorderedMap::CLASS_ID, &ResultMapper::readOneRowAsMap);
}
{
m_readRowsMethods.resize(data::type::ClassId::getClassCount(), nullptr);
setReadRowsMethod(data::type::__class::AbstractVector::CLASS_ID, &ResultMapper::readRowsAsCollection);
setReadRowsMethod(data::type::__class::AbstractList::CLASS_ID, &ResultMapper::readRowsAsCollection);
setReadRowsMethod(data::type::__class::AbstractUnorderedSet::CLASS_ID, &ResultMapper::readRowsAsCollection);
}
}
void ResultMapper::setReadOneRowMethod(const data::type::ClassId& classId, ReadOneRowMethod method) {
const v_uint32 id = classId.id;
if(id >= m_readOneRowMethods.size()) {
m_readOneRowMethods.resize(id + 1, nullptr);
}
m_readOneRowMethods[id] = method;
}
void ResultMapper::setReadRowsMethod(const data::type::ClassId& classId, ReadRowsMethod method) {
const v_uint32 id = classId.id;
if(id >= m_readRowsMethods.size()) {
m_readRowsMethods.resize(id + 1, nullptr);
}
m_readRowsMethods[id] = method;
}
oatpp::Void ResultMapper::readOneRowAsCollection(ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 rowIndex) {
auto dispatcher = static_cast<const data::type::__class::Collection::PolymorphicDispatcher*>(type->polymorphicDispatcher);
auto collection = dispatcher->createObject();
const Type* itemType = *type->params.begin();
for(v_int32 i = 0; i < dbData->colCount; i ++) {
mapping::Deserializer::InData inData(dbData->dbResult, rowIndex, i, dbData->typeResolver);
dispatcher->addItem(collection, _this->m_deserializer.deserialize(inData, itemType));
}
return collection;
}
oatpp::Void ResultMapper::readOneRowAsMap(ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 rowIndex) {
auto dispatcher = static_cast<const data::type::__class::Map::PolymorphicDispatcher*>(type->polymorphicDispatcher);
auto map = dispatcher->createObject();
const Type* keyType = dispatcher->getKeyType();
if(keyType->classId.id != oatpp::data::type::__class::String::CLASS_ID.id){
throw std::runtime_error("[oatpp::postgresql::mapping::ResultMapper::readOneRowAsMap()]: Invalid map key. Key should be String");
}
const Type* valueType = dispatcher->getValueType();
for(v_int32 i = 0; i < dbData->colCount; i ++) {
mapping::Deserializer::InData inData(dbData->dbResult, rowIndex, i, dbData->typeResolver);
dispatcher->addItem(map, dbData->colNames[i], _this->m_deserializer.deserialize(inData, valueType));
}
return map;
}
oatpp::Void ResultMapper::readOneRowAsObject(ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 rowIndex) {
auto dispatcher = static_cast<const data::type::__class::AbstractObject::PolymorphicDispatcher*>(type->polymorphicDispatcher);
auto object = dispatcher->createObject();
const auto& fieldsMap = dispatcher->getProperties()->getMap();
for(v_int32 i = 0; i < dbData->colCount; i ++) {
auto it = fieldsMap.find(*dbData->colNames[i]);
if(it != fieldsMap.end()) {
auto field = it->second;
mapping::Deserializer::InData inData(dbData->dbResult, rowIndex, i, dbData->typeResolver);
field->set(static_cast<oatpp::BaseObject*>(object.get()), _this->m_deserializer.deserialize(inData, field->type));
} else {
OATPP_LOGe("[oatpp::postgresql::mapping::ResultMapper::readRowAsObject]",
"Error. The object of type '{}' has no field to map column '{}'.",
type->nameQualifier, dbData->colNames[i]->c_str());
throw std::runtime_error("[oatpp::postgresql::mapping::ResultMapper::readRowAsObject]: Error. "
"The object of type " + std::string(type->nameQualifier) +
" has no field to map column " + *dbData->colNames[i] + ".");
}
}
return object;
}
oatpp::Void ResultMapper::readRowsAsCollection(ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 count) {
auto dispatcher = static_cast<const data::type::__class::Collection::PolymorphicDispatcher*>(type->polymorphicDispatcher);
auto collection = dispatcher->createObject();
const Type* itemType = dispatcher->getItemType();
auto leftCount = dbData->rowCount - dbData->rowIndex;
auto wantToRead = count;
if(wantToRead > leftCount) {
wantToRead = leftCount;
}
for(v_int64 i = 0; i < wantToRead; i++) {
dispatcher->addItem(collection, _this->readOneRow(dbData, itemType, dbData->rowIndex));
++ dbData->rowIndex;
}
return collection;
}
oatpp::Void ResultMapper::readOneRow(ResultData* dbData, const Type* type, v_int64 rowIndex) {
auto id = type->classId.id;
auto& method = m_readOneRowMethods[id];
if(method) {
return (*method)(this, dbData, type, rowIndex);
}
auto* interpretation = type->findInterpretation(dbData->typeResolver->getEnabledInterpretations());
if(interpretation) {
return interpretation->fromInterpretation(readOneRow(dbData, interpretation->getInterpretationType(), rowIndex));
}
throw std::runtime_error("[oatpp::postgresql::mapping::ResultMapper::readOneRow()]: "
"Error. Invalid result container type. "
"Allowed types are "
"oatpp::Vector, "
"oatpp::List, "
"oatpp::UnorderedSet, "
"oatpp::Fields, "
"oatpp::UnorderedFields, "
"oatpp::Object");
}
oatpp::Void ResultMapper::readRows(ResultData* dbData, const Type* type, v_int64 count) {
if(count == -1) {
count = dbData->rowCount;
}
auto id = type->classId.id;
auto& method = m_readRowsMethods[id];
if(method) {
return (*method)(this, dbData, type, count);
}
throw std::runtime_error("[oatpp::postgresql::mapping::ResultMapper::readRows()]: "
"Error. Invalid result container type. "
"Allowed types are oatpp::Vector, oatpp::List, oatpp::UnorderedSet");
}
}}}