You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by wu...@apache.org on 2021/05/15 01:22:29 UTC
[shardingsphere] branch master updated: extract to single class
(#10334)
This is an automated email from the ASF dual-hosted git repository.
wuweijie 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 270d701 extract to single class (#10334)
270d701 is described below
commit 270d70160454e5d5db841251c81e0ff0784fbc65
Author: totalo <48...@qq.com>
AuthorDate: Sat May 15 09:21:57 2021 +0800
extract to single class (#10334)
---
.../ConnectionScopeBinaryStatementRegistry.java | 8 ++++--
.../query/binary/PostgreSQLBinaryStatement.java | 3 +-
.../PostgreSQLBinaryStatementParameterType.java | 33 ----------------------
.../query/binary/bind/PostgreSQLComBindPacket.java | 23 ++++++++-------
.../binary/parse/PostgreSQLComParsePacket.java | 11 ++++----
.../binary/bind/PostgreSQLComBindPacketTest.java | 3 +-
.../binary/parse/PostgreSQLComParsePacketTest.java | 5 ++--
.../binary/parse/PostgreSQLComParseExecutor.java | 2 +-
8 files changed, 27 insertions(+), 61 deletions(-)
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/ConnectionScopeBinaryStatementRegistry.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/ConnectionScopeBinaryStatementRegistry.java
index 651f036..e455c4f 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/ConnectionScopeBinaryStatementRegistry.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/ConnectionScopeBinaryStatementRegistry.java
@@ -17,6 +17,8 @@
package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary;
+import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLBinaryColumnType;
+
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@@ -34,10 +36,10 @@ public final class ConnectionScopeBinaryStatementRegistry {
* @param statementId statement ID
* @param sql SQL
* @param parameterCount parameter count
- * @param binaryStatementParameterTypes binary statement parameter types
+ * @param binaryColumnTypes binary statement column types
*/
- public void register(final String statementId, final String sql, final int parameterCount, final List<PostgreSQLBinaryStatementParameterType> binaryStatementParameterTypes) {
- binaryStatements.put(statementId, new PostgreSQLBinaryStatement(sql, parameterCount, binaryStatementParameterTypes));
+ public void register(final String statementId, final String sql, final int parameterCount, final List<PostgreSQLBinaryColumnType> binaryColumnTypes) {
+ binaryStatements.put(statementId, new PostgreSQLBinaryStatement(sql, parameterCount, binaryColumnTypes));
}
/**
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/PostgreSQLBinaryStatement.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/PostgreSQLBinaryStatement.java
index 3cd01d2..754ac96 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/PostgreSQLBinaryStatement.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/PostgreSQLBinaryStatement.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.bi
import lombok.Getter;
import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLBinaryColumnType;
import java.util.List;
@@ -33,5 +34,5 @@ public final class PostgreSQLBinaryStatement {
private final int parameterCount;
- private final List<PostgreSQLBinaryStatementParameterType> parameterTypes;
+ private final List<PostgreSQLBinaryColumnType> columnTypes;
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/PostgreSQLBinaryStatementParameterType.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/PostgreSQLBinaryStatementParameterType.java
deleted file mode 100644
index 2e27438..0000000
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/PostgreSQLBinaryStatementParameterType.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * 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.postgresql.packet.command.query.binary;
-
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLBinaryColumnType;
-
-/**
- * Bind parameter type for PostgreSQL.
- */
-@RequiredArgsConstructor
-@Getter
-// TODO merge this class and PostgreSQLBinaryColumnType, remove PostgreSQLBinaryStatementParameterType
-public final class PostgreSQLBinaryStatementParameterType {
-
- private final PostgreSQLBinaryColumnType columnType;
-}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/PostgreSQLComBindPacket.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/PostgreSQLComBindPacket.java
index 81769c5..a098fb6 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/PostgreSQLComBindPacket.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/PostgreSQLComBindPacket.java
@@ -18,10 +18,10 @@
package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind;
import lombok.Getter;
+import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLBinaryColumnType;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacketType;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.PostgreSQLBinaryStatement;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.PostgreSQLBinaryStatementParameterType;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.PostgreSQLBinaryStatementRegistry;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.protocol.PostgreSQLBinaryProtocolValue;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.protocol.PostgreSQLBinaryProtocolValueFactory;
@@ -61,7 +61,7 @@ public final class PostgreSQLComBindPacket extends PostgreSQLCommandPacket {
}
PostgreSQLBinaryStatement binaryStatement = PostgreSQLBinaryStatementRegistry.getInstance().get(connectionId).getBinaryStatement(statementId);
sql = null == binaryStatement ? null : binaryStatement.getSql();
- parameters = null == sql ? Collections.emptyList() : getParameters(payload, parameterFormats, binaryStatement.getParameterTypes());
+ parameters = null == sql ? Collections.emptyList() : getParameters(payload, parameterFormats, binaryStatement.getColumnTypes());
int resultFormatsLength = payload.readInt2();
binaryRowData = parameterFormats.contains(1);
for (int i = 0; i < resultFormatsLength; i++) {
@@ -69,7 +69,7 @@ public final class PostgreSQLComBindPacket extends PostgreSQLCommandPacket {
}
}
- private List<Object> getParameters(final PostgreSQLPacketPayload payload, final List<Integer> parameterFormats, final List<PostgreSQLBinaryStatementParameterType> parameterTypes) {
+ private List<Object> getParameters(final PostgreSQLPacketPayload payload, final List<Integer> parameterFormats, final List<PostgreSQLBinaryColumnType> columnTypes) {
int parameterCount = payload.readInt2();
List<Object> result = new ArrayList<>(parameterCount);
for (int parameterIndex = 0; parameterIndex < parameterCount; parameterIndex++) {
@@ -79,7 +79,7 @@ public final class PostgreSQLComBindPacket extends PostgreSQLCommandPacket {
continue;
}
Object parameterValue = isTextParameterValue(parameterFormats, parameterIndex)
- ? getTextParameters(payload, parameterValueLength, parameterTypes.get(parameterIndex)) : getBinaryParameters(payload, parameterValueLength, parameterTypes.get(parameterIndex));
+ ? getTextParameters(payload, parameterValueLength, columnTypes.get(parameterIndex)) : getBinaryParameters(payload, parameterValueLength, columnTypes.get(parameterIndex));
result.add(parameterValue);
}
return result;
@@ -95,15 +95,14 @@ public final class PostgreSQLComBindPacket extends PostgreSQLCommandPacket {
return 0 == parameterFormats.get(parameterIndex);
}
- private Object getTextParameters(final PostgreSQLPacketPayload payload, final int parameterValueLength, final PostgreSQLBinaryStatementParameterType parameterType) {
+ private Object getTextParameters(final PostgreSQLPacketPayload payload, final int parameterValueLength, final PostgreSQLBinaryColumnType columnType) {
byte[] bytes = new byte[parameterValueLength];
payload.getByteBuf().readBytes(bytes);
- return getTextParameters(new String(bytes), parameterType);
+ return getTextParameters(new String(bytes), columnType);
}
-
- // TODO extract to single class
- private Object getTextParameters(final String textValue, final PostgreSQLBinaryStatementParameterType parameterType) {
- switch (parameterType.getColumnType()) {
+
+ private Object getTextParameters(final String textValue, final PostgreSQLBinaryColumnType columnType) {
+ switch (columnType) {
case POSTGRESQL_TYPE_BOOL:
return Boolean.valueOf(textValue);
case POSTGRESQL_TYPE_INT2:
@@ -137,8 +136,8 @@ public final class PostgreSQLComBindPacket extends PostgreSQLCommandPacket {
}
}
- private Object getBinaryParameters(final PostgreSQLPacketPayload payload, final int parameterValueLength, final PostgreSQLBinaryStatementParameterType parameterType) {
- PostgreSQLBinaryProtocolValue binaryProtocolValue = PostgreSQLBinaryProtocolValueFactory.getBinaryProtocolValue(parameterType.getColumnType());
+ private Object getBinaryParameters(final PostgreSQLPacketPayload payload, final int parameterValueLength, final PostgreSQLBinaryColumnType columnType) {
+ PostgreSQLBinaryProtocolValue binaryProtocolValue = PostgreSQLBinaryProtocolValueFactory.getBinaryProtocolValue(columnType);
return binaryProtocolValue.read(payload, parameterValueLength);
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLComParsePacket.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLComParsePacket.java
index 8d31ce1..646cd1f 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLComParsePacket.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLComParsePacket.java
@@ -21,7 +21,6 @@ import lombok.Getter;
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLBinaryColumnType;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacketType;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.PostgreSQLBinaryStatementParameterType;
import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLIdentifierTag;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
@@ -39,20 +38,20 @@ public final class PostgreSQLComParsePacket extends PostgreSQLCommandPacket {
private final String sql;
- private final List<PostgreSQLBinaryStatementParameterType> binaryStatementParameterTypes;
+ private final List<PostgreSQLBinaryColumnType> binaryStatementColumnTypes;
public PostgreSQLComParsePacket(final PostgreSQLPacketPayload payload) {
payload.readInt4();
statementId = payload.readStringNul();
sql = alterSQLToJDBCStyle(payload.readStringNul());
- binaryStatementParameterTypes = sql.isEmpty() ? Collections.emptyList() : getParameterTypes(payload);
+ binaryStatementColumnTypes = sql.isEmpty() ? Collections.emptyList() : getParameterTypes(payload);
}
- private List<PostgreSQLBinaryStatementParameterType> getParameterTypes(final PostgreSQLPacketPayload payload) {
+ private List<PostgreSQLBinaryColumnType> getParameterTypes(final PostgreSQLPacketPayload payload) {
int parameterCount = payload.readInt2();
- List<PostgreSQLBinaryStatementParameterType> result = new ArrayList<>(parameterCount);
+ List<PostgreSQLBinaryColumnType> result = new ArrayList<>(parameterCount);
for (int i = 0; i < parameterCount; i++) {
- result.add(new PostgreSQLBinaryStatementParameterType(PostgreSQLBinaryColumnType.valueOf(payload.readInt4())));
+ result.add(PostgreSQLBinaryColumnType.valueOf(payload.readInt4()));
}
return result;
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/PostgreSQLComBindPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/PostgreSQLComBindPacketTest.java
index daccdf0..2415524 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/PostgreSQLComBindPacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/PostgreSQLComBindPacketTest.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.bi
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLBinaryColumnType;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacketType;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.PostgreSQLBinaryStatementRegistry;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.PostgreSQLBinaryStatementParameterType;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Before;
import org.junit.Test;
@@ -56,7 +55,7 @@ public final class PostgreSQLComBindPacketTest {
when(payload.readInt4()).thenReturn(1);
String sql = "select * from order where id = ? ";
PostgreSQLBinaryStatementRegistry.getInstance().get(1).register(
- "sts-id", sql, 1, Collections.singletonList(new PostgreSQLBinaryStatementParameterType(PostgreSQLBinaryColumnType.POSTGRESQL_TYPE_INT8)));
+ "sts-id", sql, 1, Collections.singletonList(PostgreSQLBinaryColumnType.POSTGRESQL_TYPE_INT8));
PostgreSQLComBindPacket bindPacket = new PostgreSQLComBindPacket(payload, 1);
bindPacket.write(payload);
assertThat(bindPacket.getSql(), is(sql));
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLComParsePacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLComParsePacketTest.java
index a395fae..b4929a1 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLComParsePacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLComParsePacketTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.bi
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLBinaryColumnType;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacketType;
-import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.PostgreSQLBinaryStatementParameterType;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -50,9 +49,9 @@ public final class PostgreSQLComParsePacketTest {
assertThat(actual.getIdentifier(), is(PostgreSQLCommandPacketType.PARSE_COMMAND));
assertThat(actual.getSql(), is("sql"));
assertThat(actual.getStatementId(), is("sql"));
- List<PostgreSQLBinaryStatementParameterType> types = actual.getBinaryStatementParameterTypes();
+ List<PostgreSQLBinaryColumnType> types = actual.getBinaryStatementColumnTypes();
assertNotNull(types);
assertThat(types.size(), equalTo(1));
- assertThat(types.get(0).getColumnType(), is(PostgreSQLBinaryColumnType.POSTGRESQL_TYPE_UNSPECIFIED));
+ assertThat(types.get(0), is(PostgreSQLBinaryColumnType.POSTGRESQL_TYPE_UNSPECIFIED));
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/binary/parse/PostgreSQLComParseExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/binary/parse/PostgreSQLComParseExecutor.java
index 17e1436..fb82905 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/binary/parse/PostgreSQLComParseExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/binary/parse/PostgreSQLComParseExecutor.java
@@ -55,7 +55,7 @@ public final class PostgreSQLComParseExecutor implements CommandExecutor {
ShardingSphereSQLParserEngine sqlStatementParserEngine = new ShardingSphereSQLParserEngine(
DatabaseTypeRegistry.getTrunkDatabaseTypeName(ProxyContext.getInstance().getMetaDataContexts().getMetaData(schemaName).getResource().getDatabaseType()));
SQLStatement sqlStatement = sqlStatementParserEngine.parse(packet.getSql(), true);
- binaryStatementRegistry.register(packet.getStatementId(), packet.getSql(), sqlStatement.getParameterCount(), packet.getBinaryStatementParameterTypes());
+ binaryStatementRegistry.register(packet.getStatementId(), packet.getSql(), sqlStatement.getParameterCount(), packet.getBinaryStatementColumnTypes());
}
return Collections.singletonList(new PostgreSQLParseCompletePacket());
}