24
24
25
25
#include " Serializer.hpp"
26
26
27
+ #include " Oid.hpp"
27
28
#include " oatpp-postgresql/Types.hpp"
28
29
29
30
#if defined(WIN32) || defined(_WIN32)
35
36
namespace oatpp { namespace postgresql { namespace mapping {
36
37
37
38
Serializer::Serializer () {
39
+ setSerializerMethods ();
40
+ setTypeOidMethods ();
41
+ }
42
+
43
+ void Serializer::setSerializerMethods () {
38
44
39
45
m_methods.resize (data::mapping::type::ClassId::getClassCount (), nullptr );
40
46
41
47
setSerializerMethod (data::mapping::type::__class::String::CLASS_ID, &Serializer::serializeString);
42
- setSerializerMethod (data::mapping::type::__class::Any::CLASS_ID, nullptr );
43
48
44
49
setSerializerMethod (data::mapping::type::__class::Int8::CLASS_ID, &Serializer::serializeInt8);
45
50
setSerializerMethod (data::mapping::type::__class::UInt8::CLASS_ID, &Serializer::serializeUInt8);
@@ -57,19 +62,36 @@ Serializer::Serializer() {
57
62
setSerializerMethod (data::mapping::type::__class::Float64::CLASS_ID, &Serializer::serializeFloat64);
58
63
setSerializerMethod (data::mapping::type::__class::Boolean::CLASS_ID, &Serializer::serializeBoolean);
59
64
60
- setSerializerMethod (data::mapping::type::__class::AbstractObject::CLASS_ID, nullptr );
61
- setSerializerMethod (data::mapping::type::__class::AbstractEnum::CLASS_ID, nullptr );
65
+ // //
66
+
67
+ setSerializerMethod (postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::serializeUuid);
68
+
69
+ }
70
+
71
+ void Serializer::setTypeOidMethods () {
72
+
73
+ m_typeOidMethods.resize (data::mapping::type::ClassId::getClassCount (), nullptr );
74
+
75
+ setTypeOidMethod (data::mapping::type::__class::String::CLASS_ID, &Serializer::getTypeOid<TEXTOID>);
76
+
77
+ setTypeOidMethod (data::mapping::type::__class::Int8::CLASS_ID, &Serializer::getTypeOid<INT2OID>);
78
+ setTypeOidMethod (data::mapping::type::__class::UInt8::CLASS_ID, &Serializer::getTypeOid<INT2OID>);
62
79
63
- setSerializerMethod (data::mapping::type::__class::AbstractVector::CLASS_ID, nullptr );
64
- setSerializerMethod (data::mapping::type::__class::AbstractList::CLASS_ID, nullptr );
65
- setSerializerMethod (data::mapping::type::__class::AbstractUnorderedSet::CLASS_ID, nullptr );
80
+ setTypeOidMethod (data::mapping::type::__class::Int16::CLASS_ID, &Serializer::getTypeOid<INT2OID>);
81
+ setTypeOidMethod (data::mapping::type::__class::UInt16::CLASS_ID, &Serializer::getTypeOid<INT4OID>);
66
82
67
- setSerializerMethod (data::mapping::type::__class::AbstractPairList::CLASS_ID, nullptr );
68
- setSerializerMethod (data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, nullptr );
83
+ setTypeOidMethod (data::mapping::type::__class::Int32::CLASS_ID, &Serializer::getTypeOid<INT4OID>);
84
+ setTypeOidMethod (data::mapping::type::__class::UInt32::CLASS_ID, &Serializer::getTypeOid<INT8OID>);
85
+
86
+ setTypeOidMethod (data::mapping::type::__class::Int64::CLASS_ID, &Serializer::getTypeOid<INT8OID>);
87
+
88
+ setTypeOidMethod (data::mapping::type::__class::Float32::CLASS_ID, &Serializer::getTypeOid<FLOAT4OID>);
89
+ setTypeOidMethod (data::mapping::type::__class::Float64::CLASS_ID, &Serializer::getTypeOid<FLOAT8OID>);
90
+ setTypeOidMethod (data::mapping::type::__class::Boolean::CLASS_ID, &Serializer::getTypeOid<BOOLOID>);
69
91
70
92
// //
71
93
72
- setSerializerMethod (postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::serializeUuid );
94
+ setTypeOidMethod (postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::getTypeOid<UUIDOID> );
73
95
74
96
}
75
97
@@ -82,6 +104,15 @@ void Serializer::setSerializerMethod(const data::mapping::type::ClassId& classId
82
104
}
83
105
}
84
106
107
+ void Serializer::setTypeOidMethod (const data::mapping::type::ClassId& classId, TypeOidMethod method) {
108
+ const v_uint32 id = classId.id ;
109
+ if (id < m_methods.size ()) {
110
+ m_typeOidMethods[id] = method;
111
+ } else {
112
+ throw std::runtime_error (" [oatpp::postgresql::mapping::Serializer::setTypeOidMethod()]: Error. Unknown classId" );
113
+ }
114
+ }
115
+
85
116
void Serializer::serialize (OutputData& outData, const oatpp::Void& polymorph) const {
86
117
auto id = polymorph.valueType ->classId .id ;
87
118
auto & method = m_methods[id];
@@ -94,6 +125,20 @@ void Serializer::serialize(OutputData& outData, const oatpp::Void& polymorph) co
94
125
}
95
126
}
96
127
128
+ Oid Serializer::getTypeOid (const oatpp::Type* type) const {
129
+
130
+ auto id = type->classId .id ;
131
+ auto & method = m_typeOidMethods[id];
132
+ if (method) {
133
+ return (*method)(this , type);
134
+ }
135
+
136
+ throw std::runtime_error (" [oatpp::postgresql::mapping::Serializer::getTypeOid()]: "
137
+ " Error. Can't derive OID for type '" + std::string (type->classId .name ) +
138
+ " '" );
139
+
140
+ }
141
+
97
142
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
98
143
// Serializer utility functions
99
144
@@ -141,6 +186,7 @@ void Serializer::serializeString(OutputData& outData, const oatpp::Void& polymor
141
186
outData.data = (char *)buff->getData ();
142
187
outData.dataSize = buff->getSize ();
143
188
outData.dataFormat = 1 ;
189
+ outData.oid = TEXTOID;
144
190
} else {
145
191
serNull (outData);
146
192
}
@@ -150,6 +196,7 @@ void Serializer::serializeInt8(OutputData& outData, const oatpp::Void& polymorph
150
196
if (polymorph) {
151
197
auto v = polymorph.staticCast <oatpp::Int8>();
152
198
serInt2 (outData, *v);
199
+ outData.oid = INT2OID;
153
200
} else {
154
201
serNull (outData);
155
202
}
@@ -159,6 +206,7 @@ void Serializer::serializeUInt8(OutputData& outData, const oatpp::Void& polymorp
159
206
if (polymorph) {
160
207
auto v = polymorph.staticCast <oatpp::UInt8>();
161
208
serInt2 (outData, *v);
209
+ outData.oid = INT2OID;
162
210
} else {
163
211
serNull (outData);
164
212
}
@@ -168,6 +216,7 @@ void Serializer::serializeInt16(OutputData& outData, const oatpp::Void& polymorp
168
216
if (polymorph) {
169
217
auto v = polymorph.staticCast <oatpp::Int16>();
170
218
serInt2 (outData, *v);
219
+ outData.oid = INT2OID;
171
220
} else {
172
221
serNull (outData);
173
222
}
@@ -177,6 +226,7 @@ void Serializer::serializeUInt16(OutputData& outData, const oatpp::Void& polymor
177
226
if (polymorph) {
178
227
auto v = polymorph.staticCast <oatpp::UInt16>();
179
228
serInt4 (outData, *v);
229
+ outData.oid = INT4OID;
180
230
} else {
181
231
serNull (outData);
182
232
}
@@ -186,6 +236,7 @@ void Serializer::serializeInt32(OutputData& outData, const oatpp::Void& polymorp
186
236
if (polymorph) {
187
237
auto v = polymorph.staticCast <oatpp::Int32>();
188
238
serInt4 (outData, *v);
239
+ outData.oid = INT4OID;
189
240
} else {
190
241
serNull (outData);
191
242
}
@@ -195,6 +246,7 @@ void Serializer::serializeUInt32(OutputData& outData, const oatpp::Void& polymor
195
246
if (polymorph) {
196
247
auto v = polymorph.staticCast <oatpp::UInt32>();
197
248
serInt8 (outData, *v);
249
+ outData.oid = INT8OID;
198
250
} else {
199
251
serNull (outData);
200
252
}
@@ -204,19 +256,21 @@ void Serializer::serializeInt64(OutputData& outData, const oatpp::Void& polymorp
204
256
if (polymorph) {
205
257
auto v = polymorph.staticCast <oatpp::Int64>();
206
258
serInt8 (outData, *v);
259
+ outData.oid = INT8OID;
207
260
} else {
208
261
serNull (outData);
209
262
}
210
263
}
211
264
212
265
void Serializer::serializeUInt64 (OutputData& outData, const oatpp::Void& polymorph) {
213
- serNull (outData );
266
+ throw std::runtime_error ( " [oatpp::postgresql::mapping::Serializer::serializeUInt64()]: Error. Not implemented! " );
214
267
}
215
268
216
269
void Serializer::serializeFloat32 (OutputData& outData, const oatpp::Void& polymorph) {
217
270
if (polymorph) {
218
271
auto v = polymorph.staticCast <oatpp::Float32>();
219
272
serInt4 (outData, *((p_int32) v.get ()));
273
+ outData.oid = FLOAT4OID;
220
274
} else {
221
275
serNull (outData);
222
276
}
@@ -226,6 +280,7 @@ void Serializer::serializeFloat64(OutputData& outData, const oatpp::Void& polymo
226
280
if (polymorph) {
227
281
auto v = polymorph.staticCast <oatpp::Float64>();
228
282
serInt8 (outData, *((p_int64) v.get ()));
283
+ outData.oid = FLOAT8OID;
229
284
} else {
230
285
serNull (outData);
231
286
}
@@ -239,6 +294,7 @@ void Serializer::serializeBoolean(OutputData& outData, const oatpp::Void& polymo
239
294
outData.dataSize = 1 ;
240
295
outData.dataFormat = 1 ;
241
296
outData.data [0 ] = (bool )v;
297
+ outData.oid = BOOLOID;
242
298
} else {
243
299
serNull (outData);
244
300
}
@@ -250,6 +306,7 @@ void Serializer::serializeUuid(OutputData& outData, const oatpp::Void& polymorph
250
306
outData.data = (char *) v->getData ();
251
307
outData.dataSize = v->getSize ();
252
308
outData.dataFormat = 1 ;
309
+ outData.oid = UUIDOID;
253
310
} else {
254
311
serNull (outData);
255
312
}
0 commit comments