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



##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/dal/ExplainStatementHandler.java
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.dal;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.ExplainStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.handler.SQLStatementHandler;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.MySQLStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLExplainStatement;
+
+import java.util.Optional;
+
+/**
+ * Explain statement handler for different dialect SQL statements.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ExplainStatementHandler implements SQLStatementHandler {
+    
+    /**
+     * Get simple table segment.
+     *
+     * @param explainStatement explain statement
+     * @return simple table segment
+     */
+    public static Optional<SimpleTableSegment> getSimpleTableSegment(final 
ExplainStatement explainStatement) {
+        if (explainStatement instanceof MySQLStatement) {
+            return ((MySQLExplainStatement) explainStatement).getTable();
+        }
+        return Optional.empty();

Review comment:
       So `PostgreSQLExplainStatement` is to-do one?

##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/dal/ExplainStatementTestCase.java
##########
@@ -22,5 +22,5 @@
 /**
  * Describe statement test case.
  */
-public final class DescribeStatementTestCase extends SQLParserTestCase {
+public final class ExplainStatementTestCase extends SQLParserTestCase {

Review comment:
       We do not have tests to assert its `SQLStatement` field currently, which 
is possible to be some community issues for our volunteers.

##########
File path: 
shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dal/MySQLExplainStatement.java
##########
@@ -17,21 +17,29 @@
 
 package org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal;
 
-import lombok.Getter;
 import lombok.Setter;
 import lombok.ToString;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
-import 
org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;
-import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.ExplainStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.MySQLStatement;
 
+import java.util.Optional;
+
 /**
- * MySQL describe statement.
+ * MySQL explain statement.
  */
-@Getter
 @Setter
 @ToString
-public final class MySQLDescribeStatement extends AbstractSQLStatement 
implements DALStatement, MySQLStatement {
+public final class MySQLExplainStatement extends ExplainStatement implements 
MySQLStatement {
     
     private SimpleTableSegment table;

Review comment:
       This field will be removed `SQLStatement` later on, won't it?

##########
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:
       Maybe `TableExtractor` could provide a `extractTablesFromDML` or 
`extractTablesFromSQLStatement` function to hide these calls.




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