Skip to content

Commit

Permalink
Fix and test for postgres OID type decoding.
Browse files Browse the repository at this point in the history
Co-authored-by: k.tokarev <k.tokarev@a1-systems.com>

[#495]
  • Loading branch information
k-tokarev authored and mp911de committed Feb 17, 2022
1 parent 001ac02 commit 77fc54c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ public static Number decodeNumber(ByteBuf buffer, PostgresqlObjectId dataType, @
return Short.parseShort(ByteBufUtils.decode(buffer));
case INT4:
case INT4_ARRAY:
case OID:
case OID_ARRAY:
if (FORMAT_BINARY == format) {
return buffer.readInt();
}
return Integer.parseInt(ByteBufUtils.decode(buffer));
case INT8:
case INT8_ARRAY:
case OID:
case OID_ARRAY:
if (FORMAT_BINARY == format) {
return buffer.readLong();
}
Expand Down
11 changes: 9 additions & 2 deletions src/test/java/io/r2dbc/postgresql/codec/LongCodecUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE;
import static io.r2dbc.postgresql.client.ParameterAssert.assertThat;
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.INT8;
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.VARCHAR;
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.*;
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
import static io.r2dbc.postgresql.util.ByteBufUtils.encode;
Expand All @@ -49,6 +48,14 @@ void decode() {

assertThat(codec.decode(TEST.buffer(8).writeLong(100L), dataType, FORMAT_BINARY, Long.class)).isEqualTo(100L);
assertThat(codec.decode(encode(TEST, "100"), dataType, FORMAT_TEXT, Long.class)).isEqualTo(100L);

/* According to documentation: "The oid type is currently implemented as an unsigned four-byte integer"
* (https://www.postgresql.org/docs/12/datatype-oid.html).
* There is no "unsigned four-byte integer" common type in java, so the closest type to hold all possible oid
* values is long. 2314556683 is a valid oid for example.
*/
assertThat(codec.decode(encode(TEST, "2314556683"), OID.getObjectId(), FORMAT_TEXT, Long.class))
.isEqualTo(2314556683L);
}

@Test
Expand Down

0 comments on commit 77fc54c

Please sign in to comment.