Skip to content

Commit

Permalink
Add where assert for update and delete statement (#4085)
Browse files Browse the repository at this point in the history
* remove table check in public assert

* add where assert for delete and update

* fix table assert for alter table

* fix test case for delete_with_alias

* fix test case for delete_with_sharding_value

* fix test case for delete_with_special_character_without_sharding_value

* fix test case for delete_with_special_comments_return_without_sharding_value

* fix test case for delete_with_special_comments_returning_without_sharding_value

* fix test case for delete_without_sharding_value

* fix test case for update_equal_with_geography

* fix test case for update_with_alias

* fix test case for update_with_extra_keywords

* fix test case for update_with_or

* fix test case for update_with_special_character

* fix test case for update_with_special_comments

* fix test case for update_without_alias

* fix test case for update_without_parameters

* fix test case for update_with_special_character

* remove zero parameter-marker-start-index
  • Loading branch information
terrymanu authored and SteNicholas committed Jan 25, 2020
1 parent e0d709c commit 089eef6
Show file tree
Hide file tree
Showing 15 changed files with 382 additions and 90 deletions.
Expand Up @@ -21,15 +21,13 @@
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sql.parser.integrate.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.sql.parser.integrate.asserts.segment.parameter.ParameterMarkerAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.segment.table.TableAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.impl.AlterTableStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.impl.DeleteStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.impl.InsertStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.impl.SelectStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.impl.TCLStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.impl.UpdateStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.root.ParserResult;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.ddl.AlterTableStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.DeleteStatement;
Expand All @@ -55,8 +53,6 @@ public final class SQLStatementAssert {
*/
public static void assertIs(final SQLCaseAssertContext assertContext, final SQLStatement actual, final ParserResult expected) {
ParameterMarkerAssert.assertCount(assertContext, actual.getParametersCount(), expected.getParameters().size());
// TODO to be move TableAssert into statement details assert
TableAssert.assertIs(assertContext, actual.findSQLSegments(TableSegment.class), expected.getTables());
if (actual instanceof SelectStatement) {
SelectStatementAssert.assertIs(assertContext, (SelectStatement) actual, expected);
} else if (actual instanceof UpdateStatement) {
Expand Down
Expand Up @@ -21,7 +21,9 @@
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sql.parser.integrate.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.sql.parser.integrate.asserts.segment.table.AlterTableAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.segment.table.TableAssert;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.root.ParserResult;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
import org.apache.shardingsphere.sql.parser.sql.statement.ddl.AlterTableStatement;

/**
Expand All @@ -40,6 +42,7 @@ public final class AlterTableStatementAssert {
* @param expected expected parser result
*/
public static void assertIs(final SQLCaseAssertContext assertContext, final AlterTableStatement actual, final ParserResult expected) {
TableAssert.assertIs(assertContext, actual.findSQLSegments(TableSegment.class), expected.getTables());
if (null != expected.getAlterTable()) {
AlterTableAssert.assertIs(assertContext, actual, expected.getAlterTable());
}
Expand Down
Expand Up @@ -20,10 +20,14 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sql.parser.integrate.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.sql.parser.integrate.asserts.segment.predicate.WhereAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.segment.table.TableAssert;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.root.ParserResult;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.DeleteStatement;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Delete statement assert.
*
Expand All @@ -41,9 +45,19 @@ public final class DeleteStatementAssert {
*/
public static void assertIs(final SQLCaseAssertContext assertContext, final DeleteStatement actual, final ParserResult expected) {
assertTable(assertContext, actual, expected);
assertWhere(assertContext, actual, expected);
}

private static void assertTable(final SQLCaseAssertContext assertContext, final DeleteStatement actual, final ParserResult expected) {
TableAssert.assertIs(assertContext, actual.getTables(), expected.getTables());
}

private static void assertWhere(final SQLCaseAssertContext assertContext, final DeleteStatement actual, final ParserResult expected) {
if (null != expected.getWhere()) {
assertTrue(assertContext.getText("Actual where segment should exist."), actual.getWhere().isPresent());
WhereAssert.assertIs(assertContext, actual.getWhere().get(), expected.getWhere());
} else {
assertFalse(assertContext.getText("Actual where segment should not exist."), actual.getWhere().isPresent());
}
}
}
Expand Up @@ -20,10 +20,14 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sql.parser.integrate.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.sql.parser.integrate.asserts.segment.predicate.WhereAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.segment.table.TableAssert;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.root.ParserResult;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.UpdateStatement;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Update statement assert.
*
Expand All @@ -41,9 +45,19 @@ public final class UpdateStatementAssert {
*/
public static void assertIs(final SQLCaseAssertContext assertContext, final UpdateStatement actual, final ParserResult expected) {
assertTable(assertContext, actual, expected);
assertWhere(assertContext, actual, expected);
}

private static void assertTable(final SQLCaseAssertContext assertContext, final UpdateStatement actual, final ParserResult expected) {
TableAssert.assertIs(assertContext, actual.getTables(), expected.getTables());
}

private static void assertWhere(final SQLCaseAssertContext assertContext, final UpdateStatement actual, final ParserResult expected) {
if (null != expected.getWhere()) {
assertTrue(assertContext.getText("Actual where segment should exist."), actual.getWhere().isPresent());
WhereAssert.assertIs(assertContext, actual.getWhere().get(), expected.getWhere());
} else {
assertFalse(assertContext.getText("Actual where segment should not exist."), actual.getWhere().isPresent());
}
}
}
Expand Up @@ -21,36 +21,121 @@
<tables>
<table name="t_order" start-index="12" stop-index="18" />
</tables>
<where parameters-count="3" start-index="20" stop-index="66" literal-stop-index="77">
<and-predicate>
<predicate start-index="26" stop-index="37" literal-stop-index="40">
<column-left-value name="order_id" start-index="26" stop-index="33" />
<operator type="=" />
<compare-right-value>
<parameter-marker-expression value="0" />
<literal-expression value="1000" />
</compare-right-value>
</predicate>
<predicate start-index="43" stop-index="53" literal-start-index="46" literal-stop-index="59">
<column-left-value name="user_id" start-index="43" stop-index="49" literal-start-index="46" literal-stop-index="52" />
<operator type="=" />
<compare-right-value>
<parameter-marker-expression value="1" />
<literal-expression value="1001" />
</compare-right-value>
</predicate>
<predicate start-index="59" stop-index="66" literal-start-index="65" literal-stop-index="77">
<column-left-value name="status" start-index="59" stop-index="64" literal-start-index="65" literal-stop-index="70" />
<operator type="=" />
<compare-right-value>
<parameter-marker-expression value="2" />
<literal-expression value="init" />
</compare-right-value>
</predicate>
</and-predicate>
</where>
</parser-result>

<parser-result sql-case-id="delete_without_sharding_value" parameters="'init'">
<tables>
<table name="t_order" start-index="12" stop-index="18" />
</tables>
<where parameters-count="1" start-index="20" stop-index="33" literal-stop-index="38">
<and-predicate>
<predicate start-index="26" stop-index="33" literal-stop-index="38">
<column-left-value name="status" start-index="26" stop-index="31" />
<operator type="=" />
<compare-right-value>
<parameter-marker-expression value="0" />
<literal-expression value="init" />
</compare-right-value>
</predicate>
</and-predicate>
</where>
</parser-result>

<parser-result sql-case-id="delete_with_special_character_without_sharding_value">
<tables>
<table name="t_order" start-delimiter="`" end-delimiter="`" start-index="12" stop-index="20" />
</tables>
<where parameters-count="0" start-index="22" stop-index="42">
<and-predicate>
<predicate start-index="28" stop-index="42">
<column-left-value name="status" start-delimiter="`" end-delimiter="`" start-index="28" stop-index="35" />
<operator type="=" />
<compare-right-value>
<literal-expression value="init" />
</compare-right-value>
</predicate>
</and-predicate>
</where>
</parser-result>

<parser-result sql-case-id="delete_with_special_comments_return_without_sharding_value">
<tables>
<table name="t_order" start-index="33" stop-index="39" />
</tables>
<where parameters-count="0" start-index="41" stop-index="54">
<and-predicate>
<predicate start-index="47" stop-index="54">
<column-left-value name="status" start-index="47" stop-index="52" />
<operator type="=" />
<compare-right-value>
<literal-expression value="1" />
</compare-right-value>
</predicate>
</and-predicate>
</where>
</parser-result>

<parser-result sql-case-id="delete_with_special_comments_returning_without_sharding_value">
<tables>
<table name="t_order" start-index="34" stop-index="40" />
</tables>
<where parameters-count="0" start-index="43" stop-index="56">
<and-predicate>
<predicate start-index="49" stop-index="56">
<column-left-value name="status" start-index="49" stop-index="54" />
<operator type="=" />
<compare-right-value>
<literal-expression value="1" />
</compare-right-value>
</predicate>
</and-predicate>
</where>
</parser-result>

<parser-result sql-case-id="delete_with_alias" parameters="'init'">
<tables>
<table name="o" start-index="7" stop-index="7" />
<table name="t_order" alias="o" start-index="14" stop-index="20" />
</tables>
<where parameters-count="1" start-index="27" stop-index="40" literal-stop-index="46">
<and-predicate>
<predicate start-index="33" stop-index="40" literal-stop-index="45">
<column-left-value name="status" start-index="33" stop-index="38" />
<operator type="=" />
<compare-right-value>
<parameter-marker-expression value="0" />
<literal-expression value="init" />
</compare-right-value>
</predicate>
</and-predicate>
</where>
</parser-result>
</parser-result-sets>

0 comments on commit 089eef6

Please sign in to comment.