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 d7144be  Add ShardingConditionEngineFactory (#7691)
d7144be is described below

commit d7144beacbf899278e85e0eaba57764f890fc3d9
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Oct 3 23:15:26 2020 +0800

    Add ShardingConditionEngineFactory (#7691)
    
    * Add ShardingConditionEngineFactory
    
    * Refactor InsertClauseShardingConditionEngine
---
 .../sharding/route/engine/ShardingSQLRouter.java   | 16 ++------
 .../engine/ShardingConditionEngineFactory.java     | 47 ++++++++++++++++++++++
 .../impl/InsertClauseShardingConditionEngine.java  | 16 ++++----
 3 files changed, 59 insertions(+), 20 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java
index 4849733..85731fa 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java
@@ -27,8 +27,7 @@ import 
org.apache.shardingsphere.sharding.constant.ShardingOrder;
 import 
org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition;
 import 
org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions;
 import 
org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngine;
-import 
org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.InsertClauseShardingConditionEngine;
-import 
org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.WhereClauseShardingConditionEngine;
+import 
org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngineFactory;
 import 
org.apache.shardingsphere.sharding.route.engine.type.ShardingRouteEngineFactory;
 import 
org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
 import 
org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidatorFactory;
@@ -38,7 +37,6 @@ import org.apache.shardingsphere.sharding.rule.TableRule;
 import org.apache.shardingsphere.sharding.strategy.hint.HintShardingStrategy;
 import org.apache.shardingsphere.sharding.strategy.value.ListRouteValue;
 import org.apache.shardingsphere.sharding.strategy.value.RouteValue;
-import 
org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
 import 
org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
 import 
org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
 import 
org.apache.shardingsphere.sql.parser.binder.statement.dml.SelectStatementContext;
@@ -62,7 +60,7 @@ public final class ShardingSQLRouter implements 
SQLRouter<ShardingRule> {
         SQLStatement sqlStatement = 
logicSQL.getSqlStatementContext().getSqlStatement();
         Optional<ShardingStatementValidator> validator = 
ShardingStatementValidatorFactory.newInstance(sqlStatement);
         validator.ifPresent(v -> v.preValidate(rule, 
logicSQL.getSqlStatementContext(), logicSQL.getParameters(), 
logicSQL.getSchema().getMetaData()));
-        ShardingConditions shardingConditions = 
getShardingConditions(logicSQL, rule);
+        ShardingConditions shardingConditions = 
createShardingConditions(logicSQL, rule);
         boolean needMergeShardingValues = 
isNeedMergeShardingValues(logicSQL.getSqlStatementContext(), rule);
         if (sqlStatement instanceof DMLStatement && needMergeShardingValues) {
             checkSubqueryShardingValues(logicSQL.getSqlStatementContext(), 
rule, shardingConditions);
@@ -74,10 +72,10 @@ public final class ShardingSQLRouter implements 
SQLRouter<ShardingRule> {
     }
     
     @SuppressWarnings({"rawtypes", "unchecked"})
-    private ShardingConditions getShardingConditions(final LogicSQL logicSQL, 
final ShardingRule rule) {
+    private ShardingConditions createShardingConditions(final LogicSQL 
logicSQL, final ShardingRule rule) {
         List<ShardingCondition> shardingConditions;
         if (logicSQL.getSqlStatementContext().getSqlStatement() instanceof 
DMLStatement) {
-            ShardingConditionEngine shardingConditionEngine = 
createShardingConditionEngine(logicSQL, rule);
+            ShardingConditionEngine shardingConditionEngine = 
ShardingConditionEngineFactory.createShardingConditionEngine(logicSQL, rule);
             shardingConditions = 
shardingConditionEngine.createShardingConditions(logicSQL.getSqlStatementContext(),
 logicSQL.getParameters());
         } else {
             shardingConditions = Collections.emptyList();
@@ -85,12 +83,6 @@ public final class ShardingSQLRouter implements 
SQLRouter<ShardingRule> {
         return new ShardingConditions(shardingConditions);
     }
     
-    private ShardingConditionEngine<?> createShardingConditionEngine(final 
LogicSQL logicSQL, final ShardingRule rule) {
-        SchemaMetaData schemaMetaData = 
logicSQL.getSchema().getMetaData().getRuleSchemaMetaData().getConfiguredSchemaMetaData();
-        return logicSQL.getSqlStatementContext() instanceof 
InsertStatementContext 
-                ? new InsertClauseShardingConditionEngine(rule, 
schemaMetaData) : new WhereClauseShardingConditionEngine(rule, schemaMetaData);
-    }
-    
     private boolean isNeedMergeShardingValues(final SQLStatementContext<?> 
sqlStatementContext, final ShardingRule rule) {
         boolean selectContainsSubquery = sqlStatementContext instanceof 
SelectStatementContext && ((SelectStatementContext) 
sqlStatementContext).isContainsSubquery();
         boolean insertSelectContainsSubquery = sqlStatementContext instanceof 
InsertStatementContext && null != ((InsertStatementContext) 
sqlStatementContext).getInsertSelectContext()
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngineFactory.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngineFactory.java
new file mode 100644
index 0000000..b183688
--- /dev/null
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngineFactory.java
@@ -0,0 +1,47 @@
+/*
+ * 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.sharding.route.engine.condition.engine;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.sql.LogicSQL;
+import 
org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.InsertClauseShardingConditionEngine;
+import 
org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.WhereClauseShardingConditionEngine;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import 
org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
+import 
org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
+
+/**
+ * Sharding condition engine factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ShardingConditionEngineFactory {
+    
+    /**
+     * Create new instance of sharding condition engine.
+     *
+     * @param logicSQL logic SQL
+     * @param rule sharding rule 
+     * @return sharding condition engine
+     */
+    public static ShardingConditionEngine<?> 
createShardingConditionEngine(final LogicSQL logicSQL, final ShardingRule rule) 
{
+        SchemaMetaData schemaMetaData = 
logicSQL.getSchema().getMetaData().getRuleSchemaMetaData().getConfiguredSchemaMetaData();
+        return logicSQL.getSqlStatementContext() instanceof 
InsertStatementContext
+                ? new InsertClauseShardingConditionEngine(rule, 
schemaMetaData) : new WhereClauseShardingConditionEngine(rule, schemaMetaData);
+    }
+}
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
index 3093df8..8cc6bb1 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
@@ -56,21 +56,21 @@ public final class InsertClauseShardingConditionEngine 
implements ShardingCondit
     private final SchemaMetaData schemaMetaData;
     
     @Override
-    public List<ShardingCondition> createShardingConditions(final 
InsertStatementContext insertStatementContext, final List<Object> parameters) {
+    public List<ShardingCondition> createShardingConditions(final 
InsertStatementContext sqlStatementContext, final List<Object> parameters) {
         List<ShardingCondition> result = new LinkedList<>();
-        String tableName = 
insertStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue();
-        Collection<String> columnNames = 
getColumnNames(insertStatementContext);
-        for (InsertValueContext each : 
insertStatementContext.getInsertValueContexts()) {
+        String tableName = 
sqlStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue();
+        Collection<String> columnNames = getColumnNames(sqlStatementContext);
+        for (InsertValueContext each : 
sqlStatementContext.getInsertValueContexts()) {
             result.add(createShardingCondition(tableName, 
columnNames.iterator(), each, parameters));
         }
-        if (null != insertStatementContext.getInsertSelectContext()) {
-            SelectStatementContext selectStatementContext = 
insertStatementContext.getInsertSelectContext().getSelectStatementContext();
+        if (null != sqlStatementContext.getInsertSelectContext()) {
+            SelectStatementContext selectStatementContext = 
sqlStatementContext.getInsertSelectContext().getSelectStatementContext();
             List<ShardingCondition> shardingConditions = new 
WhereClauseShardingConditionEngine(shardingRule, 
schemaMetaData).createShardingConditions(selectStatementContext, parameters);
             result.addAll(shardingConditions);
         }
-        Optional<GeneratedKeyContext> generatedKey = 
insertStatementContext.getGeneratedKeyContext();
+        Optional<GeneratedKeyContext> generatedKey = 
sqlStatementContext.getGeneratedKeyContext();
         if (generatedKey.isPresent() && generatedKey.get().isGenerated()) {
-            
generatedKey.get().getGeneratedValues().addAll(getGeneratedKeys(tableName, 
insertStatementContext.getValueListCount()));
+            
generatedKey.get().getGeneratedValues().addAll(getGeneratedKeys(tableName, 
sqlStatementContext.getValueListCount()));
             if 
(shardingRule.isShardingColumn(generatedKey.get().getColumnName(), tableName)) {
                 appendGeneratedKeyCondition(generatedKey.get(), tableName, 
result);
             }

Reply via email to