You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/10/29 07:04:47 UTC
[shardingsphere] branch master updated: ShardingSphere Proxy
supports character set in session scope (#13332)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 fc4a613 ShardingSphere Proxy supports character set in session scope (#13332)
fc4a613 is described below
commit fc4a613b0b989856d5eaa0087dcf6fd6af9de7bd
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Fri Oct 29 15:04:10 2021 +0800
ShardingSphere Proxy supports character set in session scope (#13332)
* ShardingSphere Proxy supports character set in session scope
* Fix test cases
* Fix MySQL frontend test cases
* Refactor PostgreSQLStringBinaryProtocolValueTest
* Fix test cases in scaling-mysql
* Remove outdated comments
* Fix tests in CommandExecutorTaskTest
* Update MySQLComStmtPrepareExecutor
* Complete test cases
* Update MySQLColumnDefinition41Packet
---
.../AbstractCommandExecutorTaskAdviceTest.java | 3 +-
.../PacketPayload.java => CommonConstants.java} | 20 ++--
.../protocol/codec/DatabasePacketCodecEngine.java | 4 +-
.../ChannelAttrInitializer.java} | 23 +++--
.../db/protocol/payload/PacketPayload.java | 9 ++
.../protocol/netty/ChannelAttrInitializerTest.java | 40 ++++++++
.../mysql/codec/MySQLPacketCodecEngine.java | 8 +-
.../protocol/mysql/constant/MySQLCharacterSet.java | 114 +++++++++++++++++++++
.../protocol/mysql/constant/MySQLConstants.java} | 18 ++--
.../protocol/mysql/constant/MySQLServerInfo.java | 5 +-
.../query/MySQLColumnDefinition41Packet.java | 19 ++--
.../packet/handshake/MySQLHandshakePacket.java | 2 +-
.../protocol/mysql/payload/MySQLPacketPayload.java | 20 ++--
.../mysql/codec/MySQLPacketCodecEngineTest.java | 14 ++-
.../mysql/constant/MySQLCharacterSetTest.java} | 30 +++---
.../query/MySQLColumnDefinition41PacketTest.java | 4 +-
.../MySQLDoubleBinaryProtocolValueTest.java | 6 +-
.../MySQLFloatBinaryProtocolValueTest.java | 6 +-
.../packet/handshake/MySQLHandshakePacketTest.java | 12 +--
.../MySQLHandshakeResponse41PacketTest.java | 32 +++---
.../mysql/payload/MySQLPacketPayloadTest.java | 86 ++++++++--------
.../codec/PostgreSQLPacketCodecEngine.java | 8 +-
.../handshake/PostgreSQLComStartupPacket.java | 11 ++
.../payload/PostgreSQLPacketPayload.java | 10 +-
.../codec/PostgreSQLPacketCodecEngineTest.java | 14 ++-
.../PostgreSQLUnsupportedCommandPacketTest.java | 4 +-
...PostgreSQLBoolArrayBinaryProtocolValueTest.java | 4 +-
.../PostgreSQLDoubleBinaryProtocolValueTest.java | 6 +-
...stgreSQLFloat4ArrayBinaryProtocolValueTest.java | 4 +-
...stgreSQLFloat8ArrayBinaryProtocolValueTest.java | 4 +-
.../PostgreSQLFloatBinaryProtocolValueTest.java | 6 +-
...PostgreSQLInt2ArrayBinaryProtocolValueTest.java | 4 +-
...PostgreSQLInt4ArrayBinaryProtocolValueTest.java | 4 +-
...PostgreSQLInt8ArrayBinaryProtocolValueTest.java | 4 +-
.../PostgreSQLNumericBinaryProtocolValueTest.java | 4 +-
...stgreSQLStringArrayBinaryProtocolValueTest.java | 4 +-
.../PostgreSQLStringBinaryProtocolValueTest.java | 18 ++--
...stgreSQLUnspecifiedBinaryProtocolValueTest.java | 2 +-
...tgreSQLAuthenticationMD5PasswordPacketTest.java | 4 +-
.../PostgreSQLAuthenticationOKPacketTest.java | 4 +-
.../generic/PostgreSQLComStartupPacketTest.java | 5 +-
.../PostgreSQLComTerminationPacketTest.java | 4 +-
.../PostgreSQLCommandCompletePacketTest.java | 6 +-
.../PostgreSQLParameterStatusPacketTest.java | 4 +-
.../PostgreSQLPasswordMessagePacketTest.java | 4 +-
.../generic/PostgreSQLReadyForQueryPacketTest.java | 6 +-
.../generic/PostgreSQLSSLNegativePacketTest.java | 4 +-
.../payload/PostgreSQLPacketPayloadTest.java | 5 +-
.../jdbc/connection/BackendConnection.java | 12 ++-
.../jdbc/connection/BackendConnectionTest.java | 3 +-
.../executor/ShowProcessListExecutorTest.java | 10 +-
.../distsql/ral/SetVariableBackendHandlerTest.java | 3 +-
.../ral/ShowAllVariablesBackendHandlerTest.java | 3 +-
.../ral/ShowVariableBackendHandlerTest.java | 3 +-
.../rql/RuleDefinitionBackendHandlerTest.java | 3 +-
.../transaction/TransactionBackendHandlerTest.java | 3 +-
.../frontend/command/CommandExecutorTask.java | 3 +-
.../netty/FrontendChannelInboundHandler.java | 8 +-
.../frontend/netty/ServerHandlerInitializer.java | 4 +-
.../frontend/command/CommandExecutorTaskTest.java | 13 ++-
.../netty/ServerHandlerInitializerTest.java | 46 +++++++++
.../authentication/MySQLAuthenticationEngine.java | 10 +-
.../execute/MySQLComStmtExecuteExecutor.java | 6 +-
.../prepare/MySQLComStmtPrepareExecutor.java | 15 ++-
.../query/builder/ResponsePacketBuilder.java | 5 +-
.../fieldlist/MySQLComFieldListPacketExecutor.java | 6 +-
.../text/query/MySQLComQueryPacketExecutor.java | 6 +-
.../frontend/mysql/MySQLFrontendEngineTest.java | 12 ++-
.../MySQLAuthenticationEngineTest.java | 9 ++
.../command/MySQLCommandExecutorFactoryTest.java | 6 +-
.../execute/MySQLComStmtExecuteExecutorTest.java | 9 +-
.../query/builder/ResponsePacketBuilderTest.java | 2 +-
.../query/MySQLComQueryPacketExecutorTest.java | 12 ++-
.../PostgreSQLAuthenticationEngine.java | 9 +-
.../PostgreSQLAuthenticationEngineTest.java | 40 ++++++--
.../PostgreSQLAuthenticationHandlerTest.java | 2 +-
.../scaling/mysql/client/MySQLClient.java | 2 +
.../netty/MySQLBinlogEventPacketDecoder.java | 14 +--
.../client/netty/MySQLCommandPacketDecoder.java | 3 +-
.../client/netty/MySQLNegotiatePackageDecoder.java | 3 +-
.../netty/MySQLBinlogEventPacketDecoderTest.java | 24 +++--
.../netty/MySQLCommandPacketDecoderTest.java | 33 ++++--
.../netty/MySQLNegotiatePackageDecoderTest.java | 21 +++-
83 files changed, 718 insertions(+), 277 deletions(-)
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-test/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/advice/AbstractCommandExecutorTaskAdviceTest.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-test/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/advice/AbstractCommandExecutorTaskAdviceTest.java
index 1f59bed..0eed75b 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-test/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/advice/AbstractCommandExecutorTaskAdviceTest.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-test/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/advice/AbstractCommandExecutorTaskAdviceTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.agent.plugin.tracing.advice;
+import io.netty.util.DefaultAttributeMap;
import lombok.Getter;
import org.apache.shardingsphere.agent.api.advice.AdviceTargetObject;
import org.apache.shardingsphere.agent.plugin.tracing.AgentRunner;
@@ -34,7 +35,7 @@ public abstract class AbstractCommandExecutorTaskAdviceTest implements AdviceTes
@SuppressWarnings("ConstantConditions")
@Override
public final void prepare() {
- Object executorTask = new CommandExecutorTask(null, new BackendConnection(TransactionType.BASE), null, null);
+ Object executorTask = new CommandExecutorTask(null, new BackendConnection(TransactionType.BASE, new DefaultAttributeMap()), null, null);
targetObject = (AdviceTargetObject) executorTask;
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/CommonConstants.java
similarity index 66%
copy from shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java
copy to shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/CommonConstants.java
index ad86e29..44c322e 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/CommonConstants.java
@@ -15,19 +15,19 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.db.protocol.payload;
+package org.apache.shardingsphere.db.protocol;
-import io.netty.buffer.ByteBuf;
+import io.netty.util.AttributeKey;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.nio.charset.Charset;
/**
- * Packet payload.
+ * Common constants for protocol.
*/
-public interface PacketPayload extends AutoCloseable {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class CommonConstants {
- /**
- * Get byte buf.
- *
- * @return byte buf
- */
- ByteBuf getByteBuf();
+ public static final AttributeKey<Charset> CHARSET_ATTRIBUTE_KEY = AttributeKey.valueOf(Charset.class.getName());
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/codec/DatabasePacketCodecEngine.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/codec/DatabasePacketCodecEngine.java
index 47fb8bb..61ac2f7 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/codec/DatabasePacketCodecEngine.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/codec/DatabasePacketCodecEngine.java
@@ -22,6 +22,7 @@ import io.netty.channel.ChannelHandlerContext;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.db.protocol.payload.PacketPayload;
+import java.nio.charset.Charset;
import java.util.List;
/**
@@ -61,7 +62,8 @@ public interface DatabasePacketCodecEngine<T extends DatabasePacket<?>> {
* Create packet payload.
*
* @param message message
+ * @param charset charset
* @return packet payload
*/
- PacketPayload createPacketPayload(ByteBuf message);
+ PacketPayload createPacketPayload(ByteBuf message, Charset charset);
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/netty/ChannelAttrInitializer.java
similarity index 58%
copy from shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java
copy to shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/netty/ChannelAttrInitializer.java
index ad86e29..561463f 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/netty/ChannelAttrInitializer.java
@@ -15,19 +15,22 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.db.protocol.payload;
+package org.apache.shardingsphere.db.protocol.netty;
-import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
+
+import java.nio.charset.Charset;
/**
- * Packet payload.
+ * Channel attributes initializer.
*/
-public interface PacketPayload extends AutoCloseable {
+public final class ChannelAttrInitializer extends ChannelInboundHandlerAdapter {
- /**
- * Get byte buf.
- *
- * @return byte buf
- */
- ByteBuf getByteBuf();
+ @Override
+ public void channelActive(final ChannelHandlerContext ctx) {
+ ctx.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).setIfAbsent(Charset.defaultCharset());
+ ctx.fireChannelActive();
+ }
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java
index ad86e29..be22a9f 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.db.protocol.payload;
import io.netty.buffer.ByteBuf;
+import java.nio.charset.Charset;
+
/**
* Packet payload.
*/
@@ -30,4 +32,11 @@ public interface PacketPayload extends AutoCloseable {
* @return byte buf
*/
ByteBuf getByteBuf();
+
+ /**
+ * Get charset.
+ *
+ * @return charset
+ */
+ Charset getCharset();
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/test/java/org/apache/shardingsphere/db/protocol/netty/ChannelAttrInitializerTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/test/java/org/apache/shardingsphere/db/protocol/netty/ChannelAttrInitializerTest.java
new file mode 100644
index 0000000..796cea5
--- /dev/null
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/test/java/org/apache/shardingsphere/db/protocol/netty/ChannelAttrInitializerTest.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.db.protocol.netty;
+
+import io.netty.channel.ChannelHandlerContext;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
+import org.junit.Test;
+
+import java.nio.charset.Charset;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public final class ChannelAttrInitializerTest {
+
+ @Test
+ public void assertChannelActive() {
+ ChannelHandlerContext context = mock(ChannelHandlerContext.class, RETURNS_DEEP_STUBS);
+ new ChannelAttrInitializer().channelActive(context);
+ verify(context.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY)).setIfAbsent(any(Charset.class));
+ verify(context).fireChannelActive();
+ }
+}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngine.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngine.java
index bf5f38f..7f76448 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngine.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngine.java
@@ -19,12 +19,14 @@ package org.apache.shardingsphere.db.protocol.mysql.codec;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.codec.DatabasePacketCodecEngine;
import org.apache.shardingsphere.db.protocol.error.CommonErrorCode;
import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
+import java.nio.charset.Charset;
import java.util.List;
/**
@@ -54,7 +56,7 @@ public final class MySQLPacketCodecEngine implements DatabasePacketCodecEngine<M
@Override
public void encode(final ChannelHandlerContext context, final MySQLPacket message, final ByteBuf out) {
- MySQLPacketPayload payload = new MySQLPacketPayload(prepareMessageHeader(out).markWriterIndex());
+ MySQLPacketPayload payload = new MySQLPacketPayload(prepareMessageHeader(out).markWriterIndex(), context.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).get());
try {
message.write(payload);
// CHECKSTYLE:OFF
@@ -77,7 +79,7 @@ public final class MySQLPacketCodecEngine implements DatabasePacketCodecEngine<M
}
@Override
- public MySQLPacketPayload createPacketPayload(final ByteBuf message) {
- return new MySQLPacketPayload(message);
+ public MySQLPacketPayload createPacketPayload(final ByteBuf message, final Charset charset) {
+ return new MySQLPacketPayload(message, charset);
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSet.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSet.java
new file mode 100644
index 0000000..df8e0cc
--- /dev/null
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSet.java
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.db.protocol.mysql.constant;
+
+import lombok.Getter;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.charset.UnsupportedCharsetException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+/**
+ * Character set of MySQL.
+ *
+ * @see <a href="https://dev.mysql.com/doc/internals/en/character-set.html#packet-Protocol::CharacterSet">Character Set</a>
+ */
+@Getter
+public enum MySQLCharacterSet {
+
+ BIG5(1, () -> Charset.forName("big5")),
+ DEC8(3, () -> Charset.forName("dec8")),
+ CP850(4, () -> Charset.forName("cp850")),
+ HP8(6, () -> Charset.forName("hp8")),
+ KOI8R(7, () -> Charset.forName("koi8-u")),
+ LATIN1(8, () -> StandardCharsets.ISO_8859_1),
+ LATIN2(9, () -> Charset.forName("latin2")),
+ SWE7(10, () -> Charset.forName("swe7")),
+ ASCII(11, () -> StandardCharsets.US_ASCII),
+ UJIS(12, () -> Charset.forName("ujis")),
+ SJIS(13, () -> Charset.forName("sjis")),
+ HEBREW(16, () -> Charset.forName("hebrew")),
+ TIS620(18, () -> Charset.forName("tis620")),
+ EUCKR(19, () -> Charset.forName("euckr")),
+ KOI8U(22, () -> Charset.forName("koi8-u")),
+ GB2312(24, () -> Charset.forName("gb2312")),
+ GREEK(25, () -> Charset.forName("greek")),
+ CP1250(26, () -> Charset.forName("cp1250")),
+ GBK(28, () -> Charset.forName("gbk")),
+ LATIN5(30, () -> Charset.forName("latin5")),
+ ARMSCII8(32, () -> Charset.forName("armscii8")),
+ UTF8(33, () -> StandardCharsets.UTF_8),
+ UCS2(35, () -> Charset.forName("ucs2")),
+ CP866(36, () -> Charset.forName("cp866")),
+ KEYBCS2(37, () -> Charset.forName("keybcs2")),
+ MACCE(38, () -> Charset.forName("macce")),
+ MACROMAN(39, () -> Charset.forName("macroman")),
+ CP852(40, () -> Charset.forName("cp852")),
+ LATIN7(41, () -> Charset.forName("iso-8859-13")),
+ CP1251(51, () -> Charset.forName("cp1251")),
+ UTF16(54, () -> StandardCharsets.UTF_16),
+ UTF16LE(56, () -> StandardCharsets.UTF_16LE),
+ CP1256(57, () -> Charset.forName("cp1256")),
+ CP1257(59, () -> Charset.forName("cp1257")),
+ UTF32(60, () -> Charset.forName("utf32")),
+ BINARY(63, () -> null),
+ GEOSTD8(92, () -> Charset.forName("geostd8")),
+ CP932(95, () -> Charset.forName("cp932")),
+ EUCJPMS(97, () -> Charset.forName("eucjpms")),
+ GB18030(248, () -> Charset.forName("gb18030")),
+ UTF8MB4(255, () -> StandardCharsets.UTF_8);
+
+ private static final Map<Integer, MySQLCharacterSet> CHARACTER_SET_MAP = Collections.unmodifiableMap(Arrays.stream(values()).collect(Collectors.toMap(each -> each.id, Function.identity())));
+
+ private final int id;
+
+ private final Charset charset;
+
+ MySQLCharacterSet(final int id, final Supplier<Charset> charsetSupplier) {
+ this.id = id;
+ Charset result = null;
+ try {
+ result = charsetSupplier.get();
+ } catch (UnsupportedCharsetException ignored) {
+ }
+ charset = result;
+ }
+
+ /**
+ * Get character set by id.
+ *
+ * @param id id
+ * @return MySQL character set
+ */
+ public static MySQLCharacterSet findById(final int id) {
+ MySQLCharacterSet result = CHARACTER_SET_MAP.get(id);
+ if (null == result) {
+ throw new UnsupportedCharsetException(String.format("Character set corresponding to id %d not found", id));
+ }
+ if (null == result.getCharset()) {
+ throw new UnsupportedCharsetException(String.format("Character set %s unsupported", result.name().toLowerCase()));
+ }
+ return result;
+ }
+}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLConstants.java
similarity index 66%
copy from shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java
copy to shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLConstants.java
index ad86e29..3427794 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/payload/PacketPayload.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLConstants.java
@@ -15,19 +15,17 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.db.protocol.payload;
+package org.apache.shardingsphere.db.protocol.mysql.constant;
-import io.netty.buffer.ByteBuf;
+import io.netty.util.AttributeKey;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
/**
- * Packet payload.
+ * MySQL constants.
*/
-public interface PacketPayload extends AutoCloseable {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class MySQLConstants {
- /**
- * Get byte buf.
- *
- * @return byte buf
- */
- ByteBuf getByteBuf();
+ public static final AttributeKey<MySQLCharacterSet> MYSQL_CHARACTER_SET_ATTRIBUTE_KEY = AttributeKey.valueOf(MySQLCharacterSet.class.getName());
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfo.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfo.java
index b1514eb..16d4234 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfo.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfo.java
@@ -31,10 +31,7 @@ public final class MySQLServerInfo {
*/
public static final int PROTOCOL_VERSION = 0x0A;
- /**
- * Charset code 0x21 is utf8_general_ci.
- */
- public static final int CHARSET = 0x21;
+ public static final MySQLCharacterSet DEFAULT_CHARSET = MySQLCharacterSet.UTF8MB4;
private static final String DEFAULT_MYSQL_VERSION = "5.7.22";
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/MySQLColumnDefinition41Packet.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/MySQLColumnDefinition41Packet.java
index e46fa80..6cc743b 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/MySQLColumnDefinition41Packet.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/MySQLColumnDefinition41Packet.java
@@ -65,9 +65,10 @@ public final class MySQLColumnDefinition41Packet implements MySQLPacket {
private final boolean containDefaultValues;
public MySQLColumnDefinition41Packet(final int sequenceId, final ResultSetMetaData resultSetMetaData, final int columnIndex) throws SQLException {
- this(sequenceId, resultSetMetaData.getSchemaName(columnIndex), resultSetMetaData.getTableName(columnIndex), resultSetMetaData.getTableName(columnIndex),
- resultSetMetaData.getColumnLabel(columnIndex), resultSetMetaData.getColumnName(columnIndex), resultSetMetaData.getColumnDisplaySize(columnIndex),
- MySQLBinaryColumnType.valueOfJDBCType(resultSetMetaData.getColumnType(columnIndex)), resultSetMetaData.getScale(columnIndex), false);
+ this(sequenceId, MySQLServerInfo.DEFAULT_CHARSET.getId(), resultSetMetaData.getSchemaName(columnIndex), resultSetMetaData.getTableName(columnIndex),
+ resultSetMetaData.getTableName(columnIndex), resultSetMetaData.getColumnLabel(columnIndex), resultSetMetaData.getColumnName(columnIndex),
+ resultSetMetaData.getColumnDisplaySize(columnIndex), MySQLBinaryColumnType.valueOfJDBCType(resultSetMetaData.getColumnType(columnIndex)), resultSetMetaData.getScale(columnIndex),
+ false);
}
/*
@@ -75,17 +76,17 @@ public final class MySQLColumnDefinition41Packet implements MySQLPacket {
*
* @see <a href="https://github.com/apache/shardingsphere/issues/4358"></a>
*/
- public MySQLColumnDefinition41Packet(final int sequenceId, final String schema, final String table, final String orgTable,
- final String name, final String orgName, final int columnLength, final MySQLBinaryColumnType columnType,
+ public MySQLColumnDefinition41Packet(final int sequenceId, final int characterSet, final String schema, final String table, final String orgTable,
+ final String name, final String orgName, final int columnLength, final MySQLBinaryColumnType columnType,
final int decimals, final boolean containDefaultValues) {
- this(sequenceId, 0, schema, table, orgTable, name, orgName, columnLength, columnType, decimals, containDefaultValues);
+ this(sequenceId, characterSet, 0, schema, table, orgTable, name, orgName, columnLength, columnType, decimals, containDefaultValues);
}
- public MySQLColumnDefinition41Packet(final int sequenceId, final int flags, final String schema, final String table, final String orgTable,
- final String name, final String orgName, final int columnLength, final MySQLBinaryColumnType columnType,
+ public MySQLColumnDefinition41Packet(final int sequenceId, final int characterSet, final int flags, final String schema, final String table, final String orgTable,
+ final String name, final String orgName, final int columnLength, final MySQLBinaryColumnType columnType,
final int decimals, final boolean containDefaultValues) {
this.sequenceId = sequenceId;
- characterSet = MySQLServerInfo.CHARSET;
+ this.characterSet = characterSet;
this.flags = flags;
this.schema = schema;
this.table = table;
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacket.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacket.java
index 92ace14..84c1eb5 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacket.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacket.java
@@ -56,7 +56,7 @@ public final class MySQLHandshakePacket implements MySQLPacket {
serverVersion = MySQLServerInfo.getServerVersion();
this.connectionId = connectionId;
capabilityFlagsLower = MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower();
- characterSet = MySQLServerInfo.CHARSET;
+ characterSet = MySQLServerInfo.DEFAULT_CHARSET.getId();
statusFlag = MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT;
capabilityFlagsUpper = MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsUpper();
this.authPluginData = authPluginData;
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayload.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayload.java
index 42485d1..88affc2 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayload.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayload.java
@@ -23,6 +23,8 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.db.protocol.payload.PacketPayload;
+import java.nio.charset.Charset;
+
/**
* MySQL payload operation for MySQL packet data types.
*
@@ -34,6 +36,8 @@ public final class MySQLPacketPayload implements PacketPayload {
private final ByteBuf byteBuf;
+ private final Charset charset;
+
/**
* Read 1 byte fixed length integer from byte buffers.
*
@@ -246,7 +250,7 @@ public final class MySQLPacketPayload implements PacketPayload {
int length = (int) readIntLenenc();
byte[] result = new byte[length];
byteBuf.readBytes(result);
- return new String(result);
+ return new String(result, charset);
}
/**
@@ -276,7 +280,7 @@ public final class MySQLPacketPayload implements PacketPayload {
return;
}
writeIntLenenc(value.getBytes().length);
- byteBuf.writeBytes(value.getBytes());
+ byteBuf.writeBytes(value.getBytes(charset));
}
/**
@@ -305,7 +309,7 @@ public final class MySQLPacketPayload implements PacketPayload {
public String readStringFix(final int length) {
byte[] result = new byte[length];
byteBuf.readBytes(result);
- return new String(result);
+ return new String(result, charset);
}
/**
@@ -331,7 +335,7 @@ public final class MySQLPacketPayload implements PacketPayload {
* @param value fixed length string
*/
public void writeStringFix(final String value) {
- byteBuf.writeBytes(value.getBytes());
+ byteBuf.writeBytes(value.getBytes(charset));
}
/**
@@ -379,7 +383,7 @@ public final class MySQLPacketPayload implements PacketPayload {
byte[] result = new byte[byteBuf.bytesBefore((byte) 0)];
byteBuf.readBytes(result);
byteBuf.skipBytes(1);
- return new String(result);
+ return new String(result, charset);
}
/**
@@ -404,7 +408,7 @@ public final class MySQLPacketPayload implements PacketPayload {
* @param value null terminated string
*/
public void writeStringNul(final String value) {
- byteBuf.writeBytes(value.getBytes());
+ byteBuf.writeBytes(value.getBytes(charset));
byteBuf.writeByte(0);
}
@@ -431,7 +435,7 @@ public final class MySQLPacketPayload implements PacketPayload {
public String readStringEOF() {
byte[] result = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(result);
- return new String(result);
+ return new String(result, charset);
}
/**
@@ -442,7 +446,7 @@ public final class MySQLPacketPayload implements PacketPayload {
* @param value rest of packet string
*/
public void writeStringEOF(final String value) {
- byteBuf.writeBytes(value.getBytes());
+ byteBuf.writeBytes(value.getBytes(charset));
}
/**
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngineTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngineTest.java
index 865480a..df929e1 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngineTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/codec/MySQLPacketCodecEngineTest.java
@@ -19,13 +19,18 @@ package org.apache.shardingsphere.db.protocol.mysql.codec;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
+import io.netty.util.AttributeKey;
import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
@@ -43,12 +48,17 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class MySQLPacketCodecEngineTest {
- @Mock
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ChannelHandlerContext context;
@Mock
private ByteBuf byteBuf;
+ @Before
+ public void setup() {
+ when(context.channel().attr(AttributeKey.<Charset>valueOf(Charset.class.getName())).get()).thenReturn(StandardCharsets.UTF_8);
+ }
+
@Test
public void assertIsValidHeader() {
assertTrue(new MySQLPacketCodecEngine().isValidHeader(50));
@@ -123,6 +133,6 @@ public final class MySQLPacketCodecEngineTest {
@Test
public void assertCreatePacketPayload() {
- assertThat(new MySQLPacketCodecEngine().createPacketPayload(byteBuf).getByteBuf(), is(byteBuf));
+ assertThat(new MySQLPacketCodecEngine().createPacketPayload(byteBuf, StandardCharsets.UTF_8).getByteBuf(), is(byteBuf));
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLSSLNegativePacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSetTest.java
similarity index 53%
copy from shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLSSLNegativePacketTest.java
copy to shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSetTest.java
index 6c4b8d8..204b9f2 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLSSLNegativePacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSetTest.java
@@ -15,26 +15,30 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.db.protocol.postgresql.packet.generic;
+package org.apache.shardingsphere.db.protocol.mysql.constant;
-import io.netty.buffer.ByteBuf;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.ByteBufTestUtils;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLSSLNegativePacket;
-import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
+import java.nio.charset.UnsupportedCharsetException;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
-public final class PostgreSQLSSLNegativePacketTest {
+public final class MySQLCharacterSetTest {
@Test
- public void assertReadWrite() {
- ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(1);
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
- PostgreSQLSSLNegativePacket packet = new PostgreSQLSSLNegativePacket();
- packet.write(payload);
- assertThat(byteBuf.writerIndex(), is(1));
- assertThat(payload.readInt1(), is((int) 'N'));
+ public void assertFoundCharsetById() {
+ MySQLCharacterSet actual = MySQLCharacterSet.findById(255);
+ assertThat(actual, is(MySQLCharacterSet.UTF8MB4));
+ }
+
+ @Test(expected = UnsupportedCharsetException.class)
+ public void assertCharsetNotFoundById() {
+ MySQLCharacterSet.findById(-1);
+ }
+
+ @Test(expected = UnsupportedCharsetException.class)
+ public void assertFoundUnsupportedCharsetById() {
+ MySQLCharacterSet.findById(63);
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/MySQLColumnDefinition41PacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/MySQLColumnDefinition41PacketTest.java
index ec51e81..1102272 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/MySQLColumnDefinition41PacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/MySQLColumnDefinition41PacketTest.java
@@ -61,7 +61,7 @@ public final class MySQLColumnDefinition41PacketTest {
@Test
public void assertWriteWithPayload() {
when(payload.readInt1()).thenReturn(1, MySQLBinaryColumnType.MYSQL_TYPE_LONG.getValue(), 0);
- when(payload.readInt2()).thenReturn(MySQLServerInfo.CHARSET, 0);
+ when(payload.readInt2()).thenReturn(MySQLServerInfo.DEFAULT_CHARSET.getId(), 0);
when(payload.readInt4()).thenReturn(10);
when(payload.readIntLenenc()).thenReturn(0x0cL);
when(payload.readStringLenenc()).thenReturn("def", "logic_db", "tbl", "tbl", "id", "id");
@@ -77,7 +77,7 @@ public final class MySQLColumnDefinition41PacketTest {
verify(payload, times(2)).writeStringLenenc("tbl");
verify(payload, times(2)).writeStringLenenc("id");
verify(payload).writeIntLenenc(0x0c);
- verify(payload).writeInt2(MySQLServerInfo.CHARSET);
+ verify(payload).writeInt2(MySQLServerInfo.DEFAULT_CHARSET.getId());
verify(payload).writeInt4(10);
verify(payload).writeInt1(MySQLBinaryColumnType.MYSQL_TYPE_LONG.getValue());
verify(payload).writeInt2(0);
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/binary/execute/protocol/MySQLDoubleBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/binary/execute/protocol/MySQLDoubleBinaryProtocolValueTest.java
index fa20511..0179d71 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/binary/execute/protocol/MySQLDoubleBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/binary/execute/protocol/MySQLDoubleBinaryProtocolValueTest.java
@@ -24,6 +24,8 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
@@ -38,12 +40,12 @@ public final class MySQLDoubleBinaryProtocolValueTest {
@Test
public void assertRead() {
when(byteBuf.readDoubleLE()).thenReturn(1.0d);
- assertThat(new MySQLDoubleBinaryProtocolValue().read(new MySQLPacketPayload(byteBuf)), is(1.0d));
+ assertThat(new MySQLDoubleBinaryProtocolValue().read(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8)), is(1.0d));
}
@Test
public void assertWrite() {
- new MySQLDoubleBinaryProtocolValue().write(new MySQLPacketPayload(byteBuf), 1.0d);
+ new MySQLDoubleBinaryProtocolValue().write(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8), 1.0d);
verify(byteBuf).writeDoubleLE(1.0d);
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/binary/execute/protocol/MySQLFloatBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/binary/execute/protocol/MySQLFloatBinaryProtocolValueTest.java
index fe79f26..a1b1adf 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/binary/execute/protocol/MySQLFloatBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/binary/execute/protocol/MySQLFloatBinaryProtocolValueTest.java
@@ -24,6 +24,8 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
@@ -38,12 +40,12 @@ public final class MySQLFloatBinaryProtocolValueTest {
@Test
public void assertRead() {
when(byteBuf.readFloatLE()).thenReturn(1.0f);
- assertThat(new MySQLFloatBinaryProtocolValue().read(new MySQLPacketPayload(byteBuf)), is(1.0f));
+ assertThat(new MySQLFloatBinaryProtocolValue().read(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8)), is(1.0f));
}
@Test
public void assertWrite() {
- new MySQLFloatBinaryProtocolValue().write(new MySQLPacketPayload(byteBuf), 1.0f);
+ new MySQLFloatBinaryProtocolValue().write(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8), 1.0f);
verify(byteBuf).writeFloatLE(1.0f);
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacketTest.java
index b56e47f..ec8691c 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacketTest.java
@@ -44,7 +44,7 @@ public final class MySQLHandshakePacketTest {
@Test
public void assertNewWithPayload() {
- when(payload.readInt1()).thenReturn(0, MySQLServerInfo.PROTOCOL_VERSION, MySQLServerInfo.CHARSET, 0);
+ when(payload.readInt1()).thenReturn(0, MySQLServerInfo.PROTOCOL_VERSION, MySQLServerInfo.DEFAULT_CHARSET.getId(), 0);
when(payload.readStringNul()).thenReturn(MySQLServerInfo.getServerVersion());
when(payload.readStringNulByBytes()).thenReturn(part1, part2);
when(payload.readInt4()).thenReturn(1000);
@@ -55,7 +55,7 @@ public final class MySQLHandshakePacketTest {
assertThat(actual.getServerVersion(), is(MySQLServerInfo.getServerVersion()));
assertThat(actual.getCapabilityFlagsLower(), is(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower()));
assertThat(actual.getConnectionId(), is(1000));
- assertThat(actual.getCharacterSet(), is(MySQLServerInfo.CHARSET));
+ assertThat(actual.getCharacterSet(), is(MySQLServerInfo.DEFAULT_CHARSET.getId()));
assertThat(actual.getStatusFlag(), is(MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT));
assertThat(actual.getCapabilityFlagsUpper(), is(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsUpper()));
assertThat(actual.getAuthPluginData().getAuthPluginDataPart1(), is(part1));
@@ -65,7 +65,7 @@ public final class MySQLHandshakePacketTest {
@Test
public void assertNewWithClientPluginAuthPayload() {
- when(payload.readInt1()).thenReturn(0, MySQLServerInfo.PROTOCOL_VERSION, MySQLServerInfo.CHARSET, 0);
+ when(payload.readInt1()).thenReturn(0, MySQLServerInfo.PROTOCOL_VERSION, MySQLServerInfo.DEFAULT_CHARSET.getId(), 0);
when(payload.readStringNul()).thenReturn(MySQLServerInfo.getServerVersion(), MySQLAuthenticationMethod.SECURE_PASSWORD_AUTHENTICATION.getMethodName());
when(payload.readStringNulByBytes()).thenReturn(part1, part2);
when(payload.readInt4()).thenReturn(1000);
@@ -76,7 +76,7 @@ public final class MySQLHandshakePacketTest {
assertThat(actual.getServerVersion(), is(MySQLServerInfo.getServerVersion()));
assertThat(actual.getCapabilityFlagsLower(), is(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower()));
assertThat(actual.getConnectionId(), is(1000));
- assertThat(actual.getCharacterSet(), is(MySQLServerInfo.CHARSET));
+ assertThat(actual.getCharacterSet(), is(MySQLServerInfo.DEFAULT_CHARSET.getId()));
assertThat(actual.getStatusFlag(), is(MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT));
assertThat(actual.getCapabilityFlagsUpper(), is(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue() >> 16));
assertThat(actual.getAuthPluginData().getAuthPluginDataPart1(), is(part1));
@@ -94,7 +94,7 @@ public final class MySQLHandshakePacketTest {
verify(payload).writeInt4(1000);
verify(payload).writeStringNul(new String(authPluginData.getAuthPluginDataPart1()));
verify(payload).writeInt2(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower());
- verify(payload).writeInt1(MySQLServerInfo.CHARSET);
+ verify(payload).writeInt1(MySQLServerInfo.DEFAULT_CHARSET.getId());
verify(payload).writeInt2(MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue());
verify(payload).writeInt2(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsUpper());
verify(payload).writeInt1(authPluginData.getAuthenticationPluginData().length + 1);
@@ -113,7 +113,7 @@ public final class MySQLHandshakePacketTest {
verify(payload).writeInt4(1000);
verify(payload).writeStringNul(new String(authPluginData.getAuthPluginDataPart1()));
verify(payload).writeInt2(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower());
- verify(payload).writeInt1(MySQLServerInfo.CHARSET);
+ verify(payload).writeInt1(MySQLServerInfo.DEFAULT_CHARSET.getId());
verify(payload).writeInt2(MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue());
verify(payload).writeInt2(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue() >> 16);
verify(payload).writeInt1(authPluginData.getAuthenticationPluginData().length + 1);
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakeResponse41PacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakeResponse41PacketTest.java
index 2800892..2d3761e 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakeResponse41PacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakeResponse41PacketTest.java
@@ -40,14 +40,14 @@ public final class MySQLHandshakeResponse41PacketTest {
@Test
public void assertNewWithPayloadWithDatabase() {
- when(payload.readInt1()).thenReturn(1, MySQLServerInfo.CHARSET);
+ when(payload.readInt1()).thenReturn(1, MySQLServerInfo.DEFAULT_CHARSET.getId());
when(payload.readInt4()).thenReturn(MySQLCapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue(), 1000);
when(payload.readStringNul()).thenReturn("root", "sharding_db");
when(payload.readStringNulByBytes()).thenReturn(new byte[] {1});
MySQLHandshakeResponse41Packet actual = new MySQLHandshakeResponse41Packet(payload);
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getMaxPacketSize(), is(1000));
- assertThat(actual.getCharacterSet(), is(MySQLServerInfo.CHARSET));
+ assertThat(actual.getCharacterSet(), is(MySQLServerInfo.DEFAULT_CHARSET.getId()));
assertThat(actual.getUsername(), is("root"));
assertThat(actual.getAuthResponse(), is(new byte[] {1}));
assertThat(actual.getCapabilityFlags(), is(MySQLCapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue()));
@@ -58,14 +58,14 @@ public final class MySQLHandshakeResponse41PacketTest {
@Test
public void assertNewWithPayloadWithAuthPluginName() {
- when(payload.readInt1()).thenReturn(1, MySQLServerInfo.CHARSET);
+ when(payload.readInt1()).thenReturn(1, MySQLServerInfo.DEFAULT_CHARSET.getId());
when(payload.readInt4()).thenReturn(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue(), 1000);
when(payload.readStringNul()).thenReturn("root", MySQLAuthenticationMethod.SECURE_PASSWORD_AUTHENTICATION.getMethodName());
when(payload.readStringNulByBytes()).thenReturn(new byte[] {1});
MySQLHandshakeResponse41Packet actual = new MySQLHandshakeResponse41Packet(payload);
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getMaxPacketSize(), is(1000));
- assertThat(actual.getCharacterSet(), is(MySQLServerInfo.CHARSET));
+ assertThat(actual.getCharacterSet(), is(MySQLServerInfo.DEFAULT_CHARSET.getId()));
assertThat(actual.getUsername(), is("root"));
assertThat(actual.getAuthResponse(), is(new byte[] {1}));
assertThat(actual.getCapabilityFlags(), is(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue()));
@@ -76,14 +76,14 @@ public final class MySQLHandshakeResponse41PacketTest {
@Test
public void assertNewWithPayloadWithClientPluginAuthLenencClientData() {
- when(payload.readInt1()).thenReturn(1, MySQLServerInfo.CHARSET);
+ when(payload.readInt1()).thenReturn(1, MySQLServerInfo.DEFAULT_CHARSET.getId());
when(payload.readInt4()).thenReturn(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA.getValue(), 1000);
when(payload.readStringNul()).thenReturn("root");
when(payload.readStringLenencByBytes()).thenReturn(new byte[] {1});
MySQLHandshakeResponse41Packet actual = new MySQLHandshakeResponse41Packet(payload);
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getMaxPacketSize(), is(1000));
- assertThat(actual.getCharacterSet(), is(MySQLServerInfo.CHARSET));
+ assertThat(actual.getCharacterSet(), is(MySQLServerInfo.DEFAULT_CHARSET.getId()));
assertThat(actual.getUsername(), is("root"));
assertThat(actual.getAuthResponse(), is(new byte[] {1}));
assertThat(actual.getCapabilityFlags(), is(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA.getValue()));
@@ -94,14 +94,14 @@ public final class MySQLHandshakeResponse41PacketTest {
@Test
public void assertNewWithPayloadWithClientSecureConnection() {
- when(payload.readInt1()).thenReturn(1, MySQLServerInfo.CHARSET, 1);
+ when(payload.readInt1()).thenReturn(1, MySQLServerInfo.DEFAULT_CHARSET.getId(), 1);
when(payload.readInt4()).thenReturn(MySQLCapabilityFlag.CLIENT_SECURE_CONNECTION.getValue(), 1000);
when(payload.readStringNul()).thenReturn("root");
when(payload.readStringFixByBytes(1)).thenReturn(new byte[] {1});
MySQLHandshakeResponse41Packet actual = new MySQLHandshakeResponse41Packet(payload);
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getMaxPacketSize(), is(1000));
- assertThat(actual.getCharacterSet(), is(MySQLServerInfo.CHARSET));
+ assertThat(actual.getCharacterSet(), is(MySQLServerInfo.DEFAULT_CHARSET.getId()));
assertThat(actual.getUsername(), is("root"));
assertThat(actual.getAuthResponse(), is(new byte[] {1}));
assertThat(actual.getCapabilityFlags(), is(MySQLCapabilityFlag.CLIENT_SECURE_CONNECTION.getValue()));
@@ -112,13 +112,13 @@ public final class MySQLHandshakeResponse41PacketTest {
@Test
public void assertWriteWithDatabase() {
- MySQLHandshakeResponse41Packet actual = new MySQLHandshakeResponse41Packet(1, 100, MySQLServerInfo.CHARSET, "root");
+ MySQLHandshakeResponse41Packet actual = new MySQLHandshakeResponse41Packet(1, 100, MySQLServerInfo.DEFAULT_CHARSET.getId(), "root");
actual.setAuthResponse(new byte[] {1});
actual.setDatabase("sharding_db");
actual.write(payload);
verify(payload).writeInt4(MySQLCapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue());
verify(payload).writeInt4(100);
- verify(payload).writeInt1(MySQLServerInfo.CHARSET);
+ verify(payload).writeInt1(MySQLServerInfo.DEFAULT_CHARSET.getId());
verify(payload).writeReserved(23);
verify(payload).writeStringNul("root");
verify(payload).writeStringNul(new String(new byte[] {1}));
@@ -127,13 +127,13 @@ public final class MySQLHandshakeResponse41PacketTest {
@Test
public void assertWriteWithAuthPluginName() {
- MySQLHandshakeResponse41Packet actual = new MySQLHandshakeResponse41Packet(1, 100, MySQLServerInfo.CHARSET, "root");
+ MySQLHandshakeResponse41Packet actual = new MySQLHandshakeResponse41Packet(1, 100, MySQLServerInfo.DEFAULT_CHARSET.getId(), "root");
actual.setAuthResponse(new byte[] {1});
actual.setAuthPluginName(MySQLAuthenticationMethod.SECURE_PASSWORD_AUTHENTICATION);
actual.write(payload);
verify(payload).writeInt4(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue());
verify(payload).writeInt4(100);
- verify(payload).writeInt1(MySQLServerInfo.CHARSET);
+ verify(payload).writeInt1(MySQLServerInfo.DEFAULT_CHARSET.getId());
verify(payload).writeReserved(23);
verify(payload).writeStringNul("root");
verify(payload).writeStringNul(new String(new byte[] {1}));
@@ -142,13 +142,13 @@ public final class MySQLHandshakeResponse41PacketTest {
@Test
public void assertWriteWithClientPluginAuthLenencClientData() {
- MySQLHandshakeResponse41Packet actual = new MySQLHandshakeResponse41Packet(1, 100, MySQLServerInfo.CHARSET, "root");
+ MySQLHandshakeResponse41Packet actual = new MySQLHandshakeResponse41Packet(1, 100, MySQLServerInfo.DEFAULT_CHARSET.getId(), "root");
actual.setCapabilityFlags(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA.getValue());
actual.setAuthResponse(new byte[] {1});
actual.write(payload);
verify(payload).writeInt4(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA.getValue());
verify(payload).writeInt4(100);
- verify(payload).writeInt1(MySQLServerInfo.CHARSET);
+ verify(payload).writeInt1(MySQLServerInfo.DEFAULT_CHARSET.getId());
verify(payload).writeReserved(23);
verify(payload).writeStringNul("root");
verify(payload).writeStringLenenc(new String(new byte[] {1}));
@@ -156,13 +156,13 @@ public final class MySQLHandshakeResponse41PacketTest {
@Test
public void assertWriteWithClientSecureConnection() {
- MySQLHandshakeResponse41Packet actual = new MySQLHandshakeResponse41Packet(1, 100, MySQLServerInfo.CHARSET, "root");
+ MySQLHandshakeResponse41Packet actual = new MySQLHandshakeResponse41Packet(1, 100, MySQLServerInfo.DEFAULT_CHARSET.getId(), "root");
actual.setCapabilityFlags(MySQLCapabilityFlag.CLIENT_SECURE_CONNECTION.getValue());
actual.setAuthResponse(new byte[] {1});
actual.write(payload);
verify(payload).writeInt4(MySQLCapabilityFlag.CLIENT_SECURE_CONNECTION.getValue());
verify(payload).writeInt4(100);
- verify(payload).writeInt1(MySQLServerInfo.CHARSET);
+ verify(payload).writeInt1(MySQLServerInfo.DEFAULT_CHARSET.getId());
verify(payload).writeReserved(23);
verify(payload).writeStringNul("root");
verify(payload).writeInt1(1);
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java
index 3633f8b..614b9ae 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java
@@ -23,6 +23,8 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.times;
@@ -38,132 +40,132 @@ public final class MySQLPacketPayloadTest {
@Test
public void assertReadInt1() {
when(byteBuf.readUnsignedByte()).thenReturn((short) 1);
- assertThat(new MySQLPacketPayload(byteBuf).readInt1(), is(1));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readInt1(), is(1));
}
@Test
public void assertWriteInt1() {
- new MySQLPacketPayload(byteBuf).writeInt1(1);
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeInt1(1);
verify(byteBuf).writeByte(1);
}
@Test
public void assertReadInt2() {
when(byteBuf.readUnsignedShortLE()).thenReturn(1);
- assertThat(new MySQLPacketPayload(byteBuf).readInt2(), is(1));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readInt2(), is(1));
}
@Test
public void assertWriteInt2() {
- new MySQLPacketPayload(byteBuf).writeInt2(1);
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeInt2(1);
verify(byteBuf).writeShortLE(1);
}
@Test
public void assertReadInt3() {
when(byteBuf.readUnsignedMediumLE()).thenReturn(1);
- assertThat(new MySQLPacketPayload(byteBuf).readInt3(), is(1));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readInt3(), is(1));
}
@Test
public void assertWriteInt3() {
- new MySQLPacketPayload(byteBuf).writeInt3(1);
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeInt3(1);
verify(byteBuf).writeMediumLE(1);
}
@Test
public void assertReadInt4() {
when(byteBuf.readIntLE()).thenReturn(1);
- assertThat(new MySQLPacketPayload(byteBuf).readInt4(), is(1));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readInt4(), is(1));
}
@Test
public void assertWriteInt4() {
- new MySQLPacketPayload(byteBuf).writeInt4(1);
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeInt4(1);
verify(byteBuf).writeIntLE(1);
}
@Test
public void assertReadInt6() {
when(byteBuf.readByte()).thenReturn((byte) 0x01, (byte) 0x00);
- assertThat(new MySQLPacketPayload(byteBuf).readInt6(), is(1L));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readInt6(), is(1L));
when(byteBuf.readByte()).thenReturn((byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x80);
- assertThat(new MySQLPacketPayload(byteBuf).readInt6(), is(0x800000000000L));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readInt6(), is(0x800000000000L));
}
@Test
public void assertWriteInt6() {
- new MySQLPacketPayload(byteBuf).writeInt6(1L);
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeInt6(1L);
}
@Test
public void assertReadInt8() {
when(byteBuf.readLongLE()).thenReturn(1L);
- assertThat(new MySQLPacketPayload(byteBuf).readInt8(), is(1L));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readInt8(), is(1L));
}
@Test
public void assertWriteInt8() {
- new MySQLPacketPayload(byteBuf).writeInt8(1L);
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeInt8(1L);
verify(byteBuf).writeLongLE(1L);
}
@Test
public void assertReadIntLenencWithOneByte() {
when(byteBuf.readUnsignedByte()).thenReturn((short) 1);
- assertThat(new MySQLPacketPayload(byteBuf).readIntLenenc(), is(1L));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readIntLenenc(), is(1L));
}
@Test
public void assertReadIntLenencWithZero() {
when(byteBuf.readUnsignedByte()).thenReturn((short) 0xfb);
- assertThat(new MySQLPacketPayload(byteBuf).readIntLenenc(), is(0L));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readIntLenenc(), is(0L));
}
@Test
public void assertReadIntLenencWithTwoBytes() {
when(byteBuf.readUnsignedByte()).thenReturn((short) 0xfc);
when(byteBuf.readUnsignedShortLE()).thenReturn(100);
- assertThat(new MySQLPacketPayload(byteBuf).readIntLenenc(), is(100L));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readIntLenenc(), is(100L));
}
@Test
public void assertReadIntLenencWithThreeBytes() {
when(byteBuf.readUnsignedByte()).thenReturn((short) 0xfd);
when(byteBuf.readUnsignedMediumLE()).thenReturn(99999);
- assertThat(new MySQLPacketPayload(byteBuf).readIntLenenc(), is(99999L));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readIntLenenc(), is(99999L));
}
@Test
public void assertReadIntLenencWithFourBytes() {
when(byteBuf.readUnsignedByte()).thenReturn((short) 0xff);
when(byteBuf.readLongLE()).thenReturn(Long.MAX_VALUE);
- assertThat(new MySQLPacketPayload(byteBuf).readIntLenenc(), is(Long.MAX_VALUE));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readIntLenenc(), is(Long.MAX_VALUE));
}
@Test
public void assertWriteIntLenencWithOneByte() {
- new MySQLPacketPayload(byteBuf).writeIntLenenc(1L);
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(1L);
verify(byteBuf).writeByte(1);
}
@Test
public void assertWriteIntLenencWithTwoBytes() {
- new MySQLPacketPayload(byteBuf).writeIntLenenc(new Double(Math.pow(2, 16)).longValue() - 1);
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(new Double(Math.pow(2, 16)).longValue() - 1);
verify(byteBuf).writeByte(0xfc);
verify(byteBuf).writeShortLE(new Double(Math.pow(2, 16)).intValue() - 1);
}
@Test
public void assertWriteIntLenencWithThreeBytes() {
- new MySQLPacketPayload(byteBuf).writeIntLenenc(new Double(Math.pow(2, 24)).longValue() - 1);
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(new Double(Math.pow(2, 24)).longValue() - 1);
verify(byteBuf).writeByte(0xfd);
verify(byteBuf).writeMediumLE(new Double(Math.pow(2, 24)).intValue() - 1);
}
@Test
public void assertWriteIntLenencWithFourBytes() {
- new MySQLPacketPayload(byteBuf).writeIntLenenc(new Double(Math.pow(2, 25)).longValue() - 1);
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(new Double(Math.pow(2, 25)).longValue() - 1);
verify(byteBuf).writeByte(0xfe);
verify(byteBuf).writeLongLE(new Double(Math.pow(2, 25)).intValue() - 1);
}
@@ -171,90 +173,90 @@ public final class MySQLPacketPayloadTest {
@Test
public void assertReadStringLenenc() {
when(byteBuf.readUnsignedByte()).thenReturn((short) 0);
- assertThat(new MySQLPacketPayload(byteBuf).readStringLenenc(), is(""));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readStringLenenc(), is(""));
}
@Test
public void assertReadStringLenencByBytes() {
when(byteBuf.readUnsignedByte()).thenReturn((short) 0);
- assertThat(new MySQLPacketPayload(byteBuf).readStringLenencByBytes(), is(new byte[] {}));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readStringLenencByBytes(), is(new byte[] {}));
}
@Test
public void assertWriteStringLenencWithEmpty() {
- new MySQLPacketPayload(byteBuf).writeStringLenenc("");
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeStringLenenc("");
verify(byteBuf).writeByte(0);
}
@Test
public void assertWriteBytesLenenc() {
- new MySQLPacketPayload(byteBuf).writeBytesLenenc("value".getBytes());
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeBytesLenenc("value".getBytes());
verify(byteBuf).writeByte(5);
verify(byteBuf).writeBytes("value".getBytes());
}
@Test
public void assertWriteBytesLenencWithEmpty() {
- new MySQLPacketPayload(byteBuf).writeBytesLenenc("".getBytes());
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeBytesLenenc("".getBytes());
verify(byteBuf).writeByte(0);
}
@Test
public void assertWriteStringLenenc() {
- new MySQLPacketPayload(byteBuf).writeStringLenenc("value");
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeStringLenenc("value");
verify(byteBuf).writeByte(5);
verify(byteBuf).writeBytes("value".getBytes());
}
@Test
public void assertReadStringFix() {
- assertThat(new MySQLPacketPayload(byteBuf).readStringFix(0), is(""));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readStringFix(0), is(""));
}
@Test
public void assertReadStringFixByBytes() {
- assertThat(new MySQLPacketPayload(byteBuf).readStringFixByBytes(0), is(new byte[] {}));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readStringFixByBytes(0), is(new byte[] {}));
}
@Test
public void assertWriteStringFix() {
- new MySQLPacketPayload(byteBuf).writeStringFix("value");
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeStringFix("value");
verify(byteBuf).writeBytes("value".getBytes());
}
@Test
public void assertWriteBytes() {
- new MySQLPacketPayload(byteBuf).writeBytes("value".getBytes());
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeBytes("value".getBytes());
verify(byteBuf).writeBytes("value".getBytes());
}
@Test
public void assertReadStringVar() {
- assertThat(new MySQLPacketPayload(byteBuf).readStringVar(), is(""));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readStringVar(), is(""));
}
@Test
public void assertWriteStringVar() {
- new MySQLPacketPayload(byteBuf).writeStringVar("");
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeStringVar("");
}
@Test
public void assertReadStringNul() {
when(byteBuf.bytesBefore((byte) 0)).thenReturn(0);
- assertThat(new MySQLPacketPayload(byteBuf).readStringNul(), is(""));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readStringNul(), is(""));
verify(byteBuf).skipBytes(1);
}
@Test
public void assertReadStringNulByBytes() {
when(byteBuf.bytesBefore((byte) 0)).thenReturn(0);
- assertThat(new MySQLPacketPayload(byteBuf).readStringNulByBytes(), is(new byte[] {}));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readStringNulByBytes(), is(new byte[] {}));
verify(byteBuf).skipBytes(1);
}
@Test
public void assertWriteStringNul() {
- new MySQLPacketPayload(byteBuf).writeStringNul("value");
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeStringNul("value");
verify(byteBuf).writeBytes("value".getBytes());
verify(byteBuf).writeByte(0);
}
@@ -262,30 +264,30 @@ public final class MySQLPacketPayloadTest {
@Test
public void assertReadStringEOF() {
when(byteBuf.readableBytes()).thenReturn(0);
- assertThat(new MySQLPacketPayload(byteBuf).readStringEOF(), is(""));
+ assertThat(new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).readStringEOF(), is(""));
}
@Test
public void assertWriteStringEOF() {
- new MySQLPacketPayload(byteBuf).writeStringEOF("value");
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeStringEOF("value");
verify(byteBuf).writeBytes("value".getBytes());
}
@Test
public void assertSkipReserved() {
- new MySQLPacketPayload(byteBuf).skipReserved(10);
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).skipReserved(10);
verify(byteBuf).skipBytes(10);
}
@Test
public void assertWriteReserved() {
- new MySQLPacketPayload(byteBuf).writeReserved(10);
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeReserved(10);
verify(byteBuf, times(1)).writeZero(10);
}
@Test
public void assertClose() {
- new MySQLPacketPayload(byteBuf).close();
+ new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).close();
verify(byteBuf).release();
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngine.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngine.java
index 532f676..3cb44fa 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngine.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngine.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.db.protocol.postgresql.codec;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.codec.DatabasePacketCodecEngine;
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLErrorCode;
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLMessageSeverityLevel;
@@ -27,6 +28,7 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQ
import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLIdentifierPacket;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
+import java.nio.charset.Charset;
import java.util.List;
/**
@@ -68,7 +70,7 @@ public final class PostgreSQLPacketCodecEngine implements DatabasePacketCodecEng
if (isPostgreSQLIdentifierPacket) {
prepareMessageHeader(out, ((PostgreSQLIdentifierPacket) message).getIdentifier().getValue());
}
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(out);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(out, context.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).get());
try {
message.write(payload);
// CHECKSTYLE:OFF
@@ -98,7 +100,7 @@ public final class PostgreSQLPacketCodecEngine implements DatabasePacketCodecEng
}
@Override
- public PostgreSQLPacketPayload createPacketPayload(final ByteBuf message) {
- return new PostgreSQLPacketPayload(message);
+ public PostgreSQLPacketPayload createPacketPayload(final ByteBuf message, final Charset charset) {
+ return new PostgreSQLPacketPayload(message, charset);
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/handshake/PostgreSQLComStartupPacket.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/handshake/PostgreSQLComStartupPacket.java
index 7fcedd4..a695e3a 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/handshake/PostgreSQLComStartupPacket.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/handshake/PostgreSQLComStartupPacket.java
@@ -32,6 +32,8 @@ public final class PostgreSQLComStartupPacket implements PostgreSQLPacket {
private static final String USER_NAME_KEY = "user";
+ private static final String CLIENT_ENCODING_KEY = "client_encoding";
+
private final Map<String, String> parametersMap = new HashMap<>();
public PostgreSQLComStartupPacket(final PostgreSQLPacketPayload payload) {
@@ -59,6 +61,15 @@ public final class PostgreSQLComStartupPacket implements PostgreSQLPacket {
return parametersMap.get(USER_NAME_KEY);
}
+ /**
+ * Get client encoding.
+ *
+ * @return client encoding
+ */
+ public String getClientEncoding() {
+ return parametersMap.get(CLIENT_ENCODING_KEY);
+ }
+
@Override
public void write(final PostgreSQLPacketPayload payload) {
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/payload/PostgreSQLPacketPayload.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/payload/PostgreSQLPacketPayload.java
index 54115de..fd0d3e2 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/payload/PostgreSQLPacketPayload.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/payload/PostgreSQLPacketPayload.java
@@ -22,6 +22,8 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.db.protocol.payload.PacketPayload;
+import java.nio.charset.Charset;
+
/**
* Payload operation for PostgreSQL packet data types.
*
@@ -33,6 +35,8 @@ public final class PostgreSQLPacketPayload implements PacketPayload {
private final ByteBuf byteBuf;
+ private final Charset charset;
+
/**
* Read 1 byte fixed length integer from byte buffers.
*
@@ -132,7 +136,7 @@ public final class PostgreSQLPacketPayload implements PacketPayload {
byte[] result = new byte[byteBuf.bytesBefore((byte) 0)];
byteBuf.readBytes(result);
byteBuf.skipBytes(1);
- return new String(result);
+ return new String(result, charset);
}
/**
@@ -141,7 +145,7 @@ public final class PostgreSQLPacketPayload implements PacketPayload {
* @param value null terminated string
*/
public void writeStringNul(final String value) {
- byteBuf.writeBytes(value.getBytes());
+ byteBuf.writeBytes(value.getBytes(charset));
byteBuf.writeByte(0);
}
@@ -151,7 +155,7 @@ public final class PostgreSQLPacketPayload implements PacketPayload {
* @param value rest of packet string
*/
public void writeStringEOF(final String value) {
- byteBuf.writeBytes(value.getBytes());
+ byteBuf.writeBytes(value.getBytes(charset));
}
/**
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngineTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngineTest.java
index c6c884d..97be93e 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngineTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngineTest.java
@@ -19,15 +19,20 @@ package org.apache.shardingsphere.db.protocol.postgresql.codec;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
+import io.netty.util.AttributeKey;
import org.apache.shardingsphere.db.protocol.postgresql.packet.PostgreSQLPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLIdentifierPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLMessagePacketType;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
@@ -44,12 +49,17 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLPacketCodecEngineTest {
- @Mock
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ChannelHandlerContext context;
@Mock
private ByteBuf byteBuf;
+ @Before
+ public void setup() {
+ when(context.channel().attr(AttributeKey.<Charset>valueOf(Charset.class.getName())).get()).thenReturn(StandardCharsets.UTF_8);
+ }
+
@Test
public void assertIsValidHeader() {
assertTrue(new PostgreSQLPacketCodecEngine().isValidHeader(50));
@@ -111,6 +121,6 @@ public final class PostgreSQLPacketCodecEngineTest {
@Test
public void assertCreatePacketPayload() {
- assertThat(new PostgreSQLPacketCodecEngine().createPacketPayload(byteBuf).getByteBuf(), is(byteBuf));
+ assertThat(new PostgreSQLPacketCodecEngine().createPacketPayload(byteBuf, StandardCharsets.UTF_8).getByteBuf(), is(byteBuf));
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/admin/PostgreSQLUnsupportedCommandPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/admin/PostgreSQLUnsupportedCommandPacketTest.java
index 515023c..1072615 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/admin/PostgreSQLUnsupportedCommandPacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/admin/PostgreSQLUnsupportedCommandPacketTest.java
@@ -25,6 +25,8 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -37,7 +39,7 @@ public final class PostgreSQLUnsupportedCommandPacketTest {
@Test
public void assertWrite() {
PostgreSQLUnsupportedCommandPacket rowPacket = new PostgreSQLUnsupportedCommandPacket(PostgreSQLMessagePacketType.AUTHENTICATION_REQUEST);
- rowPacket.write(new PostgreSQLPacketPayload(byteBuf));
+ rowPacket.write(new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8));
assertThat(byteBuf.writerIndex(), is(0));
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLBoolArrayBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLBoolArrayBinaryProtocolValueTest.java
index cabf82c..0eab16f 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLBoolArrayBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLBoolArrayBinaryProtocolValueTest.java
@@ -47,7 +47,7 @@ public final class PostgreSQLBoolArrayBinaryProtocolValueTest {
byteBuf.writeInt(parameterValue.length());
byteBuf.writeCharSequence(parameterValue, StandardCharsets.ISO_8859_1);
byteBuf.readInt();
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
Object result = newInstance().read(payload, parameterValue.length());
assertNotNull(result);
assertThat(result, is(new boolean[] {true, false}));
@@ -56,7 +56,7 @@ public final class PostgreSQLBoolArrayBinaryProtocolValueTest {
@Test(expected = UnsupportedOperationException.class)
public void assertWrite() {
- newInstance().write(new PostgreSQLPacketPayload(null), "val");
+ newInstance().write(new PostgreSQLPacketPayload(null, StandardCharsets.UTF_8), "val");
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLDoubleBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLDoubleBinaryProtocolValueTest.java
index 63cd78a..db04d78 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLDoubleBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLDoubleBinaryProtocolValueTest.java
@@ -24,6 +24,8 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
@@ -43,12 +45,12 @@ public final class PostgreSQLDoubleBinaryProtocolValueTest {
@Test
public void assertRead() {
when(byteBuf.readDouble()).thenReturn(1D);
- assertThat(new PostgreSQLDoubleBinaryProtocolValue().read(new PostgreSQLPacketPayload(byteBuf), 8), is(1D));
+ assertThat(new PostgreSQLDoubleBinaryProtocolValue().read(new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8), 8), is(1D));
}
@Test
public void assertWrite() {
- new PostgreSQLDoubleBinaryProtocolValue().write(new PostgreSQLPacketPayload(byteBuf), 1D);
+ new PostgreSQLDoubleBinaryProtocolValue().write(new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8), 1D);
verify(byteBuf).writeDouble(1.0d);
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLFloat4ArrayBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLFloat4ArrayBinaryProtocolValueTest.java
index 4608856..7e2f493 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLFloat4ArrayBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLFloat4ArrayBinaryProtocolValueTest.java
@@ -47,7 +47,7 @@ public final class PostgreSQLFloat4ArrayBinaryProtocolValueTest {
byteBuf.writeInt(parameterValue.length());
byteBuf.writeCharSequence(parameterValue, StandardCharsets.ISO_8859_1);
byteBuf.readInt();
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
Object result = newInstance().read(payload, parameterValue.length());
assertNotNull(result);
assertThat(result, is(new float[] {11.1F, 12.1F}));
@@ -56,7 +56,7 @@ public final class PostgreSQLFloat4ArrayBinaryProtocolValueTest {
@Test(expected = UnsupportedOperationException.class)
public void assertWrite() {
- newInstance().write(new PostgreSQLPacketPayload(null), "val");
+ newInstance().write(new PostgreSQLPacketPayload(null, StandardCharsets.UTF_8), "val");
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLFloat8ArrayBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLFloat8ArrayBinaryProtocolValueTest.java
index eecc287..d2c556f 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLFloat8ArrayBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLFloat8ArrayBinaryProtocolValueTest.java
@@ -47,7 +47,7 @@ public final class PostgreSQLFloat8ArrayBinaryProtocolValueTest {
byteBuf.writeInt(parameterValue.length());
byteBuf.writeCharSequence(parameterValue, StandardCharsets.ISO_8859_1);
byteBuf.readInt();
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
Object result = newInstance().read(payload, parameterValue.length());
assertNotNull(result);
assertThat(result, is(new double[] {11.1D, 12.1D}));
@@ -56,7 +56,7 @@ public final class PostgreSQLFloat8ArrayBinaryProtocolValueTest {
@Test(expected = UnsupportedOperationException.class)
public void assertWrite() {
- newInstance().write(new PostgreSQLPacketPayload(null), "val");
+ newInstance().write(new PostgreSQLPacketPayload(null, StandardCharsets.UTF_8), "val");
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLFloatBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLFloatBinaryProtocolValueTest.java
index a3f9c67..46427ac 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLFloatBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLFloatBinaryProtocolValueTest.java
@@ -24,6 +24,8 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
@@ -43,12 +45,12 @@ public final class PostgreSQLFloatBinaryProtocolValueTest {
@Test
public void assertRead() {
when(byteBuf.readFloat()).thenReturn(1F);
- assertThat(new PostgreSQLFloatBinaryProtocolValue().read(new PostgreSQLPacketPayload(byteBuf), 4), is(1.0F));
+ assertThat(new PostgreSQLFloatBinaryProtocolValue().read(new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8), 4), is(1.0F));
}
@Test
public void assertWrite() {
- new PostgreSQLFloatBinaryProtocolValue().write(new PostgreSQLPacketPayload(byteBuf), 1F);
+ new PostgreSQLFloatBinaryProtocolValue().write(new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8), 1F);
verify(byteBuf).writeFloat(1.0f);
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt2ArrayBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt2ArrayBinaryProtocolValueTest.java
index 7b37ce5..80fe9f6 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt2ArrayBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt2ArrayBinaryProtocolValueTest.java
@@ -47,7 +47,7 @@ public final class PostgreSQLInt2ArrayBinaryProtocolValueTest {
byteBuf.writeInt(parameterValue.length());
byteBuf.writeCharSequence(parameterValue, StandardCharsets.ISO_8859_1);
byteBuf.readInt();
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
Object result = newInstance().read(payload, parameterValue.length());
assertNotNull(result);
assertThat(result, is(new short[] {11, 12}));
@@ -56,7 +56,7 @@ public final class PostgreSQLInt2ArrayBinaryProtocolValueTest {
@Test(expected = UnsupportedOperationException.class)
public void assertWrite() {
- newInstance().write(new PostgreSQLPacketPayload(null), "val");
+ newInstance().write(new PostgreSQLPacketPayload(null, StandardCharsets.UTF_8), "val");
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt4ArrayBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt4ArrayBinaryProtocolValueTest.java
index 2567e39..de76504 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt4ArrayBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt4ArrayBinaryProtocolValueTest.java
@@ -47,7 +47,7 @@ public final class PostgreSQLInt4ArrayBinaryProtocolValueTest {
byteBuf.writeInt(parameterValue.length());
byteBuf.writeCharSequence(parameterValue, StandardCharsets.ISO_8859_1);
byteBuf.readInt();
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
Object result = newInstance().read(payload, parameterValue.length());
assertNotNull(result);
assertThat(result, is(new int[] {11, 12}));
@@ -56,7 +56,7 @@ public final class PostgreSQLInt4ArrayBinaryProtocolValueTest {
@Test(expected = UnsupportedOperationException.class)
public void assertWrite() {
- newInstance().write(new PostgreSQLPacketPayload(null), "val");
+ newInstance().write(new PostgreSQLPacketPayload(null, StandardCharsets.UTF_8), "val");
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt8ArrayBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt8ArrayBinaryProtocolValueTest.java
index 4cbfc03..f67424b 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt8ArrayBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt8ArrayBinaryProtocolValueTest.java
@@ -47,7 +47,7 @@ public final class PostgreSQLInt8ArrayBinaryProtocolValueTest {
byteBuf.writeInt(parameterValue.length());
byteBuf.writeCharSequence(parameterValue, StandardCharsets.ISO_8859_1);
byteBuf.readInt();
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
Object result = newInstance().read(payload, parameterValue.length());
assertNotNull(result);
assertThat(result, is(new long[] {11, 12}));
@@ -56,7 +56,7 @@ public final class PostgreSQLInt8ArrayBinaryProtocolValueTest {
@Test(expected = UnsupportedOperationException.class)
public void assertWrite() {
- newInstance().write(new PostgreSQLPacketPayload(null), "val");
+ newInstance().write(new PostgreSQLPacketPayload(null, StandardCharsets.UTF_8), "val");
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLNumericBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLNumericBinaryProtocolValueTest.java
index c73dab1..14ce2048e 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLNumericBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLNumericBinaryProtocolValueTest.java
@@ -50,7 +50,7 @@ public final class PostgreSQLNumericBinaryProtocolValueTest {
byteBuf.writeInt(columnLength);
byteBuf.writeBytes(decimalText.getBytes(StandardCharsets.UTF_8));
byteBuf.readInt();
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
Object result = binaryProtocolValue.read(payload, columnLength);
assertNotNull(result);
assertTrue(result instanceof BigDecimal);
@@ -65,7 +65,7 @@ public final class PostgreSQLNumericBinaryProtocolValueTest {
BigDecimal decimal = new BigDecimal(decimalText);
int columnLength = binaryProtocolValue.getColumnLength(decimal);
ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(columnLength);
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
binaryProtocolValue.write(payload, decimal);
byte[] actualBytes = new byte[columnLength];
byteBuf.readBytes(actualBytes);
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLStringArrayBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLStringArrayBinaryProtocolValueTest.java
index 6a1ea7d..0772ff6 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLStringArrayBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLStringArrayBinaryProtocolValueTest.java
@@ -47,7 +47,7 @@ public final class PostgreSQLStringArrayBinaryProtocolValueTest {
byteBuf.writeInt(parameterValue.length());
byteBuf.writeCharSequence(parameterValue, StandardCharsets.ISO_8859_1);
byteBuf.readInt();
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
Object result = newInstance().read(payload, parameterValue.length());
assertNotNull(result);
assertThat(result, is(new String[] {"a", "b"}));
@@ -56,7 +56,7 @@ public final class PostgreSQLStringArrayBinaryProtocolValueTest {
@Test(expected = UnsupportedOperationException.class)
public void assertWrite() {
- newInstance().write(new PostgreSQLPacketPayload(null), "val");
+ newInstance().write(new PostgreSQLPacketPayload(null, StandardCharsets.UTF_8), "val");
}
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLStringBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLStringBinaryProtocolValueTest.java
index 2b2b942..89ec38b 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLStringBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLStringBinaryProtocolValueTest.java
@@ -19,13 +19,15 @@ package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.bi
import io.netty.buffer.ByteBuf;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
@@ -35,23 +37,27 @@ import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLStringBinaryProtocolValueTest {
- @InjectMocks
- private PostgreSQLPacketPayload payload;
-
@Mock
private ByteBuf byteBuf;
+ private PostgreSQLPacketPayload payload;
+
+ @Before
+ public void setup() {
+ payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
+ }
+
@Test
public void assertNewInstance() {
doAnswer((Answer<ByteBuf>) invocation -> {
- ((byte[]) invocation.getArguments()[0])[0] = 97;
+ ((byte[]) invocation.getArguments()[0])[0] = 'a';
return byteBuf;
}).when(byteBuf).readBytes(any(byte[].class));
PostgreSQLStringBinaryProtocolValue actual = new PostgreSQLStringBinaryProtocolValue();
assertThat(actual.getColumnLength("str"), is("str".length()));
assertThat(actual.read(payload, "a".length()), is("a"));
actual.write(payload, "a");
- verify(byteBuf).writeBytes("a".getBytes());
+ verify(byteBuf).writeBytes("a".getBytes(StandardCharsets.UTF_8));
actual.write(payload, new byte[1]);
verify(byteBuf).writeBytes(new byte[1]);
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLUnspecifiedBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLUnspecifiedBinaryProtocolValueTest.java
index f15a2b4..cf8b519 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLUnspecifiedBinaryProtocolValueTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLUnspecifiedBinaryProtocolValueTest.java
@@ -46,7 +46,7 @@ public final class PostgreSQLUnspecifiedBinaryProtocolValueTest {
byteBuf.writeInt(timestampStr.length());
byteBuf.writeCharSequence(timestampStr, StandardCharsets.ISO_8859_1);
byteBuf.readInt();
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
Object result = new PostgreSQLUnspecifiedBinaryProtocolValue().read(payload, timestampStr.length());
assertNotNull(result);
assertTrue(result instanceof PostgreSQLTypeUnspecifiedSQLParameter);
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLAuthenticationMD5PasswordPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLAuthenticationMD5PasswordPacketTest.java
index 5942cba..7924fae 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLAuthenticationMD5PasswordPacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLAuthenticationMD5PasswordPacketTest.java
@@ -24,6 +24,8 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.Postgre
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -34,7 +36,7 @@ public final class PostgreSQLAuthenticationMD5PasswordPacketTest {
byte[] md5Salt = "salt".getBytes();
int expectedLength = 4 + md5Salt.length;
ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(expectedLength);
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
PostgreSQLAuthenticationMD5PasswordPacket packet = new PostgreSQLAuthenticationMD5PasswordPacket(md5Salt);
assertThat(packet.getIdentifier(), is(PostgreSQLMessagePacketType.AUTHENTICATION_REQUEST));
packet.write(payload);
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLAuthenticationOKPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLAuthenticationOKPacketTest.java
index 59343c4..76f70f6 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLAuthenticationOKPacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLAuthenticationOKPacketTest.java
@@ -24,6 +24,8 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.Postgr
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -36,7 +38,7 @@ public final class PostgreSQLAuthenticationOKPacketTest {
private void assertReadWrite() {
ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(4);
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
PostgreSQLAuthenticationOKPacket packet = new PostgreSQLAuthenticationOKPacket();
assertThat(packet.getIdentifier(), is(PostgreSQLMessagePacketType.AUTHENTICATION_REQUEST));
packet.write(payload);
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLComStartupPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLComStartupPacketTest.java
index e2aa129..315cbcb 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLComStartupPacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLComStartupPacketTest.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.Postgre
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
+import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
@@ -42,6 +43,7 @@ public final class PostgreSQLComStartupPacketTest {
PostgreSQLComStartupPacket actual = new PostgreSQLComStartupPacket(payload);
assertThat(actual.getDatabase(), is("test_db"));
assertThat(actual.getUser(), is("postgres"));
+ assertThat(actual.getClientEncoding(), is("UTF8"));
assertThat(byteBuf.writerIndex(), is(packetMessageLength));
}
@@ -49,6 +51,7 @@ public final class PostgreSQLComStartupPacketTest {
Map<String, String> result = new LinkedHashMap<>(2, 1);
result.put("database", "test_db");
result.put("user", "postgres");
+ result.put("client_encoding", "UTF8");
return result;
}
@@ -62,7 +65,7 @@ public final class PostgreSQLComStartupPacketTest {
}
private PostgreSQLPacketPayload createPayload(final Map<String, String> actualParametersMap, final int actualMessageLength, final ByteBuf byteBuf) {
- PostgreSQLPacketPayload result = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload result = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
result.writeInt4(actualMessageLength);
result.writeInt4(196608);
for (Entry<String, String> entry : actualParametersMap.entrySet()) {
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLComTerminationPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLComTerminationPacketTest.java
index 9d459a8..886cd69 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLComTerminationPacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLComTerminationPacketTest.java
@@ -23,6 +23,8 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQ
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -32,7 +34,7 @@ public final class PostgreSQLComTerminationPacketTest {
public void assertReadWrite() {
ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(4);
byteBuf.writeInt(1);
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
PostgreSQLComTerminationPacket packet = new PostgreSQLComTerminationPacket(payload);
assertThat(packet.getIdentifier(), is(PostgreSQLCommandPacketType.TERMINATE));
assertThat(byteBuf.readerIndex(), is(4));
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLCommandCompletePacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLCommandCompletePacketTest.java
index 1514c1d..d7ecda9 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLCommandCompletePacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLCommandCompletePacketTest.java
@@ -22,6 +22,8 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.Postgr
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -33,7 +35,7 @@ public final class PostgreSQLCommandCompletePacketTest {
long rowCount = 1;
String expectedString = sqlCommand + " " + rowCount;
int expectedStringLength = expectedString.length();
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(ByteBufTestUtils.createByteBuf(expectedStringLength + 1));
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(ByteBufTestUtils.createByteBuf(expectedStringLength + 1), StandardCharsets.ISO_8859_1);
PostgreSQLCommandCompletePacket packet = new PostgreSQLCommandCompletePacket(sqlCommand, rowCount);
assertThat(packet.getIdentifier(), is(PostgreSQLMessagePacketType.COMMAND_COMPLETE));
packet.write(payload);
@@ -46,7 +48,7 @@ public final class PostgreSQLCommandCompletePacketTest {
long rowCount = 1;
String expectedString = sqlCommand + " 0 " + rowCount;
int expectedStringLength = expectedString.length();
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(ByteBufTestUtils.createByteBuf(expectedStringLength + 1));
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(ByteBufTestUtils.createByteBuf(expectedStringLength + 1), StandardCharsets.ISO_8859_1);
PostgreSQLCommandCompletePacket packet = new PostgreSQLCommandCompletePacket(sqlCommand, rowCount);
assertThat(packet.getIdentifier(), is(PostgreSQLMessagePacketType.COMMAND_COMPLETE));
packet.write(payload);
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLParameterStatusPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLParameterStatusPacketTest.java
index bed39ff..7b4dd3d 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLParameterStatusPacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLParameterStatusPacketTest.java
@@ -24,6 +24,8 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.Postgre
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -35,7 +37,7 @@ public final class PostgreSQLParameterStatusPacketTest {
String value = "value1";
int expectedLength = key.length() + 1 + value.length() + 1;
ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(expectedLength);
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
PostgreSQLParameterStatusPacket packet = new PostgreSQLParameterStatusPacket(key, value);
assertThat(packet.getIdentifier(), is(PostgreSQLMessagePacketType.PARAMETER_STATUS));
packet.write(payload);
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLPasswordMessagePacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLPasswordMessagePacketTest.java
index 86d1306..27ff7be 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLPasswordMessagePacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLPasswordMessagePacketTest.java
@@ -24,6 +24,8 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.Postgre
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -34,7 +36,7 @@ public final class PostgreSQLPasswordMessagePacketTest {
String md5Digest = "ce98bac7fc97f20584ea9536e744dabb";
int expectedLength = 4 + md5Digest.length() + 1;
ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(expectedLength);
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
payload.writeInt4(expectedLength);
payload.writeStringNul(md5Digest);
PostgreSQLPasswordMessagePacket packet = new PostgreSQLPasswordMessagePacket(payload);
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLReadyForQueryPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLReadyForQueryPacketTest.java
index 7c0ecb3..ec26efa 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLReadyForQueryPacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLReadyForQueryPacketTest.java
@@ -23,6 +23,8 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.Postgr
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -31,7 +33,7 @@ public final class PostgreSQLReadyForQueryPacketTest {
@Test
public void assertReadWriteWithInTransaction() {
ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(1);
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
PostgreSQLReadyForQueryPacket packet = new PostgreSQLReadyForQueryPacket(true);
assertThat(packet.getIdentifier(), is(PostgreSQLMessagePacketType.READY_FOR_QUERY));
packet.write(payload);
@@ -42,7 +44,7 @@ public final class PostgreSQLReadyForQueryPacketTest {
@Test
public void assertReadWriteWithNotInTransaction() {
ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(1);
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
PostgreSQLReadyForQueryPacket packet = new PostgreSQLReadyForQueryPacket(false);
assertThat(packet.getIdentifier(), is(PostgreSQLMessagePacketType.READY_FOR_QUERY));
packet.write(payload);
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLSSLNegativePacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLSSLNegativePacketTest.java
index 6c4b8d8..c596261 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLSSLNegativePacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/generic/PostgreSQLSSLNegativePacketTest.java
@@ -23,6 +23,8 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.Postgre
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -31,7 +33,7 @@ public final class PostgreSQLSSLNegativePacketTest {
@Test
public void assertReadWrite() {
ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(1);
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
PostgreSQLSSLNegativePacket packet = new PostgreSQLSSLNegativePacket();
packet.write(payload);
assertThat(byteBuf.writerIndex(), is(1));
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/payload/PostgreSQLPacketPayloadTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/payload/PostgreSQLPacketPayloadTest.java
index 4bec896..6be4eaa 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/payload/PostgreSQLPacketPayloadTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/payload/PostgreSQLPacketPayloadTest.java
@@ -18,10 +18,11 @@
package org.apache.shardingsphere.db.protocol.postgresql.payload;
import io.netty.buffer.ByteBuf;
-import java.nio.charset.StandardCharsets;
import org.apache.shardingsphere.db.protocol.postgresql.packet.ByteBufTestUtils;
import org.junit.Test;
+import java.nio.charset.StandardCharsets;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -30,7 +31,7 @@ public class PostgreSQLPacketPayloadTest {
@Test
public void assertReadWrite() {
ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(16, 128);
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
byte expectedInt1 = (byte) 'i';
payload.writeInt1(expectedInt1);
assertThat(payload.readInt1(), is((int) expectedInt1));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
index 754c8a9..a04248c 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
@@ -20,6 +20,8 @@ package org.apache.shardingsphere.proxy.backend.communication.jdbc.connection;
import com.google.common.base.Preconditions;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
+import io.netty.util.AttributeMap;
+import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.db.protocol.parameter.TypeUnspecifiedSQLParameter;
@@ -58,21 +60,20 @@ import java.util.stream.Collectors;
* Backend connection.
*/
@Getter
+@Setter
public final class BackendConnection implements ExecutorJDBCManager {
static {
ShardingSphereServiceLoader.register(StatementMemoryStrictlyFetchSizeSetter.class);
}
+ @Setter(AccessLevel.NONE)
private volatile String schemaName;
- @Setter
private volatile int connectionId;
- @Setter
private volatile Grantee grantee;
- @Setter
private volatile FederationExecutor federationExecutor;
private final Multimap<String, Connection> cachedConnections = LinkedHashMultimap.create();
@@ -91,10 +92,13 @@ public final class BackendConnection implements ExecutorJDBCManager {
private final Map<String, StatementMemoryStrictlyFetchSizeSetter> fetchSizeSetters;
- public BackendConnection(final TransactionType initialTransactionType) {
+ private final AttributeMap attributeMap;
+
+ public BackendConnection(final TransactionType initialTransactionType, final AttributeMap attributeMap) {
transactionStatus = new TransactionStatus(initialTransactionType);
fetchSizeSetters = ShardingSphereServiceLoader.getSingletonServiceInstances(StatementMemoryStrictlyFetchSizeSetter.class).stream()
.collect(Collectors.toMap(TypedSPI::getType, Function.identity()));
+ this.attributeMap = attributeMap;
}
/**
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnectionTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnectionTest.java
index 0f462bd..512589f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnectionTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnectionTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.proxy.backend.communication.jdbc.connection;
import com.google.common.collect.Multimap;
+import io.netty.util.DefaultAttributeMap;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
@@ -81,7 +82,7 @@ public final class BackendConnectionTest {
@Mock
private JDBCBackendDataSource backendDataSource;
- private final BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL);
+ private final BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL, new DefaultAttributeMap());
@Before
public void setUp() throws ReflectiveOperationException {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowProcessListExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowProcessListExecutorTest.java
index edf0bc2..ec20ca9 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowProcessListExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowProcessListExecutorTest.java
@@ -17,15 +17,17 @@
package org.apache.shardingsphere.proxy.backend.text.admin.mysql.executor;
-import java.lang.reflect.Field;
-import java.sql.SQLException;
-import java.util.Collections;
+import io.netty.util.DefaultAttributeMap;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.junit.Before;
import org.junit.Test;
+import java.lang.reflect.Field;
+import java.sql.SQLException;
+import java.util.Collections;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -58,7 +60,7 @@ public final class ShowProcessListExecutorTest {
@Test
public void assertExecute() throws SQLException {
- showProcessListExecutor.execute(new BackendConnection(TransactionType.LOCAL));
+ showProcessListExecutor.execute(new BackendConnection(TransactionType.LOCAL, new DefaultAttributeMap()));
assertThat(showProcessListExecutor.getQueryResultMetaData().getColumnCount(), is(8));
MergedResult mergedResult = showProcessListExecutor.getMergedResult();
while (mergedResult.next()) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/SetVariableBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/SetVariableBackendHandlerTest.java
index 414d7b6..bb0e9da 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/SetVariableBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/SetVariableBackendHandlerTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.proxy.backend.text.distsql.ral;
+import io.netty.util.DefaultAttributeMap;
import org.apache.shardingsphere.distsql.parser.statement.ral.common.set.SetVariableStatement;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
@@ -59,7 +60,7 @@ public final class SetVariableBackendHandlerTest {
private static final String SCHEMA_PATTERN = "schema_%s";
- private final BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL);
+ private final BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL, new DefaultAttributeMap());
@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/ShowAllVariablesBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/ShowAllVariablesBackendHandlerTest.java
index b171ae3..bd23ea7 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/ShowAllVariablesBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/ShowAllVariablesBackendHandlerTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.proxy.backend.text.distsql.ral;
+import io.netty.util.DefaultAttributeMap;
import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowAllVariablesStatement;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.mode.manager.ContextManager;
@@ -42,7 +43,7 @@ import static org.mockito.Mockito.when;
public final class ShowAllVariablesBackendHandlerTest {
- private final BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL);
+ private final BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL, new DefaultAttributeMap());
@Test
public void assertShowPropsVariable() throws SQLException {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/ShowVariableBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/ShowVariableBackendHandlerTest.java
index 600676b..11e235a 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/ShowVariableBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/ShowVariableBackendHandlerTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.proxy.backend.text.distsql.ral;
+import io.netty.util.DefaultAttributeMap;
import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowVariableStatement;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.mode.manager.ContextManager;
@@ -43,7 +44,7 @@ import static org.mockito.Mockito.when;
public final class ShowVariableBackendHandlerTest {
- private final BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL);
+ private final BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL, new DefaultAttributeMap());
@Test
public void assertShowTransactionType() throws SQLException {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleDefinitionBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleDefinitionBackendHandlerTest.java
index 1cf62ca..502cc46 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleDefinitionBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleDefinitionBackendHandlerTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.proxy.backend.text.distsql.rql;
+import io.netty.util.DefaultAttributeMap;
import org.apache.shardingsphere.infra.distsql.update.RuleDefinitionUpdater;
import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.mode.manager.ContextManager;
@@ -58,7 +59,7 @@ public final class RuleDefinitionBackendHandlerTest {
@Test
public void assertExecute() throws SQLException {
- BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL);
+ BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL, new DefaultAttributeMap());
backendConnection.setCurrentSchema("test");
ResponseHeader response = new RuleDefinitionBackendHandler<>(new CreateFixtureRuleStatement(), backendConnection).execute();
assertThat(response, instanceOf(UpdateResponseHeader.class));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/transaction/TransactionBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/transaction/TransactionBackendHandlerTest.java
index bb797a3..4e86ef8 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/transaction/TransactionBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/transaction/TransactionBackendHandlerTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.proxy.backend.text.transaction;
+import io.netty.util.DefaultAttributeMap;
import lombok.SneakyThrows;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
@@ -41,7 +42,7 @@ import static org.mockito.Mockito.when;
public final class TransactionBackendHandlerTest {
- private final BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL);
+ private final BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL, new DefaultAttributeMap());
@Before
@SneakyThrows(ReflectiveOperationException.class)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
index 43e6153..166b687 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
@@ -21,6 +21,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.packet.CommandPacket;
import org.apache.shardingsphere.db.protocol.packet.CommandPacketType;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
@@ -63,7 +64,7 @@ public final class CommandExecutorTask implements Runnable {
@Override
public void run() {
boolean isNeedFlush = false;
- try (PacketPayload payload = databaseProtocolFrontendEngine.getCodecEngine().createPacketPayload((ByteBuf) message)) {
+ try (PacketPayload payload = databaseProtocolFrontendEngine.getCodecEngine().createPacketPayload((ByteBuf) message, context.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).get())) {
ConnectionStatus connectionStatus = backendConnection.getConnectionStatus();
if (!backendConnection.getTransactionStatus().isInConnectionHeldTransaction()) {
connectionStatus.waitUntilConnectionRelease();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/FrontendChannelInboundHandler.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/FrontendChannelInboundHandler.java
index c5a9ffd..e835d48 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/FrontendChannelInboundHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/FrontendChannelInboundHandler.java
@@ -18,9 +18,11 @@
package org.apache.shardingsphere.proxy.frontend.netty;
import io.netty.buffer.ByteBuf;
+import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.payload.PacketPayload;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
@@ -47,9 +49,9 @@ public final class FrontendChannelInboundHandler extends ChannelInboundHandlerAd
private volatile boolean authenticated;
- public FrontendChannelInboundHandler(final DatabaseProtocolFrontendEngine databaseProtocolFrontendEngine) {
+ public FrontendChannelInboundHandler(final DatabaseProtocolFrontendEngine databaseProtocolFrontendEngine, final Channel channel) {
this.databaseProtocolFrontendEngine = databaseProtocolFrontendEngine;
- backendConnection = new BackendConnection(getTransactionRule().getDefaultType());
+ backendConnection = new BackendConnection(getTransactionRule().getDefaultType(), channel);
}
private TransactionRule getTransactionRule() {
@@ -75,7 +77,7 @@ public final class FrontendChannelInboundHandler extends ChannelInboundHandlerAd
}
private boolean authenticate(final ChannelHandlerContext context, final ByteBuf message) {
- try (PacketPayload payload = databaseProtocolFrontendEngine.getCodecEngine().createPacketPayload(message)) {
+ try (PacketPayload payload = databaseProtocolFrontendEngine.getCodecEngine().createPacketPayload(message, context.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).get())) {
AuthenticationResult authResult = databaseProtocolFrontendEngine.getAuthenticationEngine().authenticate(context, payload);
if (authResult.isFinished()) {
backendConnection.setGrantee(new Grantee(authResult.getUsername(), authResult.getHostname()));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializer.java
index 15452fe..760d1ff 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializer.java
@@ -22,6 +22,7 @@ import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.db.protocol.codec.PacketCodec;
+import org.apache.shardingsphere.db.protocol.netty.ChannelAttrInitializer;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.proxy.frontend.protocol.DatabaseProtocolFrontendEngineFactory;
import org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngine;
@@ -38,8 +39,9 @@ public final class ServerHandlerInitializer extends ChannelInitializer<SocketCha
protected void initChannel(final SocketChannel socketChannel) {
DatabaseProtocolFrontendEngine databaseProtocolFrontendEngine = DatabaseProtocolFrontendEngineFactory.newInstance(databaseType);
ChannelPipeline pipeline = socketChannel.pipeline();
+ pipeline.addLast(new ChannelAttrInitializer());
pipeline.addLast(new PacketCodec(databaseProtocolFrontendEngine.getCodecEngine()));
pipeline.addLast(new FrontendChannelLimitationInboundHandler(databaseProtocolFrontendEngine));
- pipeline.addLast(new FrontendChannelInboundHandler(databaseProtocolFrontendEngine));
+ pipeline.addLast(new FrontendChannelInboundHandler(databaseProtocolFrontendEngine, socketChannel));
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTaskTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTaskTest.java
index d3f5e1b..053c6f8 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTaskTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTaskTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.proxy.frontend.command;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.packet.CommandPacket;
import org.apache.shardingsphere.db.protocol.packet.CommandPacketType;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
@@ -36,6 +37,7 @@ import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Optional;
@@ -61,7 +63,7 @@ public final class CommandExecutorTaskTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private BackendConnection backendConnection;
- @Mock
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ChannelHandlerContext handlerContext;
@Mock
@@ -91,6 +93,7 @@ public final class CommandExecutorTaskTest {
@Before
public void setup() {
when(backendConnection.closeDatabaseCommunicationEngines(anyBoolean())).thenReturn(Collections.emptyList());
+ when(handlerContext.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).get()).thenReturn(StandardCharsets.UTF_8);
}
@Test
@@ -100,7 +103,7 @@ public final class CommandExecutorTaskTest {
when(engine.getCommandExecuteEngine().getCommandExecutor(commandPacketType, commandPacket, backendConnection)).thenReturn(queryCommandExecutor);
when(engine.getCommandExecuteEngine().getCommandPacketType(payload)).thenReturn(commandPacketType);
when(backendConnection.getConnectionStatus()).thenReturn(connectionStatus);
- when(engine.getCodecEngine().createPacketPayload(message)).thenReturn(payload);
+ when(engine.getCodecEngine().createPacketPayload(message, StandardCharsets.UTF_8)).thenReturn(payload);
when(backendConnection.closeConnections(false)).thenReturn(Collections.emptyList());
when(backendConnection.closeFederationExecutor()).thenReturn(Collections.emptyList());
CommandExecutorTask actual = new CommandExecutorTask(engine, backendConnection, handlerContext, message);
@@ -120,7 +123,7 @@ public final class CommandExecutorTaskTest {
when(engine.getCommandExecuteEngine().getCommandPacketType(payload)).thenReturn(commandPacketType);
when(engine.getCommandExecuteEngine().writeQueryData(any(ChannelHandlerContext.class), any(BackendConnection.class), any(QueryCommandExecutor.class), anyInt())).thenReturn(true);
when(backendConnection.getConnectionStatus()).thenReturn(connectionStatus);
- when(engine.getCodecEngine().createPacketPayload(message)).thenReturn(payload);
+ when(engine.getCodecEngine().createPacketPayload(message, StandardCharsets.UTF_8)).thenReturn(payload);
when(backendConnection.closeConnections(false)).thenReturn(Collections.emptyList());
when(backendConnection.closeFederationExecutor()).thenReturn(Collections.emptyList());
CommandExecutorTask actual = new CommandExecutorTask(engine, backendConnection, handlerContext, message);
@@ -144,7 +147,7 @@ public final class CommandExecutorTaskTest {
when(engine.getCommandExecuteEngine().getCommandExecutor(commandPacketType, commandPacket, backendConnection)).thenReturn(commandExecutor);
when(engine.getCommandExecuteEngine().getCommandPacketType(payload)).thenReturn(commandPacketType);
when(backendConnection.getConnectionStatus()).thenReturn(connectionStatus);
- when(engine.getCodecEngine().createPacketPayload(message)).thenReturn(payload);
+ when(engine.getCodecEngine().createPacketPayload(message, StandardCharsets.UTF_8)).thenReturn(payload);
when(backendConnection.closeConnections(false)).thenReturn(Collections.emptyList());
when(backendConnection.closeFederationExecutor()).thenReturn(Collections.emptyList());
CommandExecutorTask actual = new CommandExecutorTask(engine, backendConnection, handlerContext, message);
@@ -163,7 +166,7 @@ public final class CommandExecutorTaskTest {
RuntimeException mockException = new RuntimeException("mock");
when(backendConnection.getConnectionStatus()).thenReturn(connectionStatus);
doThrow(mockException).when(connectionStatus).switchToUsing();
- when(engine.getCodecEngine().createPacketPayload(message)).thenReturn(payload);
+ when(engine.getCodecEngine().createPacketPayload(message, StandardCharsets.UTF_8)).thenReturn(payload);
when(engine.getCommandExecuteEngine().getErrorPacket(mockException, backendConnection)).thenReturn(databasePacket);
when(engine.getCommandExecuteEngine().getOtherPacket(backendConnection)).thenReturn(Optional.of(databasePacket));
when(backendConnection.closeConnections(false)).thenReturn(Collections.emptyList());
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializerTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializerTest.java
new file mode 100644
index 0000000..2899aa5
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializerTest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.frontend.netty;
+
+import io.netty.channel.ChannelPipeline;
+import io.netty.channel.socket.SocketChannel;
+import org.apache.shardingsphere.db.protocol.codec.PacketCodec;
+import org.apache.shardingsphere.db.protocol.netty.ChannelAttrInitializer;
+import org.apache.shardingsphere.proxy.frontend.fixture.FixtureDatabaseType;
+import org.junit.Test;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public final class ServerHandlerInitializerTest {
+
+ @Test
+ public void assertInitChannel() {
+ SocketChannel channel = mock(SocketChannel.class);
+ ChannelPipeline pipeline = mock(ChannelPipeline.class);
+ when(channel.pipeline()).thenReturn(pipeline);
+ ServerHandlerInitializer initializer = new ServerHandlerInitializer(new FixtureDatabaseType());
+ initializer.initChannel(channel);
+ verify(pipeline).addLast(any(ChannelAttrInitializer.class));
+ verify(pipeline).addLast(any(PacketCodec.class));
+ verify(pipeline).addLast(any(FrontendChannelLimitationInboundHandler.class));
+ verify(pipeline).addLast(any(FrontendChannelInboundHandler.class));
+ }
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
index 04c7341..baad9bc 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
@@ -19,9 +19,12 @@ package org.apache.shardingsphere.proxy.frontend.mysql.authentication;
import com.google.common.base.Strings;
import io.netty.channel.ChannelHandlerContext;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLAuthenticationMethod;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLCapabilityFlag;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLCharacterSet;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConnectionPhase;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConstants;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerErrorCode;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
@@ -32,10 +35,10 @@ import org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLHandsha
import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
import org.apache.shardingsphere.db.protocol.payload.PacketPayload;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.frontend.connection.ConnectionIdGenerator;
-import org.apache.shardingsphere.proxy.frontend.authentication.AuthenticationResultBuilder;
import org.apache.shardingsphere.proxy.frontend.authentication.AuthenticationEngine;
import org.apache.shardingsphere.proxy.frontend.authentication.AuthenticationResult;
+import org.apache.shardingsphere.proxy.frontend.authentication.AuthenticationResultBuilder;
+import org.apache.shardingsphere.proxy.frontend.connection.ConnectionIdGenerator;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
@@ -83,6 +86,9 @@ public final class MySQLAuthenticationEngine implements AuthenticationEngine {
MySQLHandshakeResponse41Packet packet = new MySQLHandshakeResponse41Packet((MySQLPacketPayload) payload);
authResponse = packet.getAuthResponse();
sequenceId = packet.getSequenceId();
+ MySQLCharacterSet mySQLCharacterSet = MySQLCharacterSet.findById(packet.getCharacterSet());
+ context.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).set(mySQLCharacterSet.getCharset());
+ context.channel().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).set(mySQLCharacterSet);
if (!Strings.isNullOrEmpty(packet.getDatabase()) && !ProxyContext.getInstance().schemaExists(packet.getDatabase())) {
context.writeAndFlush(new MySQLErrPacket(++sequenceId, MySQLServerErrorCode.ER_BAD_DB_ERROR, packet.getDatabase()));
return AuthenticationResultBuilder.continued();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutor.java
index f08f2e7..ec60d28 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutor.java
@@ -21,6 +21,7 @@ import lombok.Getter;
import org.apache.shardingsphere.db.protocol.binary.BinaryCell;
import org.apache.shardingsphere.db.protocol.binary.BinaryRow;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLBinaryColumnType;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConstants;
import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.execute.MySQLBinaryResultSetRowPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.execute.MySQLComStmtExecutePacket;
@@ -61,6 +62,8 @@ public final class MySQLComStmtExecuteExecutor implements QueryCommandExecutor {
private final DatabaseCommunicationEngine databaseCommunicationEngine;
+ private final int characterSet;
+
@Getter
private volatile ResponseType responseType;
@@ -80,6 +83,7 @@ public final class MySQLComStmtExecuteExecutor implements QueryCommandExecutor {
}
SQLCheckEngine.check(sqlStatement, Collections.emptyList(), getRules(schemaName), schemaName, metaDataContexts.getMetaDataMap(), backendConnection.getGrantee());
databaseCommunicationEngine = DatabaseCommunicationEngineFactory.getInstance().newBinaryProtocolInstance(sqlStatementContext, packet.getSql(), packet.getParameters(), backendConnection);
+ characterSet = backendConnection.getAttributeMap().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).get().getId();
}
private static Collection<ShardingSphereRule> getRules(final String schemaName) {
@@ -97,7 +101,7 @@ public final class MySQLComStmtExecuteExecutor implements QueryCommandExecutor {
private Collection<DatabasePacket<?>> processQuery(final QueryResponseHeader queryResponseHeader) {
responseType = ResponseType.QUERY;
- Collection<DatabasePacket<?>> result = ResponsePacketBuilder.buildQueryResponsePackets(queryResponseHeader);
+ Collection<DatabasePacket<?>> result = ResponsePacketBuilder.buildQueryResponsePackets(queryResponseHeader, characterSet);
currentSequenceId = result.size();
return result;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutor.java
index b871109..95cd7aa 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutor.java
@@ -17,8 +17,8 @@
package org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.prepare;
-import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLBinaryColumnType;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConstants;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.MySQLColumnDefinition41Packet;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.MySQLBinaryStatementRegistry;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.prepare.MySQLComStmtPrepareOKPacket;
@@ -45,7 +45,6 @@ import java.util.Map;
/**
* COM_STMT_PREPARE command executor for MySQL.
*/
-@RequiredArgsConstructor
public final class MySQLComStmtPrepareExecutor implements CommandExecutor {
private static final MySQLBinaryStatementRegistry PREPARED_STATEMENT_REGISTRY = MySQLBinaryStatementRegistry.getInstance();
@@ -54,8 +53,16 @@ public final class MySQLComStmtPrepareExecutor implements CommandExecutor {
private final BackendConnection backendConnection;
+ private final int characterSet;
+
private int currentSequenceId;
+ public MySQLComStmtPrepareExecutor(final MySQLComStmtPreparePacket packet, final BackendConnection backendConnection) {
+ this.packet = packet;
+ this.backendConnection = backendConnection;
+ characterSet = backendConnection.getAttributeMap().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).get().getId();
+ }
+
@Override
public Collection<DatabasePacket<?>> execute() {
ShardingSphereSQLParserEngine sqlStatementParserEngine = new ShardingSphereSQLParserEngine(DatabaseTypeRegistry.getTrunkDatabaseTypeName(
@@ -96,7 +103,7 @@ public final class MySQLComStmtPrepareExecutor implements CommandExecutor {
private Collection<DatabasePacket<?>> createParameterColumnDefinition41Packets(final int parameterCount) {
Collection<DatabasePacket<?>> result = new LinkedList<>();
for (int i = 0; i < parameterCount; i++) {
- result.add(new MySQLColumnDefinition41Packet(++currentSequenceId, "", "", "", "?", "", 0, MySQLBinaryColumnType.MYSQL_TYPE_VAR_STRING, 0, false));
+ result.add(new MySQLColumnDefinition41Packet(++currentSequenceId, characterSet, "", "", "", "?", "", 0, MySQLBinaryColumnType.MYSQL_TYPE_VAR_STRING, 0, false));
}
result.add(new MySQLEofPacket(++currentSequenceId));
return result;
@@ -105,7 +112,7 @@ public final class MySQLComStmtPrepareExecutor implements CommandExecutor {
private Collection<DatabasePacket<?>> createProjectionColumnDefinition41Packets(final int projectionCount) {
Collection<DatabasePacket<?>> result = new LinkedList<>();
for (int i = 0; i < projectionCount; i++) {
- result.add(new MySQLColumnDefinition41Packet(++currentSequenceId, "", "", "", "", "", 0, MySQLBinaryColumnType.MYSQL_TYPE_VAR_STRING, 0, false));
+ result.add(new MySQLColumnDefinition41Packet(++currentSequenceId, characterSet, "", "", "", "", "", 0, MySQLBinaryColumnType.MYSQL_TYPE_VAR_STRING, 0, false));
}
result.add(new MySQLEofPacket(++currentSequenceId));
return result;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/builder/ResponsePacketBuilder.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/builder/ResponsePacketBuilder.java
index 856ea2b..1f0e1f2 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/builder/ResponsePacketBuilder.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/builder/ResponsePacketBuilder.java
@@ -45,15 +45,16 @@ public final class ResponsePacketBuilder {
* Build query response packets.
*
* @param queryResponseHeader query response header
+ * @param characterSet MySQL character set id
* @return query response packets
*/
- public static Collection<DatabasePacket<?>> buildQueryResponsePackets(final QueryResponseHeader queryResponseHeader) {
+ public static Collection<DatabasePacket<?>> buildQueryResponsePackets(final QueryResponseHeader queryResponseHeader, final int characterSet) {
Collection<DatabasePacket<?>> result = new LinkedList<>();
int sequenceId = 0;
List<QueryHeader> queryHeaders = queryResponseHeader.getQueryHeaders();
result.add(new MySQLFieldCountPacket(++sequenceId, queryHeaders.size()));
for (QueryHeader each : queryHeaders) {
- result.add(new MySQLColumnDefinition41Packet(++sequenceId, getColumnFieldDetailFlag(each), each.getSchema(), each.getTable(), each.getTable(),
+ result.add(new MySQLColumnDefinition41Packet(++sequenceId, characterSet, getColumnFieldDetailFlag(each), each.getSchema(), each.getTable(), each.getTable(),
each.getColumnLabel(), each.getColumnName(), each.getColumnLength(), MySQLBinaryColumnType.valueOfJDBCType(each.getColumnType()), each.getDecimals(), false));
}
result.add(new MySQLEofPacket(++sequenceId));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/fieldlist/MySQLComFieldListPacketExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/fieldlist/MySQLComFieldListPacketExecutor.java
index 704f470..0764cfd 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/fieldlist/MySQLComFieldListPacketExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/fieldlist/MySQLComFieldListPacketExecutor.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.fieldlist;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLBinaryColumnType;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConstants;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.MySQLColumnDefinition41Packet;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.fieldlist.MySQLComFieldListPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLEofPacket;
@@ -52,6 +53,8 @@ public final class MySQLComFieldListPacketExecutor implements CommandExecutor {
private final DatabaseCommunicationEngine databaseCommunicationEngine;
+ private final int characterSet;
+
private int currentSequenceId;
public MySQLComFieldListPacketExecutor(final MySQLComFieldListPacket packet, final BackendConnection backendConnection) {
@@ -64,6 +67,7 @@ public final class MySQLComFieldListPacketExecutor implements CommandExecutor {
SQLStatement sqlStatement = sqlStatementParserEngine.parse(sql, false);
SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(metaDataContexts.getMetaDataMap(), Collections.emptyList(), sqlStatement, schemaName);
databaseCommunicationEngine = DatabaseCommunicationEngineFactory.getInstance().newTextProtocolInstance(sqlStatementContext, sql, backendConnection);
+ characterSet = backendConnection.getAttributeMap().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).get().getId();
}
@Override
@@ -77,7 +81,7 @@ public final class MySQLComFieldListPacketExecutor implements CommandExecutor {
while (databaseCommunicationEngine.next()) {
String columnName = databaseCommunicationEngine.getQueryResponseRow().getCells().iterator().next().getData().toString();
result.add(new MySQLColumnDefinition41Packet(
- ++currentSequenceId, schemaName, packet.getTable(), packet.getTable(), columnName, columnName, 100, MySQLBinaryColumnType.MYSQL_TYPE_VARCHAR, 0, true));
+ ++currentSequenceId, characterSet, schemaName, packet.getTable(), packet.getTable(), columnName, columnName, 100, MySQLBinaryColumnType.MYSQL_TYPE_VARCHAR, 0, true));
}
result.add(new MySQLEofPacket(++currentSequenceId));
return result;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutor.java
index 9d06568..316e9e9 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutor.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.query;
import lombok.Getter;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConstants;
import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.MySQLTextResultSetRowPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.query.MySQLComQueryPacket;
@@ -43,6 +44,8 @@ public final class MySQLComQueryPacketExecutor implements QueryCommandExecutor {
private final TextProtocolBackendHandler textProtocolBackendHandler;
+ private final int characterSet;
+
@Getter
private volatile ResponseType responseType;
@@ -50,6 +53,7 @@ public final class MySQLComQueryPacketExecutor implements QueryCommandExecutor {
public MySQLComQueryPacketExecutor(final MySQLComQueryPacket packet, final BackendConnection backendConnection) throws SQLException {
textProtocolBackendHandler = TextProtocolBackendHandlerFactory.newInstance(DatabaseTypeRegistry.getActualDatabaseType("MySQL"), packet.getSql(), backendConnection);
+ characterSet = backendConnection.getAttributeMap().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).get().getId();
}
@Override
@@ -60,7 +64,7 @@ public final class MySQLComQueryPacketExecutor implements QueryCommandExecutor {
private Collection<DatabasePacket<?>> processQuery(final QueryResponseHeader queryResponseHeader) {
responseType = ResponseType.QUERY;
- Collection<DatabasePacket<?>> result = ResponsePacketBuilder.buildQueryResponsePackets(queryResponseHeader);
+ Collection<DatabasePacket<?>> result = ResponsePacketBuilder.buildQueryResponsePackets(queryResponseHeader, characterSet);
currentSequenceId = result.size();
return result;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngineTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngineTest.java
index 1adb829..7cd6334 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngineTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngineTest.java
@@ -19,11 +19,14 @@ package org.apache.shardingsphere.proxy.frontend.mysql;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
+import io.netty.util.Attribute;
import lombok.SneakyThrows;
import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.authority.rule.builder.AuthorityRuleBuilder;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConnectionPhase;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConstants;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLHandshakePacket;
@@ -89,6 +92,9 @@ public final class MySQLFrontendEngineTest {
@Before
public void setUp() {
resetConnectionIdGenerator();
+ when(context.channel()).thenReturn(channel);
+ when(channel.attr(CommonConstants.CHARSET_ATTRIBUTE_KEY)).thenReturn(mock(Attribute.class));
+ when(channel.attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY)).thenReturn(mock(Attribute.class));
}
@SneakyThrows(ReflectiveOperationException.class)
@@ -109,10 +115,10 @@ public final class MySQLFrontendEngineTest {
public void assertAuthWhenLoginSuccess() {
setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH);
initProxyContext(new ShardingSphereUser("root", "", ""));
+ when(payload.readInt1()).thenReturn(1);
when(payload.readStringNul()).thenReturn("root");
when(payload.readStringNulByBytes()).thenReturn("root".getBytes());
when(channel.remoteAddress()).thenReturn(new InetSocketAddress("localhost", 3307));
- when(context.channel()).thenReturn(channel);
AuthenticationResult actual = mysqlFrontendEngine.getAuthenticationEngine().authenticate(context, payload);
assertThat(actual.getUsername(), is("root"));
assertNull(actual.getDatabase());
@@ -124,10 +130,10 @@ public final class MySQLFrontendEngineTest {
public void assertAuthWhenLoginFailure() {
setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH);
initProxyContext(new ShardingSphereUser("root", "error", ""));
+ when(payload.readInt1()).thenReturn(1);
when(payload.readStringNul()).thenReturn("root");
when(payload.readStringNulByBytes()).thenReturn("root".getBytes());
when(channel.remoteAddress()).thenReturn(new InetSocketAddress("localhost", 3307));
- when(context.channel()).thenReturn(channel);
AuthenticationResult actual = mysqlFrontendEngine.getAuthenticationEngine().authenticate(context, payload);
assertThat(actual.getUsername(), is("root"));
assertNull(actual.getDatabase());
@@ -139,9 +145,9 @@ public final class MySQLFrontendEngineTest {
public void assertErrorMsgWhenLoginFailure() throws UnknownHostException {
setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH);
initProxyContext(new ShardingSphereUser("root", "error", ""));
+ when(payload.readInt1()).thenReturn(1);
when(payload.readStringNul()).thenReturn("root");
when(payload.readStringNulByBytes()).thenReturn("root".getBytes());
- when(context.channel()).thenReturn(channel);
when(channel.remoteAddress()).thenReturn(new InetSocketAddress(InetAddress.getByAddress(new byte[]{(byte) 192, (byte) 168, (byte) 0, (byte) 102}), 3307));
AuthenticationResult actual = mysqlFrontendEngine.getAuthenticationEngine().authenticate(context, payload);
assertThat(actual.getUsername(), is("root"));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngineTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngineTest.java
index 5e0feac..eb33eca 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngineTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngineTest.java
@@ -19,9 +19,12 @@ package org.apache.shardingsphere.proxy.frontend.mysql.authentication;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
+import io.netty.util.Attribute;
import lombok.SneakyThrows;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLCapabilityFlag;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConnectionPhase;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConstants;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerErrorCode;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
@@ -92,7 +95,10 @@ public final class MySQLAuthenticationEngineTest {
Channel channel = mock(Channel.class);
when(payload.readStringNulByBytes()).thenReturn("root".getBytes());
when(channel.remoteAddress()).thenReturn(new InetSocketAddress("localhost", 3307));
+ when(channel.attr(CommonConstants.CHARSET_ATTRIBUTE_KEY)).thenReturn(mock(Attribute.class));
+ when(channel.attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY)).thenReturn(mock(Attribute.class));
when(channelHandlerContext.channel()).thenReturn(channel);
+ when(payload.readInt1()).thenReturn(1);
when(payload.readInt4()).thenReturn(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue());
authenticationEngine.authenticate(channelHandlerContext, payload);
assertThat(getConnectionPhase(), is(MySQLConnectionPhase.AUTHENTICATION_METHOD_MISMATCH));
@@ -161,6 +167,7 @@ public final class MySQLAuthenticationEngineTest {
private MySQLPacketPayload getPayload(final String username, final String database, final byte[] authResponse) {
MySQLPacketPayload result = mock(MySQLPacketPayload.class);
+ when(result.readInt1()).thenReturn(1);
when(result.readInt4()).thenReturn(MySQLCapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue());
when(result.readStringNul()).thenReturn(username).thenReturn(database);
when(result.readStringNulByBytes()).thenReturn(authResponse);
@@ -176,6 +183,8 @@ public final class MySQLAuthenticationEngineTest {
private Channel getChannel() {
Channel result = mock(Channel.class);
doReturn(getRemoteAddress()).when(result).remoteAddress();
+ when(result.attr(CommonConstants.CHARSET_ATTRIBUTE_KEY)).thenReturn(mock(Attribute.class));
+ when(result.attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY)).thenReturn(mock(Attribute.class));
return result;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/MySQLCommandExecutorFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/MySQLCommandExecutorFactoryTest.java
index 5194d6c..e3f9899 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/MySQLCommandExecutorFactoryTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/MySQLCommandExecutorFactoryTest.java
@@ -17,6 +17,8 @@
package org.apache.shardingsphere.proxy.frontend.mysql.command;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLCharacterSet;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConstants;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.MySQLCommandPacketType;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.admin.initdb.MySQLComInitDbPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.close.MySQLComStmtClosePacket;
@@ -50,6 +52,7 @@ import org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.query.M
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@@ -68,13 +71,14 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class MySQLCommandExecutorFactoryTest {
- @Mock
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private BackendConnection backendConnection;
@Before
public void setUp() throws ReflectiveOperationException {
when(backendConnection.getSchemaName()).thenReturn("logic_db");
when(backendConnection.getDefaultSchemaName()).thenReturn("logic_db");
+ when(backendConnection.getAttributeMap().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).get()).thenReturn(MySQLCharacterSet.UTF8MB4);
Field contextManagerField = ProxyContext.getInstance().getClass().getDeclaredField("contextManager");
contextManagerField.setAccessible(true);
ShardingSphereMetaData metaData = mockShardingSphereMetaData();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutorTest.java
index a35e99d..fb115c8 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutorTest.java
@@ -17,6 +17,8 @@
package org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.execute;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLCharacterSet;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConstants;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.execute.MySQLComStmtExecutePacket;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
@@ -38,6 +40,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.FieldSetter;
import org.mockito.junit.MockitoJUnitRunner;
@@ -60,6 +63,9 @@ public final class MySQLComStmtExecuteExecutorTest {
@Mock
private DatabaseCommunicationEngine databaseCommunicationEngine;
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private BackendConnection backendConnection;
+
@Before
public void setUp() throws ReflectiveOperationException {
Field contextManagerField = ProxyContext.getInstance().getClass().getDeclaredField("contextManager");
@@ -71,6 +77,7 @@ public final class MySQLComStmtExecuteExecutorTest {
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
contextManagerField.set(ProxyContext.getInstance(), contextManager);
+ when(backendConnection.getAttributeMap().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).get()).thenReturn(MySQLCharacterSet.UTF8MB4);
}
private ShardingSphereMetaData mockMetaData() {
@@ -82,7 +89,6 @@ public final class MySQLComStmtExecuteExecutorTest {
@Test
public void assertIsQueryResponse() throws NoSuchFieldException, SQLException {
- BackendConnection backendConnection = mock(BackendConnection.class);
when(backendConnection.getSchemaName()).thenReturn("logic_db");
when(backendConnection.getDefaultSchemaName()).thenReturn("logic_db");
MySQLComStmtExecutePacket packet = mock(MySQLComStmtExecutePacket.class);
@@ -96,7 +102,6 @@ public final class MySQLComStmtExecuteExecutorTest {
@Test
public void assertIsUpdateResponse() throws NoSuchFieldException, SQLException {
- BackendConnection backendConnection = mock(BackendConnection.class);
when(backendConnection.getSchemaName()).thenReturn("logic_db");
when(backendConnection.getDefaultSchemaName()).thenReturn("logic_db");
MySQLComStmtExecutePacket packet = mock(MySQLComStmtExecutePacket.class);
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/builder/ResponsePacketBuilderTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/builder/ResponsePacketBuilderTest.java
index 9f13885..266f0f4 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/builder/ResponsePacketBuilderTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/builder/ResponsePacketBuilderTest.java
@@ -47,7 +47,7 @@ public final class ResponsePacketBuilderTest {
QueryHeader queryHeader2 = new QueryHeader("schema2", "table2", "columnLabel2", "columnName2", 8, "VARCHAR", 7, 9, false, true, true, true);
List<QueryHeader> queryHeaders = Arrays.asList(queryHeader1, queryHeader2);
QueryResponseHeader queryResponseHeader = new QueryResponseHeader(queryHeaders);
- Collection<DatabasePacket<?>> actual = ResponsePacketBuilder.buildQueryResponsePackets(queryResponseHeader);
+ Collection<DatabasePacket<?>> actual = ResponsePacketBuilder.buildQueryResponsePackets(queryResponseHeader, 255);
assertTrue(actual.stream().findAny().isPresent());
assertThat(actual.stream().findAny().get(), anyOf(instanceOf(MySQLFieldCountPacket.class), instanceOf(MySQLColumnDefinition41Packet.class), instanceOf(MySQLEofPacket.class)));
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutorTest.java
index 39ed500..a228638 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutorTest.java
@@ -17,7 +17,10 @@
package org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.query;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLCharacterSet;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConstants;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.query.MySQLComQueryPacket;
+import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.response.header.query.QueryResponseHeader;
import org.apache.shardingsphere.proxy.backend.response.header.query.impl.QueryHeader;
import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
@@ -27,6 +30,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.FieldSetter;
import org.mockito.junit.MockitoJUnitRunner;
@@ -48,14 +52,18 @@ public final class MySQLComQueryPacketExecutorTest {
@Mock
private MySQLComQueryPacket packet;
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private BackendConnection backendConnection;
+
@Before
public void setUp() {
when(packet.getSql()).thenReturn("");
+ when(backendConnection.getAttributeMap().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).get()).thenReturn(MySQLCharacterSet.UTF8MB4);
}
@Test
public void assertIsQueryResponse() throws SQLException, NoSuchFieldException {
- MySQLComQueryPacketExecutor mysqlComQueryPacketExecutor = new MySQLComQueryPacketExecutor(packet, null);
+ MySQLComQueryPacketExecutor mysqlComQueryPacketExecutor = new MySQLComQueryPacketExecutor(packet, backendConnection);
FieldSetter.setField(mysqlComQueryPacketExecutor, MySQLComQueryPacketExecutor.class.getDeclaredField("textProtocolBackendHandler"), textProtocolBackendHandler);
when(textProtocolBackendHandler.execute()).thenReturn(new QueryResponseHeader(Collections.singletonList(mock(QueryHeader.class))));
mysqlComQueryPacketExecutor.execute();
@@ -64,7 +72,7 @@ public final class MySQLComQueryPacketExecutorTest {
@Test
public void assertIsUpdateResponse() throws SQLException, NoSuchFieldException {
- MySQLComQueryPacketExecutor mysqlComQueryPacketExecutor = new MySQLComQueryPacketExecutor(packet, null);
+ MySQLComQueryPacketExecutor mysqlComQueryPacketExecutor = new MySQLComQueryPacketExecutor(packet, backendConnection);
FieldSetter.setField(mysqlComQueryPacketExecutor, MySQLComQueryPacketExecutor.class.getDeclaredField("textProtocolBackendHandler"), textProtocolBackendHandler);
when(textProtocolBackendHandler.execute()).thenReturn(new UpdateResponseHeader(mock(SQLStatement.class)));
mysqlComQueryPacketExecutor.execute();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
index a202b93..91c34d6 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.proxy.frontend.postgresql.authentication;
import com.google.common.base.Strings;
import io.netty.channel.ChannelHandlerContext;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.payload.PacketPayload;
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLErrorCode;
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLServerInfo;
@@ -41,6 +42,8 @@ import org.apache.shardingsphere.proxy.frontend.postgresql.authentication.except
import org.apache.shardingsphere.proxy.frontend.postgresql.authentication.exception.PostgreSQLAuthenticationException;
import org.apache.shardingsphere.proxy.frontend.postgresql.authentication.exception.PostgreSQLProtocolViolationException;
+import java.nio.charset.Charset;
+
/**
* Authentication engine for PostgreSQL.
*/
@@ -52,6 +55,8 @@ public final class PostgreSQLAuthenticationEngine implements AuthenticationEngin
private boolean startupMessageReceived;
+ private String clientEncoding;
+
private byte[] md5Salt;
private AuthenticationResult currentAuthResult;
@@ -76,6 +81,8 @@ public final class PostgreSQLAuthenticationEngine implements AuthenticationEngin
private AuthenticationResult processStartupMessage(final ChannelHandlerContext context, final PostgreSQLPacketPayload payload) {
startupMessageReceived = true;
PostgreSQLComStartupPacket comStartupPacket = new PostgreSQLComStartupPacket(payload);
+ clientEncoding = comStartupPacket.getClientEncoding();
+ context.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).set(Charset.forName(clientEncoding));
String user = comStartupPacket.getUser();
if (Strings.isNullOrEmpty(user)) {
throw new InvalidAuthorizationSpecificationException("no PostgreSQL user name specified in startup packet");
@@ -99,7 +106,7 @@ public final class PostgreSQLAuthenticationEngine implements AuthenticationEngin
// TODO implement PostgreSQLServerInfo like MySQLServerInfo
context.write(new PostgreSQLAuthenticationOKPacket());
context.write(new PostgreSQLParameterStatusPacket("server_version", PostgreSQLServerInfo.getServerVersion()));
- context.write(new PostgreSQLParameterStatusPacket("client_encoding", "UTF8"));
+ context.write(new PostgreSQLParameterStatusPacket("client_encoding", clientEncoding));
context.write(new PostgreSQLParameterStatusPacket("server_encoding", "UTF8"));
context.write(new PostgreSQLParameterStatusPacket("integer_datetimes", "on"));
context.writeAndFlush(new PostgreSQLReadyForQueryPacket(false));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java
index 8eef257..2b6a867 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java
@@ -21,10 +21,12 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.buffer.UnpooledHeapByteBuf;
import io.netty.channel.ChannelHandlerContext;
+import io.netty.util.Attribute;
import lombok.SneakyThrows;
import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.authority.rule.builder.AuthorityRuleBuilder;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.payload.PacketPayload;
import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLAuthenticationMD5PasswordPacket;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
@@ -42,11 +44,17 @@ import org.apache.shardingsphere.proxy.frontend.authentication.AuthenticationRes
import org.apache.shardingsphere.proxy.frontend.postgresql.authentication.exception.InvalidAuthorizationSpecificationException;
import org.apache.shardingsphere.proxy.frontend.postgresql.authentication.exception.PostgreSQLAuthenticationException;
import org.apache.shardingsphere.proxy.frontend.postgresql.authentication.exception.PostgreSQLProtocolViolationException;
+import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Properties;
@@ -59,14 +67,19 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLAuthenticationEngineTest {
private final String username = "root";
private final String password = "sharding";
- private ByteBuf createByteBuf(final int initialCapacity, final int maxCapacity) {
- return new UnpooledHeapByteBuf(UnpooledByteBufAllocator.DEFAULT, initialCapacity, maxCapacity);
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private ChannelHandlerContext channelHandlerContext;
+
+ @Before
+ public void setup() {
+ when(channelHandlerContext.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY)).thenReturn(mock(Attribute.class));
}
@Test
@@ -74,27 +87,29 @@ public final class PostgreSQLAuthenticationEngineTest {
ByteBuf byteBuf = createByteBuf(8, 8);
byteBuf.writeInt(8);
byteBuf.writeInt(80877103);
- PacketPayload payload = new PostgreSQLPacketPayload(byteBuf);
+ PacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
AuthenticationResult actual = new PostgreSQLAuthenticationEngine().authenticate(mock(ChannelHandlerContext.class), payload);
assertFalse(actual.isFinished());
}
@Test(expected = InvalidAuthorizationSpecificationException.class)
public void assertUserNotSet() {
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(createByteBuf(8, 512));
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(createByteBuf(8, 512), StandardCharsets.UTF_8);
payload.writeInt4(64);
payload.writeInt4(196608);
- new PostgreSQLAuthenticationEngine().authenticate(mock(ChannelHandlerContext.class), payload);
+ payload.writeStringNul("client_encoding");
+ payload.writeStringNul("UTF8");
+ new PostgreSQLAuthenticationEngine().authenticate(channelHandlerContext, payload);
}
@Test(expected = PostgreSQLProtocolViolationException.class)
public void assertAuthenticateWithNonPasswordMessage() {
PostgreSQLAuthenticationEngine authenticationEngine = new PostgreSQLAuthenticationEngine();
setAlreadyReceivedStartupMessage(authenticationEngine);
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(createByteBuf(8, 16));
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(createByteBuf(8, 16), StandardCharsets.UTF_8);
payload.writeInt1('F');
payload.writeInt8(0);
- authenticationEngine.authenticate(mock(ChannelHandlerContext.class), payload);
+ authenticationEngine.authenticate(channelHandlerContext, payload);
}
@SneakyThrows
@@ -116,12 +131,13 @@ public final class PostgreSQLAuthenticationEngineTest {
}
private void assertLogin(final String inputPassword) {
- PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(createByteBuf(16, 128));
+ PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(createByteBuf(16, 128), StandardCharsets.UTF_8);
payload.writeInt4(64);
payload.writeInt4(196608);
payload.writeStringNul("user");
payload.writeStringNul(username);
- ChannelHandlerContext channelHandlerContext = mock(ChannelHandlerContext.class);
+ payload.writeStringNul("client_encoding");
+ payload.writeStringNul("UTF8");
PostgreSQLAuthenticationEngine engine = new PostgreSQLAuthenticationEngine();
AuthenticationResult actual = engine.authenticate(channelHandlerContext, payload);
assertFalse(actual.isFinished());
@@ -130,7 +146,7 @@ public final class PostgreSQLAuthenticationEngineTest {
verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
PostgreSQLAuthenticationMD5PasswordPacket md5PasswordPacket = argumentCaptor.getValue();
byte[] md5Salt = getMd5Salt(md5PasswordPacket);
- payload = new PostgreSQLPacketPayload(createByteBuf(16, 128));
+ payload = new PostgreSQLPacketPayload(createByteBuf(16, 128), StandardCharsets.UTF_8);
String md5Digest = md5Encode(username, inputPassword, md5Salt);
payload.writeInt1('p');
payload.writeInt4(4 + md5Digest.length() + 1);
@@ -143,6 +159,10 @@ public final class PostgreSQLAuthenticationEngineTest {
assertThat(actual.isFinished(), is(password.equals(inputPassword)));
}
+ private ByteBuf createByteBuf(final int initialCapacity, final int maxCapacity) {
+ return new UnpooledHeapByteBuf(UnpooledByteBufAllocator.DEFAULT, initialCapacity, maxCapacity);
+ }
+
private MetaDataContexts getMetaDataContexts(final ShardingSphereUser user) {
return new MetaDataContexts(mock(MetaDataPersistService.class), new LinkedHashMap<>(),
buildGlobalRuleMetaData(user), mock(ExecutorEngine.class), new ConfigurationProperties(new Properties()), mock(OptimizerContext.class));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandlerTest.java
index a29f428..7eea3c1 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandlerTest.java
@@ -76,7 +76,7 @@ public final class PostgreSQLAuthenticationHandlerTest {
@Before
public void init() {
- payload = new PostgreSQLPacketPayload(createByteBuf(16, 128));
+ payload = new PostgreSQLPacketPayload(createByteBuf(16, 128), StandardCharsets.UTF_8);
String md5Digest = md5Encode(username, password, md5Salt.getBytes(StandardCharsets.UTF_8));
payload.writeInt4(4 + md5Digest.length() + 1);
payload.writeStringNul(md5Digest);
diff --git a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/MySQLClient.java b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/MySQLClient.java
index 416b3b4..1a9aec9 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/MySQLClient.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/MySQLClient.java
@@ -38,6 +38,7 @@ import org.apache.shardingsphere.db.protocol.mysql.packet.command.binlog.MySQLCo
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.query.MySQLComQueryPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
+import org.apache.shardingsphere.db.protocol.netty.ChannelAttrInitializer;
import org.apache.shardingsphere.scaling.mysql.binlog.event.AbstractBinlogEvent;
import org.apache.shardingsphere.scaling.mysql.client.netty.MySQLBinlogEventPacketDecoder;
import org.apache.shardingsphere.scaling.mysql.client.netty.MySQLCommandPacketDecoder;
@@ -82,6 +83,7 @@ public final class MySQLClient {
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(final SocketChannel socketChannel) {
+ socketChannel.pipeline().addLast(new ChannelAttrInitializer());
socketChannel.pipeline().addLast(new PacketCodec(new MySQLPacketCodecEngine()));
socketChannel.pipeline().addLast(new MySQLNegotiatePackageDecoder());
socketChannel.pipeline().addLast(new MySQLCommandPacketDecoder());
diff --git a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLBinlogEventPacketDecoder.java b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLBinlogEventPacketDecoder.java
index 7c49991..e1a30d6 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLBinlogEventPacketDecoder.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLBinlogEventPacketDecoder.java
@@ -17,6 +17,12 @@
package org.apache.shardingsphere.scaling.mysql.client.netty;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLBinlogEventType;
import org.apache.shardingsphere.db.protocol.mysql.packet.binlog.MySQLBinlogEventHeader;
import org.apache.shardingsphere.db.protocol.mysql.packet.binlog.management.MySQLBinlogFormatDescriptionEventPacket;
@@ -30,11 +36,7 @@ import org.apache.shardingsphere.scaling.mysql.binlog.event.DeleteRowsEvent;
import org.apache.shardingsphere.scaling.mysql.binlog.event.PlaceholderEvent;
import org.apache.shardingsphere.scaling.mysql.binlog.event.UpdateRowsEvent;
import org.apache.shardingsphere.scaling.mysql.binlog.event.WriteRowsEvent;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.ByteBufUtil;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.codec.ByteToMessageDecoder;
-import lombok.extern.slf4j.Slf4j;
+
import java.util.List;
/**
@@ -52,7 +54,7 @@ public final class MySQLBinlogEventPacketDecoder extends ByteToMessageDecoder {
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) {
- MySQLPacketPayload payload = new MySQLPacketPayload(in);
+ MySQLPacketPayload payload = new MySQLPacketPayload(in, ctx.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).get());
skipSequenceId(payload);
checkError(payload);
MySQLBinlogEventHeader binlogEventHeader = new MySQLBinlogEventHeader(payload);
diff --git a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLCommandPacketDecoder.java b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLCommandPacketDecoder.java
index dd73e68..2bd6120 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLCommandPacketDecoder.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLCommandPacketDecoder.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.scaling.mysql.client.netty;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.MySQLColumnDefinition41Packet;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.MySQLFieldCountPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.MySQLTextResultSetRowPacket;
@@ -44,7 +45,7 @@ public final class MySQLCommandPacketDecoder extends ByteToMessageDecoder {
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) {
- MySQLPacketPayload payload = new MySQLPacketPayload(in);
+ MySQLPacketPayload payload = new MySQLPacketPayload(in, ctx.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).get());
decodeCommandPacket(payload, out);
}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLNegotiatePackageDecoder.java b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLNegotiatePackageDecoder.java
index 5215f47..3b2841a 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLNegotiatePackageDecoder.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/main/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLNegotiatePackageDecoder.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.scaling.mysql.client.netty;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
@@ -39,7 +40,7 @@ public final class MySQLNegotiatePackageDecoder extends ByteToMessageDecoder {
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) {
- MySQLPacketPayload payload = new MySQLPacketPayload(in);
+ MySQLPacketPayload payload = new MySQLPacketPayload(in, ctx.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).get());
if (!handshakeReceived) {
out.add(decodeHandshakePacket(payload));
handshakeReceived = true;
diff --git a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLBinlogEventPacketDecoderTest.java b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLBinlogEventPacketDecoderTest.java
index 0de1a01..14ed3da 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLBinlogEventPacketDecoderTest.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLBinlogEventPacketDecoderTest.java
@@ -18,6 +18,8 @@
package org.apache.shardingsphere.scaling.mysql.client.netty;
import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLBinlogEventType;
import org.apache.shardingsphere.db.protocol.mysql.packet.binlog.row.MySQLBinlogTableMapEventPacket;
import org.apache.shardingsphere.scaling.core.util.ReflectionUtil;
@@ -28,9 +30,11 @@ import org.apache.shardingsphere.scaling.mysql.binlog.event.WriteRowsEvent;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@@ -44,6 +48,9 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class MySQLBinlogEventPacketDecoderTest {
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private ChannelHandlerContext channelHandlerContext;
+
@Mock
private ByteBuf byteBuf;
@@ -58,25 +65,26 @@ public final class MySQLBinlogEventPacketDecoderTest {
public void setUp() throws NoSuchFieldException, IllegalAccessException {
binlogEventPacketDecoder = new MySQLBinlogEventPacketDecoder(4);
binlogContext = ReflectionUtil.getFieldValue(binlogEventPacketDecoder, "binlogContext", BinlogContext.class);
+ when(channelHandlerContext.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).get()).thenReturn(StandardCharsets.UTF_8);
}
@Test(expected = RuntimeException.class)
public void assertDecodeWithPacketError() {
when(byteBuf.readUnsignedByte()).thenReturn((short) 255);
- binlogEventPacketDecoder.decode(null, byteBuf, null);
+ binlogEventPacketDecoder.decode(channelHandlerContext, byteBuf, null);
}
@Test(expected = UnsupportedOperationException.class)
public void assertDecodeWithReadError() {
when(byteBuf.isReadable()).thenReturn(true);
- binlogEventPacketDecoder.decode(null, byteBuf, new LinkedList<>());
+ binlogEventPacketDecoder.decode(channelHandlerContext, byteBuf, new LinkedList<>());
}
@Test
public void assertDecodeRotateEvent() {
when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) 0, (short) MySQLBinlogEventType.ROTATE_EVENT.getValue());
List<Object> decodedEvents = new LinkedList<>();
- binlogEventPacketDecoder.decode(null, byteBuf, decodedEvents);
+ binlogEventPacketDecoder.decode(channelHandlerContext, byteBuf, decodedEvents);
assertTrue(decodedEvents.isEmpty());
assertThat(binlogContext.getFileName(), is(""));
}
@@ -86,7 +94,7 @@ public final class MySQLBinlogEventPacketDecoderTest {
when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) 0, (short) MySQLBinlogEventType.FORMAT_DESCRIPTION_EVENT.getValue(), (short) 19);
when(byteBuf.readUnsignedShortLE()).thenReturn(4);
List<Object> decodedEvents = new LinkedList<>();
- binlogEventPacketDecoder.decode(null, byteBuf, decodedEvents);
+ binlogEventPacketDecoder.decode(channelHandlerContext, byteBuf, decodedEvents);
assertTrue(decodedEvents.isEmpty());
assertThat(binlogContext.getChecksumLength(), is(4));
}
@@ -95,7 +103,7 @@ public final class MySQLBinlogEventPacketDecoderTest {
public void assertDecodeTableMapEvent() {
when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) 0, (short) MySQLBinlogEventType.TABLE_MAP_EVENT.getValue(), (short) 0);
List<Object> decodedEvents = new LinkedList<>();
- binlogEventPacketDecoder.decode(null, byteBuf, decodedEvents);
+ binlogEventPacketDecoder.decode(channelHandlerContext, byteBuf, decodedEvents);
assertTrue(decodedEvents.isEmpty());
assertThat(binlogContext.getTableMap().size(), is(1));
assertThat(binlogContext.getTableMap().get(0L), instanceOf(MySQLBinlogTableMapEventPacket.class));
@@ -108,7 +116,7 @@ public final class MySQLBinlogEventPacketDecoderTest {
binlogContext.getTableMap().put(0L, tableMapEventPacket);
when(tableMapEventPacket.getColumnDefs()).thenReturn(Collections.emptyList());
List<Object> decodedEvents = new LinkedList<>();
- binlogEventPacketDecoder.decode(null, byteBuf, decodedEvents);
+ binlogEventPacketDecoder.decode(channelHandlerContext, byteBuf, decodedEvents);
assertThat(decodedEvents.size(), is(1));
assertThat(decodedEvents.get(0), instanceOf(WriteRowsEvent.class));
}
@@ -120,7 +128,7 @@ public final class MySQLBinlogEventPacketDecoderTest {
binlogContext.getTableMap().put(0L, tableMapEventPacket);
when(tableMapEventPacket.getColumnDefs()).thenReturn(Collections.emptyList());
List<Object> decodedEvents = new LinkedList<>();
- binlogEventPacketDecoder.decode(null, byteBuf, decodedEvents);
+ binlogEventPacketDecoder.decode(channelHandlerContext, byteBuf, decodedEvents);
assertThat(decodedEvents.size(), is(1));
assertThat(decodedEvents.get(0), instanceOf(UpdateRowsEvent.class));
}
@@ -132,7 +140,7 @@ public final class MySQLBinlogEventPacketDecoderTest {
binlogContext.getTableMap().put(0L, tableMapEventPacket);
when(tableMapEventPacket.getColumnDefs()).thenReturn(Collections.emptyList());
List<Object> decodedEvents = new LinkedList<>();
- binlogEventPacketDecoder.decode(null, byteBuf, decodedEvents);
+ binlogEventPacketDecoder.decode(channelHandlerContext, byteBuf, decodedEvents);
assertThat(decodedEvents.size(), is(1));
assertThat(decodedEvents.get(0), instanceOf(DeleteRowsEvent.class));
}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLCommandPacketDecoderTest.java b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLCommandPacketDecoderTest.java
index 3af395c..cd46498 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLCommandPacketDecoderTest.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLCommandPacketDecoderTest.java
@@ -18,15 +18,20 @@
package org.apache.shardingsphere.scaling.mysql.client.netty;
import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLEofPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
import org.apache.shardingsphere.scaling.mysql.client.InternalResultSet;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
@@ -38,14 +43,22 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class MySQLCommandPacketDecoderTest {
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private ChannelHandlerContext channelHandlerContext;
+
@Mock
private ByteBuf byteBuf;
+ @Before
+ public void setup() {
+ when(channelHandlerContext.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).get()).thenReturn(StandardCharsets.UTF_8);
+ }
+
@Test
- public void assertDecodeOkPacket() throws NoSuchFieldException, IllegalAccessException {
+ public void assertDecodeOkPacket() {
MySQLCommandPacketDecoder commandPacketDecoder = new MySQLCommandPacketDecoder();
List<Object> actual = new LinkedList<>();
- commandPacketDecoder.decode(null, mockOkPacket(), actual);
+ commandPacketDecoder.decode(channelHandlerContext, mockOkPacket(), actual);
assertPacketByType(actual, MySQLOKPacket.class);
}
@@ -56,10 +69,10 @@ public final class MySQLCommandPacketDecoderTest {
}
@Test
- public void assertDecodeErrPacket() throws NoSuchFieldException, IllegalAccessException {
+ public void assertDecodeErrPacket() {
MySQLCommandPacketDecoder commandPacketDecoder = new MySQLCommandPacketDecoder();
List<Object> actual = new LinkedList<>();
- commandPacketDecoder.decode(null, mockErrPacket(), actual);
+ commandPacketDecoder.decode(channelHandlerContext, mockErrPacket(), actual);
assertPacketByType(actual, MySQLErrPacket.class);
}
@@ -70,14 +83,14 @@ public final class MySQLCommandPacketDecoderTest {
}
@Test
- public void assertDecodeQueryCommPacket() throws NoSuchFieldException, IllegalAccessException {
+ public void assertDecodeQueryCommPacket() {
MySQLCommandPacketDecoder commandPacketDecoder = new MySQLCommandPacketDecoder();
List<Object> actual = new LinkedList<>();
- commandPacketDecoder.decode(null, mockEmptyResultSetPacket(), actual);
- commandPacketDecoder.decode(null, mockFieldDefinition41Packet(), actual);
- commandPacketDecoder.decode(null, mockEofPacket(), actual);
- commandPacketDecoder.decode(null, mockEmptyResultSetPacket(), actual);
- commandPacketDecoder.decode(null, mockEofPacket(), actual);
+ commandPacketDecoder.decode(channelHandlerContext, mockEmptyResultSetPacket(), actual);
+ commandPacketDecoder.decode(channelHandlerContext, mockFieldDefinition41Packet(), actual);
+ commandPacketDecoder.decode(channelHandlerContext, mockEofPacket(), actual);
+ commandPacketDecoder.decode(channelHandlerContext, mockEmptyResultSetPacket(), actual);
+ commandPacketDecoder.decode(channelHandlerContext, mockEofPacket(), actual);
assertPacketByType(actual, InternalResultSet.class);
}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLNegotiatePackageDecoderTest.java b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLNegotiatePackageDecoderTest.java
index 1e3511a..203078d 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLNegotiatePackageDecoderTest.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLNegotiatePackageDecoderTest.java
@@ -20,15 +20,20 @@ package org.apache.shardingsphere.scaling.mysql.client.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelHandlerContext;
+import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthMoreDataPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthSwitchRequestPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLHandshakePacket;
import org.apache.shardingsphere.scaling.core.util.ReflectionUtil;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
@@ -40,20 +45,28 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class MySQLNegotiatePackageDecoderTest {
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private ChannelHandlerContext channelHandlerContext;
+
@Mock
private ByteBuf byteBuf;
+ @Before
+ public void setup() {
+ when(channelHandlerContext.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).get()).thenReturn(StandardCharsets.UTF_8);
+ }
+
@Test(expected = IllegalArgumentException.class)
public void assertDecodeUnsupportedProtocolVersion() {
MySQLNegotiatePackageDecoder commandPacketDecoder = new MySQLNegotiatePackageDecoder();
- commandPacketDecoder.decode(null, byteBuf, null);
+ commandPacketDecoder.decode(channelHandlerContext, byteBuf, null);
}
@Test
public void assertDecodeHandshakePacket() {
MySQLNegotiatePackageDecoder commandPacketDecoder = new MySQLNegotiatePackageDecoder();
List<Object> actual = new LinkedList<>();
- commandPacketDecoder.decode(null, mockHandshakePacket(), actual);
+ commandPacketDecoder.decode(channelHandlerContext, mockHandshakePacket(), actual);
assertHandshakePacket(actual);
}
@@ -85,7 +98,7 @@ public final class MySQLNegotiatePackageDecoderTest {
MySQLNegotiatePackageDecoder negotiatePackageDecoder = new MySQLNegotiatePackageDecoder();
ReflectionUtil.setFieldValue(negotiatePackageDecoder, "handshakeReceived", true);
List<Object> actual = new LinkedList<>();
- negotiatePackageDecoder.decode(null, authSwitchRequestPacket(), actual);
+ negotiatePackageDecoder.decode(channelHandlerContext, authSwitchRequestPacket(), actual);
assertPacketByType(actual, MySQLAuthSwitchRequestPacket.class);
}
@@ -101,7 +114,7 @@ public final class MySQLNegotiatePackageDecoderTest {
MySQLNegotiatePackageDecoder negotiatePackageDecoder = new MySQLNegotiatePackageDecoder();
ReflectionUtil.setFieldValue(negotiatePackageDecoder, "handshakeReceived", true);
List<Object> actual = new LinkedList<>();
- negotiatePackageDecoder.decode(null, authMoreDataPacket(), actual);
+ negotiatePackageDecoder.decode(channelHandlerContext, authMoreDataPacket(), actual);
assertPacketByType(actual, MySQLAuthMoreDataPacket.class);
}