Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
public final class PostgreSQLInt8ArrayBinaryProtocolValue implements PostgreSQLBinaryProtocolValue {

private static final PostgreSQLArrayParameterDecoder ARRAY_PARAMETER_DECODER = new PostgreSQLArrayParameterDecoder();
private static final int DIMENSIONS = 1;
private static final int FLAGS_NO_NULLS = 0;
private static final int LOWER_BOUND = 1;
private static final int INT8_LENGTH = 8;

@Override
public int getColumnLength(final PostgreSQLPacketPayload payload, final Object value) {
Expand All @@ -41,6 +45,29 @@ public Object read(final PostgreSQLPacketPayload payload, final int parameterVal

@Override
public void write(final PostgreSQLPacketPayload payload, final Object value) {
throw new UnsupportedSQLOperationException("PostgreSQLInt8ArrayBinaryProtocolValue.write()");
if (!(value instanceof Object[])) {
throw new IllegalArgumentException("Expected Object[] for int8 array, but got: " + value.getClass().getSimpleName());
}
Object[] elements = (Object[]) value;
final int DIMENSIONS = 1;
final int FLAGS_NO_NULLS = 0;
final int INT8_OID = 20;
final int LOWER_BOUND = 1;
final int INT8_LENGTH = 8;
payload.writeInt4(DIMENSIONS);
payload.writeInt4(FLAGS_NO_NULLS);
payload.writeInt4(INT8_OID);
payload.writeInt4(elements.length);
payload.writeInt4(LOWER_BOUND);
for (Object element : elements) {
if (element == null) {
payload.writeInt4(-1);
} else if (element instanceof Number) {
payload.writeInt4(INT8_LENGTH);
payload.writeInt8(((Number) element).longValue());
} else {
throw new IllegalArgumentException("Invalid element type in int8 array: " + element);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
import org.junit.jupiter.api.Test;
import io.netty.buffer.Unpooled;

import java.nio.charset.StandardCharsets;

Expand Down Expand Up @@ -52,6 +53,19 @@ void assertRead() {

@Test
void assertWrite() {
assertThrows(UnsupportedSQLOperationException.class, () -> new PostgreSQLInt8ArrayBinaryProtocolValue().write(new PostgreSQLPacketPayload(null, StandardCharsets.UTF_8), "val"));
Object[] input = new Object[]{11L, 12L};
ByteBuf byteBuf = Unpooled.buffer();
PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
new PostgreSQLInt8ArrayBinaryProtocolValue().write(payload, input);
byteBuf.readerIndex(0);
assertThat(byteBuf.readInt(), is(1));
assertThat(byteBuf.readInt(), is(0));
assertThat(byteBuf.readInt(), is(20));
assertThat(byteBuf.readInt(), is(2));
assertThat(byteBuf.readInt(), is(1));
assertThat(byteBuf.readInt(), is(8));
assertThat(byteBuf.readLong(), is(11L));
assertThat(byteBuf.readInt(), is(8));
assertThat(byteBuf.readLong(), is(12L));
}
}
Loading