2424
2525#include " Serializer.hpp"
2626
27+ #include " Oid.hpp"
2728#include " oatpp-postgresql/Types.hpp"
2829
2930#if defined(WIN32) || defined(_WIN32)
3536namespace oatpp { namespace postgresql { namespace mapping {
3637
3738Serializer::Serializer () {
39+ setSerializerMethods ();
40+ setTypeOidMethods ();
41+ }
42+
43+ void Serializer::setSerializerMethods () {
3844
3945 m_methods.resize (data::mapping::type::ClassId::getClassCount (), nullptr );
4046
4147 setSerializerMethod (data::mapping::type::__class::String::CLASS_ID, &Serializer::serializeString);
42- setSerializerMethod (data::mapping::type::__class::Any::CLASS_ID, nullptr );
4348
4449 setSerializerMethod (data::mapping::type::__class::Int8::CLASS_ID, &Serializer::serializeInt8);
4550 setSerializerMethod (data::mapping::type::__class::UInt8::CLASS_ID, &Serializer::serializeUInt8);
@@ -57,19 +62,36 @@ Serializer::Serializer() {
5762 setSerializerMethod (data::mapping::type::__class::Float64::CLASS_ID, &Serializer::serializeFloat64);
5863 setSerializerMethod (data::mapping::type::__class::Boolean::CLASS_ID, &Serializer::serializeBoolean);
5964
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>);
6279
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>);
6682
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>);
6991
7092 // //
7193
72- setSerializerMethod (postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::serializeUuid );
94+ setTypeOidMethod (postgresql::mapping::type::__class::Uuid::CLASS_ID, &Serializer::getTypeOid<UUIDOID> );
7395
7496}
7597
@@ -82,6 +104,15 @@ void Serializer::setSerializerMethod(const data::mapping::type::ClassId& classId
82104 }
83105}
84106
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+
85116void Serializer::serialize (OutputData& outData, const oatpp::Void& polymorph) const {
86117 auto id = polymorph.valueType ->classId .id ;
87118 auto & method = m_methods[id];
@@ -94,6 +125,20 @@ void Serializer::serialize(OutputData& outData, const oatpp::Void& polymorph) co
94125 }
95126}
96127
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+
97142// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
98143// Serializer utility functions
99144
@@ -141,6 +186,7 @@ void Serializer::serializeString(OutputData& outData, const oatpp::Void& polymor
141186 outData.data = (char *)buff->getData ();
142187 outData.dataSize = buff->getSize ();
143188 outData.dataFormat = 1 ;
189+ outData.oid = TEXTOID;
144190 } else {
145191 serNull (outData);
146192 }
@@ -150,6 +196,7 @@ void Serializer::serializeInt8(OutputData& outData, const oatpp::Void& polymorph
150196 if (polymorph) {
151197 auto v = polymorph.staticCast <oatpp::Int8>();
152198 serInt2 (outData, *v);
199+ outData.oid = INT2OID;
153200 } else {
154201 serNull (outData);
155202 }
@@ -159,6 +206,7 @@ void Serializer::serializeUInt8(OutputData& outData, const oatpp::Void& polymorp
159206 if (polymorph) {
160207 auto v = polymorph.staticCast <oatpp::UInt8>();
161208 serInt2 (outData, *v);
209+ outData.oid = INT2OID;
162210 } else {
163211 serNull (outData);
164212 }
@@ -168,6 +216,7 @@ void Serializer::serializeInt16(OutputData& outData, const oatpp::Void& polymorp
168216 if (polymorph) {
169217 auto v = polymorph.staticCast <oatpp::Int16>();
170218 serInt2 (outData, *v);
219+ outData.oid = INT2OID;
171220 } else {
172221 serNull (outData);
173222 }
@@ -177,6 +226,7 @@ void Serializer::serializeUInt16(OutputData& outData, const oatpp::Void& polymor
177226 if (polymorph) {
178227 auto v = polymorph.staticCast <oatpp::UInt16>();
179228 serInt4 (outData, *v);
229+ outData.oid = INT4OID;
180230 } else {
181231 serNull (outData);
182232 }
@@ -186,6 +236,7 @@ void Serializer::serializeInt32(OutputData& outData, const oatpp::Void& polymorp
186236 if (polymorph) {
187237 auto v = polymorph.staticCast <oatpp::Int32>();
188238 serInt4 (outData, *v);
239+ outData.oid = INT4OID;
189240 } else {
190241 serNull (outData);
191242 }
@@ -195,6 +246,7 @@ void Serializer::serializeUInt32(OutputData& outData, const oatpp::Void& polymor
195246 if (polymorph) {
196247 auto v = polymorph.staticCast <oatpp::UInt32>();
197248 serInt8 (outData, *v);
249+ outData.oid = INT8OID;
198250 } else {
199251 serNull (outData);
200252 }
@@ -204,19 +256,21 @@ void Serializer::serializeInt64(OutputData& outData, const oatpp::Void& polymorp
204256 if (polymorph) {
205257 auto v = polymorph.staticCast <oatpp::Int64>();
206258 serInt8 (outData, *v);
259+ outData.oid = INT8OID;
207260 } else {
208261 serNull (outData);
209262 }
210263}
211264
212265void Serializer::serializeUInt64 (OutputData& outData, const oatpp::Void& polymorph) {
213- serNull (outData );
266+ throw std::runtime_error ( " [oatpp::postgresql::mapping::Serializer::serializeUInt64()]: Error. Not implemented! " );
214267}
215268
216269void Serializer::serializeFloat32 (OutputData& outData, const oatpp::Void& polymorph) {
217270 if (polymorph) {
218271 auto v = polymorph.staticCast <oatpp::Float32>();
219272 serInt4 (outData, *((p_int32) v.get ()));
273+ outData.oid = FLOAT4OID;
220274 } else {
221275 serNull (outData);
222276 }
@@ -226,6 +280,7 @@ void Serializer::serializeFloat64(OutputData& outData, const oatpp::Void& polymo
226280 if (polymorph) {
227281 auto v = polymorph.staticCast <oatpp::Float64>();
228282 serInt8 (outData, *((p_int64) v.get ()));
283+ outData.oid = FLOAT8OID;
229284 } else {
230285 serNull (outData);
231286 }
@@ -239,6 +294,7 @@ void Serializer::serializeBoolean(OutputData& outData, const oatpp::Void& polymo
239294 outData.dataSize = 1 ;
240295 outData.dataFormat = 1 ;
241296 outData.data [0 ] = (bool )v;
297+ outData.oid = BOOLOID;
242298 } else {
243299 serNull (outData);
244300 }
@@ -250,6 +306,7 @@ void Serializer::serializeUuid(OutputData& outData, const oatpp::Void& polymorph
250306 outData.data = (char *) v->getData ();
251307 outData.dataSize = v->getSize ();
252308 outData.dataFormat = 1 ;
309+ outData.oid = UUIDOID;
253310 } else {
254311 serNull (outData);
255312 }
0 commit comments