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 2021/05/18 11:04:36 UTC

[GitHub] [shardingsphere] strongduanmu commented on a change in pull request #10377: extract abstract explain statement & support postgresql explain statement

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



##########
File path: shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dal/ExplainStatementContext.java
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.dal;
+
+import lombok.Getter;
+import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
+import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
+import org.apache.shardingsphere.infra.binder.type.TableAvailable;
+import org.apache.shardingsphere.sql.parser.sql.common.extractor.TableExtractor;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.ExplainStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dal.ExplainStatementHandler;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+/**
+ * Explain statement context.
+ */
+@Getter
+public final class ExplainStatementContext extends CommonSQLStatementContext<ExplainStatement> implements TableAvailable {
+    
+    private final TablesContext tablesContext;
+    
+    public ExplainStatementContext(final ExplainStatement sqlStatement) {
+        super(sqlStatement);
+        tablesContext = new TablesContext(extractTablesFromExplain(sqlStatement));
+    }
+    
+    private Collection<SimpleTableSegment> extractTablesFromExplain(final ExplainStatement sqlStatement) {
+        Collection<SimpleTableSegment> result = new LinkedList<>();
+        ExplainStatementHandler.getSimpleTableSegment(sqlStatement).ifPresent(result::add);
+        SQLStatement explainableStatement = sqlStatement.getStatement().orElse(null);
+        TableExtractor extractor = new TableExtractor();
+        if (explainableStatement instanceof SelectStatement) {
+            extractor.extractTablesFromSelect((SelectStatement) explainableStatement);
+        } else if (explainableStatement instanceof InsertStatement) {
+            extractor.extractTablesFromInsert((InsertStatement) explainableStatement);
+        } else if (explainableStatement instanceof UpdateStatement) {
+            extractor.extractTablesFromUpdate((UpdateStatement) explainableStatement);
+        } else if (explainableStatement instanceof DeleteStatement) {
+            extractor.extractTablesFromDelete((DeleteStatement) explainableStatement);

Review comment:
       > extractTablesFromSQLStatement
   
   @tristaZero This is a great suggestion. There are similar logic in other places. Extracting to TableExtractor will make the code clearer. 👍




-- 
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:
users@infra.apache.org