This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 37cb4f9b6db Refactor SQLSupportedChecker (#32392)
37cb4f9b6db is described below

commit 37cb4f9b6dbe107aab1849adde3a851ab00c058c
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Aug 4 19:58:32 2024 +0800

    Refactor SQLSupportedChecker (#32392)
    
    * Refactor SQLSupportedChecker
    
    * Refactor SQLSupportedChecker
    
    * Refactor SQLSupportedChecker
    
    * Refactor SQLSupportedChecker
---
 .../sql/EncryptOrderByItemSupportedChecker.java    | 12 ++-----
 .../EncryptOrderByItemSupportedCheckerTest.java    | 10 +-----
 .../infra/checker/SQLSupportedCheckEngine.java     | 13 ++++---
 .../infra/checker/SQLSupportedChecker.java         |  4 ++-
 .../shardingsphere/infra/checker/SchemaAware.java  | 42 ----------------------
 .../connection/kernel/KernelProcessorTest.java     |  6 ++--
 6 files changed, 18 insertions(+), 69 deletions(-)

diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptOrderByItemSupportedChecker.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptOrderByItemSupportedChecker.java
index f66f8f16a03..69a12b049fc 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptOrderByItemSupportedChecker.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptOrderByItemSupportedChecker.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.encrypt.checker.sql;
 
-import lombok.Setter;
 import 
org.apache.shardingsphere.encrypt.exception.syntax.UnsupportedEncryptSQLException;
 import org.apache.shardingsphere.encrypt.rule.EncryptRule;
 import org.apache.shardingsphere.encrypt.rule.table.EncryptTable;
@@ -26,7 +25,6 @@ import 
org.apache.shardingsphere.infra.binder.context.segment.select.orderby.Ord
 import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
 import 
org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
 import org.apache.shardingsphere.infra.checker.SQLSupportedChecker;
-import org.apache.shardingsphere.infra.checker.SchemaAware;
 import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
 import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
@@ -42,12 +40,7 @@ import java.util.Optional;
  * Order by item supported checker for encrypt.
  */
 @HighFrequencyInvocation
-@Setter
-public final class EncryptOrderByItemSupportedChecker implements 
SQLSupportedChecker<SelectStatementContext, EncryptRule>, SchemaAware {
-    
-    private Map<String, ShardingSphereSchema> schemas;
-    
-    private ShardingSphereSchema defaultSchema;
+public final class EncryptOrderByItemSupportedChecker implements 
SQLSupportedChecker<SelectStatementContext, EncryptRule> {
     
     @Override
     public boolean isCheck(final SQLStatementContext sqlStatementContext) {
@@ -67,8 +60,7 @@ public final class EncryptOrderByItemSupportedChecker 
implements SQLSupportedChe
     }
     
     @Override
-    public void check(final EncryptRule encryptRule, final 
SelectStatementContext sqlStatementContext) {
-        ShardingSphereSchema schema = 
sqlStatementContext.getTablesContext().getSchemaName().map(schemas::get).orElseGet(()
 -> defaultSchema);
+    public void check(final EncryptRule encryptRule, final 
ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext) {
         for (OrderByItem each : getOrderByItems(sqlStatementContext)) {
             if (each.getSegment() instanceof ColumnOrderByItemSegment) {
                 ColumnSegment columnSegment = ((ColumnOrderByItemSegment) 
each.getSegment()).getColumn();
diff --git 
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptOrderByItemSupportedCheckerTest.java
 
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptOrderByItemSupportedCheckerTest.java
index 21c1eaa0ecd..989e1cae997 100644
--- 
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptOrderByItemSupportedCheckerTest.java
+++ 
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptOrderByItemSupportedCheckerTest.java
@@ -37,7 +37,6 @@ import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.Owner
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
 import 
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import java.util.Collections;
@@ -50,18 +49,11 @@ import static org.mockito.Mockito.when;
 
 class EncryptOrderByItemSupportedCheckerTest {
     
-    private final EncryptOrderByItemSupportedChecker checker = new 
EncryptOrderByItemSupportedChecker();
-    
     private final DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
     
-    @BeforeEach
-    void setup() {
-        checker.setSchemas(Collections.singletonMap("test", 
mock(ShardingSphereSchema.class)));
-    }
-    
     @Test
     void assertCheck() {
-        assertThrows(UnsupportedEncryptSQLException.class, () -> 
checker.check(mockEncryptRule(), buildSelectStatementContext()));
+        assertThrows(UnsupportedEncryptSQLException.class, () -> new 
EncryptOrderByItemSupportedChecker().check(mockEncryptRule(), 
mock(ShardingSphereSchema.class), buildSelectStatementContext()));
     }
     
     private EncryptRule mockEncryptRule() {
diff --git 
a/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SQLSupportedCheckEngine.java
 
b/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SQLSupportedCheckEngine.java
index 1d47e45e068..4ed0433ef26 100644
--- 
a/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SQLSupportedCheckEngine.java
+++ 
b/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SQLSupportedCheckEngine.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.infra.checker;
 
 import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
 import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
 import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
@@ -42,17 +43,19 @@ public final class SQLSupportedCheckEngine {
      */
     @SuppressWarnings({"rawtypes", "unchecked"})
     public void checkSQL(final Collection<ShardingSphereRule> rules, final 
SQLStatementContext sqlStatementContext, final Map<String, 
ShardingSphereSchema> schemas, final String databaseName) {
+        ShardingSphereSchema schema = getSchema(sqlStatementContext, schemas, 
databaseName);
         for (Entry<ShardingSphereRule, SQLSupportedCheckersFactory> entry : 
OrderedSPILoader.getServices(SQLSupportedCheckersFactory.class, 
rules).entrySet()) {
             Collection<SQLSupportedChecker> checkers = 
entry.getValue().getCheckers();
             for (SQLSupportedChecker each : checkers) {
-                if (each instanceof SchemaAware) {
-                    ((SchemaAware) each).setSchemas(schemas);
-                    ((SchemaAware) each).setDefaultSchema(schemas.get(new 
DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(databaseName)));
-                }
                 if (each.isCheck(sqlStatementContext)) {
-                    each.check(entry.getKey(), sqlStatementContext);
+                    each.check(entry.getKey(), schema, sqlStatementContext);
                 }
             }
         }
     }
+    
+    private static ShardingSphereSchema getSchema(final SQLStatementContext 
sqlStatementContext, final Map<String, ShardingSphereSchema> schemas, final 
String databaseName) {
+        ShardingSphereSchema defaultSchema = schemas.get(new 
DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(databaseName));
+        return sqlStatementContext instanceof TableAvailable ? 
((TableAvailable) 
sqlStatementContext).getTablesContext().getSchemaName().map(schemas::get).orElse(defaultSchema)
 : defaultSchema;
+    }
 }
diff --git 
a/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SQLSupportedChecker.java
 
b/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SQLSupportedChecker.java
index da5b0e4deed..fbd17989ace 100644
--- 
a/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SQLSupportedChecker.java
+++ 
b/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SQLSupportedChecker.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.infra.checker;
 
 import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 
 /**
@@ -40,7 +41,8 @@ public interface SQLSupportedChecker<T extends 
SQLStatementContext, R extends Sh
      * Check SQL.
      *
      * @param rule rule
+     * @param schema schema
      * @param sqlStatementContext to be checked SQL statement context
      */
-    void check(R rule, T sqlStatementContext);
+    void check(R rule, ShardingSphereSchema schema, T sqlStatementContext);
 }
diff --git 
a/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SchemaAware.java
 
b/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SchemaAware.java
deleted file mode 100644
index d7ea0abca07..00000000000
--- 
a/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SchemaAware.java
+++ /dev/null
@@ -1,42 +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.infra.checker;
-
-import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
-
-import java.util.Map;
-
-/**
- * Schema aware.
- */
-public interface SchemaAware {
-    
-    /**
-     * Set schema meta data.
-     *
-     * @param schemas schema meta data map
-     */
-    void setSchemas(Map<String, ShardingSphereSchema> schemas);
-    
-    /**
-     * Set default schema.
-     *
-     * @param defaultSchema default schema
-     */
-    void setDefaultSchema(ShardingSphereSchema defaultSchema);
-}
diff --git 
a/infra/context/src/test/java/org/apache/shardingsphere/infra/connection/kernel/KernelProcessorTest.java
 
b/infra/context/src/test/java/org/apache/shardingsphere/infra/connection/kernel/KernelProcessorTest.java
index 8b0ffe06066..f47f7bdbd59 100644
--- 
a/infra/context/src/test/java/org/apache/shardingsphere/infra/connection/kernel/KernelProcessorTest.java
+++ 
b/infra/context/src/test/java/org/apache/shardingsphere/infra/connection/kernel/KernelProcessorTest.java
@@ -32,6 +32,7 @@ import 
org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
 import org.apache.shardingsphere.infra.session.query.QueryContext;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
 import org.apache.shardingsphere.sqltranslator.context.SQLTranslatorContext;
 import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule;
@@ -55,15 +56,16 @@ class KernelProcessorTest {
     
     @Test
     void assertGenerateExecutionContext() {
+        DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
         SQLStatementContext sqlStatementContext = 
mock(CommonSQLStatementContext.class);
         
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(SelectStatement.class));
+        when(sqlStatementContext.getDatabaseType()).thenReturn(databaseType);
         ConnectionContext connectionContext = mock(ConnectionContext.class);
         
when(connectionContext.getCurrentDatabaseName()).thenReturn(Optional.of(DefaultDatabase.LOGIC_NAME));
         ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
         ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, 
RETURNS_DEEP_STUBS);
         
when(resourceMetaData.getStorageUnits()).thenReturn(Collections.emptyMap());
-        ShardingSphereDatabase database = new 
ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
-                resourceMetaData, new RuleMetaData(mockShardingSphereRule()), 
Collections.emptyMap());
+        ShardingSphereDatabase database = new 
ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, databaseType, 
resourceMetaData, new RuleMetaData(mockShardingSphereRule()), 
Collections.emptyMap());
         
when(metaData.containsDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(true);
         
when(metaData.getDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(database);
         QueryContext queryContext = new QueryContext(sqlStatementContext, 
"SELECT * FROM tbl", Collections.emptyList(), new HintValueContext(), 
connectionContext, metaData);

Reply via email to