@@ -50,46 +50,80 @@ ResultMapper::ResultMapper() {
50
50
{
51
51
m_readOneRowMethods.resize (data::mapping::type::ClassId::getClassCount (), nullptr );
52
52
53
- setReadOneRowMethod (data::mapping::type::__class::AbstractObject::CLASS_ID, &ResultMapper::readRowAsObject );
53
+ setReadOneRowMethod (data::mapping::type::__class::AbstractObject::CLASS_ID, &ResultMapper::readOneRowAsObject );
54
54
55
- setReadOneRowMethod (data::mapping::type::__class::AbstractVector::CLASS_ID, &ResultMapper::readRowAsList<oatpp::AbstractVector> );
56
- setReadOneRowMethod (data::mapping::type::__class::AbstractList::CLASS_ID, &ResultMapper::readRowAsList<oatpp::AbstractList> );
57
- setReadOneRowMethod (data::mapping::type::__class::AbstractUnorderedSet::CLASS_ID, &ResultMapper::readRowAsList<oatpp::AbstractUnorderedSet> );
55
+ setReadOneRowMethod (data::mapping::type::__class::AbstractVector::CLASS_ID, &ResultMapper::readOneRowAsCollection );
56
+ setReadOneRowMethod (data::mapping::type::__class::AbstractList::CLASS_ID, &ResultMapper::readOneRowAsCollection );
57
+ setReadOneRowMethod (data::mapping::type::__class::AbstractUnorderedSet::CLASS_ID, &ResultMapper::readOneRowAsCollection );
58
58
59
- setReadOneRowMethod (data::mapping::type::__class::AbstractPairList::CLASS_ID, &ResultMapper::readRowAsKeyValue<oatpp::AbstractFields> );
60
- setReadOneRowMethod (data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, &ResultMapper::readRowAsKeyValue<oatpp::AbstractUnorderedFields> );
59
+ setReadOneRowMethod (data::mapping::type::__class::AbstractPairList::CLASS_ID, &ResultMapper::readOneRowAsMap );
60
+ setReadOneRowMethod (data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, &ResultMapper::readOneRowAsMap );
61
61
}
62
62
63
63
{
64
64
m_readRowsMethods.resize (data::mapping::type::ClassId::getClassCount (), nullptr );
65
65
66
- setReadRowsMethod (data::mapping::type::__class::AbstractVector::CLASS_ID, &ResultMapper::readRowsAsList<oatpp::AbstractVector> );
67
- setReadRowsMethod (data::mapping::type::__class::AbstractList::CLASS_ID, &ResultMapper::readRowsAsList<oatpp::AbstractList> );
68
- setReadRowsMethod (data::mapping::type::__class::AbstractUnorderedSet::CLASS_ID, &ResultMapper::readRowsAsList<oatpp::AbstractUnorderedSet> );
66
+ setReadRowsMethod (data::mapping::type::__class::AbstractVector::CLASS_ID, &ResultMapper::readRowsAsCollection );
67
+ setReadRowsMethod (data::mapping::type::__class::AbstractList::CLASS_ID, &ResultMapper::readRowsAsCollection );
68
+ setReadRowsMethod (data::mapping::type::__class::AbstractUnorderedSet::CLASS_ID, &ResultMapper::readRowsAsCollection );
69
69
70
70
}
71
71
72
72
}
73
73
74
74
void ResultMapper::setReadOneRowMethod (const data::mapping::type::ClassId& classId, ReadOneRowMethod method) {
75
75
const v_uint32 id = classId.id ;
76
- if (id < m_readOneRowMethods.size ()) {
77
- m_readOneRowMethods[id] = method;
78
- } else {
79
- throw std::runtime_error (" [oatpp::postgresql::mapping::ResultMapper::setReadOneRowMethod()]: Error. Unknown classId" );
76
+ if (id >= m_readOneRowMethods.size ()) {
77
+ m_readOneRowMethods.resize (id + 1 , nullptr );
80
78
}
79
+ m_readOneRowMethods[id] = method;
81
80
}
82
81
83
82
void ResultMapper::setReadRowsMethod (const data::mapping::type::ClassId& classId, ReadRowsMethod method) {
84
83
const v_uint32 id = classId.id ;
85
- if (id < m_readRowsMethods.size ()) {
86
- m_readRowsMethods[id] = method;
87
- } else {
88
- throw std::runtime_error (" [oatpp::postgresql::mapping::ResultMapper::setReadRowsMethod()]: Error. Unknown classId" );
84
+ if (id >= m_readRowsMethods.size ()) {
85
+ m_readRowsMethods.resize (id + 1 , nullptr );
89
86
}
87
+ m_readRowsMethods[id] = method;
90
88
}
91
89
92
- oatpp::Void ResultMapper::readRowAsObject (ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 rowIndex) {
90
+ oatpp::Void ResultMapper::readOneRowAsCollection (ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 rowIndex) {
91
+
92
+ auto dispatcher = static_cast <const data::mapping::type::__class::Collection::PolymorphicDispatcher*>(type->polymorphicDispatcher );
93
+ auto collection = dispatcher->createObject ();
94
+
95
+ const Type* itemType = *type->params .begin ();
96
+
97
+ for (v_int32 i = 0 ; i < dbData->colCount ; i ++) {
98
+ mapping::Deserializer::InData inData (dbData->dbResult , rowIndex, i, dbData->typeResolver );
99
+ dispatcher->addItem (collection, _this->m_deserializer .deserialize (inData, itemType));
100
+ }
101
+
102
+ return collection;
103
+
104
+ }
105
+
106
+ oatpp::Void ResultMapper::readOneRowAsMap (ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 rowIndex) {
107
+
108
+ auto dispatcher = static_cast <const data::mapping::type::__class::Map::PolymorphicDispatcher*>(type->polymorphicDispatcher );
109
+ auto map = dispatcher->createObject ();
110
+
111
+ const Type* keyType = dispatcher->getKeyType ();
112
+ if (keyType->classId .id != oatpp::data::mapping::type::__class::String::CLASS_ID.id ){
113
+ throw std::runtime_error (" [oatpp::postgresql::mapping::ResultMapper::readOneRowAsMap()]: Invalid map key. Key should be String" );
114
+ }
115
+
116
+ const Type* valueType = map.getValueType ();
117
+ for (v_int32 i = 0 ; i < dbData->colCount ; i ++) {
118
+ mapping::Deserializer::InData inData (dbData->dbResult , rowIndex, i, dbData->typeResolver );
119
+ dispatcher->addItem (map, dbData->colNames [i], _this->m_deserializer .deserialize (inData, valueType));
120
+ }
121
+
122
+ return map;
123
+
124
+ }
125
+
126
+ oatpp::Void ResultMapper::readOneRowAsObject (ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 rowIndex) {
93
127
94
128
auto dispatcher = static_cast <const data::mapping::type::__class::AbstractObject::PolymorphicDispatcher*>(type->polymorphicDispatcher );
95
129
auto object = dispatcher->createObject ();
@@ -118,6 +152,28 @@ oatpp::Void ResultMapper::readRowAsObject(ResultMapper* _this, ResultData* dbDat
118
152
119
153
}
120
154
155
+ oatpp::Void ResultMapper::readRowsAsCollection (ResultMapper* _this, ResultData* dbData, const Type* type, v_int64 count) {
156
+
157
+ auto dispatcher = static_cast <const data::mapping::type::__class::Collection::PolymorphicDispatcher*>(type->polymorphicDispatcher );
158
+ auto collection = dispatcher->createObject ();
159
+
160
+ const Type* itemType = dispatcher->getItemType ();
161
+
162
+ auto leftCount = dbData->rowCount - dbData->rowIndex ;
163
+ auto wantToRead = count;
164
+ if (wantToRead > leftCount) {
165
+ wantToRead = leftCount;
166
+ }
167
+
168
+ for (v_int64 i = 0 ; i < wantToRead; i++) {
169
+ dispatcher->addItem (collection, _this->readOneRow (dbData, itemType, dbData->rowIndex ));
170
+ ++ dbData->rowIndex ;
171
+ }
172
+
173
+ return collection;
174
+
175
+ }
176
+
121
177
oatpp::Void ResultMapper::readOneRow (ResultData* dbData, const Type* type, v_int64 rowIndex) {
122
178
123
179
auto id = type->classId .id ;
0 commit comments