Skip to content

Commit

Permalink
Refactor ParserAssertHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
guangyun1013 committed Sep 4, 2017
1 parent d252040 commit ae716d6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,4 @@ public class SQLToken {

@XmlAttribute(name = "begin-position")
private int beginPosition;

@XmlAttribute(name = "support")
private Support support = Support.ALL;

public enum Support {
ALL, PREPARED_STATEMENT, STATEMENT
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.dangdang.ddframe.rdb.sharding.parsing.parser.expression.SQLNumberExpression;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.expression.SQLPlaceholderExpression;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.expression.SQLTextExpression;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.SQLToken.Support;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.Value;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.GeneratedKeyToken;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.ItemsToken;
Expand All @@ -33,7 +32,6 @@

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

public class ParserAssertHelper {
Expand Down Expand Up @@ -101,9 +99,12 @@ private static List<com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.SQLTok
final boolean isPreparedStatement) {
List<com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.SQLToken> result = new ArrayList<>(sqlTokens.size());
for (com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.SQLToken each : sqlTokens) {
if (isPreparedStatement && (Support.ALL.equals(each.getSupport()) || Support.PREPARED_STATEMENT.equals(each.getSupport()))) {
result.add(each);
} else if (!isPreparedStatement && (Support.ALL.equals(each.getSupport()) || Support.STATEMENT.equals(each.getSupport()))) {
if (isPreparedStatement) {
if (!(each instanceof com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.OffsetToken
|| each instanceof com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.RowCountToken)) {
result.add(each);
}
} else {
result.add(each);
}
}
Expand All @@ -112,31 +113,31 @@ private static List<com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.SQLTok

private static SQLToken buildExpectedSQLToken(final com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.SQLToken sqlToken, final boolean isPreparedStatement) {
if (sqlToken instanceof com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.TableToken) {
return new TableToken(sqlToken.getBeginPosition(), ((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.TableToken)sqlToken).getOriginalLiterals());
return new TableToken(sqlToken.getBeginPosition(), ((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.TableToken) sqlToken).getOriginalLiterals());
} else if (sqlToken instanceof com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.ItemsToken) {
ItemsToken itemsToken = new ItemsToken(sqlToken.getBeginPosition());
itemsToken.getItems().addAll(((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.ItemsToken)sqlToken).getItems());
itemsToken.getItems().addAll(((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.ItemsToken) sqlToken).getItems());
return itemsToken;
} else if (sqlToken instanceof com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.GeneratedKeyToken) {
if (isPreparedStatement) {
return new GeneratedKeyToken(((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.GeneratedKeyToken)sqlToken).getBeginPositionOfPreparedStatement());
return new GeneratedKeyToken(((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.GeneratedKeyToken) sqlToken).getBeginPositionOfPreparedStatement());
} else {
return new GeneratedKeyToken(((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.GeneratedKeyToken)sqlToken).getBeginPositionOfStatement());
return new GeneratedKeyToken(((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.GeneratedKeyToken) sqlToken).getBeginPositionOfStatement());
}
} else if (sqlToken instanceof com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.MultipleInsertValuesToken) {
MultipleInsertValuesToken multipleInsertValuesToken = new MultipleInsertValuesToken(sqlToken.getBeginPosition());
multipleInsertValuesToken.getValues().addAll(((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.MultipleInsertValuesToken)sqlToken).getValues());
multipleInsertValuesToken.getValues().addAll(((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.MultipleInsertValuesToken) sqlToken).getValues());
return multipleInsertValuesToken;
} else if (sqlToken instanceof com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.RowCountToken) {
return new RowCountToken(sqlToken.getBeginPosition(), ((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.RowCountToken)sqlToken).getRowCount());
return new RowCountToken(sqlToken.getBeginPosition(), ((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.RowCountToken) sqlToken).getRowCount());
} else if (sqlToken instanceof com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.OrderByToken) {
if (isPreparedStatement) {
return new OrderByToken(((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.OrderByToken)sqlToken).getBeginPositionOfPreparedStatement());
return new OrderByToken(((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.OrderByToken) sqlToken).getBeginPositionOfPreparedStatement());
} else {
return new OrderByToken(((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.OrderByToken)sqlToken).getBeginPositionOfStatement());
return new OrderByToken(((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.OrderByToken) sqlToken).getBeginPositionOfStatement());
}
} else if (sqlToken instanceof com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.OffsetToken) {
return new OffsetToken(sqlToken.getBeginPosition(), ((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.OffsetToken)sqlToken).getOffset());
return new OffsetToken(sqlToken.getBeginPosition(), ((com.dangdang.ddframe.rdb.sharding.parsing.parser.jaxb.OffsetToken) sqlToken).getOffset());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
<table-tokens>
<table-token begin-position="20" original-literals="t_order" />
</table-tokens>
<row-count-token row-count="5" begin-position="51" support="STATEMENT" />
<row-count-token row-count="5" begin-position="51" />
<order-by-token begin-position-of-statement="45" begin-position-of-prepared-statement="45" />
<group-by-columns>
<group-by-column name="user_id" order-by-type="ASC" />
Expand All @@ -229,7 +229,7 @@
<table-tokens>
<table-token begin-position="35" original-literals="t_order" />
</table-tokens>
<row-count-token row-count="5" begin-position="89" support="STATEMENT" />
<row-count-token row-count="5" begin-position="89" />
<aggregation-select-items>
<aggregation-select-item inner-expression="(order_id)" aggregation-type="SUM"/>
</aggregation-select-items>
Expand Down
24 changes: 12 additions & 12 deletions sharding-jdbc-core/src/test/resources/parser/select_pagination.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<item>i.item_id AS ORDER_BY_DERIVED_0 </item>
</items>
</items-token>
<offset-token begin-position="188" offset="5" support="STATEMENT" />
<offset-token begin-position="188" offset="5" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="0" literal="1" type="int" />
Expand Down Expand Up @@ -44,7 +44,7 @@
<item>i.item_id AS ORDER_BY_DERIVED_0 </item>
</items>
</items-token>
<row-count-token begin-position="187" row-count="5" support="STATEMENT" />
<row-count-token begin-position="187" row-count="5" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="0" literal="1" type="int" />
Expand All @@ -69,7 +69,7 @@
<table-token begin-position="167" original-literals="t_order" />
<table-token begin-position="182" original-literals="t_order_item" />
</table-tokens>
<row-count-token begin-position="27" row-count="3" support="STATEMENT" />
<row-count-token begin-position="27" row-count="3" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="1" literal="1" type="int" />
Expand Down Expand Up @@ -99,7 +99,7 @@
<item>i.item_id AS ORDER_BY_DERIVED_0 </item>
</items>
</items-token>
<row-count-token begin-position="363" row-count="3" support="STATEMENT" />
<row-count-token begin-position="363" row-count="3" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="0" literal="1" type="int" />
Expand Down Expand Up @@ -129,8 +129,8 @@
<item>i.item_id AS ORDER_BY_DERIVED_0 </item>
</items>
</items-token>
<offset-token begin-position="195" offset="5" support="STATEMENT" />
<row-count-token begin-position="198" row-count="3" support="STATEMENT" />
<offset-token begin-position="195" offset="5" />
<row-count-token begin-position="198" row-count="3" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="0" literal="1" type="int" />
Expand Down Expand Up @@ -160,8 +160,8 @@
<item>i.item_id AS ORDER_BY_DERIVED_0 </item>
</items>
</items-token>
<offset-token begin-position="188" offset="5" support="STATEMENT" />
<row-count-token begin-position="196" row-count="3" support="STATEMENT" />
<offset-token begin-position="188" offset="5" />
<row-count-token begin-position="196" row-count="3" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="0" literal="1" type="int" />
Expand All @@ -186,8 +186,8 @@
<table-token begin-position="167" original-literals="t_order" />
<table-token begin-position="182" original-literals="t_order_item" />
</table-tokens>
<offset-token begin-position="338" offset="6" support="STATEMENT" />
<row-count-token begin-position="27" row-count="3" support="STATEMENT" />
<offset-token begin-position="338" offset="6" />
<row-count-token begin-position="27" row-count="3" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="1" literal="1" type="int" />
Expand Down Expand Up @@ -217,8 +217,8 @@
<item>i.item_id AS ORDER_BY_DERIVED_0 </item>
</items>
</items-token>
<offset-token begin-position="386" offset="3" support="STATEMENT" />
<row-count-token begin-position="363" row-count="5" support="STATEMENT" />
<offset-token begin-position="386" offset="3" />
<row-count-token begin-position="363" row-count="5" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="0" literal="1" type="int" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<table-token begin-position="22" original-literals="`t_order`" />
<table-token begin-position="39" original-literals="`t_order_item`" />
</table-tokens>
<offset-token begin-position="220" offset="5" support="STATEMENT" />
<row-count-token begin-position="223" row-count="3" support="STATEMENT" />
<offset-token begin-position="220" offset="5" />
<row-count-token begin-position="223" row-count="3" />
<items-token begin-position="17">
<items>
<item>i.item_id AS ORDER_BY_DERIVED_0 </item>
Expand Down Expand Up @@ -44,8 +44,8 @@
<table-token begin-position="22" original-literals="`t_order`" />
<table-token begin-position="39" original-literals="`t_order_item`" />
</table-tokens>
<offset-token begin-position="220" offset="5" support="STATEMENT" />
<row-count-token begin-position="223" row-count="3" support="STATEMENT" />
<offset-token begin-position="220" offset="5" />
<row-count-token begin-position="223" row-count="3" />
<items-token begin-position="17">
<items>
<item>i.item_id AS ORDER_BY_DERIVED_0 </item>
Expand Down Expand Up @@ -78,8 +78,8 @@
<table-token begin-position="165" original-literals="t_order" />
<table-token begin-position="180" original-literals="t_order_item" />
</table-tokens>
<offset-token begin-position="355" offset="6" support="STATEMENT" />
<row-count-token begin-position="26" row-count="3" support="STATEMENT" />
<offset-token begin-position="355" offset="6" />
<row-count-token begin-position="26" row-count="3" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="1" literal="1" type="int" />
Expand Down Expand Up @@ -107,8 +107,8 @@
<table-token begin-position="167" original-literals="t_order" />
<table-token begin-position="182" original-literals="t_order_item" />
</table-tokens>
<offset-token begin-position="357" offset="6" support="STATEMENT" />
<row-count-token begin-position="27" row-count="3" support="STATEMENT" />
<offset-token begin-position="357" offset="6" />
<row-count-token begin-position="27" row-count="3" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="1" literal="1" type="int" />
Expand Down Expand Up @@ -141,8 +141,8 @@
<item>i.user_id AS GROUP_BY_DERIVED_0 </item>
</items>
</items-token>
<offset-token begin-position="356" offset="6" support="STATEMENT" />
<row-count-token begin-position="26" row-count="3" support="STATEMENT" />
<offset-token begin-position="356" offset="6" />
<row-count-token begin-position="26" row-count="3" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="1" literal="1" type="int" />
Expand Down Expand Up @@ -175,8 +175,8 @@
<item>i.user_id AS GROUP_BY_DERIVED_0 </item>
</items>
</items-token>
<offset-token begin-position="358" offset="6" support="STATEMENT" />
<row-count-token begin-position="27" row-count="3" support="STATEMENT" />
<offset-token begin-position="358" offset="6" />
<row-count-token begin-position="27" row-count="3" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="1" literal="1" type="int" />
Expand Down Expand Up @@ -210,8 +210,8 @@
<item>i.item_id AS GROUP_BY_DERIVED_0 </item>
</items>
</items-token>
<offset-token begin-position="405" offset="3" support="STATEMENT" />
<row-count-token begin-position="382" row-count="5" support="STATEMENT" />
<offset-token begin-position="405" offset="3" />
<row-count-token begin-position="382" row-count="5" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="0" literal="1" type="int" />
Expand Down Expand Up @@ -245,8 +245,8 @@
<item>i.user_id AS GROUP_BY_DERIVED_0 </item>
</items>
</items-token>
<offset-token begin-position="405" offset="3" support="STATEMENT" />
<row-count-token begin-position="382" row-count="5" support="STATEMENT" />
<offset-token begin-position="405" offset="3" />
<row-count-token begin-position="382" row-count="5" />
<conditions>
<condition column-name="user_id" table-name="t_order" operator="IN">
<value index="0" literal="1" type="int" />
Expand Down

0 comments on commit ae716d6

Please sign in to comment.