|
| 1 | +package io.r2dbc.postgresql.codec; |
| 2 | + |
| 3 | +import io.netty.buffer.ByteBuf; |
| 4 | +import io.netty.buffer.ByteBufAllocator; |
| 5 | +import io.r2dbc.postgresql.client.Parameter; |
| 6 | +import io.r2dbc.postgresql.message.Format; |
| 7 | +import io.r2dbc.postgresql.type.PostgresqlObjectId; |
| 8 | +import io.r2dbc.postgresql.util.Assert; |
| 9 | +import io.r2dbc.postgresql.util.ByteBufUtils; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +abstract class AbstractGeometryCodec<T> extends AbstractCodec<T> { |
| 15 | + |
| 16 | + protected final PostgresqlObjectId postgresqlObjectId; |
| 17 | + |
| 18 | + protected final ByteBufAllocator byteBufAllocator; |
| 19 | + |
| 20 | + AbstractGeometryCodec(Class<T> type, PostgresqlObjectId postgresqlObjectId, ByteBufAllocator byteBufAllocator) { |
| 21 | + super(type); |
| 22 | + this.postgresqlObjectId = Assert.requireNonNull(postgresqlObjectId, "postgresqlObjectId must not be null"); |
| 23 | + this.byteBufAllocator = Assert.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); |
| 24 | + } |
| 25 | + |
| 26 | + abstract T doDecodeBinary(ByteBuf byteBuffer); |
| 27 | + |
| 28 | + abstract T doDecodeText(String text); |
| 29 | + |
| 30 | + abstract ByteBuf doEncodeBinary(T value); |
| 31 | + |
| 32 | + @Override |
| 33 | + boolean doCanDecode(PostgresqlObjectId type, Format format) { |
| 34 | + Assert.requireNonNull(type, "type must not be null"); |
| 35 | + Assert.requireNonNull(format, "format must not be null"); |
| 36 | + return postgresqlObjectId == type; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + T doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class<? extends T> type) { |
| 41 | + if (format == Format.FORMAT_BINARY) { |
| 42 | + return doDecodeBinary(buffer); |
| 43 | + } |
| 44 | + return doDecodeText(ByteBufUtils.decode(buffer)); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + Parameter doEncode(T value) { |
| 49 | + Assert.requireNonNull(value, "value must not be null"); |
| 50 | + return create(this.postgresqlObjectId, Format.FORMAT_BINARY, () -> doEncodeBinary(value)); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Parameter encodeNull() { |
| 55 | + return createNull(postgresqlObjectId, Format.FORMAT_BINARY); |
| 56 | + } |
| 57 | + |
| 58 | + protected List<String> tokenizeTextData(String string) { |
| 59 | + List<String> tokens = new ArrayList<>(); |
| 60 | + |
| 61 | + for (int p = 0, s = 0; p < string.length(); p++) { |
| 62 | + char c = string.charAt(p); |
| 63 | + |
| 64 | + if (c == '(' || c == '[' || c == '<' || c == '{') { |
| 65 | + s++; |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + if (c == ',' || c == ')' || c == ']' || c == '>' || c == '}') { |
| 70 | + if (s != p) { |
| 71 | + tokens.add(string.substring(s, p)); |
| 72 | + s = p + 1; |
| 73 | + } else { |
| 74 | + s++; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return tokens; |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments