未验证 提交 31db55e7 编写于 作者: L Liang Zhang 提交者: GitHub

Remove ErrorResponse (#7369)

* Refactor MySQLComStmtExecuteExecutor

* Refactor MySQLComStmtExecuteExecutor

* Use protocol framework to process internal COM_QUERY exception

* Use protocol framework to process internal COM_QUERY exception

* Refactor PostgreSQLComBindExecutor

* Refactor PostgreSQLComQueryExecutor

* Remove ErrorResponse

* Revise PostgreSQLCommandExecuteEngine
上级 fbb8da43
......@@ -31,8 +31,9 @@ public interface DatabaseCommunicationEngine {
* Execute command.
*
* @return backend response
* @throws SQLException SQL exception
*/
BackendResponse execute();
BackendResponse execute() throws SQLException;
/**
* Goto next result value.
......
......@@ -18,11 +18,10 @@
package org.apache.shardingsphere.proxy.backend.communication.jdbc;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.governance.core.event.persist.MetaDataPersistEvent;
import org.apache.shardingsphere.governance.core.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.context.SchemaContext;
import org.apache.shardingsphere.governance.core.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.governance.core.event.persist.MetaDataPersistEvent;
import org.apache.shardingsphere.infra.executor.sql.QueryResult;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.executor.sql.log.SQLLogger;
......@@ -37,14 +36,12 @@ import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicati
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.ConnectionStatus;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.execute.SQLExecuteEngine;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.TableModifyInTransactionException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.sharding.route.engine.exception.TableExistsException;
import org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaData;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
......@@ -83,15 +80,10 @@ public final class JDBCDatabaseCommunicationEngine implements DatabaseCommunicat
}
@Override
public BackendResponse execute() {
try {
ExecutionContext executionContext = executeEngine.generateExecutionContext(sql);
logSQL(executionContext);
return doExecute(executionContext);
} catch (final TableExistsException | ShardingSphereConfigurationException | SQLException ex) {
// TODO Particular handling needed for `createTable` without shardingRule and dataNode.
return new ErrorResponse(ex);
}
public BackendResponse execute() throws SQLException {
ExecutionContext executionContext = executeEngine.generateExecutionContext(sql);
logSQL(executionContext);
return doExecute(executionContext);
}
private void logSQL(final ExecutionContext executionContext) {
......@@ -106,7 +98,7 @@ public final class JDBCDatabaseCommunicationEngine implements DatabaseCommunicat
}
SQLStatementContext<?> sqlStatementContext = executionContext.getSqlStatementContext();
if (isExecuteDDLInXATransaction(sqlStatementContext.getSqlStatement())) {
return new ErrorResponse(new TableModifyInTransactionException(getTableName(sqlStatementContext)));
throw new TableModifyInTransactionException(getTableName(sqlStatementContext));
}
response = executeEngine.execute(executionContext);
refreshTableMetaData(executionContext.getSqlStatementContext());
......
/*
* 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.backend.response.error;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
/**
* Error response.
*/
@RequiredArgsConstructor
@Getter
public final class ErrorResponse implements BackendResponse {
private final Exception cause;
}
......@@ -20,16 +20,14 @@ package org.apache.shardingsphere.proxy.backend.text.admin;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Collection;
import java.util.LinkedList;
import java.sql.SQLException;
/**
* Backend handler for broadcast.
......@@ -46,19 +44,13 @@ public final class BroadcastBackendHandler implements TextProtocolBackendHandler
private final BackendConnection backendConnection;
@Override
public BackendResponse execute() {
Collection<BackendResponse> responses = new LinkedList<>();
public BackendResponse execute() throws SQLException {
String originalSchema = backendConnection.getSchema();
for (String each : ProxyContext.getInstance().getAllSchemaNames()) {
backendConnection.setCurrentSchema(each);
responses.add(databaseCommunicationEngineFactory.newTextProtocolInstance(sqlStatement, sql, backendConnection).execute());
databaseCommunicationEngineFactory.newTextProtocolInstance(sqlStatement, sql, backendConnection).execute();
}
backendConnection.setCurrentSchema(originalSchema);
for (BackendResponse each : responses) {
if (each instanceof ErrorResponse) {
return each;
}
}
return new UpdateResponse();
}
......
......@@ -18,22 +18,21 @@
package org.apache.shardingsphere.proxy.backend.text.admin;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.governance.core.event.persist.DataSourcePersistEvent;
import org.apache.shardingsphere.governance.core.event.persist.RulePersistEvent;
import org.apache.shardingsphere.governance.core.event.persist.SchemaNamePersistEvent;
import org.apache.shardingsphere.governance.core.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.context.impl.StandardSchemaContexts;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.governance.core.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.governance.core.event.persist.DataSourcePersistEvent;
import org.apache.shardingsphere.governance.core.event.persist.RulePersistEvent;
import org.apache.shardingsphere.governance.core.event.persist.SchemaNamePersistEvent;
import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.proxy.config.util.DataSourceParameterConverter;
import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
......@@ -67,17 +66,17 @@ public final class RDLBackendHandler implements TextProtocolBackendHandler {
private final SQLStatement sqlStatement;
@Override
public BackendResponse execute() {
public BackendResponse execute() throws SQLException {
SQLStatementContext<?> context = getSQLStatementContext();
if (!isRegistryCenterExisted()) {
return new ErrorResponse(new SQLException("No Registry center to execute `%s` SQL", context.getClass().getSimpleName()));
throw new SQLException(String.format("No Registry center to execute `%s` SQL", context.getClass().getSimpleName()));
}
return getBackendResponse(context);
}
private BackendResponse execute(final CreateDatabaseStatementContext context) {
if (ProxyContext.getInstance().getAllSchemaNames().contains(context.getSqlStatement().getDatabaseName())) {
return new ErrorResponse(new DBCreateExistsException(context.getSqlStatement().getDatabaseName()));
throw new DBCreateExistsException(context.getSqlStatement().getDatabaseName());
}
// TODO Need to get the executed feedback from registry center for returning.
ShardingSphereEventBus.getInstance().post(new SchemaNamePersistEvent(context.getSqlStatement().getDatabaseName(), false));
......@@ -88,7 +87,7 @@ public final class RDLBackendHandler implements TextProtocolBackendHandler {
private BackendResponse execute(final DropDatabaseStatementContext context) {
if (!ProxyContext.getInstance().getAllSchemaNames().contains(context.getSqlStatement().getDatabaseName())) {
return new ErrorResponse(new DBCreateExistsException(context.getSqlStatement().getDatabaseName()));
throw new DBCreateExistsException(context.getSqlStatement().getDatabaseName());
}
// TODO Need to get the executed feedback from registry center for returning.
ShardingSphereEventBus.getInstance().post(new SchemaNamePersistEvent(context.getSqlStatement().getDatabaseName(), true));
......@@ -109,8 +108,8 @@ public final class RDLBackendHandler implements TextProtocolBackendHandler {
}
private BackendResponse execute(final CreateShardingRuleStatementContext context) {
YamlShardingRuleConfiguration configurations = new CreateShardingRuleStatementContextConverter().convert(context);
Collection<RuleConfiguration> rules = new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singleton(configurations));
YamlShardingRuleConfiguration configs = new CreateShardingRuleStatementContextConverter().convert(context);
Collection<RuleConfiguration> rules = new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singleton(configs));
// TODO Need to get the executed feedback from registry center for returning.
ShardingSphereEventBus.getInstance().post(new RulePersistEvent(backendConnection.getSchema(), rules));
UpdateResponse result = new UpdateResponse();
......
......@@ -23,12 +23,11 @@ import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.Que
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
......@@ -53,10 +52,10 @@ public final class ShowTablesBackendHandler implements TextProtocolBackendHandle
private DatabaseCommunicationEngine databaseCommunicationEngine;
@Override
public BackendResponse execute() {
public BackendResponse execute() throws SQLException {
SchemaContext context = ProxyContext.getInstance().getSchema(backendConnection.getSchema());
if (null == context) {
return new ErrorResponse(new NoDatabaseSelectedException());
throw new NoDatabaseSelectedException();
}
if (!context.isComplete()) {
return getDefaultQueryResponse(backendConnection.getSchema());
......
......@@ -22,11 +22,10 @@ import org.apache.shardingsphere.infra.context.SchemaContext;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
......@@ -50,11 +49,11 @@ public final class UnicastBackendHandler implements TextProtocolBackendHandler {
private DatabaseCommunicationEngine databaseCommunicationEngine;
@Override
public BackendResponse execute() {
public BackendResponse execute() throws SQLException {
if (null == backendConnection.getSchema()) {
Map<String, SchemaContext> schemaContexts = ProxyContext.getInstance().getSchemaContexts().getSchemaContexts();
if (schemaContexts.isEmpty()) {
return new ErrorResponse(new NoDatabaseSelectedException());
throw new NoDatabaseSelectedException();
}
// TODO we should remove set default ShardingSphere schema after parser can recognize all DAL broadcast SQL.
backendConnection.setCurrentSchema(schemaContexts.keySet().iterator().next());
......
......@@ -19,12 +19,11 @@ package org.apache.shardingsphere.proxy.backend.text.admin;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.UnknownDatabaseException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLUseStatement;
......@@ -48,7 +47,7 @@ public final class UseDatabaseBackendHandler implements TextProtocolBackendHandl
backendConnection.setCurrentSchema(schema);
return new UpdateResponse();
}
return new ErrorResponse(new UnknownDatabaseException(schema));
throw new UnknownDatabaseException(schema);
}
......
......@@ -23,12 +23,11 @@ import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.Que
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
......@@ -53,10 +52,10 @@ public final class QueryBackendHandler implements TextProtocolBackendHandler {
private DatabaseCommunicationEngine databaseCommunicationEngine;
@Override
public BackendResponse execute() {
public BackendResponse execute() throws SQLException {
SchemaContext context = ProxyContext.getInstance().getSchema(backendConnection.getSchema());
if (null == context) {
return new ErrorResponse(new NoDatabaseSelectedException());
throw new NoDatabaseSelectedException();
}
if (!context.isComplete()) {
return getDefaultQueryResponse(backendConnection.getSchema());
......
......@@ -23,11 +23,10 @@ import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.QueryHeader;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.wrapper.StatementExecutorWrapper;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.sctl.exception.InvalidShardingCTLFormatException;
......@@ -55,7 +54,7 @@ public final class ShardingCTLExplainBackendHandler implements TextProtocolBacke
public BackendResponse execute() {
Optional<ShardingCTLExplainStatement> explainStatement = new ShardingCTLExplainParser(sql).doParse();
if (!explainStatement.isPresent()) {
return new ErrorResponse(new InvalidShardingCTLFormatException(sql));
throw new InvalidShardingCTLFormatException(sql);
}
SchemaContext schema = ProxyContext.getInstance().getSchema(backendConnection.getSchema());
StatementExecutorWrapper statementExecutorWrapper =
......
......@@ -17,16 +17,15 @@
package org.apache.shardingsphere.proxy.backend.text.sctl.hint;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.sctl.exception.InvalidShardingCTLFormatException;
import org.apache.shardingsphere.proxy.backend.text.sctl.hint.internal.HintCommand;
import org.apache.shardingsphere.proxy.backend.text.sctl.hint.internal.HintCommandExecutor;
import org.apache.shardingsphere.proxy.backend.text.sctl.hint.internal.HintCommandExecutorFactory;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import java.sql.SQLException;
import java.util.Optional;
......@@ -55,7 +54,7 @@ public final class ShardingCTLHintBackendHandler implements TextProtocolBackendH
}
Optional<ShardingCTLHintStatement> shardingTCLStatement = new ShardingCTLHintParser(sql).doParse();
if (!shardingTCLStatement.isPresent()) {
return new ErrorResponse(new InvalidShardingCTLFormatException(sql));
throw new InvalidShardingCTLFormatException(sql);
}
HintCommand hintCommand = shardingTCLStatement.get().getHintCommand();
hintCommandExecutor = HintCommandExecutorFactory.newInstance(hintCommand, backendConnection, sql);
......
......@@ -19,7 +19,6 @@ package org.apache.shardingsphere.proxy.backend.text.sctl.hint.internal.executor
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.text.sctl.exception.UnsupportedShardingCTLTypeException;
import org.apache.shardingsphere.proxy.backend.text.sctl.hint.internal.command.HintErrorParameterCommand;
......@@ -33,6 +32,6 @@ public final class HintErrorParameterExecutor extends AbstractHintUpdateExecutor
@Override
public BackendResponse execute(final HintErrorParameterCommand command) {
return new ErrorResponse(new UnsupportedShardingCTLTypeException(sql));
throw new UnsupportedShardingCTLTypeException(sql);
}
}
......@@ -19,7 +19,6 @@ package org.apache.shardingsphere.proxy.backend.text.sctl.set;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
......@@ -47,16 +46,16 @@ public final class ShardingCTLSetBackendHandler implements TextProtocolBackendHa
public BackendResponse execute() {
Optional<ShardingCTLSetStatement> shardingTCLStatement = new ShardingCTLSetParser(sql).doParse();
if (!shardingTCLStatement.isPresent()) {
return new ErrorResponse(new InvalidShardingCTLFormatException(sql));
throw new InvalidShardingCTLFormatException(sql);
}
if ("TRANSACTION_TYPE".equals(shardingTCLStatement.get().getKey())) {
try {
backendConnection.setTransactionType(TransactionType.valueOf(shardingTCLStatement.get().getValue()));
} catch (final IllegalArgumentException ex) {
return new ErrorResponse(new UnsupportedShardingCTLTypeException(sql));
throw new UnsupportedShardingCTLTypeException(sql);
}
} else {
return new ErrorResponse(new UnsupportedShardingCTLTypeException(sql));
throw new UnsupportedShardingCTLTypeException(sql);
}
return new UpdateResponse();
}
......
......@@ -21,7 +21,6 @@ import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.Que
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
......@@ -55,7 +54,7 @@ public final class ShardingCTLShowBackendHandler implements TextProtocolBackendH
public BackendResponse execute() {
Optional<ShardingCTLShowStatement> showStatement = new ShardingCTLShowParser(sql).doParse();
if (!showStatement.isPresent()) {
return new ErrorResponse(new InvalidShardingCTLFormatException(sql));
throw new InvalidShardingCTLFormatException(sql);
}
switch (showStatement.get().getValue()) {
case "TRANSACTION_TYPE":
......@@ -63,7 +62,7 @@ public final class ShardingCTLShowBackendHandler implements TextProtocolBackendH
case "CACHED_CONNECTIONS":
return createResponsePackets("CACHED_CONNECTIONS", backendConnection.getConnectionSize());
default:
return new ErrorResponse(new UnsupportedShardingCTLTypeException(sql));
throw new UnsupportedShardingCTLTypeException(sql);
}
}
......
......@@ -20,7 +20,6 @@ package org.apache.shardingsphere.proxy.backend.text.transaction;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendTransactionManager;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
......@@ -44,15 +43,7 @@ public final class TransactionBackendHandler implements TextProtocolBackendHandl
}
@Override
public BackendResponse execute() {
try {
return doTransaction();
} catch (final SQLException ex) {
return new ErrorResponse(ex);
}
}
private BackendResponse doTransaction() throws SQLException {
public BackendResponse execute() throws SQLException {
switch (operationType) {
case BEGIN:
backendTransactionManager.begin();
......
......@@ -26,10 +26,9 @@ import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.junit.Before;
import org.junit.Test;
......@@ -78,7 +77,7 @@ public final class BroadcastBackendHandlerTest {
}
@Test
public void assertExecuteSuccess() {
public void assertExecuteSuccess() throws SQLException {
mockDatabaseCommunicationEngine(new UpdateResponse());
BroadcastBackendHandler broadcastBackendHandler = new BroadcastBackendHandler("SET timeout = 1000", mock(SQLStatement.class), backendConnection);
setBackendHandlerFactory(broadcastBackendHandler);
......@@ -97,17 +96,7 @@ public final class BroadcastBackendHandlerTest {
return result;
}
@Test
public void assertExecuteFailure() {
ErrorResponse errorResponse = new ErrorResponse(new SQLException("no reason", "X999", -1));
mockDatabaseCommunicationEngine(errorResponse);
BroadcastBackendHandler broadcastBackendHandler = new BroadcastBackendHandler("SET timeout = 1000", mock(SQLStatement.class), backendConnection);
setBackendHandlerFactory(broadcastBackendHandler);
assertThat(broadcastBackendHandler.execute(), instanceOf(ErrorResponse.class));
verify(databaseCommunicationEngine, times(10)).execute();
}
private void mockDatabaseCommunicationEngine(final BackendResponse backendResponse) {
private void mockDatabaseCommunicationEngine(final BackendResponse backendResponse) throws SQLException {
when(databaseCommunicationEngine.execute()).thenReturn(backendResponse);
when(databaseCommunicationEngineFactory.newTextProtocolInstance(any(), anyString(), any())).thenReturn(databaseCommunicationEngine);
}
......
......@@ -25,11 +25,10 @@ import org.apache.shardingsphere.infra.context.SchemaContexts;
import org.apache.shardingsphere.infra.context.impl.StandardSchemaContexts;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.rdl.parser.statement.rdl.CreateDataSourcesStatement;
import org.apache.shardingsphere.rdl.parser.statement.rdl.CreateShardingRuleStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
......@@ -39,11 +38,14 @@ import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
......@@ -60,40 +62,51 @@ public final class RDLBackendHandlerTest {
}
@Test
public void assertExecuteCreateDatabaseContext() {
public void assertExecuteCreateDatabaseContext() throws SQLException {
BackendConnection connection = mock(BackendConnection.class);
when(connection.getSchema()).thenReturn("schema");
RDLBackendHandler executeEngine = new RDLBackendHandler(connection, new CreateDatabaseStatement("new_db"));
BackendResponse response = executeEngine.execute();
assertThat(response, instanceOf(ErrorResponse.class));
try {
executeEngine.execute();
} catch (final SQLException ex) {
assertThat(ex.getMessage(), is("No Registry center to execute `CreateDatabaseStatementContext` SQL"));
}
setGovernanceSchemaContexts(true);
response = executeEngine.execute();
BackendResponse response = executeEngine.execute();
assertThat(response, instanceOf(UpdateResponse.class));
}
@Test
public void assertExecuteDropDatabaseContext() {
public void assertExecuteDropDatabaseContext() throws SQLException {
BackendConnection connection = mock(BackendConnection.class);
when(connection.getSchema()).thenReturn("schema");
RDLBackendHandler executeEngine = new RDLBackendHandler(connection, new DropDatabaseStatement("schema"));
BackendResponse response = executeEngine.execute();
assertThat(response, instanceOf(ErrorResponse.class));
try {
executeEngine.execute();
} catch (final SQLException ex) {
assertThat(ex.getMessage(), is("No Registry center to execute `DropDatabaseStatementContext` SQL"));
}
setGovernanceSchemaContexts(true);
response = executeEngine.execute();
BackendResponse response = executeEngine.execute();
assertThat(response, instanceOf(UpdateResponse.class));
}
@Test
public void assertExecuteCreateDatabaseContextWithException() {
public void assertExecuteCreateDatabaseContextWithException() throws SQLException {
BackendConnection connection = mock(BackendConnection.class);
when(connection.getSchema()).thenReturn("schema");
RDLBackendHandler executeEngine = new RDLBackendHandler(connection, new CreateDatabaseStatement("schema"));
BackendResponse response = executeEngine.execute();
assertThat(response, instanceOf(ErrorResponse.class));
try {
executeEngine.execute();
} catch (final SQLException ex) {
assertThat(ex.getMessage(), is("No Registry center to execute `CreateDatabaseStatementContext` SQL"));
}
setGovernanceSchemaContexts(true);
response = executeEngine.execute();
assertThat(response, instanceOf(ErrorResponse.class));
assertThat(((ErrorResponse) response).getCause(), instanceOf(DBCreateExistsException.class));
try {
executeEngine.execute();
} catch (final DBCreateExistsException ex) {
assertNull(ex.getMessage());
}
}
private Map<String, SchemaContext> getSchemaContextMap() {
......@@ -102,26 +115,32 @@ public final class RDLBackendHandlerTest {
}
@Test
public void assertExecuteDataSourcesContext() {
public void assertExecuteDataSourcesContext() throws SQLException {
BackendConnection connection = mock(BackendConnection.class);
when(connection.getSchema()).thenReturn("schema");
RDLBackendHandler executeEngine = new RDLBackendHandler(connection, mock(CreateDataSourcesStatement.class));
BackendResponse response = executeEngine.execute();
assertThat(response, instanceOf(ErrorResponse.class));
try {
executeEngine.execute();
} catch (final SQLException ex) {
assertThat(ex.getMessage(), is("No Registry center to execute `CreateDataSourcesStatementContext` SQL"));
}
setGovernanceSchemaContexts(true);
response = executeEngine.execute();
BackendResponse response = executeEngine.execute();
assertThat(response, instanceOf(UpdateResponse.class));
}
@Test
public void assertExecuteShardingRuleContext() {
public void assertExecuteShardingRuleContext() throws SQLException {
BackendConnection connection = mock(BackendConnection.class);
when(connection.getSchema()).thenReturn("schema");
RDLBackendHandler executeEngine = new RDLBackendHandler(connection, mock(CreateShardingRuleStatement.class));
BackendResponse response = executeEngine.execute();
assertThat(response, instanceOf(ErrorResponse.class));
try {
executeEngine.execute();
} catch (final SQLException ex) {
assertThat(ex.getMessage(), is("No Registry center to execute `CreateShardingRuleStatementContext` SQL"));
}
setGovernanceSchemaContexts(true);
response = executeEngine.execute();
BackendResponse response = executeEngine.execute();
assertThat(response, instanceOf(UpdateResponse.class));
}
......
......@@ -83,7 +83,7 @@ public class ShowTablesBackendHandlerTest {
}
@Test
public void assertExecuteShowTablesBackendHandler() {
public void assertExecuteShowTablesBackendHandler() throws SQLException {
QueryResponse actual = (QueryResponse) tablesBackendHandler.execute();
assertThat(actual, instanceOf(QueryResponse.class));
assertThat(actual.getQueryHeaders().size(), is(1));
......
......@@ -39,6 +39,7 @@ import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
......@@ -62,8 +63,7 @@ public final class UnicastBackendHandlerTest {
private DatabaseCommunicationEngineFactory databaseCommunicationEngineFactory;
@Before
@SneakyThrows(ReflectiveOperationException.class)
public void setUp() {
public void setUp() throws SQLException, IllegalAccessException, NoSuchFieldException {
Field schemaContexts = ProxyContext.getInstance().getClass().getDeclaredField("schemaContexts");
schemaContexts.setAccessible(true);
schemaContexts.set(ProxyContext.getInstance(),
......@@ -80,7 +80,7 @@ public final class UnicastBackendHandlerTest {
}
@Test
public void assertExecuteWhileSchemaIsNull() {
public void assertExecuteWhileSchemaIsNull() throws SQLException {
UnicastBackendHandler backendHandler = new UnicastBackendHandler("show variable like %s", mock(SQLStatement.class), backendConnection);
backendConnection.setCurrentSchema(String.format(SCHEMA_PATTERN, 8));
setDatabaseCommunicationEngine(backendHandler);
......@@ -90,7 +90,7 @@ public final class UnicastBackendHandlerTest {
}
@Test
public void assertExecuteWhileSchemaNotNull() {
public void assertExecuteWhileSchemaNotNull() throws SQLException {
backendConnection.setCurrentSchema(String.format(SCHEMA_PATTERN, 0));
UnicastBackendHandler backendHandler = new UnicastBackendHandler("show variable like %s", mock(SQLStatement.class), backendConnection);
setDatabaseCommunicationEngine(backendHandler);
......@@ -99,7 +99,7 @@ public final class UnicastBackendHandlerTest {
backendHandler.execute();
}
private void setUnderlyingHandler(final BackendResponse backendResponse) {
private void setUnderlyingHandler(final BackendResponse backendResponse) throws SQLException {
DatabaseCommunicationEngine databaseCommunicationEngine = mock(DatabaseCommunicationEngine.class);
when(databaseCommunicationEngine.execute()).thenReturn(backendResponse);
when(databaseCommunicationEngineFactory.newTextProtocolInstance(any(), anyString(), any())).thenReturn(databaseCommunicationEngine);
......
......@@ -17,7 +17,6 @@
package org.apache.shardingsphere.proxy.backend.text.admin;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.auth.ProxyUser;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
......@@ -25,10 +24,10 @@ import org.apache.shardingsphere.infra.context.SchemaContext;
import org.apache.shardingsphere.infra.context.impl.StandardSchemaContexts;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.UnknownDatabaseException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLUseStatement;
import org.junit.Before;
import org.junit.Test;
......@@ -45,7 +44,6 @@ import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
......@@ -57,8 +55,7 @@ public final class UseDatabaseBackendHandlerTest {
private BackendConnection backendConnection;
@Before
@SneakyThrows(ReflectiveOperationException.class)
public void setUp() {
public void setUp() throws NoSuchFieldException, IllegalAccessException {
backendConnection = mock(BackendConnection.class);
when(backendConnection.getUsername()).thenReturn("root");
Field schemaContexts = ProxyContext.getInstance().getClass().getDeclaredField("schemaContexts");
......@@ -92,13 +89,11 @@ public final class UseDatabaseBackendHandlerTest {
assertThat(actual, instanceOf(UpdateResponse.class));
}
@Test
@Test(expected = UnknownDatabaseException.class)
public void assertExecuteUseStatementNotExist() {
MySQLUseStatement useStatement = mock(MySQLUseStatement.class);
when(useStatement.getSchema()).thenReturn("not_exist");
UseDatabaseBackendHandler useSchemaBackendHandler = new UseDatabaseBackendHandler(useStatement, backendConnection);
BackendResponse actual = useSchemaBackendHandler.execute();
assertThat(actual, instanceOf(ErrorResponse.class));
verify(backendConnection, times(0)).setCurrentSchema(anyString());
useSchemaBackendHandler.execute();
}
}
......@@ -30,12 +30,11 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.datasource.DataSourceMetaDatas;
import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.text.sctl.exception.InvalidShardingCTLFormatException;
import org.apache.shardingsphere.proxy.backend.text.sctl.exception.UnsupportedShardingCTLTypeException;
import org.apache.shardingsphere.proxy.backend.text.sctl.hint.internal.HintManagerHolder;
......@@ -79,12 +78,11 @@ public final class ShardingCTLHintBackendHandlerTest {
new ShardingCTLHintBackendHandler("", backendConnection).execute();
}
@Test
@Test(expected = InvalidShardingCTLFormatException.class)
public void assertInvalidShardingCTLFormat() {
clearThreadLocal();
String sql = "sctl:hint1 xx=yy";
ShardingCTLHintBackendHandler shardingCTLHintBackendHandler = new ShardingCTLHintBackendHandler(sql, backendConnection);
assertThat(((ErrorResponse) shardingCTLHintBackendHandler.execute()).getCause(), instanceOf(InvalidShardingCTLFormatException.class));
new ShardingCTLHintBackendHandler(sql, backendConnection).execute();
}
private void clearThreadLocal() {
......@@ -214,11 +212,10 @@ public final class ShardingCTLHintBackendHandlerTest {
return Collections.singletonMap("schema", result);
}
@Test
@Test(expected = UnsupportedShardingCTLTypeException.class)
public void assertUnsupportedShardingCTLType() {
clearThreadLocal();
String sql = "sctl:hint xx=yy";
ShardingCTLHintBackendHandler shardingCTLHintBackendHandler = new ShardingCTLHintBackendHandler(sql, backendConnection);
assertThat(((ErrorResponse) shardingCTLHintBackendHandler.execute()).getCause(), instanceOf(UnsupportedShardingCTLTypeException.class));
new ShardingCTLHintBackendHandler(sql, backendConnection).execute();
}
}
......@@ -24,10 +24,11 @@ import org.apache.shardingsphere.infra.context.SchemaContext;
import org.apache.shardingsphere.infra.context.impl.StandardSchemaContexts;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.text.sctl.exception.InvalidShardingCTLFormatException;
import org.apache.shardingsphere.proxy.backend.text.sctl.exception.UnsupportedShardingCTLTypeException;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.junit.Before;
import org.junit.Test;
......@@ -92,26 +93,19 @@ public final class ShardingCTLSetBackendHandlerTest {
assertThat(backendConnection.getTransactionType(), is(TransactionType.LOCAL));
}
@Test
@Test(expected = UnsupportedShardingCTLTypeException.class)
public void assertSwitchTransactionTypeFailed() {
backendConnection.setCurrentSchema(String.format(SCHEMA_PATTERN, 0));
ShardingCTLSetBackendHandler shardingCTLBackendHandler = new ShardingCTLSetBackendHandler("sctl:set transaction_type=XXX", backendConnection);
BackendResponse actual = shardingCTLBackendHandler.execute();
assertThat(actual, instanceOf(ErrorResponse.class));
assertThat(backendConnection.getTransactionType(), is(TransactionType.LOCAL));
new ShardingCTLSetBackendHandler("sctl:set transaction_type=XXX", backendConnection).execute();
}
@Test
@Test(expected = UnsupportedShardingCTLTypeException.class)
public void assertNotSupportedSCTL() {
ShardingCTLSetBackendHandler shardingCTLBackendHandler = new ShardingCTLSetBackendHandler("sctl:set @@session=XXX", backendConnection);
BackendResponse actual = shardingCTLBackendHandler.execute();
assertThat(actual, instanceOf(ErrorResponse.class));
new ShardingCTLSetBackendHandler("sctl:set @@session=XXX", backendConnection).execute();
}
@Test
@Test(expected = InvalidShardingCTLFormatException.class)
public void assertFormatErrorSCTL() {
ShardingCTLSetBackendHandler shardingCTLBackendHandler = new ShardingCTLSetBackendHandler("sctl:set yyyyy", backendConnection);
BackendResponse actual = shardingCTLBackendHandler.execute();
assertThat(actual, instanceOf(ErrorResponse.class));
new ShardingCTLSetBackendHandler("sctl:set yyyyy", backendConnection).execute();
}
}
......@@ -19,7 +19,6 @@ package org.apache.shardingsphere.proxy.backend.text.sctl.show;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.text.sctl.exception.InvalidShardingCTLFormatException;
......@@ -61,21 +60,15 @@ public final class ShardingCTLShowBackendHandlerTest {
assertThat(queryData.getData().iterator().next(), is(0));
}
@Test
@Test(expected = UnsupportedShardingCTLTypeException.class)
public void assertShowCachedConnectionFailed() {
backendConnection.setCurrentSchema("schema");
ShardingCTLShowBackendHandler backendHandler = new ShardingCTLShowBackendHandler("sctl:show cached_connectionss", backendConnection);
BackendResponse actual = backendHandler.execute();
assertThat(actual, instanceOf(ErrorResponse.class));
assertThat(((ErrorResponse) actual).getCause(), instanceOf(UnsupportedShardingCTLTypeException.class));
new ShardingCTLShowBackendHandler("sctl:show cached_connectionss", backendConnection).execute();
}
@Test
@Test(expected = InvalidShardingCTLFormatException.class)
public void assertShowCTLFormatError() {
backendConnection.setCurrentSchema("schema");
ShardingCTLShowBackendHandler backendHandler = new ShardingCTLShowBackendHandler("sctl:show=xx", backendConnection);
BackendResponse actual = backendHandler.execute();
assertThat(actual, instanceOf(ErrorResponse.class));
assertThat(((ErrorResponse) actual).getCause(), instanceOf(InvalidShardingCTLFormatException.class));
new ShardingCTLShowBackendHandler("sctl:show=xx", backendConnection).execute();
}
}
......@@ -26,6 +26,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import java.sql.SQLException;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
......@@ -35,7 +37,7 @@ public final class TransactionBackendHandlerTest {
private final BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL);
@Test
public void assertExecute() {
public void assertExecute() throws SQLException {
TransactionBackendHandler transactionBackendHandler = new TransactionBackendHandler(TransactionOperationType.BEGIN, backendConnection);
BackendResponse actual = transactionBackendHandler.execute();
assertThat(actual, instanceOf(UpdateResponse.class));
......
......@@ -35,13 +35,11 @@ import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicati
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.frontend.command.executor.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.command.executor.ResponseType;
import org.apache.shardingsphere.proxy.frontend.mysql.MySQLErrPacketFactory;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.sql.SQLException;
......@@ -65,36 +63,22 @@ public final class MySQLComStmtExecuteExecutor implements QueryCommandExecutor {
public MySQLComStmtExecuteExecutor(final MySQLComStmtExecutePacket comStmtExecutePacket, final BackendConnection backendConnection) {
SQLStatement sqlStatement = ProxyContext.getInstance().getSchema(backendConnection.getSchema()).getRuntimeContext().getSqlParserEngine().parse(comStmtExecutePacket.getSql(), true);
databaseCommunicationEngine = DatabaseCommunicationEngineFactory.getInstance().newBinaryProtocolInstance(sqlStatement,
comStmtExecutePacket.getSql(), comStmtExecutePacket.getParameters(), backendConnection);
databaseCommunicationEngine = DatabaseCommunicationEngineFactory.getInstance().newBinaryProtocolInstance(
sqlStatement, comStmtExecutePacket.getSql(), comStmtExecutePacket.getParameters(), backendConnection);
}
@Override
public Collection<DatabasePacket<?>> execute() {
public Collection<DatabasePacket<?>> execute() throws SQLException {
if (ProxyContext.getInstance().getSchemaContexts().isCircuitBreak()) {
return Collections.singletonList(new MySQLErrPacket(1, CommonErrorCode.CIRCUIT_BREAK_MODE));
}
BackendResponse backendResponse = getBackendResponse();
BackendResponse backendResponse = databaseCommunicationEngine.execute();
if (backendResponse instanceof QueryResponse) {
responseType = ResponseType.QUERY;
return createQueryPacket((QueryResponse) backendResponse);
}
if (backendResponse instanceof UpdateResponse) {
responseType = ResponseType.UPDATE;
return Collections.singletonList(createUpdatePacket((UpdateResponse) backendResponse));
}
responseType = ResponseType.ERROR;
return Collections.singletonList(createErrorPacket(((ErrorResponse) backendResponse).getCause()));
}
private BackendResponse getBackendResponse() {
try {
return databaseCommunicationEngine.execute();
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:OFF
return new ErrorResponse(ex);
}
responseType = ResponseType.UPDATE;
return Collections.singletonList(createUpdatePacket((UpdateResponse) backendResponse));
}
private Collection<DatabasePacket<?>> createQueryPacket(final QueryResponse backendResponse) {
......@@ -113,10 +97,6 @@ public final class MySQLComStmtExecuteExecutor implements QueryCommandExecutor {
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();
......
......@@ -25,18 +25,13 @@ import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.frontend.command.executor.CommandExecutor;
import org.apache.shardingsphere.proxy.frontend.mysql.MySQLErrPacketFactory;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* COM_FIELD_LIST packet executor for MySQL.
......@@ -63,12 +58,8 @@ public final class MySQLComFieldListPacketExecutor implements CommandExecutor {
@Override
public Collection<DatabasePacket<?>> execute() throws SQLException {
BackendResponse backendResponse = databaseCommunicationEngine.execute();
return backendResponse instanceof ErrorResponse ? createErrorPackets((ErrorResponse) backendResponse) : createColumnDefinition41Packets();
}
private List<DatabasePacket<?>> createErrorPackets(final ErrorResponse backendResponse) {
return Collections.singletonList(MySQLErrPacketFactory.newInstance(1, backendResponse.getCause()));
databaseCommunicationEngine.execute();
return createColumnDefinition41Packets();
}
private Collection<DatabasePacket<?>> createColumnDefinition41Packets() throws SQLException {
......
......@@ -35,14 +35,12 @@ import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.Que
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
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.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandlerFactory;
import org.apache.shardingsphere.proxy.frontend.command.executor.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.command.executor.ResponseType;
import org.apache.shardingsphere.proxy.frontend.mysql.MySQLErrPacketFactory;
import java.sql.SQLException;
import java.util.Collection;
......@@ -67,31 +65,17 @@ public final class MySQLComQueryPacketExecutor implements QueryCommandExecutor {
}
@Override
public Collection<DatabasePacket<?>> execute() {
public Collection<DatabasePacket<?>> execute() throws SQLException {
if (ProxyContext.getInstance().getSchemaContexts().isCircuitBreak()) {
return Collections.singletonList(new MySQLErrPacket(1, CommonErrorCode.CIRCUIT_BREAK_MODE));
}
BackendResponse backendResponse = getBackendResponse();
BackendResponse backendResponse = textProtocolBackendHandler.execute();
if (backendResponse instanceof QueryResponse) {
responseType = ResponseType.QUERY;
return createQueryPackets((QueryResponse) backendResponse);
}
if (backendResponse instanceof UpdateResponse) {
responseType = ResponseType.UPDATE;
return Collections.singletonList(createUpdatePacket((UpdateResponse) backendResponse));
}
responseType = ResponseType.ERROR;
return Collections.singletonList(createErrorPacket(((ErrorResponse) backendResponse).getCause()));
}
private BackendResponse getBackendResponse() {
try {
return textProtocolBackendHandler.execute();
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:OFF
return new ErrorResponse(ex);
}
responseType = ResponseType.UPDATE;
return Collections.singletonList(createUpdatePacket((UpdateResponse) backendResponse));
}
private Collection<DatabasePacket<?>> createQueryPackets(final QueryResponse backendResponse) {
......@@ -127,10 +111,6 @@ public final class MySQLComQueryPacketExecutor implements QueryCommandExecutor {
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();
......
......@@ -27,10 +27,9 @@ import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.QueryHeader;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.frontend.command.executor.ResponseType;
import org.apache.shardingsphere.rdl.parser.engine.ShardingSphereSQLParserEngine;
import org.junit.Before;
......@@ -54,9 +53,6 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class MySQLComStmtExecuteExecutorTest {
@Mock
private SQLException sqlException;
@Mock
private DatabaseCommunicationEngine databaseCommunicationEngine;
......@@ -78,7 +74,7 @@ public final class MySQLComStmtExecuteExecutorTest {
}
@Test
public void assertIsQueryResponse() throws NoSuchFieldException {
public void assertIsQueryResponse() throws NoSuchFieldException, SQLException {
BackendConnection backendConnection = mock(BackendConnection.class);
when(backendConnection.getSchema()).thenReturn("schema");
MySQLComStmtExecuteExecutor mysqlComStmtExecuteExecutor = new MySQLComStmtExecuteExecutor(mock(MySQLComStmtExecutePacket.class), backendConnection);
......@@ -89,7 +85,7 @@ public final class MySQLComStmtExecuteExecutorTest {
}
@Test
public void assertIsUpdateResponse() throws NoSuchFieldException {
public void assertIsUpdateResponse() throws NoSuchFieldException, SQLException {
BackendConnection backendConnection = mock(BackendConnection.class);
when(backendConnection.getSchema()).thenReturn("schema");
MySQLComStmtExecuteExecutor mysqlComStmtExecuteExecutor = new MySQLComStmtExecuteExecutor(mock(MySQLComStmtExecutePacket.class), backendConnection);
......@@ -98,15 +94,4 @@ public final class MySQLComStmtExecuteExecutorTest {
mysqlComStmtExecuteExecutor.execute();
assertThat(mysqlComStmtExecuteExecutor.getResponseType(), is(ResponseType.UPDATE));
}
@Test
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 ErrorResponse(sqlException));
mysqlComStmtExecuteExecutor.execute();
assertThat(mysqlComStmtExecuteExecutor.getResponseType(), is(ResponseType.ERROR));
}
}
......@@ -19,7 +19,6 @@ package org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.query;
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;
......@@ -41,9 +40,6 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class MySQLComQueryPacketExecutorTest {
@Mock
private SQLException sqlException;
@Mock
private TextProtocolBackendHandler textProtocolBackendHandler;
......@@ -64,12 +60,4 @@ public final class MySQLComQueryPacketExecutorTest {
mysqlComQueryPacketExecutor.execute();
assertThat(mysqlComQueryPacketExecutor.getResponseType(), is(ResponseType.UPDATE));
}
@Test
public void assertIsErrorResponse() throws SQLException, NoSuchFieldException {
FieldSetter.setField(mysqlComQueryPacketExecutor, MySQLComQueryPacketExecutor.class.getDeclaredField("textProtocolBackendHandler"), textProtocolBackendHandler);
when(textProtocolBackendHandler.execute()).thenReturn(new ErrorResponse(sqlException));
mysqlComQueryPacketExecutor.execute();
assertThat(mysqlComQueryPacketExecutor.getResponseType(), is(ResponseType.ERROR));
}
}
......@@ -83,7 +83,7 @@ public final class PostgreSQLCommandExecuteEngine implements CommandExecuteEngin
context.write(new PostgreSQLReadyForQueryPacket());
return;
}
if (ResponseType.ERROR == queryCommandExecutor.getResponseType() || ResponseType.UPDATE == queryCommandExecutor.getResponseType()) {
if (ResponseType.UPDATE == queryCommandExecutor.getResponseType()) {
context.write(new PostgreSQLReadyForQueryPacket());
return;
}
......
......@@ -37,13 +37,11 @@ import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicati
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.frontend.command.executor.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.command.executor.ResponseType;
import org.apache.shardingsphere.proxy.frontend.postgresql.PostgreSQLErrPacketFactory;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.sql.ResultSetMetaData;
......@@ -80,7 +78,7 @@ public final class PostgreSQLComBindExecutor implements QueryCommandExecutor {
}
@Override
public Collection<DatabasePacket<?>> execute() {
public Collection<DatabasePacket<?>> execute() throws SQLException {
if (ProxyContext.getInstance().getSchemaContexts().isCircuitBreak()) {
return Collections.singletonList(new PostgreSQLErrorResponsePacket());
}
......@@ -89,7 +87,7 @@ public final class PostgreSQLComBindExecutor implements QueryCommandExecutor {
if (null == databaseCommunicationEngine) {
return result;
}
BackendResponse backendResponse = getBackendResponse();
BackendResponse backendResponse = databaseCommunicationEngine.execute();
if (backendResponse instanceof QueryResponse) {
createQueryPacket((QueryResponse) backendResponse).ifPresent(result::add);
}
......@@ -97,22 +95,6 @@ public final class PostgreSQLComBindExecutor implements QueryCommandExecutor {
responseType = ResponseType.UPDATE;
result.add(createUpdatePacket((UpdateResponse) backendResponse));
}
if (backendResponse instanceof ErrorResponse) {
responseType = ResponseType.ERROR;
result.add(createErrorPacket((ErrorResponse) backendResponse));
}
return result;
}
private BackendResponse getBackendResponse() {
BackendResponse result;
try {
result = databaseCommunicationEngine.execute();
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:OFF
result = new ErrorResponse(ex);
}
return result;
}
......@@ -142,10 +124,6 @@ public final class PostgreSQLComBindExecutor implements QueryCommandExecutor {
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();
......
......@@ -32,14 +32,12 @@ import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.Que
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
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.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandlerFactory;
import org.apache.shardingsphere.proxy.frontend.command.executor.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.command.executor.ResponseType;
import org.apache.shardingsphere.proxy.frontend.postgresql.PostgreSQLErrPacketFactory;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
......@@ -64,33 +62,17 @@ public final class PostgreSQLComQueryExecutor implements QueryCommandExecutor {
}
@Override
public Collection<DatabasePacket<?>> execute() {
public Collection<DatabasePacket<?>> execute() throws SQLException {
if (ProxyContext.getInstance().getSchemaContexts().isCircuitBreak()) {
return Collections.singletonList(new PostgreSQLErrorResponsePacket());
}
BackendResponse backendResponse = getBackendResponse();
BackendResponse backendResponse = textProtocolBackendHandler.execute();
if (backendResponse instanceof QueryResponse) {
Optional<PostgreSQLRowDescriptionPacket> result = createQueryPacket((QueryResponse) backendResponse);
return result.<List<DatabasePacket<?>>>map(Collections::singletonList).orElseGet(Collections::emptyList);
}
if (backendResponse instanceof UpdateResponse) {
responseType = ResponseType.UPDATE;
return Collections.singletonList(createUpdatePacket((UpdateResponse) backendResponse));
}
responseType = ResponseType.ERROR;
return Collections.singletonList(createErrorPacket((ErrorResponse) backendResponse));
}
private BackendResponse getBackendResponse() {
BackendResponse result;
try {
result = textProtocolBackendHandler.execute();
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:OFF
result = new ErrorResponse(ex);
}
return result;
responseType = ResponseType.UPDATE;
return Collections.singletonList(createUpdatePacket((UpdateResponse) backendResponse));
}
private Optional<PostgreSQLRowDescriptionPacket> createQueryPacket(final QueryResponse queryResponse) {
......@@ -119,10 +101,6 @@ public final class PostgreSQLComQueryExecutor implements QueryCommandExecutor {
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();
......
......@@ -43,9 +43,9 @@ public final class PostgreSQLCommandExecuteEngineTest {
@Test
@SneakyThrows
public void assertWriteQueryDataWithError() {
public void assertWriteQueryDataWithUpdate() {
PostgreSQLCommandExecuteEngine postgreSQLCommandExecuteEngine = new PostgreSQLCommandExecuteEngine();
when(queryCommandExecutor.getResponseType()).thenReturn(ResponseType.ERROR);
when(queryCommandExecutor.getResponseType()).thenReturn(ResponseType.UPDATE);
postgreSQLCommandExecuteEngine.writeQueryData(channelHandlerContext, null, queryCommandExecutor, 0);
verify(channelHandlerContext, times(1)).write(isA(PostgreSQLReadyForQueryPacket.class));
}
......
/*
* 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.postgresql.command.query.binary.bind;
import lombok.SneakyThrows;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.PostgreSQLComBindPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLErrorResponsePacket;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.SchemaContext;
import org.apache.shardingsphere.infra.context.impl.StandardSchemaContexts;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.frontend.command.executor.ResponseType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.FieldSetter;
import org.mockito.junit.MockitoJUnitRunner;
import org.postgresql.util.PSQLException;
import org.postgresql.util.ServerErrorMessage;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
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;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLComBindExecutorTest {
@Mock
private DatabaseCommunicationEngine databaseCommunicationEngine;
@Test
@SneakyThrows
public void assertExecuteHasError() {
Field schemaContexts = ProxyContext.getInstance().getClass().getDeclaredField("schemaContexts");
schemaContexts.setAccessible(true);
schemaContexts.set(ProxyContext.getInstance(),
new StandardSchemaContexts(getSchemaContextMap(), new Authentication(), new ConfigurationProperties(new Properties()), new MySQLDatabaseType()));
BackendConnection connection = mock(BackendConnection.class);
when(connection.getSchema()).thenReturn("schema");
PostgreSQLComBindExecutor postgreSQLComBindExecutor = new PostgreSQLComBindExecutor(mock(PostgreSQLComBindPacket.class), connection);
FieldSetter.setField(postgreSQLComBindExecutor, PostgreSQLComBindExecutor.class.getDeclaredField("databaseCommunicationEngine"), databaseCommunicationEngine);
ErrorResponse errorResponse = new ErrorResponse(new PSQLException(mock(ServerErrorMessage.class)));
when(databaseCommunicationEngine.execute()).thenReturn(errorResponse);
assertThat(((LinkedList) postgreSQLComBindExecutor.execute()).get(1), instanceOf(PostgreSQLErrorResponsePacket.class));
assertThat(postgreSQLComBindExecutor.getResponseType(), is(ResponseType.ERROR));
}
private Map<String, SchemaContext> getSchemaContextMap() {
SchemaContext result = new SchemaContext("schema", null, null);
return Collections.singletonMap("schema", result);
}
}
/*
* 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.postgresql.command.query.text;
import lombok.SneakyThrows;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.text.PostgreSQLComQueryPacket;
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.apache.shardingsphere.proxy.frontend.command.executor.ResponseType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.FieldSetter;
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;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLComQueryExecutorTest {
@Mock
private TextProtocolBackendHandler textProtocolBackendHandler;
@Test
@SneakyThrows
public void assertExecuteReturnErrorResponsePacket() {
PostgreSQLComQueryExecutor postgreSQLComQueryExecutor = new PostgreSQLComQueryExecutor(mock(PostgreSQLComQueryPacket.class), null);
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(), instanceOf(PostgreSQLErrorResponsePacket.class));
assertThat(postgreSQLComQueryExecutor.getResponseType(), is(ResponseType.ERROR));
}
}
......@@ -22,5 +22,5 @@ package org.apache.shardingsphere.proxy.frontend.command.executor;
*/
public enum ResponseType {
QUERY, UPDATE, ERROR
QUERY, UPDATE
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册