You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/05/13 04:51:03 UTC

[GitHub] [shardingsphere] strongduanmu commented on a diff in pull request #17516: refactor TableExtractor to merge two extractTablesFromInsert methods

strongduanmu commented on code in PR #17516:
URL: https://github.com/apache/shardingsphere/pull/17516#discussion_r871997309


##########
shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/InsertStatementContextUtil.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.binder.statement.dml;
+
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.InsertMultiTableElementSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.dml.OracleInsertStatement;
+
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Insert SQL statement context util.
+ */
+public final class InsertStatementContextUtil {
+    
+    /**
+     * Get first index on MultiInsertStatement.
+     * @param sqlStatementContext InsertStatementContext
+     * 
+     * @return column names collection
+     */
+    public static SimpleTableSegment getTable(final InsertStatementContext sqlStatementContext) {

Review Comment:
   @cheese8 Can we remove InsertStatementContextUtil class, and move  getTable method to InsertStatementContext.



##########
shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/InsertStatementContextUtil.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.binder.statement.dml;
+
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.InsertMultiTableElementSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.dml.OracleInsertStatement;
+
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Insert SQL statement context util.
+ */
+public final class InsertStatementContextUtil {
+    
+    /**
+     * Get first index on MultiInsertStatement.
+     * @param sqlStatementContext InsertStatementContext
+     * 
+     * @return column names collection
+     */
+    public static SimpleTableSegment getTable(final InsertStatementContext sqlStatementContext) {
+        return getInsertStatements(sqlStatementContext.getSqlStatement()).get(0).getTable();
+    }
+    
+    /**
+     * Get InsertStatement collection from MultiInsertStatement.
+     * @param sqlStatement InsertStatement
+     *
+     * @return InsertStatement collection
+     */
+    public static List<InsertStatement> getInsertStatements(final InsertStatement sqlStatement) {
+        Optional<InsertMultiTableElementSegment> optional = getInsertMultiTableElementSegment(sqlStatement);
+        if (optional.isPresent()) {
+            return new LinkedList<>(optional.get().getInsertStatements());
+        }
+        return Collections.singletonList(sqlStatement);
+    }
+    
+    private static Optional<InsertMultiTableElementSegment> getInsertMultiTableElementSegment(final InsertStatement sqlStatement) {

Review Comment:
   @cheese8 Please move this logic to InsertStatementHandler.



##########
shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/InsertStatementContext.java:
##########
@@ -62,38 +63,51 @@ public final class InsertStatementContext extends CommonSQLStatementContext<Inse
     
     private final List<String> columnNames;
     
+    private final Map<Integer, List<String>> columnNamesMap = new LinkedHashMap<>();
+    
     private final Map<String, ShardingSphereMetaData> metaDataMap;
     
     private final String defaultDatabaseName;
     
     private final List<String> insertColumnNames;
     
-    private final List<List<ExpressionSegment>> valueExpressions;
+    private final Map<Integer, List<List<ExpressionSegment>>> valueExpressions = new LinkedHashMap<>();
     
     private List<InsertValueContext> insertValueContexts;
     
+    private Map<Integer, List<InsertValueContext>> insertValueContextsMap = new LinkedHashMap<>();
+    
     private InsertSelectContext insertSelectContext;
     
     private OnDuplicateUpdateContext onDuplicateKeyUpdateValueContext;
     
-    private GeneratedKeyContext generatedKeyContext;
+    private Map<Integer, GeneratedKeyContext> generatedKeyContexts = new LinkedHashMap<>();
     
     public InsertStatementContext(final Map<String, ShardingSphereMetaData> metaDataMap, final List<Object> parameters,
                                   final InsertStatement sqlStatement, final String defaultDatabaseName) {
         super(sqlStatement);
         this.metaDataMap = metaDataMap;
         this.defaultDatabaseName = defaultDatabaseName;
         insertColumnNames = getInsertColumnNames();
-        valueExpressions = getAllValueExpressions(sqlStatement);
-        AtomicInteger parametersOffset = new AtomicInteger(0);
-        insertValueContexts = getInsertValueContexts(parameters, parametersOffset, valueExpressions);
-        insertSelectContext = getInsertSelectContext(metaDataMap, parameters, parametersOffset, defaultDatabaseName).orElse(null);
-        onDuplicateKeyUpdateValueContext = getOnDuplicateKeyUpdateValueContext(parameters, parametersOffset).orElse(null);
+        
         tablesContext = new TablesContext(getAllSimpleTableSegments(), getDatabaseType());
         ShardingSphereSchema schema = getSchema(metaDataMap, defaultDatabaseName);
-        columnNames = useDefaultColumns() ? schema.getAllColumnNames(sqlStatement.getTable().getTableName().getIdentifier().getValue()) : insertColumnNames;
-        generatedKeyContext = new GeneratedKeyContextEngine(sqlStatement, schema)
-                .createGenerateKeyContext(insertColumnNames, getAllValueExpressions(sqlStatement), parameters).orElse(null);
+        
+        AtomicInteger parametersOffset = new AtomicInteger(0);

Review Comment:
   Please add unit test for this new logic.



##########
shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/InsertStatementContext.java:
##########
@@ -62,38 +63,51 @@ public final class InsertStatementContext extends CommonSQLStatementContext<Inse
     
     private final List<String> columnNames;
     
+    private final Map<Integer, List<String>> columnNamesMap = new LinkedHashMap<>();
+    
     private final Map<String, ShardingSphereMetaData> metaDataMap;
     
     private final String defaultDatabaseName;
     
     private final List<String> insertColumnNames;
     
-    private final List<List<ExpressionSegment>> valueExpressions;
+    private final Map<Integer, List<List<ExpressionSegment>>> valueExpressions = new LinkedHashMap<>();
     
     private List<InsertValueContext> insertValueContexts;
     
+    private Map<Integer, List<InsertValueContext>> insertValueContextsMap = new LinkedHashMap<>();
+    
     private InsertSelectContext insertSelectContext;
     
     private OnDuplicateUpdateContext onDuplicateKeyUpdateValueContext;
     
-    private GeneratedKeyContext generatedKeyContext;
+    private Map<Integer, GeneratedKeyContext> generatedKeyContexts = new LinkedHashMap<>();
     
     public InsertStatementContext(final Map<String, ShardingSphereMetaData> metaDataMap, final List<Object> parameters,
                                   final InsertStatement sqlStatement, final String defaultDatabaseName) {
         super(sqlStatement);
         this.metaDataMap = metaDataMap;
         this.defaultDatabaseName = defaultDatabaseName;
         insertColumnNames = getInsertColumnNames();
-        valueExpressions = getAllValueExpressions(sqlStatement);
-        AtomicInteger parametersOffset = new AtomicInteger(0);
-        insertValueContexts = getInsertValueContexts(parameters, parametersOffset, valueExpressions);
-        insertSelectContext = getInsertSelectContext(metaDataMap, parameters, parametersOffset, defaultDatabaseName).orElse(null);
-        onDuplicateKeyUpdateValueContext = getOnDuplicateKeyUpdateValueContext(parameters, parametersOffset).orElse(null);
+        
         tablesContext = new TablesContext(getAllSimpleTableSegments(), getDatabaseType());
         ShardingSphereSchema schema = getSchema(metaDataMap, defaultDatabaseName);
-        columnNames = useDefaultColumns() ? schema.getAllColumnNames(sqlStatement.getTable().getTableName().getIdentifier().getValue()) : insertColumnNames;
-        generatedKeyContext = new GeneratedKeyContextEngine(sqlStatement, schema)
-                .createGenerateKeyContext(insertColumnNames, getAllValueExpressions(sqlStatement), parameters).orElse(null);
+        
+        AtomicInteger parametersOffset = new AtomicInteger(0);
+        List<InsertStatement> insertStatements = InsertStatementContextUtil.getInsertStatements(sqlStatement);
+        for (int cursor = 0; cursor < insertStatements.size(); cursor++) {

Review Comment:
   index may be better.



-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org