Skip to content

Commit 77fc54c

Browse files
k-tokarevmp911de
authored andcommitted
Fix and test for postgres OID type decoding.
Co-authored-by: k.tokarev <[email protected]> [#495]
1 parent 001ac02 commit 77fc54c

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/main/java/io/r2dbc/postgresql/codec/NumericDecodeUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ public static Number decodeNumber(ByteBuf buffer, PostgresqlObjectId dataType, @
6262
return Short.parseShort(ByteBufUtils.decode(buffer));
6363
case INT4:
6464
case INT4_ARRAY:
65-
case OID:
66-
case OID_ARRAY:
6765
if (FORMAT_BINARY == format) {
6866
return buffer.readInt();
6967
}
7068
return Integer.parseInt(ByteBufUtils.decode(buffer));
7169
case INT8:
7270
case INT8_ARRAY:
71+
case OID:
72+
case OID_ARRAY:
7373
if (FORMAT_BINARY == format) {
7474
return buffer.readLong();
7575
}

src/test/java/io/r2dbc/postgresql/codec/LongCodecUnitTests.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE;
2323
import static io.r2dbc.postgresql.client.ParameterAssert.assertThat;
24-
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.INT8;
25-
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.VARCHAR;
24+
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.*;
2625
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
2726
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
2827
import static io.r2dbc.postgresql.util.ByteBufUtils.encode;
@@ -49,6 +48,14 @@ void decode() {
4948

5049
assertThat(codec.decode(TEST.buffer(8).writeLong(100L), dataType, FORMAT_BINARY, Long.class)).isEqualTo(100L);
5150
assertThat(codec.decode(encode(TEST, "100"), dataType, FORMAT_TEXT, Long.class)).isEqualTo(100L);
51+
52+
/* According to documentation: "The oid type is currently implemented as an unsigned four-byte integer"
53+
* (https://www.postgresql.org/docs/12/datatype-oid.html).
54+
* There is no "unsigned four-byte integer" common type in java, so the closest type to hold all possible oid
55+
* values is long. 2314556683 is a valid oid for example.
56+
*/
57+
assertThat(codec.decode(encode(TEST, "2314556683"), OID.getObjectId(), FORMAT_TEXT, Long.class))
58+
.isEqualTo(2314556683L);
5259
}
5360

5461
@Test

0 commit comments

Comments
 (0)