From 650afbf4330299bf818e2a7a12c100745bd556b4 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Fri, 26 Jan 2024 23:07:44 +0800 Subject: [PATCH] Merge DatabaseNameUtils (#29870) --- .../distsql/rdl/RDLBackendHandlerFactory.java | 7 +-- .../LegacyRuleDefinitionBackendHandler.java | 6 +-- .../distsql/rul/type/PreviewExecutor.java | 4 +- .../proxy/backend/util/DatabaseNameUtils.java | 46 ------------------- 4 files changed, 9 insertions(+), 54 deletions(-) delete mode 100644 proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/DatabaseNameUtils.java diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/RDLBackendHandlerFactory.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/RDLBackendHandlerFactory.java index 679ffa5983979..8bc471f6592fc 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/RDLBackendHandlerFactory.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/RDLBackendHandlerFactory.java @@ -21,6 +21,7 @@ import lombok.NoArgsConstructor; import org.apache.shardingsphere.distsql.handler.type.rdl.resource.ResourceDefinitionExecutor; import org.apache.shardingsphere.distsql.handler.type.rdl.resource.aware.DatabaseAwareResourceDefinitionExecutor; +import org.apache.shardingsphere.distsql.handler.util.DatabaseNameUtils; import org.apache.shardingsphere.distsql.statement.rdl.RDLStatement; import org.apache.shardingsphere.distsql.statement.rdl.resource.ResourceDefinitionStatement; import org.apache.shardingsphere.distsql.statement.rdl.rule.RuleDefinitionStatement; @@ -29,10 +30,9 @@ import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler; import org.apache.shardingsphere.proxy.backend.handler.distsql.DistSQLBackendHandler; import org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.resource.ResourceDefinitionBackendHandler; -import org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.RuleDefinitionBackendHandler; import org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.LegacyRuleDefinitionBackendHandler; +import org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.RuleDefinitionBackendHandler; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; -import org.apache.shardingsphere.proxy.backend.util.DatabaseNameUtils; /** * RDL backend handler factory. @@ -58,7 +58,8 @@ public static ProxyBackendHandler newInstance(final RDLStatement sqlStatement, f private static ResourceDefinitionBackendHandler getResourceBackendHandler(final ResourceDefinitionStatement sqlStatement, final ConnectionSession connectionSession) { ResourceDefinitionExecutor executor = TypedSPILoader.getService(ResourceDefinitionExecutor.class, sqlStatement.getClass()); if (executor instanceof DatabaseAwareResourceDefinitionExecutor) { - ((DatabaseAwareResourceDefinitionExecutor) executor).setDatabase(ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, connectionSession))); + ((DatabaseAwareResourceDefinitionExecutor) executor).setDatabase( + ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, connectionSession.getDatabaseName()))); } return new ResourceDefinitionBackendHandler(sqlStatement, executor); } diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandler.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandler.java index 47bd94f47e753..c71bd0770ea85 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandler.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandler.java @@ -20,8 +20,9 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; -import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDefinitionExecutor; +import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; +import org.apache.shardingsphere.distsql.handler.util.DatabaseNameUtils; import org.apache.shardingsphere.distsql.statement.rdl.rule.RuleDefinitionStatement; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator; @@ -34,7 +35,6 @@ 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.session.ConnectionSession; -import org.apache.shardingsphere.proxy.backend.util.DatabaseNameUtils; import org.apache.shardingsphere.readwritesplitting.distsql.handler.update.DropReadwriteSplittingRuleExecutor; import org.apache.shardingsphere.readwritesplitting.distsql.statement.DropReadwriteSplittingRuleStatement; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; @@ -60,7 +60,7 @@ public final class LegacyRuleDefinitionBackendHandler implements DistSQLBackendH @SuppressWarnings({"rawtypes", "unchecked"}) @Override public ResponseHeader execute() { - ShardingSphereDatabase database = ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, connectionSession)); + ShardingSphereDatabase database = ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, connectionSession.getDatabaseName())); DatabaseRuleDefinitionExecutor executor = TypedSPILoader.getService(DatabaseRuleDefinitionExecutor.class, sqlStatement.getClass()); Class ruleConfigClass = executor.getRuleConfigurationClass(); RuleConfiguration currentRuleConfig = findCurrentRuleConfiguration(database, ruleConfigClass).orElse(null); diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/type/PreviewExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/type/PreviewExecutor.java index 9c1aa24318f5b..9749ab933d43e 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/type/PreviewExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/type/PreviewExecutor.java @@ -19,6 +19,7 @@ import com.google.common.base.Preconditions; import lombok.Setter; +import org.apache.shardingsphere.distsql.handler.util.DatabaseNameUtils; import org.apache.shardingsphere.distsql.statement.rul.sql.PreviewStatement; import org.apache.shardingsphere.infra.binder.context.aware.CursorDefinitionAware; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; @@ -56,7 +57,6 @@ import org.apache.shardingsphere.proxy.backend.exception.RuleNotExistedException; import org.apache.shardingsphere.proxy.backend.handler.distsql.rul.aware.ConnectionSessionAwareRULExecutor; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; -import org.apache.shardingsphere.proxy.backend.util.DatabaseNameUtils; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLInsertStatement; import org.apache.shardingsphere.sqlfederation.engine.SQLFederationEngine; @@ -86,7 +86,7 @@ public Collection getColumnNames() { @Override public Collection getRows(final ShardingSphereMetaData metaData, final PreviewStatement sqlStatement) throws SQLException { - ShardingSphereDatabase database = ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, connectionSession)); + ShardingSphereDatabase database = ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, connectionSession.getDatabaseName())); String toBePreviewedSQL = SQLHintUtils.removeHint(sqlStatement.getSql()); HintValueContext hintValueContext = SQLHintUtils.extractHint(sqlStatement.getSql()).orElseGet(HintValueContext::new); SQLStatement toBePreviewedStatement = metaData.getGlobalRuleMetaData().getSingleRule(SQLParserRule.class).getSQLParserEngine(database.getProtocolType()).parse(toBePreviewedSQL, false); diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/DatabaseNameUtils.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/DatabaseNameUtils.java deleted file mode 100644 index 0aaf40d70da7b..0000000000000 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/DatabaseNameUtils.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.shardingsphere.proxy.backend.util; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; -import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseSegment; -import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; -import org.apache.shardingsphere.sql.parser.sql.common.statement.available.FromDatabaseAvailable; - -import java.util.Optional; - -/** - * Database name utility class. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class DatabaseNameUtils { - - /** - * Get database name. - * - * @param sqlStatement SQL statement - * @param connectionSession connection session - * @return database name - */ - public static String getDatabaseName(final SQLStatement sqlStatement, final ConnectionSession connectionSession) { - Optional databaseSegment = sqlStatement instanceof FromDatabaseAvailable ? ((FromDatabaseAvailable) sqlStatement).getDatabase() : Optional.empty(); - return databaseSegment.map(optional -> optional.getIdentifier().getValue()).orElse(connectionSession.getDatabaseName()); - } -}