This is an automated email from the ASF dual-hosted git repository.
RaigorJiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 826bf91c0de Implement write methods for
PostgreSQLUnspecifiedBinaryProtocolValue (#38570)
826bf91c0de is described below
commit 826bf91c0dec56935bdb228ff45f7d5760bee7ec
Author: Guimu <[email protected]>
AuthorDate: Mon May 25 11:47:26 2026 +0800
Implement write methods for PostgreSQLUnspecifiedBinaryProtocolValue
(#38570)
* Implement write methods for PostgreSQLUnspecifiedBinaryProtocolValue
Closes #35830 (partial)
* Apply Spotless formatting for PostgreSQL unspecified binary protocol test
---
.../PostgreSQLUnspecifiedBinaryProtocolValue.java | 5 +--
...stgreSQLUnspecifiedBinaryProtocolValueTest.java | 52 +++++++++++++++++-----
2 files changed, 42 insertions(+), 15 deletions(-)
diff --git
a/database/protocol/dialect/postgresql/src/main/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLUnspecifiedBinaryProtocolValue.java
b/database/protocol/dialect/postgresql/src/main/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLUnspecifiedBinaryProtocolValue.java
index 2f47a322c02..3e03784b873 100644
---
a/database/protocol/dialect/postgresql/src/main/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLUnspecifiedBinaryProtocolValue.java
+++
b/database/protocol/dialect/postgresql/src/main/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLUnspecifiedBinaryProtocolValue.java
@@ -19,7 +19,6 @@ package
org.apache.shardingsphere.database.protocol.postgresql.packet.command.qu
import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLTypeUnspecifiedSQLParameter;
import
org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload;
-import
org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
/**
* Binary protocol value for unspecified for PostgreSQL.
@@ -28,7 +27,7 @@ public final class PostgreSQLUnspecifiedBinaryProtocolValue
implements PostgreSQ
@Override
public int getColumnLength(final PostgreSQLPacketPayload payload, final
Object value) {
- throw new UnsupportedSQLOperationException("getColumnLength");
+ return value.toString().getBytes(payload.getCharset()).length;
}
@Override
@@ -41,6 +40,6 @@ public final class PostgreSQLUnspecifiedBinaryProtocolValue
implements PostgreSQ
@Override
public void write(final PostgreSQLPacketPayload payload, final Object
value) {
- throw new UnsupportedSQLOperationException("write");
+ payload.writeStringEOF(value.toString());
}
}
diff --git
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLUnspecifiedBinaryProtocolValueTest.java
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLUnspecifiedBinaryProtocolValueTest.java
index f8a8f553bbf..a1ec455fd74 100644
---
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLUnspecifiedBinaryProtocolValueTest.java
+++
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLUnspecifiedBinaryProtocolValueTest.java
@@ -21,41 +21,69 @@ import io.netty.buffer.ByteBuf;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.ByteBufTestUtils;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLTypeUnspecifiedSQLParameter;
import
org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload;
-import
org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
import java.nio.charset.StandardCharsets;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isA;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+@ExtendWith(MockitoExtension.class)
class PostgreSQLUnspecifiedBinaryProtocolValueTest {
+ @Mock
+ private ByteBuf byteBuf;
+
+ private PostgreSQLPacketPayload payload;
+
+ @BeforeEach
+ void setup() {
+ payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
+ }
+
@Test
void assertGetColumnLength() {
- assertThrows(UnsupportedSQLOperationException.class, () -> new
PostgreSQLUnspecifiedBinaryProtocolValue().getColumnLength(new
PostgreSQLPacketPayload(null, StandardCharsets.UTF_8), "val"));
+ PostgreSQLUnspecifiedBinaryProtocolValue actual = new
PostgreSQLUnspecifiedBinaryProtocolValue();
+ assertThat(actual.getColumnLength(payload, "val"), is(3));
+ assertThat(actual.getColumnLength(payload, new
PostgreSQLTypeUnspecifiedSQLParameter("test")), is(4));
+ }
+
+ @Test
+ void assertGetColumnLengthWithMultiByteCharset() {
+ PostgreSQLUnspecifiedBinaryProtocolValue actual = new
PostgreSQLUnspecifiedBinaryProtocolValue();
+ assertThat(actual.getColumnLength(payload, "中文"), is(6));
}
@Test
void assertRead() {
String timestampStr = "2020-08-23 15:57:03+08";
int expectedLength = 4 + timestampStr.length();
- ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(expectedLength);
- byteBuf.writeInt(timestampStr.length());
- byteBuf.writeCharSequence(timestampStr, StandardCharsets.ISO_8859_1);
- byteBuf.readInt();
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf,
StandardCharsets.UTF_8);
- Object actual = new
PostgreSQLUnspecifiedBinaryProtocolValue().read(payload, timestampStr.length());
+ ByteBuf readBuf = ByteBufTestUtils.createByteBuf(expectedLength);
+ readBuf.writeInt(timestampStr.length());
+ readBuf.writeCharSequence(timestampStr, StandardCharsets.ISO_8859_1);
+ readBuf.readInt();
+ PostgreSQLPacketPayload readPayload = new
PostgreSQLPacketPayload(readBuf, StandardCharsets.UTF_8);
+ Object actual = new
PostgreSQLUnspecifiedBinaryProtocolValue().read(readPayload,
timestampStr.length());
assertThat(actual, isA(PostgreSQLTypeUnspecifiedSQLParameter.class));
assertThat(actual.toString(), is(timestampStr));
- assertThat(byteBuf.readerIndex(), is(expectedLength));
+ assertThat(readBuf.readerIndex(), is(expectedLength));
}
@Test
void assertWrite() {
- assertThrows(UnsupportedSQLOperationException.class, () -> new
PostgreSQLUnspecifiedBinaryProtocolValue().write(mock(PostgreSQLPacketPayload.class),
"val"));
+ new PostgreSQLUnspecifiedBinaryProtocolValue().write(payload, "val");
+ verify(byteBuf).writeBytes("val".getBytes(StandardCharsets.UTF_8));
+ }
+
+ @Test
+ void assertWriteWithTypeUnspecifiedParameter() {
+ new PostgreSQLUnspecifiedBinaryProtocolValue().write(payload, new
PostgreSQLTypeUnspecifiedSQLParameter("test"));
+ verify(byteBuf).writeBytes("test".getBytes(StandardCharsets.UTF_8));
}
}