diff --git a/src/main/java/io/r2dbc/postgresql/codec/StringCodec.java b/src/main/java/io/r2dbc/postgresql/codec/StringCodec.java index ccc243d6..b8cacd68 100644 --- a/src/main/java/io/r2dbc/postgresql/codec/StringCodec.java +++ b/src/main/java/io/r2dbc/postgresql/codec/StringCodec.java @@ -27,6 +27,7 @@ import java.util.EnumSet; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import static io.r2dbc.postgresql.codec.PostgresqlObjectId.BPCHAR; import static io.r2dbc.postgresql.codec.PostgresqlObjectId.CHAR; @@ -48,14 +49,24 @@ final class StringCodec extends AbstractCodec implements ArrayCodecDeleg private final ByteBufAllocator byteBufAllocator; + private final PostgresTypeIdentifier defaultType; + + private final PostgresTypeIdentifier arrayType; + StringCodec(ByteBufAllocator byteBufAllocator) { + this(byteBufAllocator, VARCHAR, VARCHAR_ARRAY); + } + + StringCodec(ByteBufAllocator byteBufAllocator, PostgresTypeIdentifier defaultType, PostgresTypeIdentifier arrayType) { super(String.class); this.byteBufAllocator = Assert.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); + this.defaultType = Assert.requireNonNull(defaultType, "defaultType must not be null"); + this.arrayType = Assert.requireNonNull(arrayType, "arrayType must not be null"); } @Override public EncodedParameter encodeNull() { - return createNull(FORMAT_TEXT, VARCHAR); + return createNull(FORMAT_TEXT, this.defaultType); } @Override @@ -63,7 +74,7 @@ boolean doCanDecode(PostgresqlObjectId type, Format format) { Assert.requireNonNull(format, "format must not be null"); Assert.requireNonNull(type, "type must not be null"); - return SUPPORTED_TYPES.contains(type); + return this.defaultType == type || SUPPORTED_TYPES.contains(type); } @Override @@ -75,7 +86,7 @@ String doDecode(ByteBuf buffer, PostgresTypeIdentifier dataType, @Nullable Forma @Override EncodedParameter doEncode(String value) { - return doEncode(value, VARCHAR); + return doEncode(value, this.defaultType); } @Override @@ -94,12 +105,12 @@ public String encodeToText(String value) { @Override public PostgresTypeIdentifier getArrayDataType() { - return VARCHAR_ARRAY; + return this.arrayType; } @Override public Iterable getDataTypes() { - return SUPPORTED_TYPES.stream().map(PostgresTypeIdentifier.class::cast).collect(Collectors.toList()); + return Stream.concat(Stream.of(this.defaultType), SUPPORTED_TYPES.stream()).map(PostgresTypeIdentifier.class::cast).collect(Collectors.toSet()); } private static class StringDecoder implements Codec, Decoder {