27
27
import java .util .EnumSet ;
28
28
import java .util .Set ;
29
29
import java .util .stream .Collectors ;
30
+ import java .util .stream .Stream ;
30
31
31
32
import static io .r2dbc .postgresql .codec .PostgresqlObjectId .BPCHAR ;
32
33
import static io .r2dbc .postgresql .codec .PostgresqlObjectId .CHAR ;
@@ -48,22 +49,32 @@ final class StringCodec extends AbstractCodec<String> implements ArrayCodecDeleg
48
49
49
50
private final ByteBufAllocator byteBufAllocator ;
50
51
52
+ private final PostgresTypeIdentifier defaultType ;
53
+
54
+ private final PostgresTypeIdentifier arrayType ;
55
+
51
56
StringCodec (ByteBufAllocator byteBufAllocator ) {
57
+ this (byteBufAllocator , VARCHAR , VARCHAR_ARRAY );
58
+ }
59
+
60
+ StringCodec (ByteBufAllocator byteBufAllocator , PostgresTypeIdentifier defaultType , PostgresTypeIdentifier arrayType ) {
52
61
super (String .class );
53
62
this .byteBufAllocator = Assert .requireNonNull (byteBufAllocator , "byteBufAllocator must not be null" );
63
+ this .defaultType = Assert .requireNonNull (defaultType , "defaultType must not be null" );
64
+ this .arrayType = Assert .requireNonNull (arrayType , "arrayType must not be null" );
54
65
}
55
66
56
67
@ Override
57
68
public EncodedParameter encodeNull () {
58
- return createNull (FORMAT_TEXT , VARCHAR );
69
+ return createNull (FORMAT_TEXT , this . defaultType );
59
70
}
60
71
61
72
@ Override
62
73
boolean doCanDecode (PostgresqlObjectId type , Format format ) {
63
74
Assert .requireNonNull (format , "format must not be null" );
64
75
Assert .requireNonNull (type , "type must not be null" );
65
76
66
- return SUPPORTED_TYPES .contains (type );
77
+ return this . defaultType == type || SUPPORTED_TYPES .contains (type );
67
78
}
68
79
69
80
@ Override
@@ -75,7 +86,7 @@ String doDecode(ByteBuf buffer, PostgresTypeIdentifier dataType, @Nullable Forma
75
86
76
87
@ Override
77
88
EncodedParameter doEncode (String value ) {
78
- return doEncode (value , VARCHAR );
89
+ return doEncode (value , this . defaultType );
79
90
}
80
91
81
92
@ Override
@@ -94,12 +105,12 @@ public String encodeToText(String value) {
94
105
95
106
@ Override
96
107
public PostgresTypeIdentifier getArrayDataType () {
97
- return VARCHAR_ARRAY ;
108
+ return this . arrayType ;
98
109
}
99
110
100
111
@ Override
101
112
public Iterable <PostgresTypeIdentifier > getDataTypes () {
102
- return SUPPORTED_TYPES .stream ().map (PostgresTypeIdentifier .class ::cast ).collect (Collectors .toList ());
113
+ return Stream . concat ( Stream . of ( this . defaultType ), SUPPORTED_TYPES .stream ()) .map (PostgresTypeIdentifier .class ::cast ).collect (Collectors .toSet ());
103
114
}
104
115
105
116
private static class StringDecoder implements Codec <String >, Decoder <String > {
0 commit comments