strongduanmu commented on a change in pull request #7573:
URL: https://github.com/apache/shardingsphere/pull/7573#discussion_r493522821



##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/dml/InsertStatementHandler.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.sql.parser.sql.dialect.handler.dml;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WithSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.MySQLStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLInsertStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.PostgreSQLStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml.PostgreSQLInsertStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.SQLServerStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerInsertStatement;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * InsertStatement handler for different dialect SQLStatements.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class InsertStatementHandler {
+
+    /**
+     * Get OnDuplicateKeyColumnsSegment.
+     *
+     * @param insertStatement InsertStatement
+     * @return OnDuplicateKeyColumnsSegment
+     */
+    public static Optional<OnDuplicateKeyColumnsSegment> 
getOnDuplicateKeyColumnsSegment(final InsertStatement insertStatement) {
+        if (insertStatement instanceof MySQLStatement) {
+            return ((MySQLInsertStatement) 
insertStatement).getOnDuplicateKeyColumns();
+        }
+        return Optional.empty();
+    }
+
+    /**
+     * Get SetAssignmentSegment.
+     *
+     * @param insertStatement InsertStatement
+     * @return SetAssignmentSegment
+     */
+    public static Optional<SetAssignmentSegment> getSetAssignmentSegment(final 
InsertStatement insertStatement) {
+        if (insertStatement instanceof MySQLStatement) {
+            return ((MySQLInsertStatement) insertStatement).getSetAssignment();
+        }
+        return Optional.empty();
+    }
+
+    /**
+     * Get WithSegment.
+     *
+     * @param insertStatement InsertStatement
+     * @return WithSegment
+     */
+    public static Optional<WithSegment> getWithSegment(final InsertStatement 
insertStatement) {
+        if (insertStatement instanceof PostgreSQLStatement) {
+            return ((PostgreSQLInsertStatement) 
insertStatement).getWithSegment();
+        }
+        if (insertStatement instanceof SQLServerStatement) {
+            return ((SQLServerInsertStatement) 
insertStatement).getWithSegment();
+        }
+        return Optional.empty();
+    }
+    
+    /**
+     * Get column names.
+     *
+     * @param insertStatement InsertStatement
+     * @return column names collection
+     */
+    public static List<String> getColumnNames(final InsertStatement 
insertStatement) {
+        Optional<SetAssignmentSegment> setAssignment = 
getSetAssignmentSegment(insertStatement);
+        return setAssignment.isPresent() ? 
getColumnNamesForSetAssignment(setAssignment.get()) : 
getColumnNamesForInsertColumns(insertStatement.getColumns());
+    }
+    
+    private static List<String> getColumnNamesForSetAssignment(final 
SetAssignmentSegment setAssignment) {
+        List<String> result = new LinkedList<>();
+        for (AssignmentSegment each : setAssignment.getAssignments()) {
+            
result.add(each.getColumn().getIdentifier().getValue().toLowerCase());
+        }
+        return result;
+    }
+    
+    private static List<String> getColumnNamesForInsertColumns(final 
Collection<ColumnSegment> columns) {
+        List<String> result = new LinkedList<>();
+        for (ColumnSegment each : columns) {
+            result.add(each.getIdentifier().getValue().toLowerCase());
+        }
+        return result;
+    }
+    
+    /**
+     * Get value count for per value list.
+     *
+     * @param insertStatement InsertStatement
+     * @return value count
+     */
+    public static int getValueCountForPerGroup(final InsertStatement 
insertStatement) {

Review comment:
       > Here is my thought about this point,
   > I am a little worried that we mix up the `handler` and `context` for one 
`SQLStatement`. For me, `handler` is to hide the handling for database dialects 
and provide the basic functions to get the dialect segments. While, `context` 
is to give some of the functions containing `ShardingSphere` logics (or simply 
speaking, for routing and rewriting)
   > I'd like to listen to your thinking. If I missed something, please let me 
know. :-)
   
   @tristaZero I agree with your point on the responsibilities for `handler` 
and `context`, and I will optimize the code according to your suggestions.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to