@@ -155,6 +155,100 @@ public T decode(ByteBuf buffer, PostgresTypeIdentifier dataType, Format format,
155
155
156
156
}
157
157
158
+ static class EnumStringCodec implements Codec <String >, CodecMetadata {
159
+
160
+ private final ByteBufAllocator byteBufAllocator ;
161
+
162
+ private final int oid ;
163
+
164
+ EnumStringCodec (ByteBufAllocator byteBufAllocator , int oid ) {
165
+ this .oid = oid ;
166
+ this .byteBufAllocator = byteBufAllocator ;
167
+ }
168
+
169
+ @ Override
170
+ public boolean canDecode (int dataType , Format format , Class <?> type ) {
171
+ Assert .requireNonNull (type , "type must not be null" );
172
+ return type .isAssignableFrom (String .class ) && dataType == this .oid ;
173
+ }
174
+
175
+ @ Override
176
+ public boolean canEncode (Object value ) {
177
+ Assert .requireNonNull (value , "value must not be null" );
178
+ return value instanceof String ;
179
+ }
180
+
181
+ @ Override
182
+ public boolean canEncodeNull (Class <?> type ) {
183
+ Assert .requireNonNull (type , "type must not be null" );
184
+ return String .class .equals (type );
185
+ }
186
+
187
+ @ Override
188
+ public String decode (@ Nullable ByteBuf buffer , int dataType , Format format , Class <? extends String > type ) {
189
+ Assert .requireNonNull (buffer , "byteBuf must not be null" );
190
+ return ByteBufUtils .decode (buffer );
191
+ }
192
+
193
+ @ Override
194
+ public EncodedParameter encode (Object value ) {
195
+ return encode (value , this .oid );
196
+ }
197
+
198
+ @ Override
199
+ public EncodedParameter encode (Object value , int dataType ) {
200
+ Assert .requireNonNull (value , "value must not be null" );
201
+ return new EncodedParameter (
202
+ FORMAT_TEXT ,
203
+ dataType ,
204
+ Mono .fromSupplier (() -> ByteBufUtils .encode (this .byteBufAllocator , ((String ) value ))
205
+ ));
206
+ }
207
+
208
+ @ Override
209
+ public EncodedParameter encodeNull () {
210
+ return new EncodedParameter (Format .FORMAT_BINARY , this .oid , NULL_VALUE );
211
+ }
212
+
213
+ @ Override
214
+ public Class <?> type () {
215
+ return String .class ;
216
+ }
217
+
218
+ @ Override
219
+ public Iterable <PostgresTypeIdentifier > getDataTypes () {
220
+ return Collections .singleton (AbstractCodec .getDataType (this .oid ));
221
+ }
222
+
223
+ }
224
+
225
+ static class EnumStringArrayCodec extends EnumStringCodec implements ArrayCodecDelegate <String > {
226
+
227
+ private final PostgresTypeIdentifier arrayType ;
228
+
229
+ EnumStringArrayCodec (ByteBufAllocator byteBufAllocator , int oid , PostgresTypeIdentifier arrayType ) {
230
+ super (byteBufAllocator , oid );
231
+ this .arrayType = arrayType ;
232
+ }
233
+
234
+ @ Override
235
+ public String encodeToText (String value ) {
236
+ Assert .requireNonNull (value , "value must not be null" );
237
+ return ArrayCodec .escapeArrayElement (value );
238
+ }
239
+
240
+ @ Override
241
+ public PostgresTypeIdentifier getArrayDataType () {
242
+ return arrayType ;
243
+ }
244
+
245
+ @ Override
246
+ public String decode (ByteBuf buffer , PostgresTypeIdentifier dataType , Format format , Class <? extends String > type ) {
247
+ return decode (buffer , dataType .getObjectId (), format , type );
248
+ }
249
+
250
+ }
251
+
158
252
/**
159
253
* Builder for {@link CodecRegistrar} to register {@link EnumCodec} for one or more enum type mappings.
160
254
*/
@@ -228,20 +322,23 @@ public CodecRegistrar build() {
228
322
missing .remove (it .getName ());
229
323
logger .debug ("Registering codec for type '{}' with oid {} using Java enum type '{}'" , it .getName (), it .getOid (), enumClass .getName ());
230
324
231
- if (this .registrationPriority == RegistrationPriority .LAST ) {
232
-
233
- if (it .getArrayObjectId () > 0 ) {
234
- registry .addLast (new ArrayCodec (allocator , new EnumArrayCodec (allocator , enumClass , it .getOid (), it .asArrayType ()), enumClass ));
235
- }
325
+ EnumCodec enumCodec = new EnumCodec (allocator , enumClass , it .getOid ());
326
+ EnumStringCodec enumStringCodec = new EnumStringCodec (allocator , it .getOid ());
327
+ List <ArrayCodec > arrayCodecs = new ArrayList <>();
328
+ if (it .getArrayObjectId () > 0 ) {
329
+ PostgresTypes .PostgresType arrayType = it .asArrayType ();
330
+ arrayCodecs .add (new ArrayCodec (allocator , new EnumArrayCodec (allocator , enumClass , it .getOid (), arrayType ), enumClass ));
331
+ arrayCodecs .add (new ArrayCodec (allocator , new EnumStringArrayCodec (allocator , it .getOid (), arrayType ), String .class ));
332
+ }
236
333
237
- registry .addLast (new EnumCodec (allocator , enumClass , it .getOid ()));
334
+ if (this .registrationPriority == RegistrationPriority .LAST ) {
335
+ arrayCodecs .forEach (registry ::addLast );
336
+ registry .addLast (enumCodec );
337
+ registry .addLast (enumStringCodec );
238
338
} else {
239
-
240
- if (it .getArrayObjectId () > 0 ) {
241
- registry .addFirst (new ArrayCodec (allocator , new EnumArrayCodec (allocator , enumClass , it .getOid (), it .asArrayType ()), enumClass ));
242
- }
243
-
244
- registry .addFirst (new EnumCodec (allocator , enumClass , it .getOid ()));
339
+ arrayCodecs .forEach (registry ::addFirst );
340
+ registry .addFirst (enumCodec );
341
+ registry .addFirst (enumStringCodec );
245
342
}
246
343
}).doOnComplete (() -> {
247
344
0 commit comments