You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/08/24 14:16:54 UTC
[shardingsphere] branch master updated: Refactor BackendResponse
judge sequence of QueryCommandExecutor (#7042)
This is an automated email from the ASF dual-hosted git repository.
zhyee 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 c6ea1ad Refactor BackendResponse judge sequence of QueryCommandExecutor (#7042)
c6ea1ad is described below
commit c6ea1adee26db09e743ec1ae9aec32b371fd7621
Author: Liang Zhang <te...@163.com>
AuthorDate: Mon Aug 24 22:16:34 2020 +0800
Refactor BackendResponse judge sequence of QueryCommandExecutor (#7042)
* Decouple AuthenticationEngine and BackendConnection
* Decouple AuthenticationEngine and BackendConnection
* Refactor BackendResponse judge sequence of MySQLComQueryPacketExecutor
* Refactor BackendResponse judge sequence of MySQLComStmtExecuteExecutor
* Refactor BackendResponse judge sequence of PostgreSQLComBindExecutor
* Refactor BackendResponse judge sequence of PostgreSQLComQueryExecutor
* Refactor MySQLComQueryPacketExecutor
* Refactor MySQLComStmtExecuteExecutor
* Refactor PostgreSQLComBindExecutor
* Refactor PostgreSQLComQueryExecutor
---
.../codec/PostgreSQLPacketCodecEngineTest.java | 3 +--
.../execute/MySQLComStmtExecuteExecutor.java | 26 ++++++++++----------
.../text/query/MySQLComQueryPacketExecutor.java | 26 ++++++++++----------
.../execute/MySQLComStmtExecuteExecutorTest.java | 25 ++++++++-----------
.../query/MySQLComQueryPacketExecutorTest.java | 22 +++++++----------
.../binary/bind/PostgreSQLComBindExecutor.java | 28 +++++++++++-----------
.../query/text/PostgreSQLComQueryExecutor.java | 26 ++++++++++----------
.../query/text/PostgreSQLComQueryExecutorTest.java | 7 +++---
8 files changed, 77 insertions(+), 86 deletions(-)
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 e78b10e..77d6883 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
@@ -22,7 +22,6 @@ import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import org.apache.shardingsphere.db.protocol.postgresql.packet.PostgreSQLPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacketType;
-import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
@@ -65,7 +64,7 @@ public class PostgreSQLPacketCodecEngineTest {
when(byteBuf.readInt()).thenReturn(50);
List<Object> out = new LinkedList<>();
new PostgreSQLPacketCodecEngine().decode(context, byteBuf, out, 54);
- assertThat(out.size(), CoreMatchers.is(1));
+ assertThat(out.size(), is(1));
}
@Test
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 0aafc90..7088c0d 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
@@ -81,24 +81,16 @@ public final class MySQLComStmtExecuteExecutor implements QueryCommandExecutor {
return Collections.singletonList(new MySQLErrPacket(1, CommonErrorCode.CIRCUIT_BREAK_MODE));
}
BackendResponse backendResponse = databaseCommunicationEngine.execute();
- if (backendResponse instanceof ErrorResponse) {
- isErrorResponse = true;
- return Collections.singletonList(createErrorPacket(((ErrorResponse) backendResponse).getCause()));
+ if (backendResponse instanceof QueryResponse) {
+ isQueryResponse = true;
+ return createQueryPacket((QueryResponse) backendResponse);
}
if (backendResponse instanceof UpdateResponse) {
isUpdateResponse = true;
return Collections.singletonList(createUpdatePacket((UpdateResponse) backendResponse));
}
- isQueryResponse = true;
- return createQueryPacket((QueryResponse) backendResponse);
- }
-
- private MySQLErrPacket createErrorPacket(final Exception cause) {
- return MySQLErrPacketFactory.newInstance(1, cause);
- }
-
- private MySQLOKPacket createUpdatePacket(final UpdateResponse updateResponse) {
- return new MySQLOKPacket(1, updateResponse.getUpdateCount(), updateResponse.getLastInsertId());
+ isErrorResponse = true;
+ return Collections.singletonList(createErrorPacket(((ErrorResponse) backendResponse).getCause()));
}
private Collection<DatabasePacket<?>> createQueryPacket(final QueryResponse backendResponse) {
@@ -113,6 +105,14 @@ public final class MySQLComStmtExecuteExecutor implements QueryCommandExecutor {
return result;
}
+ private MySQLOKPacket createUpdatePacket(final UpdateResponse updateResponse) {
+ return new MySQLOKPacket(1, updateResponse.getUpdateCount(), updateResponse.getLastInsertId());
+ }
+
+ private MySQLErrPacket createErrorPacket(final Exception cause) {
+ return MySQLErrPacketFactory.newInstance(1, cause);
+ }
+
@Override
public boolean next() throws SQLException {
return databaseCommunicationEngine.next();
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 f39c4e2..15aa62e 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
@@ -77,24 +77,16 @@ public final class MySQLComQueryPacketExecutor implements QueryCommandExecutor {
return Collections.singletonList(new MySQLErrPacket(1, CommonErrorCode.CIRCUIT_BREAK_MODE));
}
BackendResponse backendResponse = textProtocolBackendHandler.execute();
- if (backendResponse instanceof ErrorResponse) {
- isErrorResponse = true;
- return Collections.singletonList(createErrorPacket(((ErrorResponse) backendResponse).getCause()));
+ if (backendResponse instanceof QueryResponse) {
+ isQueryResponse = true;
+ return createQueryPackets((QueryResponse) backendResponse);
}
if (backendResponse instanceof UpdateResponse) {
isUpdateResponse = true;
return Collections.singletonList(createUpdatePacket((UpdateResponse) backendResponse));
}
- isQueryResponse = true;
- return createQueryPackets((QueryResponse) backendResponse);
- }
-
- private MySQLErrPacket createErrorPacket(final Exception cause) {
- return MySQLErrPacketFactory.newInstance(1, cause);
- }
-
- private MySQLOKPacket createUpdatePacket(final UpdateResponse updateResponse) {
- return new MySQLOKPacket(1, updateResponse.getUpdateCount(), updateResponse.getLastInsertId());
+ isErrorResponse = true;
+ return Collections.singletonList(createErrorPacket(((ErrorResponse) backendResponse).getCause()));
}
private Collection<DatabasePacket<?>> createQueryPackets(final QueryResponse backendResponse) {
@@ -126,6 +118,14 @@ public final class MySQLComQueryPacketExecutor implements QueryCommandExecutor {
return result;
}
+ private MySQLOKPacket createUpdatePacket(final UpdateResponse updateResponse) {
+ return new MySQLOKPacket(1, updateResponse.getUpdateCount(), updateResponse.getLastInsertId());
+ }
+
+ private MySQLErrPacket createErrorPacket(final Exception cause) {
+ return MySQLErrPacketFactory.newInstance(1, cause);
+ }
+
@Override
public boolean next() throws SQLException {
return textProtocolBackendHandler.next();
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 baae84c..cacc06e 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,7 +17,6 @@
package org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.execute;
-import lombok.SneakyThrows;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.execute.MySQLComStmtExecutePacket;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
@@ -33,7 +32,6 @@ import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import org.apache.shardingsphere.rdl.parser.engine.ShardingSphereSQLParserEngine;
-import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -47,6 +45,7 @@ import java.util.Collections;
import java.util.Map;
import java.util.Properties;
+import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -61,8 +60,7 @@ public final class MySQLComStmtExecuteExecutorTest {
private DatabaseCommunicationEngine databaseCommunicationEngine;
@Before
- @SneakyThrows(ReflectiveOperationException.class)
- public void setUp() {
+ public void setUp() throws ReflectiveOperationException {
Field schemaContexts = ProxySchemaContexts.getInstance().getClass().getDeclaredField("schemaContexts");
schemaContexts.setAccessible(true);
schemaContexts.set(ProxySchemaContexts.getInstance(),
@@ -79,38 +77,35 @@ public final class MySQLComStmtExecuteExecutorTest {
}
@Test
- @SneakyThrows
- public void assertIsErrorResponse() {
+ public void assertIsQueryResponse() throws NoSuchFieldException {
BackendConnection backendConnection = mock(BackendConnection.class);
when(backendConnection.getSchema()).thenReturn("schema");
MySQLComStmtExecuteExecutor mysqlComStmtExecuteExecutor = new MySQLComStmtExecuteExecutor(mock(MySQLComStmtExecutePacket.class), backendConnection);
FieldSetter.setField(mysqlComStmtExecuteExecutor, MySQLComStmtExecuteExecutor.class.getDeclaredField("databaseCommunicationEngine"), databaseCommunicationEngine);
- when(databaseCommunicationEngine.execute()).thenReturn(new ErrorResponse(sqlException));
+ when(databaseCommunicationEngine.execute()).thenReturn(new QueryResponse(Collections.singletonList(mock(QueryHeader.class))));
mysqlComStmtExecuteExecutor.execute();
- assertThat(mysqlComStmtExecuteExecutor.isErrorResponse(), Matchers.is(true));
+ assertThat(mysqlComStmtExecuteExecutor.isQueryResponse(), is(true));
}
@Test
- @SneakyThrows
- public void assertIsUpdateResponse() {
+ public void assertIsUpdateResponse() throws NoSuchFieldException {
BackendConnection backendConnection = mock(BackendConnection.class);
when(backendConnection.getSchema()).thenReturn("schema");
MySQLComStmtExecuteExecutor mysqlComStmtExecuteExecutor = new MySQLComStmtExecuteExecutor(mock(MySQLComStmtExecutePacket.class), backendConnection);
FieldSetter.setField(mysqlComStmtExecuteExecutor, MySQLComStmtExecuteExecutor.class.getDeclaredField("databaseCommunicationEngine"), databaseCommunicationEngine);
when(databaseCommunicationEngine.execute()).thenReturn(new UpdateResponse());
mysqlComStmtExecuteExecutor.execute();
- assertThat(mysqlComStmtExecuteExecutor.isUpdateResponse(), Matchers.is(true));
+ assertThat(mysqlComStmtExecuteExecutor.isUpdateResponse(), is(true));
}
@Test
- @SneakyThrows
- public void assertIsQuery() {
+ public void assertIsErrorResponse() throws NoSuchFieldException {
BackendConnection backendConnection = mock(BackendConnection.class);
when(backendConnection.getSchema()).thenReturn("schema");
MySQLComStmtExecuteExecutor mysqlComStmtExecuteExecutor = new MySQLComStmtExecuteExecutor(mock(MySQLComStmtExecutePacket.class), backendConnection);
FieldSetter.setField(mysqlComStmtExecuteExecutor, MySQLComStmtExecuteExecutor.class.getDeclaredField("databaseCommunicationEngine"), databaseCommunicationEngine);
- when(databaseCommunicationEngine.execute()).thenReturn(new QueryResponse(Collections.singletonList(mock(QueryHeader.class))));
+ when(databaseCommunicationEngine.execute()).thenReturn(new ErrorResponse(sqlException));
mysqlComStmtExecuteExecutor.execute();
- assertThat(mysqlComStmtExecuteExecutor.isQueryResponse(), Matchers.is(true));
+ assertThat(mysqlComStmtExecuteExecutor.isErrorResponse(), is(true));
}
}
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 ac79d1c..b197d1b 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,14 +17,12 @@
package org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.query;
-import lombok.SneakyThrows;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.query.MySQLComQueryPacket;
import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.QueryHeader;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
-import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -34,6 +32,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.sql.SQLException;
import java.util.Collections;
+import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -50,29 +49,26 @@ public final class MySQLComQueryPacketExecutorTest {
private final MySQLComQueryPacketExecutor mysqlComQueryPacketExecutor = new MySQLComQueryPacketExecutor(mock(MySQLComQueryPacket.class), null);
@Test
- @SneakyThrows
- public void assertIsErrorResponse() {
+ public void assertIsQueryResponse() throws SQLException, NoSuchFieldException {
FieldSetter.setField(mysqlComQueryPacketExecutor, MySQLComQueryPacketExecutor.class.getDeclaredField("textProtocolBackendHandler"), textProtocolBackendHandler);
- when(textProtocolBackendHandler.execute()).thenReturn(new ErrorResponse(sqlException));
+ when(textProtocolBackendHandler.execute()).thenReturn(new QueryResponse(Collections.singletonList(mock(QueryHeader.class))));
mysqlComQueryPacketExecutor.execute();
- assertThat(mysqlComQueryPacketExecutor.isErrorResponse(), Matchers.is(true));
+ assertThat(mysqlComQueryPacketExecutor.isQueryResponse(), is(true));
}
@Test
- @SneakyThrows
- public void assertIsUpdateResponse() {
+ public void assertIsUpdateResponse() throws SQLException, NoSuchFieldException {
FieldSetter.setField(mysqlComQueryPacketExecutor, MySQLComQueryPacketExecutor.class.getDeclaredField("textProtocolBackendHandler"), textProtocolBackendHandler);
when(textProtocolBackendHandler.execute()).thenReturn(new UpdateResponse());
mysqlComQueryPacketExecutor.execute();
- assertThat(mysqlComQueryPacketExecutor.isUpdateResponse(), Matchers.is(true));
+ assertThat(mysqlComQueryPacketExecutor.isUpdateResponse(), is(true));
}
@Test
- @SneakyThrows
- public void assertIsQuery() {
+ public void assertIsErrorResponse() throws SQLException, NoSuchFieldException {
FieldSetter.setField(mysqlComQueryPacketExecutor, MySQLComQueryPacketExecutor.class.getDeclaredField("textProtocolBackendHandler"), textProtocolBackendHandler);
- when(textProtocolBackendHandler.execute()).thenReturn(new QueryResponse(Collections.singletonList(mock(QueryHeader.class))));
+ when(textProtocolBackendHandler.execute()).thenReturn(new ErrorResponse(sqlException));
mysqlComQueryPacketExecutor.execute();
- assertThat(mysqlComQueryPacketExecutor.isQueryResponse(), Matchers.is(true));
+ assertThat(mysqlComQueryPacketExecutor.isErrorResponse(), is(true));
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/binary/bind/PostgreSQLComBindExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/binary/bind/PostgreSQLComBindExecutor.java
index 6b063d3..27adb99 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/binary/bind/PostgreSQLComBindExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/binary/bind/PostgreSQLComBindExecutor.java
@@ -95,28 +95,20 @@ public final class PostgreSQLComBindExecutor implements QueryCommandExecutor {
return result;
}
BackendResponse backendResponse = databaseCommunicationEngine.execute();
- if (backendResponse instanceof ErrorResponse) {
- isErrorResponse = true;
- result.add(createErrorPacket((ErrorResponse) backendResponse));
+ if (backendResponse instanceof QueryResponse) {
+ createQueryPacket((QueryResponse) backendResponse).ifPresent(result::add);
}
if (backendResponse instanceof UpdateResponse) {
isUpdateResponse = true;
result.add(createUpdatePacket((UpdateResponse) backendResponse));
}
- if (backendResponse instanceof QueryResponse) {
- createQueryPacket((QueryResponse) backendResponse).ifPresent(result::add);
+ if (backendResponse instanceof ErrorResponse) {
+ isErrorResponse = true;
+ result.add(createErrorPacket((ErrorResponse) backendResponse));
}
return result;
}
- private PostgreSQLErrorResponsePacket createErrorPacket(final ErrorResponse errorResponse) {
- return PostgreSQLErrPacketFactory.newInstance(errorResponse.getCause());
- }
-
- private PostgreSQLCommandCompletePacket createUpdatePacket(final UpdateResponse updateResponse) {
- return new PostgreSQLCommandCompletePacket(updateResponse.getType(), updateResponse.getUpdateCount());
- }
-
private Optional<PostgreSQLRowDescriptionPacket> createQueryPacket(final QueryResponse queryResponse) {
List<PostgreSQLColumnDescription> columnDescriptions = getPostgreSQLColumnDescriptions(queryResponse);
isQueryResponse = !columnDescriptions.isEmpty();
@@ -129,7 +121,7 @@ public final class PostgreSQLComBindExecutor implements QueryCommandExecutor {
private List<PostgreSQLColumnDescription> getPostgreSQLColumnDescriptions(final QueryResponse queryResponse) {
List<PostgreSQLColumnDescription> result = new LinkedList<>();
List<QueryResult> queryResults = queryResponse.getQueryResults();
- ResultSetMetaData resultSetMetaData = !queryResults.isEmpty() ? queryResults.get(0).getResultSetMetaData() : null;
+ ResultSetMetaData resultSetMetaData = queryResults.isEmpty() ? null : queryResults.get(0).getResultSetMetaData();
int columnIndex = 0;
for (QueryHeader each : queryResponse.getQueryHeaders()) {
result.add(new PostgreSQLColumnDescription(each.getColumnName(), ++columnIndex, each.getColumnType(), each.getColumnLength(), resultSetMetaData));
@@ -137,6 +129,14 @@ public final class PostgreSQLComBindExecutor implements QueryCommandExecutor {
return result;
}
+ private PostgreSQLCommandCompletePacket createUpdatePacket(final UpdateResponse updateResponse) {
+ return new PostgreSQLCommandCompletePacket(updateResponse.getType(), updateResponse.getUpdateCount());
+ }
+
+ private PostgreSQLErrorResponsePacket createErrorPacket(final ErrorResponse errorResponse) {
+ return PostgreSQLErrPacketFactory.newInstance(errorResponse.getCause());
+ }
+
@Override
public boolean next() throws SQLException {
return null != databaseCommunicationEngine && databaseCommunicationEngine.next();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/text/PostgreSQLComQueryExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/text/PostgreSQLComQueryExecutor.java
index 65dc4fc..9260319 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/text/PostgreSQLComQueryExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/text/PostgreSQLComQueryExecutor.java
@@ -74,24 +74,16 @@ public final class PostgreSQLComQueryExecutor implements QueryCommandExecutor {
return Collections.singletonList(new PostgreSQLErrorResponsePacket());
}
BackendResponse backendResponse = textProtocolBackendHandler.execute();
- if (backendResponse instanceof ErrorResponse) {
- isErrorResponse = true;
- return Collections.singletonList(createErrorPacket((ErrorResponse) backendResponse));
+ if (backendResponse instanceof QueryResponse) {
+ Optional<PostgreSQLRowDescriptionPacket> result = createQueryPacket((QueryResponse) backendResponse);
+ return result.<List<DatabasePacket<?>>>map(Collections::singletonList).orElseGet(Collections::emptyList);
}
if (backendResponse instanceof UpdateResponse) {
isUpdateResponse = true;
return Collections.singletonList(createUpdatePacket((UpdateResponse) backendResponse));
}
- Optional<PostgreSQLRowDescriptionPacket> result = createQueryPacket((QueryResponse) backendResponse);
- return result.<List<DatabasePacket<?>>>map(Collections::singletonList).orElseGet(Collections::emptyList);
- }
-
- private PostgreSQLErrorResponsePacket createErrorPacket(final ErrorResponse errorResponse) {
- return PostgreSQLErrPacketFactory.newInstance(errorResponse.getCause());
- }
-
- private PostgreSQLCommandCompletePacket createUpdatePacket(final UpdateResponse updateResponse) {
- return new PostgreSQLCommandCompletePacket(updateResponse.getType(), updateResponse.getUpdateCount());
+ isErrorResponse = true;
+ return Collections.singletonList(createErrorPacket((ErrorResponse) backendResponse));
}
private Optional<PostgreSQLRowDescriptionPacket> createQueryPacket(final QueryResponse queryResponse) {
@@ -114,6 +106,14 @@ public final class PostgreSQLComQueryExecutor implements QueryCommandExecutor {
return result;
}
+ private PostgreSQLCommandCompletePacket createUpdatePacket(final UpdateResponse updateResponse) {
+ return new PostgreSQLCommandCompletePacket(updateResponse.getType(), updateResponse.getUpdateCount());
+ }
+
+ private PostgreSQLErrorResponsePacket createErrorPacket(final ErrorResponse errorResponse) {
+ return PostgreSQLErrPacketFactory.newInstance(errorResponse.getCause());
+ }
+
@Override
public boolean next() throws SQLException {
return textProtocolBackendHandler.next();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/text/PostgreSQLComQueryExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/text/PostgreSQLComQueryExecutorTest.java
index dbb6dc3..a01ee43 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/text/PostgreSQLComQueryExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/text/PostgreSQLComQueryExecutorTest.java
@@ -22,7 +22,6 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.tex
import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLErrorResponsePacket;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
-import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -31,6 +30,8 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.postgresql.util.PSQLException;
import org.postgresql.util.ServerErrorMessage;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -48,7 +49,7 @@ public final class PostgreSQLComQueryExecutorTest {
FieldSetter.setField(postgreSQLComQueryExecutor, PostgreSQLComQueryExecutor.class.getDeclaredField("textProtocolBackendHandler"), textProtocolBackendHandler);
ErrorResponse errorResponse = new ErrorResponse(new PSQLException(mock(ServerErrorMessage.class)));
when(textProtocolBackendHandler.execute()).thenReturn(errorResponse);
- assertThat(postgreSQLComQueryExecutor.execute().iterator().next(), Matchers.instanceOf(PostgreSQLErrorResponsePacket.class));
- assertThat(postgreSQLComQueryExecutor.isErrorResponse(), Matchers.is(true));
+ assertThat(postgreSQLComQueryExecutor.execute().iterator().next(), instanceOf(PostgreSQLErrorResponsePacket.class));
+ assertThat(postgreSQLComQueryExecutor.isErrorResponse(), is(true));
}
}