Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix postgres OID type decoding #495

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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