Skip to content

Commit

Permalink
Add RQLBackendHandlerFactory (apache#8676)
Browse files Browse the repository at this point in the history
* Add RQLBackendHandlerFactory

* Add RQLBackendHandlerFactory
  • Loading branch information
terrymanu authored Dec 17, 2020
1 parent 2a04bb2 commit a860f6c
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static TextProtocolBackendHandler newInstance(final DatabaseType database
return databaseAdminBackendHandler.get();
}
}
Optional<TextProtocolBackendHandler> distSQLBackendHandler = DistSQLBackendHandlerFactory.newInstance(sqlStatement, backendConnection);
Optional<TextProtocolBackendHandler> distSQLBackendHandler = DistSQLBackendHandlerFactory.newInstance(databaseType, sqlStatement, backendConnection);
if (distSQLBackendHandler.isPresent()) {
return distSQLBackendHandler.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.distsql.parser.statement.rdl.RDLStatement;
import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRuleStatement;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rql.DataSourcesQueryBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rql.RuleQueryBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.RDLBackendHandlerFactory;
import org.apache.shardingsphere.proxy.backend.text.distsql.rql.RQLBackendHandlerFactory;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropDatabaseStatement;

import java.sql.SQLException;
import java.util.Optional;
Expand All @@ -43,21 +38,17 @@ public final class DistSQLBackendHandlerFactory {
/**
* Create new instance of DistSQL backend handler.
*
* @param databaseType database type
* @param sqlStatement SQL statement
* @param backendConnection backend connection
* @return text protocol backend handler
* @throws SQLException SQL exception
*/
public static Optional<TextProtocolBackendHandler> newInstance(final SQLStatement sqlStatement, final BackendConnection backendConnection) throws SQLException {
if (sqlStatement instanceof ShowRuleStatement) {
return Optional.of(new RuleQueryBackendHandler((ShowRuleStatement) sqlStatement, backendConnection));
public static Optional<TextProtocolBackendHandler> newInstance(final DatabaseType databaseType, final SQLStatement sqlStatement, final BackendConnection backendConnection) throws SQLException {
Optional<TextProtocolBackendHandler> rqlBackendHandler = RQLBackendHandlerFactory.newInstance(sqlStatement, backendConnection);
if (rqlBackendHandler.isPresent()) {
return rqlBackendHandler;
}
if (sqlStatement instanceof ShowResourcesStatement) {
return Optional.of(new DataSourcesQueryBackendHandler((ShowResourcesStatement) sqlStatement, backendConnection));
}
if (sqlStatement instanceof RDLStatement || sqlStatement instanceof CreateDatabaseStatement || sqlStatement instanceof DropDatabaseStatement) {
return RDLBackendHandlerFactory.newInstance(sqlStatement, backendConnection);
}
return Optional.empty();
return RDLBackendHandlerFactory.newInstance(databaseType, sqlStatement, backendConnection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ public final class RDLBackendHandlerFactory {
/**
* Create new instance of RDL backend handler.
*
* @param databaseType database type
* @param sqlStatement SQL statement
* @param backendConnection backend connection
* @return RDL backend handler
* @throws SQLException SQL exception
*/
public static Optional<TextProtocolBackendHandler> newInstance(final SQLStatement sqlStatement, final BackendConnection backendConnection) throws SQLException {
checkRegistryCenterExisted(sqlStatement);
return createRDLBackendHandler(sqlStatement, backendConnection);
public static Optional<TextProtocolBackendHandler> newInstance(final DatabaseType databaseType, final SQLStatement sqlStatement, final BackendConnection backendConnection) throws SQLException {
Optional<TextProtocolBackendHandler> result = createRDLBackendHandler(databaseType, sqlStatement, backendConnection);
if (result.isPresent()) {
checkRegistryCenterExisted(sqlStatement);
}
return result;
}

private static void checkRegistryCenterExisted(final SQLStatement sqlStatement) throws SQLException {
Expand All @@ -64,8 +68,7 @@ private static void checkRegistryCenterExisted(final SQLStatement sqlStatement)
}
}

private static Optional<TextProtocolBackendHandler> createRDLBackendHandler(final SQLStatement sqlStatement, final BackendConnection backendConnection) {
DatabaseType databaseType = ProxyContext.getInstance().getMetaDataContexts().getMetaData(backendConnection.getSchemaName()).getResource().getDatabaseType();
private static Optional<TextProtocolBackendHandler> createRDLBackendHandler(final DatabaseType databaseType, final SQLStatement sqlStatement, final BackendConnection backendConnection) {
if (sqlStatement instanceof AddResourceStatement) {
return Optional.of(new AddResourceBackendHandler(databaseType, (AddResourceStatement) sqlStatement, backendConnection));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.text.distsql.rql;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRuleStatement;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.DataSourcesQueryBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.RuleQueryBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;

import java.util.Optional;

/**
* RQL backend handler factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class RQLBackendHandlerFactory {

/**
* Create new instance of RDL backend handler.
*
* @param sqlStatement SQL statement
* @param backendConnection backend connection
* @return RDL backend handler
*/
public static Optional<TextProtocolBackendHandler> newInstance(final SQLStatement sqlStatement, final BackendConnection backendConnection) {
if (sqlStatement instanceof ShowRuleStatement) {
return Optional.of(new RuleQueryBackendHandler((ShowRuleStatement) sqlStatement, backendConnection));
}
if (sqlStatement instanceof ShowResourcesStatement) {
return Optional.of(new DataSourcesQueryBackendHandler((ShowResourcesStatement) sqlStatement, backendConnection));
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.proxy.backend.text.distsql.rql;
package org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl;

import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.proxy.backend.text.distsql.rql;
package org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl;

import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRuleStatement;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ private void assertExecuteCreateDatabaseContext(final CreateDatabaseStatement sq
when(connection.getSchemaName()).thenReturn("schema");
sqlStatement.setDatabaseName("new_db");
try {
RDLBackendHandlerFactory.newInstance(sqlStatement, connection);
RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), sqlStatement, connection);
} catch (final SQLException ex) {
assertThat(ex.getMessage(), is(String.format("No Registry center to execute `%s` SQL", sqlStatement.getClass().getSimpleName())));
}
setGovernanceMetaDataContexts(true);
Optional<TextProtocolBackendHandler> rdlBackendHandler = RDLBackendHandlerFactory.newInstance(sqlStatement, connection);
Optional<TextProtocolBackendHandler> rdlBackendHandler = RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), sqlStatement, connection);
assertTrue(rdlBackendHandler.isPresent());
ResponseHeader response = rdlBackendHandler.get().execute();
assertThat(response, instanceOf(UpdateResponseHeader.class));
Expand All @@ -111,12 +111,12 @@ private void assertExecuteDropDatabaseContext(final DropDatabaseStatement sqlSta
when(connection.getSchemaName()).thenReturn("schema");
sqlStatement.setDatabaseName("schema");
try {
RDLBackendHandlerFactory.newInstance(sqlStatement, connection);
RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), sqlStatement, connection);
} catch (final SQLException ex) {
assertThat(ex.getMessage(), is(String.format("No Registry center to execute `%s` SQL", sqlStatement.getClass().getSimpleName())));
}
setGovernanceMetaDataContexts(true);
Optional<TextProtocolBackendHandler> rdlBackendHandler = RDLBackendHandlerFactory.newInstance(sqlStatement, connection);
Optional<TextProtocolBackendHandler> rdlBackendHandler = RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), sqlStatement, connection);
assertTrue(rdlBackendHandler.isPresent());
ResponseHeader response = rdlBackendHandler.get().execute();
assertThat(response, instanceOf(UpdateResponseHeader.class));
Expand All @@ -137,13 +137,13 @@ public void assertExecuteCreateDatabaseContextWithException(final CreateDatabase
when(connection.getSchemaName()).thenReturn("schema");
sqlStatement.setDatabaseName("schema");
try {
RDLBackendHandlerFactory.newInstance(sqlStatement, connection);
RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), sqlStatement, connection);
} catch (final SQLException ex) {
assertThat(ex.getMessage(), is(String.format("No Registry center to execute `%s` SQL", sqlStatement.getClass().getSimpleName())));
}
setGovernanceMetaDataContexts(true);
try {
RDLBackendHandlerFactory.newInstance(sqlStatement, connection);
RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), sqlStatement, connection);
} catch (final DBCreateExistsException ex) {
assertNull(ex.getMessage());
}
Expand All @@ -160,12 +160,12 @@ public void assertExecuteDataSourcesContext() throws SQLException {
BackendConnection connection = mock(BackendConnection.class);
when(connection.getSchemaName()).thenReturn("schema");
try {
RDLBackendHandlerFactory.newInstance(mock(AddResourceStatement.class), connection);
RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(AddResourceStatement.class), connection);
} catch (final SQLException ex) {
assertThat(ex.getMessage(), is("No Registry center to execute `AddResourceStatement` SQL"));
}
setGovernanceMetaDataContexts(true);
Optional<TextProtocolBackendHandler> rdlBackendHandler = RDLBackendHandlerFactory.newInstance(mock(AddResourceStatement.class), connection);
Optional<TextProtocolBackendHandler> rdlBackendHandler = RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(AddResourceStatement.class), connection);
assertTrue(rdlBackendHandler.isPresent());
ResponseHeader response = rdlBackendHandler.get().execute();
assertThat(response, instanceOf(UpdateResponseHeader.class));
Expand All @@ -176,12 +176,12 @@ public void assertExecuteShardingRuleContext() throws SQLException {
BackendConnection connection = mock(BackendConnection.class);
when(connection.getSchemaName()).thenReturn("schema");
try {
RDLBackendHandlerFactory.newInstance(mock(CreateShardingRuleStatement.class), connection);
RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(CreateShardingRuleStatement.class), connection);
} catch (final SQLException ex) {
assertThat(ex.getMessage(), is("No Registry center to execute `CreateShardingRuleStatement` SQL"));
}
setGovernanceMetaDataContexts(true);
Optional<TextProtocolBackendHandler> rdlBackendHandler = RDLBackendHandlerFactory.newInstance(mock(CreateShardingRuleStatement.class), connection);
Optional<TextProtocolBackendHandler> rdlBackendHandler = RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(CreateShardingRuleStatement.class), connection);
assertTrue(rdlBackendHandler.isPresent());
ResponseHeader response = rdlBackendHandler.get().execute();
assertThat(response, instanceOf(UpdateResponseHeader.class));
Expand Down

0 comments on commit a860f6c

Please sign in to comment.