This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new dc79298 Split RDLBackendHandler and RDLDetailsBackendHandler (#8654)
dc79298 is described below
commit dc792981eee92391bf5b7b54a5a71828cd569e31
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Dec 16 19:20:13 2020 +0800
Split RDLBackendHandler and RDLDetailsBackendHandler (#8654)
* Add CreateDatabaseBackendHandler
* Add AddResourceBackendHandler
* Add CreateShardingRuleBackendHandler
* Add CreateShardingRuleBackendHandler
* Add DropDatabaseBackendHandler
* Refactor RDLBackendHandler
* Fix typo
---
.../distsql/parser/core/DistSQLVisitor.java | 4 +-
...cesStatement.java => AddResourceStatement.java} | 2 +-
...ntext.java => AddResourceStatementContext.java} | 6 +-
.../text/distsql/rdl/RDLBackendHandler.java | 83 +++++-----------------
.../distsql/rdl/SchemaRequiredBackendHandler.java | 1 +
.../rdl/detail/AddResourceBackendHandler.java | 49 +++++++++++++
.../rdl/detail/CreateDatabaseBackendHandler.java | 51 +++++++++++++
.../detail/CreateShardingRuleBackendHandler.java | 51 +++++++++++++
.../rdl/detail/DropDatabaseBackendHandler.java | 51 +++++++++++++
.../DropShardingRuleBackendHandler.java | 3 +-
.../rdl/{ => detail}/RDLBackendDetailHandler.java | 2 +-
.../text/distsql/RDLBackendHandlerTest.java | 6 +-
...CreateDataSourcesStatementContextConverter.java | 4 +-
...teDataSourcesStatementContextConverterTest.java | 8 +--
.../command/query/PostgreSQLCommand.java | 4 +-
.../command/query/PostgreSQLCommandTest.java | 4 +-
16 files changed, 243 insertions(+), 86 deletions(-)
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
index 944cae5..a50ace7 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
+++
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
@@ -32,7 +32,7 @@ import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.S
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.TableNameContext;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
import org.apache.shardingsphere.distsql.parser.segment.TableRuleSegment;
-import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourcesStatement;
+import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourceStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingRuleStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingRuleStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
@@ -57,7 +57,7 @@ public final class DistSQLVisitor extends
DistSQLStatementBaseVisitor<ASTNode> {
for (DataSourceContext each : ctx.dataSource()) {
connectionInfos.add((DataSourceSegment) visit(each));
}
- return new AddResourcesStatement(connectionInfos);
+ return new AddResourceStatement(connectionInfos);
}
@Override
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/impl/AddResourcesStatement.java
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/impl/AddResourceStatement.java
similarity index 94%
rename from
shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/impl/AddResourcesStatement.java
rename to
shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/impl/AddResourceStatement.java
index 05eaff9..01c8a1c 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/impl/AddResourcesStatement.java
+++
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/impl/AddResourceStatement.java
@@ -29,7 +29,7 @@ import java.util.Collection;
*/
@RequiredArgsConstructor
@Getter
-public final class AddResourcesStatement extends CreateRDLStatement {
+public final class AddResourceStatement extends CreateRDLStatement {
private final Collection<DataSourceSegment> dataSources;
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/AddResourcesStatementContext.java
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/AddResourceStatementContext.java
similarity index 83%
rename from
shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/AddResourcesStatementContext.java
rename to
shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/AddResourceStatementContext.java
index 439bf50..4ed4093 100644
---
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/AddResourcesStatementContext.java
+++
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/AddResourceStatementContext.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.infra.binder.statement.rdl;
import lombok.Getter;
-import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourcesStatement;
+import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourceStatement;
import
org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
@@ -26,11 +26,11 @@ import
org.apache.shardingsphere.infra.database.type.DatabaseType;
* Add resource statement context.
*/
@Getter
-public final class AddResourcesStatementContext extends
CommonSQLStatementContext<AddResourcesStatement> {
+public final class AddResourceStatementContext extends
CommonSQLStatementContext<AddResourceStatement> {
private final DatabaseType databaseType;
- public AddResourcesStatementContext(final AddResourcesStatement
sqlStatement, final DatabaseType databaseType) {
+ public AddResourceStatementContext(final AddResourceStatement
sqlStatement, final DatabaseType databaseType) {
super(sqlStatement);
this.databaseType = databaseType;
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandler.java
index e8e6d98..7633f99 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandler.java
@@ -18,43 +18,31 @@
package org.apache.shardingsphere.proxy.backend.text.distsql.rdl;
import lombok.RequiredArgsConstructor;
-import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourcesStatement;
+import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourceStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingRuleStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingRuleStatement;
-import
org.apache.shardingsphere.governance.core.event.model.datasource.DataSourcePersistEvent;
-import
org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsPersistEvent;
-import
org.apache.shardingsphere.governance.core.event.model.schema.SchemaNamePersistEvent;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.ddl.CreateDatabaseStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.ddl.DropDatabaseStatementContext;
-import
org.apache.shardingsphere.infra.binder.statement.rdl.AddResourcesStatementContext;
+import
org.apache.shardingsphere.infra.binder.statement.rdl.AddResourceStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.rdl.CreateShardingRuleStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.rdl.DropShardingRuleStatementContext;
-import org.apache.shardingsphere.infra.config.RuleConfiguration;
-import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import
org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
-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.header.ResponseHeader;
-import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
-import
org.apache.shardingsphere.proxy.config.util.DataSourceParameterConverter;
-import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
-import
org.apache.shardingsphere.proxy.converter.CreateDataSourcesStatementContextConverter;
-import
org.apache.shardingsphere.sharding.converter.CreateShardingRuleStatementContextConverter;
-import
org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.detail.AddResourceBackendHandler;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.detail.CreateDatabaseBackendHandler;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.detail.CreateShardingRuleBackendHandler;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.detail.DropDatabaseBackendHandler;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.detail.DropShardingRuleBackendHandler;
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.Collection;
-import java.util.Collections;
-import java.util.Map;
/**
* RDL backend handler.
@@ -71,42 +59,7 @@ public final class RDLBackendHandler implements
TextProtocolBackendHandler {
if (!isRegistryCenterExisted()) {
throw new SQLException(String.format("No Registry center to
execute `%s` SQL", sqlStatement.getClass().getSimpleName()));
}
- return getResponseHeader(backendConnection.getSchemaName(),
getSQLStatementContext());
- }
-
- private ResponseHeader execute(final CreateDatabaseStatementContext
context) {
- if
(ProxyContext.getInstance().getAllSchemaNames().contains(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));
- return new UpdateResponseHeader(context.getSqlStatement());
- }
-
- private ResponseHeader execute(final AddResourcesStatementContext context)
{
- Map<String, YamlDataSourceParameter> parameters =
CreateDataSourcesStatementContextConverter.convert(context);
- Map<String, DataSourceConfiguration> dataSources =
DataSourceParameterConverter.getDataSourceConfigurationMap(
-
DataSourceParameterConverter.getDataSourceParameterMapFromYamlConfiguration(parameters));
- // TODO Need to get the executed feedback from registry center for
returning.
- ShardingSphereEventBus.getInstance().post(new
DataSourcePersistEvent(backendConnection.getSchemaName(), dataSources));
- return new UpdateResponseHeader(context.getSqlStatement());
- }
-
- private ResponseHeader execute(final CreateShardingRuleStatementContext
context) {
- YamlShardingRuleConfiguration config =
CreateShardingRuleStatementContextConverter.convert(context);
- Collection<RuleConfiguration> rules = new
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singleton(config));
- // TODO Need to get the executed feedback from registry center for
returning.
- ShardingSphereEventBus.getInstance().post(new
RuleConfigurationsPersistEvent(backendConnection.getSchemaName(), rules));
- return new UpdateResponseHeader(context.getSqlStatement());
- }
-
- private ResponseHeader execute(final DropDatabaseStatementContext context)
{
- if
(!ProxyContext.getInstance().getAllSchemaNames().contains(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));
- return new UpdateResponseHeader(context.getSqlStatement());
+ return getResponseHeader(getSQLStatementContext());
}
private boolean isRegistryCenterExisted() {
@@ -115,8 +68,8 @@ public final class RDLBackendHandler implements
TextProtocolBackendHandler {
private SQLStatementContext<?> getSQLStatementContext() {
DatabaseType databaseType =
ProxyContext.getInstance().getMetaDataContexts().getMetaData(backendConnection.getSchemaName()).getResource().getDatabaseType();
- if (sqlStatement instanceof AddResourcesStatement) {
- return new AddResourcesStatementContext((AddResourcesStatement)
sqlStatement, databaseType);
+ if (sqlStatement instanceof AddResourceStatement) {
+ return new AddResourceStatementContext((AddResourceStatement)
sqlStatement, databaseType);
}
if (sqlStatement instanceof CreateShardingRuleStatement) {
return new
CreateShardingRuleStatementContext((CreateShardingRuleStatement) sqlStatement);
@@ -133,21 +86,21 @@ public final class RDLBackendHandler implements
TextProtocolBackendHandler {
throw new
UnsupportedOperationException(sqlStatement.getClass().getName());
}
- private ResponseHeader getResponseHeader(final String schemaName, final
SQLStatementContext<?> context) {
- if (context instanceof CreateDatabaseStatementContext) {
- return execute((CreateDatabaseStatementContext) context);
+ private ResponseHeader getResponseHeader(final SQLStatementContext<?>
context) {
+ if (context instanceof AddResourceStatementContext) {
+ return new AddResourceBackendHandler().execute(backendConnection,
(AddResourceStatementContext) context);
}
- if (context instanceof AddResourcesStatementContext) {
- return execute((AddResourcesStatementContext) context);
+ if (context instanceof CreateDatabaseStatementContext) {
+ return new
CreateDatabaseBackendHandler().execute(backendConnection,
(CreateDatabaseStatementContext) context);
}
if (context instanceof CreateShardingRuleStatementContext) {
- return execute((CreateShardingRuleStatementContext) context);
+ return new
CreateShardingRuleBackendHandler().execute(backendConnection,
(CreateShardingRuleStatementContext) context);
}
if (context instanceof DropDatabaseStatementContext) {
- return execute((DropDatabaseStatementContext) context);
+ return new DropDatabaseBackendHandler().execute(backendConnection,
(DropDatabaseStatementContext) context);
}
if (context instanceof DropShardingRuleStatementContext) {
- new DropShardingRuleBackendHandler().execute(schemaName,
(DropShardingRuleStatementContext) context);
+ new DropShardingRuleBackendHandler().execute(backendConnection,
(DropShardingRuleStatementContext) context);
}
throw new UnsupportedOperationException(context.getClass().getName());
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/SchemaRequiredBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/SchemaRequiredBackendHandler.java
index bc78778..1fdfead 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/SchemaRequiredBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/SchemaRequiredBackendHandler.java
@@ -23,6 +23,7 @@ import
org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import
org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
import
org.apache.shardingsphere.proxy.backend.exception.UnknownDatabaseException;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.detail.RDLBackendDetailHandler;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.available.FromSchemaAvailable;
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/AddResourceBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/AddResourceBackendHandler.java
new file mode 100644
index 0000000..f328ca5
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/AddResourceBackendHandler.java
@@ -0,0 +1,49 @@
+/*
+ * 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.rdl.detail;
+
+import
org.apache.shardingsphere.governance.core.event.model.datasource.DataSourcePersistEvent;
+import
org.apache.shardingsphere.infra.binder.statement.rdl.AddResourceStatementContext;
+import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
+import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+import
org.apache.shardingsphere.proxy.config.util.DataSourceParameterConverter;
+import
org.apache.shardingsphere.proxy.converter.CreateDataSourcesStatementContextConverter;
+
+import java.util.Map;
+
+/**
+ * Add resource backend handler.
+ */
+public final class AddResourceBackendHandler implements
RDLBackendDetailHandler<AddResourceStatementContext> {
+
+ @Override
+ public ResponseHeader execute(final BackendConnection backendConnection,
final AddResourceStatementContext sqlStatementContext) {
+ Map<String, DataSourceConfiguration> dataSources =
DataSourceParameterConverter.getDataSourceConfigurationMap(
+
DataSourceParameterConverter.getDataSourceParameterMapFromYamlConfiguration(CreateDataSourcesStatementContextConverter.convert(sqlStatementContext)));
+ post(backendConnection, dataSources);
+ return new UpdateResponseHeader(sqlStatementContext.getSqlStatement());
+ }
+
+ private void post(final BackendConnection backendConnection, final
Map<String, DataSourceConfiguration> dataSources) {
+ // TODO Need to get the executed feedback from registry center for
returning.
+ ShardingSphereEventBus.getInstance().post(new
DataSourcePersistEvent(backendConnection.getSchemaName(), dataSources));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/CreateDatabaseBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/CreateDatabaseBackendHandler.java
new file mode 100644
index 0000000..58bdce2
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/CreateDatabaseBackendHandler.java
@@ -0,0 +1,51 @@
+/*
+ * 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.rdl.detail;
+
+import
org.apache.shardingsphere.governance.core.event.model.schema.SchemaNamePersistEvent;
+import
org.apache.shardingsphere.infra.binder.statement.ddl.CreateDatabaseStatementContext;
+import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
+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.header.ResponseHeader;
+import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+
+/**
+ * Create database backend handler.
+ */
+public final class CreateDatabaseBackendHandler implements
RDLBackendDetailHandler<CreateDatabaseStatementContext> {
+
+ @Override
+ public ResponseHeader execute(final BackendConnection backendConnection,
final CreateDatabaseStatementContext sqlStatementContext) {
+ check(sqlStatementContext);
+ post(sqlStatementContext);
+ return new UpdateResponseHeader(sqlStatementContext.getSqlStatement());
+ }
+
+ private void check(final CreateDatabaseStatementContext
sqlStatementContext) {
+ if
(ProxyContext.getInstance().getAllSchemaNames().contains(sqlStatementContext.getSqlStatement().getDatabaseName()))
{
+ throw new
DBCreateExistsException(sqlStatementContext.getSqlStatement().getDatabaseName());
+ }
+ }
+
+ private void post(final CreateDatabaseStatementContext
sqlStatementContext) {
+ // TODO Need to get the executed feedback from registry center for
returning.
+ ShardingSphereEventBus.getInstance().post(new
SchemaNamePersistEvent(sqlStatementContext.getSqlStatement().getDatabaseName(),
false));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/CreateShardingRuleBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/CreateShardingRuleBackendHandler.java
new file mode 100644
index 0000000..5d2c538
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/CreateShardingRuleBackendHandler.java
@@ -0,0 +1,51 @@
+/*
+ * 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.rdl.detail;
+
+import
org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsPersistEvent;
+import
org.apache.shardingsphere.infra.binder.statement.rdl.CreateShardingRuleStatementContext;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
+import
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.SchemaRequiredBackendHandler;
+import
org.apache.shardingsphere.sharding.converter.CreateShardingRuleStatementContextConverter;
+import
org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * Create sharding rule backend handler.
+ */
+public final class CreateShardingRuleBackendHandler extends
SchemaRequiredBackendHandler<CreateShardingRuleStatementContext> {
+
+ @Override
+ public ResponseHeader execute(final String schemaName, final
CreateShardingRuleStatementContext sqlStatementContext) {
+ YamlShardingRuleConfiguration config =
CreateShardingRuleStatementContextConverter.convert(sqlStatementContext);
+ Collection<RuleConfiguration> rules = new
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singleton(config));
+ post(schemaName, rules);
+ return new UpdateResponseHeader(sqlStatementContext.getSqlStatement());
+ }
+
+ private void post(final String schemaName, final
Collection<RuleConfiguration> rules) {
+ ShardingSphereEventBus.getInstance().post(new
RuleConfigurationsPersistEvent(schemaName, rules));
+ // TODO Need to get the executed feedback from registry center for
returning.
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/DropDatabaseBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/DropDatabaseBackendHandler.java
new file mode 100644
index 0000000..7b76343
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/DropDatabaseBackendHandler.java
@@ -0,0 +1,51 @@
+/*
+ * 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.rdl.detail;
+
+import
org.apache.shardingsphere.governance.core.event.model.schema.SchemaNamePersistEvent;
+import
org.apache.shardingsphere.infra.binder.statement.ddl.DropDatabaseStatementContext;
+import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
+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.header.ResponseHeader;
+import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+
+/**
+ * Drop database backend handler.
+ */
+public final class DropDatabaseBackendHandler implements
RDLBackendDetailHandler<DropDatabaseStatementContext> {
+
+ @Override
+ public ResponseHeader execute(final BackendConnection backendConnection,
final DropDatabaseStatementContext sqlStatementContext) {
+ check(sqlStatementContext);
+ post(sqlStatementContext);
+ return new UpdateResponseHeader(sqlStatementContext.getSqlStatement());
+ }
+
+ private void check(final DropDatabaseStatementContext sqlStatementContext)
{
+ if
(!ProxyContext.getInstance().getAllSchemaNames().contains(sqlStatementContext.getSqlStatement().getDatabaseName()))
{
+ throw new
DBCreateExistsException(sqlStatementContext.getSqlStatement().getDatabaseName());
+ }
+ }
+
+ private void post(final DropDatabaseStatementContext sqlStatementContext) {
+ // TODO Need to get the executed feedback from registry center for
returning.
+ ShardingSphereEventBus.getInstance().post(new
SchemaNamePersistEvent(sqlStatementContext.getSqlStatement().getDatabaseName(),
true));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/DropShardingRuleBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/DropShardingRuleBackendHandler.java
similarity index 98%
rename from
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/DropShardingRuleBackendHandler.java
rename to
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/DropShardingRuleBackendHandler.java
index c8e836e..0c40e72 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/DropShardingRuleBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/DropShardingRuleBackendHandler.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.text.distsql.rdl;
+package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.detail;
import com.google.common.base.Preconditions;
import
org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsPersistEvent;
@@ -27,6 +27,7 @@ import
org.apache.shardingsphere.proxy.backend.exception.ShardingTableRuleNotExi
import org.apache.shardingsphere.proxy.backend.exception.TablesInUsedException;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.SchemaRequiredBackendHandler;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendDetailHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/RDLBackendDetailHandler.java
similarity index 99%
rename from
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendDetailHandler.java
rename to
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/RDLBackendDetailHandler.java
index cd40ae5..1b7e0cc 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendDetailHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/detail/RDLBackendDetailHandler.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.text.distsql.rdl;
+package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.detail;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerTest.java
index 12df1f6..f7de396 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerTest.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.proxy.backend.text.distsql;
import lombok.SneakyThrows;
-import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourcesStatement;
+import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourceStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingRuleStatement;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
@@ -155,11 +155,11 @@ public final class RDLBackendHandlerTest {
public void assertExecuteDataSourcesContext() throws SQLException {
BackendConnection connection = mock(BackendConnection.class);
when(connection.getSchemaName()).thenReturn("schema");
- RDLBackendHandler executeEngine = new
RDLBackendHandler(mock(AddResourcesStatement.class), connection);
+ RDLBackendHandler executeEngine = new
RDLBackendHandler(mock(AddResourceStatement.class), connection);
try {
executeEngine.execute();
} catch (final SQLException ex) {
- assertThat(ex.getMessage(), is("No Registry center to execute
`AddResourcesStatement` SQL"));
+ assertThat(ex.getMessage(), is("No Registry center to execute
`AddResourceStatement` SQL"));
}
setGovernanceMetaDataContexts(true);
ResponseHeader response = executeEngine.execute();
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/converter/CreateDataSourcesStatementContextConverter.java
b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/converter/CreateDataSourcesStatementContextConverter.java
index d05516e..30a2e1a 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/converter/CreateDataSourcesStatementContextConverter.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/converter/CreateDataSourcesStatementContextConverter.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.proxy.converter;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
-import
org.apache.shardingsphere.infra.binder.statement.rdl.AddResourcesStatementContext;
+import
org.apache.shardingsphere.infra.binder.statement.rdl.AddResourceStatementContext;
import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
@@ -40,7 +40,7 @@ public final class CreateDataSourcesStatementContextConverter
{
* @param sqlStatementContext create data source statement context
* @return YAML data source parameter map
*/
- public static Map<String, YamlDataSourceParameter> convert(final
AddResourcesStatementContext sqlStatementContext) {
+ public static Map<String, YamlDataSourceParameter> convert(final
AddResourceStatementContext sqlStatementContext) {
Map<String, YamlDataSourceParameter> result = new
LinkedHashMap<>(sqlStatementContext.getSqlStatement().getDataSources().size(),
1);
for (DataSourceSegment each :
sqlStatementContext.getSqlStatement().getDataSources()) {
DataSourceParameter parameter = new DataSourceParameter();
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/converter/CreateDataSourcesStatementContextConverterTest.java
b/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/converter/CreateDataSourcesStatementContextConverterTest.java
index be38950..95a01ae 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/converter/CreateDataSourcesStatementContextConverterTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/converter/CreateDataSourcesStatementContextConverterTest.java
@@ -18,8 +18,8 @@
package org.apache.shardingsphere.proxy.converter;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
-import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourcesStatement;
-import
org.apache.shardingsphere.infra.binder.statement.rdl.AddResourcesStatementContext;
+import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourceStatement;
+import
org.apache.shardingsphere.infra.binder.statement.rdl.AddResourceStatementContext;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
import org.junit.Before;
@@ -36,11 +36,11 @@ import static org.junit.Assert.assertTrue;
public final class CreateDataSourcesStatementContextConverterTest {
- private AddResourcesStatementContext sqlStatement;
+ private AddResourceStatementContext sqlStatement;
@Before
public void setUp() {
- sqlStatement = new AddResourcesStatementContext(new
AddResourcesStatement(createDataSourceConnectionSegments()), new
MySQLDatabaseType());
+ sqlStatement = new AddResourceStatementContext(new
AddResourceStatement(createDataSourceConnectionSegments()), new
MySQLDatabaseType());
}
private Collection<DataSourceSegment> createDataSourceConnectionSegments()
{
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/PostgreSQLCommand.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/PostgreSQLCommand.java
index 096c3cb..13e58f6 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/PostgreSQLCommand.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/PostgreSQLCommand.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.proxy.frontend.postgresql.command.query;
import lombok.RequiredArgsConstructor;
-import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourcesStatement;
+import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourceStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingRuleStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
@@ -50,7 +50,7 @@ public final class PostgreSQLCommand {
if (sqlStatement instanceof UpdateStatement) {
return "UPDATE";
}
- if (sqlStatement instanceof CreateDatabaseStatement || sqlStatement
instanceof AddResourcesStatement || sqlStatement instanceof
CreateShardingRuleStatement) {
+ if (sqlStatement instanceof CreateDatabaseStatement || sqlStatement
instanceof AddResourceStatement || sqlStatement instanceof
CreateShardingRuleStatement) {
return "CREATE";
}
if (sqlStatement instanceof DropDatabaseStatement) {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/PostgreSQLCommandTest.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/PostgreSQLCommandTest.java
index 66a1507..01fbd78 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/PostgreSQLCommandTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/PostgreSQLCommandTest.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.proxy.frontend.postgresql.command.query;
-import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourcesStatement;
+import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourceStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingRuleStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
@@ -65,7 +65,7 @@ public final class PostgreSQLCommandTest {
@Test
public void
assertPostgreSQLCommandWhenSQLStatementInstanceOfCreateDataSourcesStatement() {
- assertThat(new
PostgreSQLCommand(mock(AddResourcesStatement.class)).getSQLCommand(),
is("CREATE"));
+ assertThat(new
PostgreSQLCommand(mock(AddResourceStatement.class)).getSQLCommand(),
is("CREATE"));
}
@Test