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

wuweijie 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 3088154  Refactor SQLCheckEngine to rules based (#10034)
3088154 is described below

commit 30881543fc20d5fd3ebfde523aafaf425cfcb782
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Apr 11 11:43:30 2021 +0800

    Refactor SQLCheckEngine to rules based (#10034)
---
 .../authority/checker/AuthorityChecker.java        |  3 +--
 .../shardingsphere/infra/check/SQLCheckEngine.java | 26 ++++++----------------
 .../shardingsphere/infra/check/SQLChecker.java     |  4 +---
 .../statement/ShardingSpherePreparedStatement.java |  2 +-
 .../core/statement/ShardingSphereStatement.java    |  2 +-
 .../text/TextProtocolBackendHandlerFactory.java    | 24 ++++++++++++--------
 .../mysql/executor/ShowDatabasesExecutor.java      | 19 ++++++++--------
 .../admin/mysql/executor/UseDatabaseExecutor.java  | 22 ++++++++++++------
 .../mysql/auth/MySQLAuthenticationHandler.java     | 14 +++++++++---
 .../admin/initdb/MySQLComInitDbExecutor.java       | 16 ++++++++-----
 .../execute/MySQLComStmtExecuteExecutor.java       | 13 ++++++-----
 .../auth/PostgreSQLAuthenticationHandler.java      | 14 +++++++++---
 12 files changed, 91 insertions(+), 68 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
index eae958a..f2e6651 100644
--- 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
+++ 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
@@ -23,7 +23,6 @@ import 
org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
 import org.apache.shardingsphere.authority.rule.AuthorityRule;
 import org.apache.shardingsphere.infra.check.SQLCheckResult;
 import org.apache.shardingsphere.infra.check.SQLChecker;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.metadata.user.Grantee;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowDatabasesStatement;
@@ -46,7 +45,7 @@ public final class AuthorityChecker implements 
SQLChecker<AuthorityRule> {
     }
     
     @Override
-    public SQLCheckResult check(final SQLStatement sqlStatement, final 
List<Object> parameters, final ShardingSphereMetaData metaData, final Grantee 
grantee, final AuthorityRule authorityRule) {
+    public SQLCheckResult check(final SQLStatement sqlStatement, final 
List<Object> parameters, final Grantee grantee, final AuthorityRule 
authorityRule) {
         if (null == grantee) {
             return new SQLCheckResult(true, "");
         }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/check/SQLCheckEngine.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/check/SQLCheckEngine.java
index 2ed72c0..df6265e 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/check/SQLCheckEngine.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/check/SQLCheckEngine.java
@@ -19,8 +19,6 @@ package org.apache.shardingsphere.infra.check;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import 
org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
 import org.apache.shardingsphere.infra.metadata.user.Grantee;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
@@ -28,7 +26,6 @@ import 
org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 import java.util.Collection;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map.Entry;
 
@@ -46,14 +43,13 @@ public final class SQLCheckEngine {
      * Check schema.
      *
      * @param schemaName schema name
-     * @param metaData meta data
-     * @param globalRuleMetaData global rule meta data
+     * @param rules rules
      * @param grantee grantee
      * @return check result
      */
     @SuppressWarnings({"rawtypes", "unchecked"})
-    public static boolean check(final String schemaName, final 
ShardingSphereMetaData metaData, final ShardingSphereRuleMetaData 
globalRuleMetaData, final Grantee grantee) {
-        for (Entry<ShardingSphereRule, SQLChecker> entry : 
OrderedSPIRegistry.getRegisteredServices(getRules(metaData, 
globalRuleMetaData), SQLChecker.class).entrySet()) {
+    public static boolean check(final String schemaName, final 
Collection<ShardingSphereRule> rules, final Grantee grantee) {
+        for (Entry<ShardingSphereRule, SQLChecker> entry : 
OrderedSPIRegistry.getRegisteredServices(rules, SQLChecker.class).entrySet()) {
             boolean checkResult = entry.getValue().check(schemaName, grantee, 
entry.getKey());
             if (!checkResult) {
                 return false;
@@ -67,24 +63,16 @@ public final class SQLCheckEngine {
      * 
      * @param sqlStatement SQL statement
      * @param parameters SQL parameters
-     * @param metaData meta data
-     * @param globalRuleMetaData global rule meta data
+     * @param rules rules
      * @param grantee grantee
      */
     @SuppressWarnings({"rawtypes", "unchecked"})
-    public static void check(final SQLStatement sqlStatement, final 
List<Object> parameters, 
-                             final ShardingSphereMetaData metaData, final 
ShardingSphereRuleMetaData globalRuleMetaData, final Grantee grantee) {
-        for (Entry<ShardingSphereRule, SQLChecker> entry : 
OrderedSPIRegistry.getRegisteredServices(getRules(metaData, 
globalRuleMetaData), SQLChecker.class).entrySet()) {
-            SQLCheckResult checkResult = entry.getValue().check(sqlStatement, 
parameters, metaData, grantee, entry.getKey());
+    public static void check(final SQLStatement sqlStatement, final 
List<Object> parameters, final Collection<ShardingSphereRule> rules, final 
Grantee grantee) {
+        for (Entry<ShardingSphereRule, SQLChecker> entry : 
OrderedSPIRegistry.getRegisteredServices(rules, SQLChecker.class).entrySet()) {
+            SQLCheckResult checkResult = entry.getValue().check(sqlStatement, 
parameters, grantee, entry.getKey());
             if (!checkResult.isPassed()) {
                 throw new SQLCheckException(checkResult.getErrorMessage());
             }
         }
     }
-    
-    private static Collection<ShardingSphereRule> getRules(final 
ShardingSphereMetaData metaData, final ShardingSphereRuleMetaData 
globalRuleMetaData) {
-        Collection<ShardingSphereRule> result = new 
LinkedList<>(metaData.getRuleMetaData().getRules());
-        result.addAll(globalRuleMetaData.getRules());
-        return result;
-    }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/check/SQLChecker.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/check/SQLChecker.java
index 9f517b1..98dd268 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/check/SQLChecker.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/check/SQLChecker.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.infra.check;
 
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.metadata.user.Grantee;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.spi.ordered.OrderedSPI;
@@ -46,10 +45,9 @@ public interface SQLChecker<T extends ShardingSphereRule> 
extends OrderedSPI<T>
      * 
      * @param sqlStatement SQL statement
      * @param parameters SQL parameters
-     * @param metaData meta data
      * @param grantee grantee
      * @param rule rule
      * @return SQL check result
      */
-    SQLCheckResult check(SQLStatement sqlStatement, List<Object> parameters, 
ShardingSphereMetaData metaData, Grantee grantee, T rule);
+    SQLCheckResult check(SQLStatement sqlStatement, List<Object> parameters, 
Grantee grantee, T rule);
 }
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
index 236d0e1..1676c65 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
@@ -358,7 +358,7 @@ public final class ShardingSpherePreparedStatement extends 
AbstractPreparedState
     
     private ExecutionContext createExecutionContext() {
         LogicSQL logicSQL = createLogicSQL();
-        
SQLCheckEngine.check(logicSQL.getSqlStatementContext().getSqlStatement(), 
logicSQL.getParameters(), metaDataContexts.getDefaultMetaData(), 
metaDataContexts.getGlobalRuleMetaData(), null);
+        
SQLCheckEngine.check(logicSQL.getSqlStatementContext().getSqlStatement(), 
logicSQL.getParameters(), 
metaDataContexts.getDefaultMetaData().getRuleMetaData().getRules(), null);
         ExecutionContext result = 
kernelProcessor.generateExecutionContext(logicSQL, 
metaDataContexts.getDefaultMetaData(), metaDataContexts.getProps());
         findGeneratedKey(result).ifPresent(generatedKey -> 
generatedValues.addAll(generatedKey.getGeneratedValues()));
         return result;
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
index 123111f..4a65711 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
@@ -408,7 +408,7 @@ public final class ShardingSphereStatement extends 
AbstractStatementAdapter {
     private ExecutionContext createExecutionContext(final String sql) throws 
SQLException {
         clearStatements();
         LogicSQL logicSQL = createLogicSQL(sql);
-        
SQLCheckEngine.check(logicSQL.getSqlStatementContext().getSqlStatement(), 
logicSQL.getParameters(), metaDataContexts.getDefaultMetaData(), 
metaDataContexts.getGlobalRuleMetaData(), null);
+        
SQLCheckEngine.check(logicSQL.getSqlStatementContext().getSqlStatement(), 
logicSQL.getParameters(), 
metaDataContexts.getDefaultMetaData().getRuleMetaData().getRules(), null);
         return kernelProcessor.generateExecutionContext(logicSQL, 
metaDataContexts.getDefaultMetaData(), metaDataContexts.getProps());
     }
     
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
index 0f2ce97..1561bf1 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
@@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.check.SQLCheckEngine;
 import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
@@ -39,7 +40,9 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.TCLStatement;
 
 import java.sql.SQLException;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.LinkedList;
 import java.util.Optional;
 
 /**
@@ -75,7 +78,7 @@ public final class TextProtocolBackendHandlerFactory {
         if (extraHandler.isPresent()) {
             return extraHandler.get();
         }
-        sqlCheck(backendConnection, sqlStatement);
+        SQLCheckEngine.check(sqlStatement, Collections.emptyList(), 
getRules(backendConnection.getSchemaName()), backendConnection.getGrantee());
         if (sqlStatement instanceof TCLStatement) {
             return TransactionBackendHandlerFactory.newInstance((TCLStatement) 
sqlStatement, sql, backendConnection);
         }
@@ -87,14 +90,6 @@ public final class TextProtocolBackendHandlerFactory {
         return databaseAdminBackendHandler.orElseGet(() -> 
DatabaseBackendHandlerFactory.newInstance(sqlStatement, sql, 
backendConnection));
     }
     
-    private static void sqlCheck(final BackendConnection backendConnection, 
final SQLStatement sqlStatement) {
-        // TODO :Authority, need to refactor after GlobalRule is added
-        if (!Strings.isNullOrEmpty(backendConnection.getSchemaName())) {
-            MetaDataContexts contexts = 
ProxyContext.getInstance().getMetaDataContexts();
-            SQLCheckEngine.check(sqlStatement, Collections.emptyList(), 
contexts.getMetaData(backendConnection.getSchemaName()), 
contexts.getGlobalRuleMetaData(), backendConnection.getGrantee());
-        }
-    }
-    
     private static DatabaseType getBackendDatabaseType(final DatabaseType 
defaultDatabaseType, final BackendConnection backendConnection) {
         return Strings.isNullOrEmpty(backendConnection.getSchemaName())
                 ? defaultDatabaseType : 
ProxyContext.getInstance().getMetaDataContexts().getMetaData(backendConnection.getSchemaName()).getResource().getDatabaseType();
@@ -103,4 +98,15 @@ public final class TextProtocolBackendHandlerFactory {
     private static Optional<ExtraTextProtocolBackendHandler> 
findExtraTextProtocolBackendHandler(final SQLStatement sqlStatement) {
         return 
ShardingSphereServiceLoader.getSingletonServiceInstances(ExtraTextProtocolBackendHandler.class).stream().filter(each
 -> each.accept(sqlStatement)).findFirst();
     }
+    
+    private static Collection<ShardingSphereRule> getRules(final String 
schemaName) {
+        MetaDataContexts contexts = 
ProxyContext.getInstance().getMetaDataContexts();
+        if (Strings.isNullOrEmpty(schemaName)) {
+            return contexts.getGlobalRuleMetaData().getRules();
+        }
+        Collection<ShardingSphereRule> result;
+        result = new 
LinkedList<>(contexts.getMetaData(schemaName).getRuleMetaData().getRules());
+        result.addAll(contexts.getGlobalRuleMetaData().getRules());
+        return result;
+    }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowDatabasesExecutor.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowDatabasesExecutor.java
index e9c374b..e7528df 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowDatabasesExecutor.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowDatabasesExecutor.java
@@ -24,8 +24,7 @@ import 
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryRe
 import 
org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultColumnMetaData;
 import 
org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultMetaData;
 import org.apache.shardingsphere.infra.merge.result.MergedResult;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import 
org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import 
org.apache.shardingsphere.proxy.backend.text.admin.executor.DatabaseAdminQueryExecutor;
@@ -52,14 +51,14 @@ public final class ShowDatabasesExecutor implements 
DatabaseAdminQueryExecutor {
     }
     
     private Collection<Object> getSchemaNames(final BackendConnection 
backendConnection) {
-        ShardingSphereRuleMetaData globalRuleMetaData = 
ProxyContext.getInstance().getMetaDataContexts().getGlobalRuleMetaData();
         try {
-            SQLCheckEngine.check(new MySQLShowDatabasesStatement(), 
Collections.emptyList(), getMetaData(), globalRuleMetaData, 
backendConnection.getGrantee());
+            SQLCheckEngine.check(
+                    new MySQLShowDatabasesStatement(), 
Collections.emptyList(), 
ProxyContext.getInstance().getMetaDataContexts().getGlobalRuleMetaData().getRules(),
 backendConnection.getGrantee());
             return new 
ArrayList<>(ProxyContext.getInstance().getAllSchemaNames());
         } catch (final SQLCheckException ex) {
             Collection<Object> result = new LinkedList<>();
             for (String each : ProxyContext.getInstance().getAllSchemaNames()) 
{
-                if (SQLCheckEngine.check(each, 
ProxyContext.getInstance().getMetaData(each), globalRuleMetaData, 
backendConnection.getGrantee())) {
+                if (SQLCheckEngine.check(each, getRules(each), 
backendConnection.getGrantee())) {
                     result.add(each);
                 }
             }
@@ -67,11 +66,11 @@ public final class ShowDatabasesExecutor implements 
DatabaseAdminQueryExecutor {
         }
     }
     
-    // TODO the metadata is first one, we need to confirm which schema should 
use.
-    private ShardingSphereMetaData getMetaData() {
-        return ProxyContext.getInstance().getAllSchemaNames().isEmpty()
-                ? new ShardingSphereMetaData("", null, new 
ShardingSphereRuleMetaData(Collections.emptyList(), Collections.emptyList()), 
null)
-                : 
ProxyContext.getInstance().getMetaData(ProxyContext.getInstance().getAllSchemaNames().get(0));
+    private Collection<ShardingSphereRule> getRules(final String schemaName) {
+        Collection<ShardingSphereRule> result;
+        result = new 
LinkedList<>(ProxyContext.getInstance().getMetaDataContexts().getMetaData(schemaName).getRuleMetaData().getRules());
+        
result.addAll(ProxyContext.getInstance().getMetaDataContexts().getGlobalRuleMetaData().getRules());
+        return result;
     }
     
     @Override
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/UseDatabaseExecutor.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/UseDatabaseExecutor.java
index 96308913..cc3337c 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/UseDatabaseExecutor.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/UseDatabaseExecutor.java
@@ -19,7 +19,7 @@ package 
org.apache.shardingsphere.proxy.backend.text.admin.mysql.executor;
 
 import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.infra.check.SQLCheckEngine;
-import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 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;
@@ -27,6 +27,9 @@ import 
org.apache.shardingsphere.proxy.backend.text.admin.executor.DatabaseAdmin
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.UseStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
 
+import java.util.Collection;
+import java.util.LinkedList;
+
 /**
  * Use database executor.
  */
@@ -37,12 +40,17 @@ public final class UseDatabaseExecutor implements 
DatabaseAdminExecutor {
     
     @Override
     public void execute(final BackendConnection backendConnection) {
-        String schema = SQLUtil.getExactlyValue(useStatement.getSchema());
-        MetaDataContexts metaDataContexts = 
ProxyContext.getInstance().getMetaDataContexts();
-        if (!ProxyContext.getInstance().schemaExists(schema)
-                && SQLCheckEngine.check(schema, 
metaDataContexts.getMetaData(schema), metaDataContexts.getGlobalRuleMetaData(), 
backendConnection.getGrantee())) {
-            throw new UnknownDatabaseException(schema);
+        String schemaName = SQLUtil.getExactlyValue(useStatement.getSchema());
+        if (!ProxyContext.getInstance().schemaExists(schemaName) && 
SQLCheckEngine.check(schemaName, getRules(schemaName), 
backendConnection.getGrantee())) {
+            throw new UnknownDatabaseException(schemaName);
         }
-        backendConnection.setCurrentSchema(schema);
+        backendConnection.setCurrentSchema(schemaName);
+    }
+    
+    private Collection<ShardingSphereRule> getRules(final String schemaName) {
+        Collection<ShardingSphereRule> result;
+        result = new 
LinkedList<>(ProxyContext.getInstance().getMetaDataContexts().getMetaData(schemaName).getRuleMetaData().getRules());
+        
result.addAll(ProxyContext.getInstance().getMetaDataContexts().getGlobalRuleMetaData().getRules());
+        return result;
     }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationHandler.java
index aa3b650..cd8b243 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationHandler.java
@@ -23,12 +23,14 @@ import org.apache.commons.codec.digest.DigestUtils;
 import 
org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerErrorCode;
 import 
org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthPluginData;
 import org.apache.shardingsphere.infra.check.SQLCheckEngine;
-import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
 import org.apache.shardingsphere.infra.metadata.user.Grantee;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.LinkedList;
 import java.util.Optional;
 
 /**
@@ -55,8 +57,7 @@ public final class MySQLAuthenticationHandler {
         if (!user.isPresent() || !isPasswordRight(user.get().getPassword(), 
authResponse)) {
             return Optional.of(MySQLServerErrorCode.ER_ACCESS_DENIED_ERROR);
         }
-        MetaDataContexts metaDataContexts = 
ProxyContext.getInstance().getMetaDataContexts();
-        return null == databaseName || SQLCheckEngine.check(databaseName, 
metaDataContexts.getMetaData(databaseName), 
metaDataContexts.getGlobalRuleMetaData(), user.get().getGrantee())
+        return null == databaseName || SQLCheckEngine.check(databaseName, 
getRules(databaseName), user.get().getGrantee())
                 ? Optional.empty() : 
Optional.of(MySQLServerErrorCode.ER_DBACCESS_DENIED_ERROR);
     }
     
@@ -81,4 +82,11 @@ public final class MySQLAuthenticationHandler {
         }
         return result;
     }
+    
+    private Collection<ShardingSphereRule> getRules(final String databaseName) 
{
+        Collection<ShardingSphereRule> result;
+        result = new 
LinkedList<>(ProxyContext.getInstance().getMetaDataContexts().getMetaData(databaseName).getRuleMetaData().getRules());
+        
result.addAll(ProxyContext.getInstance().getMetaDataContexts().getGlobalRuleMetaData().getRules());
+        return result;
+    }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/initdb/MySQLComInitDbExecutor.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/initdb/MySQLComInitDbExecutor.java
index 1ba4000..c0f31a8 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/initdb/MySQLComInitDbExecutor.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/initdb/MySQLComInitDbExecutor.java
@@ -22,7 +22,7 @@ import 
org.apache.shardingsphere.db.protocol.mysql.packet.command.admin.initdb.M
 import 
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
 import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
 import org.apache.shardingsphere.infra.check.SQLCheckEngine;
-import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 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;
@@ -31,6 +31,7 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.LinkedList;
 
 /**
  * COM_INIT_DB command executor for MySQL.
@@ -44,13 +45,18 @@ public final class MySQLComInitDbExecutor implements 
CommandExecutor {
     
     @Override
     public Collection<DatabasePacket<?>> execute() {
-        String schema = SQLUtil.getExactlyValue(packet.getSchema());
-        MetaDataContexts metaDataContexts = 
ProxyContext.getInstance().getMetaDataContexts();
-        if (ProxyContext.getInstance().schemaExists(schema)
-                && SQLCheckEngine.check(schema, 
metaDataContexts.getMetaData(schema), metaDataContexts.getGlobalRuleMetaData(), 
backendConnection.getGrantee())) {
+        String schemaName = SQLUtil.getExactlyValue(packet.getSchema());
+        if (ProxyContext.getInstance().schemaExists(schemaName) && 
SQLCheckEngine.check(schemaName, getRules(schemaName), 
backendConnection.getGrantee())) {
             backendConnection.setCurrentSchema(packet.getSchema());
             return Collections.singletonList(new MySQLOKPacket(1));
         }
         throw new UnknownDatabaseException(packet.getSchema());
     }
+    
+    private Collection<ShardingSphereRule> getRules(final String schemaName) {
+        Collection<ShardingSphereRule> result;
+        result = new 
LinkedList<>(ProxyContext.getInstance().getMetaDataContexts().getMetaData(schemaName).getRuleMetaData().getRules());
+        
result.addAll(ProxyContext.getInstance().getMetaDataContexts().getGlobalRuleMetaData().getRules());
+        return result;
+    }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutor.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutor.java
index c403c31..188c7d2 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutor.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutor.java
@@ -26,9 +26,9 @@ import 
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.e
 import 
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.execute.MySQLComStmtExecutePacket;
 import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
 import org.apache.shardingsphere.infra.check.SQLCheckEngine;
-import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
 import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 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;
@@ -46,6 +46,7 @@ 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.stream.Collectors;
 
 /**
@@ -64,13 +65,15 @@ public final class MySQLComStmtExecuteExecutor implements 
QueryCommandExecutor {
         ShardingSphereSQLParserEngine sqlStatementParserEngine = new 
ShardingSphereSQLParserEngine(DatabaseTypeRegistry.getTrunkDatabaseTypeName(
                 
ProxyContext.getInstance().getMetaDataContexts().getMetaData(backendConnection.getSchemaName()).getResource().getDatabaseType()));
         SQLStatement sqlStatement = 
sqlStatementParserEngine.parse(packet.getSql(), true);
-        sqlCheck(backendConnection, sqlStatement);
+        SQLCheckEngine.check(sqlStatement, Collections.emptyList(), 
getRules(backendConnection.getSchemaName()), backendConnection.getGrantee());
         databaseCommunicationEngine = 
DatabaseCommunicationEngineFactory.getInstance().newBinaryProtocolInstance(sqlStatement,
 packet.getSql(), packet.getParameters(), backendConnection);
     }
     
-    private static void sqlCheck(final BackendConnection backendConnection, 
final SQLStatement sqlStatement) {
-        MetaDataContexts contexts = 
ProxyContext.getInstance().getMetaDataContexts();
-        SQLCheckEngine.check(sqlStatement, Collections.emptyList(), 
contexts.getMetaData(backendConnection.getSchemaName()), 
contexts.getGlobalRuleMetaData(), backendConnection.getGrantee());
+    private static Collection<ShardingSphereRule> getRules(final String 
schemaName) {
+        Collection<ShardingSphereRule> result;
+        result = new 
LinkedList<>(ProxyContext.getInstance().getMetaDataContexts().getMetaData(schemaName).getRuleMetaData().getRules());
+        
result.addAll(ProxyContext.getInstance().getMetaDataContexts().getGlobalRuleMetaData().getRules());
+        return result;
     }
     
     @Override
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationHandler.java
index a893964..a2a79e7 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationHandler.java
@@ -24,12 +24,14 @@ import org.apache.commons.codec.digest.DigestUtils;
 import 
org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLErrorCode;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLPasswordMessagePacket;
 import org.apache.shardingsphere.infra.check.SQLCheckEngine;
-import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
 import org.apache.shardingsphere.infra.metadata.user.Grantee;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 
 import java.security.MessageDigest;
+import java.util.Collection;
+import java.util.LinkedList;
 import java.util.Optional;
 
 /**
@@ -57,8 +59,7 @@ public final class PostgreSQLAuthenticationHandler {
         if (!expectedMd5Digest.equals(md5Digest)) {
             return new 
PostgreSQLLoginResult(PostgreSQLErrorCode.INVALID_PASSWORD, 
String.format("password authentication failed for user \"%s\"", username));
         }
-        MetaDataContexts metaDataContexts = 
ProxyContext.getInstance().getMetaDataContexts();
-        return null == databaseName || SQLCheckEngine.check(databaseName, 
metaDataContexts.getMetaData(databaseName), 
metaDataContexts.getGlobalRuleMetaData(), user.get().getGrantee())
+        return null == databaseName || SQLCheckEngine.check(databaseName, 
getRules(databaseName), user.get().getGrantee())
                 ? new 
PostgreSQLLoginResult(PostgreSQLErrorCode.SUCCESSFUL_COMPLETION, null)
                 : new 
PostgreSQLLoginResult(PostgreSQLErrorCode.PRIVILEGE_NOT_GRANTED, 
String.format("Access denied for user '%s' to database '%s'", username, 
databaseName));
     }
@@ -70,4 +71,11 @@ public final class PostgreSQLAuthenticationHandler {
         messageDigest.update(md5Salt);
         return "md5" + new String(Hex.encodeHex(messageDigest.digest(), true));
     }
+    
+    private static Collection<ShardingSphereRule> getRules(final String 
databaseName) {
+        Collection<ShardingSphereRule> result;
+        result = new 
LinkedList<>(ProxyContext.getInstance().getMetaDataContexts().getMetaData(databaseName).getRuleMetaData().getRules());
+        
result.addAll(ProxyContext.getInstance().getMetaDataContexts().getGlobalRuleMetaData().getRules());
+        return result;
+    }
 }

Reply via email to