You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2020/10/11 22:54:18 UTC

[shardingsphere] branch master updated: Remove SQLStatementContext.isReadOnly (#7746)

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

menghaoran 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 91f7483  Remove SQLStatementContext.isReadOnly (#7746)
91f7483 is described below

commit 91f74837c5f1621ce817ecb12924489da89fd554
Author: Liang Zhang <te...@163.com>
AuthorDate: Mon Oct 12 06:53:48 2020 +0800

    Remove SQLStatementContext.isReadOnly (#7746)
---
 .../engine/ConsensusReplicationSQLRouter.java      | 31 +++++++-------
 .../binder/statement/SQLStatementContext.java      | 10 -----
 .../sql/parser/sql/common/util/SQLUtil.java        | 48 ++++------------------
 3 files changed, 23 insertions(+), 66 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-consensus-replication/shardingsphere-consensus-replication-route/src/main/java/org/apache/shardingsphere/replication/consensus/route/engine/ConsensusReplicationSQLRouter.java b/shardingsphere-features/shardingsphere-consensus-replication/shardingsphere-consensus-replication-route/src/main/java/org/apache/shardingsphere/replication/consensus/route/engine/ConsensusReplicationSQLRouter.java
index 0f06fed..b024977 100644
--- a/shardingsphere-features/shardingsphere-consensus-replication/shardingsphere-consensus-replication-route/src/main/java/org/apache/shardingsphere/replication/consensus/route/engine/ConsensusReplicationSQLRouter.java
+++ b/shardingsphere-features/shardingsphere-consensus-replication/shardingsphere-consensus-replication-route/src/main/java/org/apache/shardingsphere/replication/consensus/route/engine/ConsensusReplicationSQLRouter.java
@@ -26,6 +26,7 @@ import org.apache.shardingsphere.infra.sql.LogicSQL;
 import org.apache.shardingsphere.replication.consensus.constant.ConsensusReplicationOrder;
 import org.apache.shardingsphere.replication.consensus.rule.ConsensusReplicationRule;
 import org.apache.shardingsphere.replication.consensus.rule.ConsensusReplicationTableRule;
+import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -41,12 +42,12 @@ public final class ConsensusReplicationSQLRouter implements SQLRouter<ConsensusR
     @Override
     public RouteContext createRouteContext(final LogicSQL logicSQL, final ConsensusReplicationRule rule, final ConfigurationProperties props) {
         RouteContext result = new RouteContext();
-        ConsensusReplicationTableRule replicaRoutingRule = rule.getReplicaTableRules().iterator().next();
-        ConsensusReplicationGroup replicaGroup = new ConsensusReplicationGroup(replicaRoutingRule.getPhysicsTable(), replicaRoutingRule.getReplicaGroupId(), replicaRoutingRule.getReplicaPeers(),
-                replicaRoutingRule.getDataSourceName());
+        ConsensusReplicationTableRule tableRule = rule.getReplicaTableRules().iterator().next();
+        ConsensusReplicationGroup replicaGroup = new ConsensusReplicationGroup(
+                tableRule.getPhysicsTable(), tableRule.getReplicaGroupId(), tableRule.getReplicaPeers(), tableRule.getDataSourceName());
         Map<String, ConsensusReplicationGroup> replicaGroups = Collections.singletonMap(ConsensusReplicationGroup.BLANK_CONSENSUS_REPLICATION_GROUP_KEY, replicaGroup);
-        result.getRouteStageContexts().put(getTypeClass(), 
-                new ConsensusReplicationRouteStageContext(logicSQL.getSchema().getName(), replicaGroups, logicSQL.getSqlStatementContext().isReadOnly()));
+        boolean isReadOnly = SQLUtil.isReadOnly(logicSQL.getSqlStatementContext().getSqlStatement());
+        result.getRouteStageContexts().put(getTypeClass(), new ConsensusReplicationRouteStageContext(logicSQL.getSchema().getName(), replicaGroups, isReadOnly));
         return result;
     }
     
@@ -64,24 +65,24 @@ public final class ConsensusReplicationSQLRouter implements SQLRouter<ConsensusR
                 routeReplicaGroups(routeMappers, rule, replicaGroups);
             }
         }
-        routeContext.getRouteStageContexts().put(getTypeClass(), 
-                new ConsensusReplicationRouteStageContext(logicSQL.getSchema().getName(), replicaGroups, logicSQL.getSqlStatementContext().isReadOnly()));
+        boolean isReadOnly = SQLUtil.isReadOnly(logicSQL.getSqlStatementContext().getSqlStatement());
+        routeContext.getRouteStageContexts().put(getTypeClass(), new ConsensusReplicationRouteStageContext(logicSQL.getSchema().getName(), replicaGroups, isReadOnly));
     }
     
     private void routeReplicaGroups(final Collection<RouteMapper> routeMappers, final ConsensusReplicationRule rule, final Map<String, ConsensusReplicationGroup> replicaGroups) {
         for (RouteMapper each : routeMappers) {
             String actualTableName = each.getActualName();
-            Optional<ConsensusReplicationTableRule> replicaRoutingRuleOptional = rule.findRoutingByTable(actualTableName);
+            Optional<ConsensusReplicationTableRule> tableRuleOptional = rule.findRoutingByTable(actualTableName);
             ConsensusReplicationGroup replicaGroup;
-            if (replicaRoutingRuleOptional.isPresent()) {
-                ConsensusReplicationTableRule replicaRoutingRule = replicaRoutingRuleOptional.get();
-                replicaGroup = new ConsensusReplicationGroup(replicaRoutingRule.getPhysicsTable(), replicaRoutingRule.getReplicaGroupId(), replicaRoutingRule.getReplicaPeers(),
-                        replicaRoutingRule.getDataSourceName());
+            if (tableRuleOptional.isPresent()) {
+                ConsensusReplicationTableRule tableRule = tableRuleOptional.get();
+                replicaGroup = new ConsensusReplicationGroup(
+                        tableRule.getPhysicsTable(), tableRule.getReplicaGroupId(), tableRule.getReplicaPeers(), tableRule.getDataSourceName());
                 replicaGroups.put(actualTableName, replicaGroup);
             } else {
-                ConsensusReplicationTableRule replicaRoutingRule = rule.getReplicaTableRules().iterator().next();
-                replicaGroup = new ConsensusReplicationGroup(replicaRoutingRule.getPhysicsTable(), replicaRoutingRule.getReplicaGroupId(), replicaRoutingRule.getReplicaPeers(),
-                        replicaRoutingRule.getDataSourceName());
+                ConsensusReplicationTableRule tableRule = rule.getReplicaTableRules().iterator().next();
+                replicaGroup = new ConsensusReplicationGroup(
+                        tableRule.getPhysicsTable(), tableRule.getReplicaGroupId(), tableRule.getReplicaPeers(), tableRule.getDataSourceName());
             }
             replicaGroups.put(actualTableName, replicaGroup);
         }
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/SQLStatementContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/SQLStatementContext.java
index 37842c7..dbe536a 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/SQLStatementContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/SQLStatementContext.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.infra.binder.statement;
 
 import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
 
 /**
  * SQL statement context.
@@ -41,13 +40,4 @@ public interface SQLStatementContext<T extends SQLStatement> {
      * @return tables context
      */
     TablesContext getTablesContext();
-    
-    /**
-     * Determine whether SQL is read-only.
-     *
-     * @return true if read-only, otherwise false
-     */
-    default boolean isReadOnly() {
-        return SQLUtil.isReadOnly(getSqlStatement());
-    }
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLUtil.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLUtil.java
index 2b50199..5a0343d 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLUtil.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLUtil.java
@@ -29,16 +29,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.SetStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dcl.DCLStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dcl.GrantStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dcl.RevokeStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.AlterIndexStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.AlterTableStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateIndexStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropIndexStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropTableStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.TruncateStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
@@ -55,7 +46,6 @@ import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQ
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLResetStatement;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLUninstallPluginStatement;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLUseStatement;
-import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dcl.SQLServerDenyUserStatement;
 
 import java.math.BigDecimal;
 import java.math.BigInteger;
@@ -182,14 +172,14 @@ public final class SQLUtil {
         if (sqlStatement instanceof DMLStatement) {
             return isReadOnly((DMLStatement) sqlStatement);
         }
+        if (sqlStatement instanceof DALStatement) {
+            return isReadOnly((DALStatement) sqlStatement);
+        }
         if (sqlStatement instanceof DDLStatement) {
-            return isReadOnly((DDLStatement) sqlStatement);
+            return false;
         }
         if (sqlStatement instanceof DCLStatement) {
-            return isReadOnly((DCLStatement) sqlStatement);
-        }
-        if (sqlStatement instanceof DALStatement) {
-            return isReadOnly((DALStatement) sqlStatement);
+            return false;
         }
         throw new UnsupportedOperationException(String.format("Unsupported SQL Type `%s`", sqlStatement.getClass().getSimpleName()));
     }
@@ -197,35 +187,11 @@ public final class SQLUtil {
     private static boolean isReadOnly(final DMLStatement sqlStatement) {
         if (sqlStatement instanceof SelectStatement) {
             return true;
-        } else if (sqlStatement instanceof UpdateStatement
-                | sqlStatement instanceof DeleteStatement
-                | sqlStatement instanceof InsertStatement) {
-            return false;
         }
-        throw new UnsupportedOperationException(String.format("Unsupported SQL Type `%s`", sqlStatement.getClass().getSimpleName()));
-    }
-    
-    private static boolean isReadOnly(final DDLStatement sqlStatement) {
-        if (sqlStatement instanceof CreateTableStatement
-                | sqlStatement instanceof AlterTableStatement
-                | sqlStatement instanceof DropTableStatement
-                | sqlStatement instanceof CreateIndexStatement
-                | sqlStatement instanceof AlterIndexStatement
-                | sqlStatement instanceof DropIndexStatement
-                | sqlStatement instanceof TruncateStatement
-                | sqlStatement instanceof AlterTableStatement) {
-            return false;
-        }
-        return false;
-    }
-    
-    private static boolean isReadOnly(final DCLStatement sqlStatement) {
-        if (sqlStatement instanceof GrantStatement
-                | sqlStatement instanceof RevokeStatement
-                | sqlStatement instanceof SQLServerDenyUserStatement) {
+        if (sqlStatement instanceof UpdateStatement || sqlStatement instanceof DeleteStatement || sqlStatement instanceof InsertStatement) {
             return false;
         }
-        return false;
+        throw new UnsupportedOperationException(String.format("Unsupported SQL Type `%s`", sqlStatement.getClass().getSimpleName()));
     }
     
     private static boolean isReadOnly(final DALStatement sqlStatement) {