You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/05/25 04:45:48 UTC

[shardingsphere] branch master updated: Add assertion implementation for ExplainStatement (#10438)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 64e5dc9  Add assertion implementation for ExplainStatement (#10438)
64e5dc9 is described below

commit 64e5dc99de3b28da70bb4e508ad00cd4a1224ea8
Author: totalo <48...@qq.com>
AuthorDate: Tue May 25 12:45:11 2021 +0800

    Add assertion implementation for ExplainStatement (#10438)
    
    * explain case
    
    * explain case
    
    * explain
    
    * Add assertion implementation for ExplainStatement
    
    * checkstyle
    
    * fix ci error
---
 .../statement/dal/impl/ExplainStatementAssert.java | 19 +++++
 .../statement/dal/ExplainStatementTestCase.java    | 23 ++++++
 .../src/main/resources/case/dal/explain.xml        | 91 ++++++++++++++++++++++
 .../main/resources/sql/supported/dal/explain.xml   | 24 ++++++
 4 files changed, 157 insertions(+)

diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dal/impl/ExplainStatementAssert.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dal/impl/ExplainStatementAssert.java
index 4cc40fc..e09abd3 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dal/impl/ExplainStatementAssert.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dal/impl/ExplainStatementAssert.java
@@ -21,8 +21,12 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.ExplainStatement;
 import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
+import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.SQLStatementAssert;
 import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.dal.ExplainStatementTestCase;
 
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
 /**
  * Explain statement assert.
  */
@@ -37,5 +41,20 @@ public final class ExplainStatementAssert {
      * @param expected expected explain statement test case
      */
     public static void assertIs(final SQLCaseAssertContext assertContext, final ExplainStatement actual, final ExplainStatementTestCase expected) {
+        if (null != expected.getSelectClause()) {
+            assertTrue(assertContext.getText("Actual statement should exist."), actual.getStatement().isPresent());
+            SQLStatementAssert.assertIs(assertContext, actual.getStatement().get(), expected.getSelectClause());
+        } else if (null != expected.getUpdateClause()) {
+            assertTrue(assertContext.getText("Actual statement should exist."), actual.getStatement().isPresent());
+            SQLStatementAssert.assertIs(assertContext, actual.getStatement().get(), expected.getUpdateClause());
+        } else if (null != expected.getInsertClause()) {
+            assertTrue(assertContext.getText("Actual statement should exist."), actual.getStatement().isPresent());
+            SQLStatementAssert.assertIs(assertContext, actual.getStatement().get(), expected.getInsertClause());
+        } else if (null != expected.getDeleteClause()) {
+            assertTrue(assertContext.getText("Actual statement should exist."), actual.getStatement().isPresent());
+            SQLStatementAssert.assertIs(assertContext, actual.getStatement().get(), expected.getDeleteClause());
+        } else {
+            assertFalse(assertContext.getText("Actual statement should not exist."), actual.getStatement().isPresent());
+        }
     }
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/dal/ExplainStatementTestCase.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/dal/ExplainStatementTestCase.java
index c6268e5..63571d1 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/dal/ExplainStatementTestCase.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/dal/ExplainStatementTestCase.java
@@ -17,10 +17,33 @@
 
 package org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.dal;
 
+import lombok.Getter;
+import lombok.Setter;
 import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.SQLParserTestCase;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.dml.DeleteStatementTestCase;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.dml.InsertStatementTestCase;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.dml.SelectStatementTestCase;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.dml.UpdateStatementTestCase;
+
+import javax.xml.bind.annotation.XmlElement;
 
 /**
  * Describe statement test case.
  */
+@Getter
+@Setter
 public final class ExplainStatementTestCase extends SQLParserTestCase {
+
+    @XmlElement(name = "select")
+    private SelectStatementTestCase selectClause;
+
+    @XmlElement(name = "insert")
+    private InsertStatementTestCase insertClause;
+
+    @XmlElement(name = "update")
+    private UpdateStatementTestCase updateClause;
+
+    @XmlElement(name = "delete")
+    private DeleteStatementTestCase deleteClause;
+
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/dal/explain.xml b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/dal/explain.xml
new file mode 100644
index 0000000..d0cde0f
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/dal/explain.xml
@@ -0,0 +1,91 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<sql-parser-test-cases>
+    <describe sql-case-id="explain_select_constant_without_table">
+        <select>
+            <projections start-index="15" stop-index="20">
+                <expression-projection text="1" alias="a" start-index="15" stop-index="20" />
+            </projections>
+        </select>
+    </describe>
+
+    <describe sql-case-id="explain_update_without_condition">
+        <update>
+            <table start-index="15" stop-index="21">
+                <simple-table name="t_order" start-index="15" stop-index="21"/>
+            </table>
+            <set start-index="23" stop-index="45">
+                <assignment start-index="23" stop-index="45">
+                    <column name="status" start-index="27" stop-index="32" />
+                    <assignment-value>
+                        <literal-expression value="finished" start-index="36" stop-index="45" />
+                    </assignment-value>
+                </assignment>
+            </set>
+        </update>
+    </describe>
+
+    <describe sql-case-id="explain_insert_without_parameters">
+        <insert>
+            <table name="t_order" start-index="20" stop-index="26" />
+            <columns start-index="28" stop-index="54">
+                <column name="order_id" start-index="29" stop-index="36" />
+                <column name="user_id" start-index="39" stop-index="45" />
+                <column name="status" start-index="48" stop-index="53" />
+            </columns>
+            <values>
+                <value>
+                    <assignment-value>
+                        <parameter-marker-expression value="0" />
+                        <literal-expression value="1" start-index="64" stop-index="64" />
+                    </assignment-value>
+                    <assignment-value>
+                        <parameter-marker-expression value="1" />
+                        <literal-expression value="1" start-index="67" stop-index="67" />
+                    </assignment-value>
+                    <assignment-value>
+                        <parameter-marker-expression value="2" />
+                        <literal-expression value="insert" start-index="70" stop-index="77" />
+                    </assignment-value>
+                </value>
+            </values>
+        </insert>
+    </describe>
+
+    <describe sql-case-id="explain_delete_without_sharding_value">
+        <delete>
+            <table name="t_order" start-index="20" stop-index="26" />
+            <where start-index="28" stop-index="41" literal-stop-index="46">
+                <expr>
+                    <binary-operation-expression start-index="34" stop-index="41" literal-stop-index="46">
+                        <left>
+                            <column name="status" start-index="34" stop-index="39" />
+                        </left>
+                        <operator>=</operator>
+                        <right>
+                            <literal-expression value="init" start-index="41" stop-index="46" />
+                            <parameter-marker-expression value="0" start-index="41" stop-index="41" />
+                        </right>
+                    </binary-operation-expression>
+                </expr>
+            </where>
+        </delete>
+    </describe>
+
+</sql-parser-test-cases>
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/dal/explain.xml b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/dal/explain.xml
new file mode 100644
index 0000000..2530e16
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/dal/explain.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<sql-cases>
+    <sql-case id="explain_select_constant_without_table" value="EXPLAIN SELECT 1 as a" db-types="PostgreSQL, MySQL" />
+    <sql-case id="explain_update_without_condition" value="EXPLAIN UPDATE t_order SET status = 'finished'" db-types="PostgreSQL, MySQL" />
+    <sql-case id="explain_insert_without_parameters" value="EXPLAIN INSERT INTO t_order (order_id, user_id, status) VALUES (1, 1, 'insert')" db-types="PostgreSQL, MySQL" />
+    <sql-case id="explain_delete_without_sharding_value" value="EXPLAIN DELETE FROM t_order WHERE status='init'" db-types="PostgreSQL, MySQL" />
+</sql-cases>