diff --git a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/engine/encrypt/EncryptInsertOptimizeEngineTest.java b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/engine/encrypt/EncryptInsertOptimizeEngineTest.java index 59eb701cf8f7e..dadbd9927b5c5 100644 --- a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/engine/encrypt/EncryptInsertOptimizeEngineTest.java +++ b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/engine/encrypt/EncryptInsertOptimizeEngineTest.java @@ -84,8 +84,8 @@ public void assertInsertStatementWithValuesWithPlaceHolderWithEncrypt() { private InsertStatement createInsertStatementWithValuesWithPlaceHolderWithEncrypt() { InsertStatement result = new InsertStatement(); result.getTables().add(new Table("t_encrypt", null)); - result.addSQLToken(new TableToken(12, "t_encrypt", QuoteCharacter.NONE, 0)); - result.addSQLToken(new InsertValuesToken(34)); + result.addSQLToken(new TableToken(12, 20, "t_encrypt", QuoteCharacter.NONE)); + result.addSQLToken(new InsertValuesToken(21, 46)); result.getColumnNames().add("col1"); result.getColumnNames().add("col2"); result.getValues().add(new InsertValue(Arrays.asList(new SQLParameterMarkerExpression(0), new SQLParameterMarkerExpression(1)))); @@ -112,8 +112,8 @@ public void assertInsertStatementWithValuesWithoutPlaceHolderWithQueryEncrypt() private InsertStatement createInsertStatementWithValuesWithoutPlaceHolderWithQueryEncrypt() { InsertStatement result = new InsertStatement(); result.getTables().add(new Table("t_query_encrypt", null)); - result.addSQLToken(new TableToken(12, "t_query_encrypt", QuoteCharacter.NONE, 0)); - result.addSQLToken(new InsertValuesToken(40)); + result.addSQLToken(new TableToken(12, 27, "t_query_encrypt", QuoteCharacter.NONE)); + result.addSQLToken(new InsertValuesToken(27, 52)); result.getColumnNames().add("col1"); result.getColumnNames().add("col2"); result.getValues().add(new InsertValue(Arrays.asList(new SQLNumberExpression(1), new SQLNumberExpression(2)))); @@ -138,8 +138,8 @@ public void assertInsertStatementWithSetWithoutPlaceHolderWithEncrypt() { private InsertStatement createInsertStatementWithSetWithoutPlaceHolderWithEncrypt() { InsertStatement result = new InsertStatement(); result.getTables().add(new Table("t_encrypt", null)); - result.addSQLToken(new TableToken(12, "t_encrypt", QuoteCharacter.NONE, 0)); - result.addSQLToken(new InsertSetToken(34)); + result.addSQLToken(new TableToken(12, 20, "t_encrypt", QuoteCharacter.NONE)); + result.addSQLToken(new InsertSetToken(12, 20)); result.getColumnNames().add("col1"); result.getColumnNames().add("col2"); result.getValues().add(new InsertValue(Arrays.asList(new SQLNumberExpression(1), new SQLNumberExpression(2)))); @@ -166,8 +166,8 @@ public void assertInsertStatementWithSetWithPlaceHolderWithQueryEncrypt() { private InsertStatement createInsertStatementWithSetWithPlaceHolderWithQueryEncrypt() { InsertStatement result = new InsertStatement(); result.getTables().add(new Table("t_query_encrypt", null)); - result.addSQLToken(new TableToken(12, "t_query_encrypt", QuoteCharacter.NONE, 0)); - result.addSQLToken(new InsertSetToken(40)); + result.addSQLToken(new TableToken(12, 26, "t_query_encrypt", QuoteCharacter.NONE)); + result.addSQLToken(new InsertSetToken(12, 27)); result.getColumnNames().add("col1"); result.getColumnNames().add("col2"); result.getValues().add(new InsertValue(Arrays.asList(new SQLParameterMarkerExpression(0), new SQLParameterMarkerExpression(1)))); diff --git a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/engine/sharding/InsertOptimizeEngineTest.java b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/engine/sharding/InsertOptimizeEngineTest.java index 65360d75b7b4c..a6102a204e5ff 100644 --- a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/engine/sharding/InsertOptimizeEngineTest.java +++ b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/engine/sharding/InsertOptimizeEngineTest.java @@ -118,8 +118,8 @@ private void initializeInsertValuesWithPlaceHolder() { insertValuesStatementWithPlaceHolder = new InsertStatement(); insertValuesStatementWithPlaceHolder.getTables().add(new Table("t_order", null)); insertValuesStatementWithPlaceHolder.setParametersIndex(4); - insertValuesStatementWithPlaceHolder.addSQLToken(new TableToken(12, "t_order", QuoteCharacter.NONE, 0)); - insertValuesStatementWithPlaceHolder.addSQLToken(new InsertValuesToken(39)); + insertValuesStatementWithPlaceHolder.addSQLToken(new TableToken(12, 18, "t_order", QuoteCharacter.NONE)); + insertValuesStatementWithPlaceHolder.addSQLToken(new InsertValuesToken(21, 46)); AndCondition andCondition1 = new AndCondition(); andCondition1.getConditions().add(new Condition(new Column("user_id", "t_order"), new SQLParameterMarkerExpression(0))); insertValuesStatementWithPlaceHolder.getRouteConditions().getOrCondition().getAndConditions().add(andCondition1); @@ -137,8 +137,8 @@ private void initializeInsertValuesWithPlaceHolderWithEncrypt() { insertValuesStatementWithPlaceHolderWithEncrypt = new InsertStatement(); insertValuesStatementWithPlaceHolderWithEncrypt.getTables().add(new Table("t_encrypt", null)); insertValuesStatementWithPlaceHolderWithEncrypt.setParametersIndex(4); - insertValuesStatementWithPlaceHolderWithEncrypt.addSQLToken(new TableToken(12, "t_encrypt", QuoteCharacter.NONE, 0)); - insertValuesStatementWithPlaceHolderWithEncrypt.addSQLToken(new InsertValuesToken(39)); + insertValuesStatementWithPlaceHolderWithEncrypt.addSQLToken(new TableToken(12, 20, "t_encrypt", QuoteCharacter.NONE)); + insertValuesStatementWithPlaceHolderWithEncrypt.addSQLToken(new InsertValuesToken(21, 46)); AndCondition andCondition1 = new AndCondition(); andCondition1.getConditions().add(new Condition(new Column("user_id", "t_encrypt"), new SQLParameterMarkerExpression(0))); insertValuesStatementWithPlaceHolderWithEncrypt.getRouteConditions().getOrCondition().getAndConditions().add(andCondition1); @@ -156,8 +156,8 @@ private void initializeInsertValuesWithoutPlaceHolder() { insertValuesStatementWithoutPlaceHolder = new InsertStatement(); insertValuesStatementWithoutPlaceHolder.getTables().add(new Table("t_order", null)); insertValuesStatementWithoutPlaceHolder.setParametersIndex(0); - insertValuesStatementWithoutPlaceHolder.addSQLToken(new TableToken(12, "t_order", QuoteCharacter.NONE, 0)); - insertValuesStatementWithoutPlaceHolder.addSQLToken(new InsertValuesToken(42)); + insertValuesStatementWithoutPlaceHolder.addSQLToken(new TableToken(12, 20, "t_order", QuoteCharacter.NONE)); + insertValuesStatementWithoutPlaceHolder.addSQLToken(new InsertValuesToken(27, 52)); ItemsToken itemsToken = new ItemsToken(34); itemsToken.getItems().add("order_id"); insertValuesStatementWithoutPlaceHolder.addSQLToken(itemsToken); @@ -170,8 +170,8 @@ private void initializeInsertValuesWithoutPlaceHolderWithQueryEncrypt() { insertValuesStatementWithoutPlaceHolderWithQueryEncrypt = new InsertStatement(); insertValuesStatementWithoutPlaceHolderWithQueryEncrypt.getTables().add(new Table("t_encrypt_query", null)); insertValuesStatementWithoutPlaceHolderWithQueryEncrypt.setParametersIndex(0); - insertValuesStatementWithoutPlaceHolderWithQueryEncrypt.addSQLToken(new TableToken(12, "t_encrypt_query", QuoteCharacter.NONE, 0)); - insertValuesStatementWithoutPlaceHolderWithQueryEncrypt.addSQLToken(new InsertValuesToken(42)); + insertValuesStatementWithoutPlaceHolderWithQueryEncrypt.addSQLToken(new TableToken(12, 26, "t_encrypt_query", QuoteCharacter.NONE)); + insertValuesStatementWithoutPlaceHolderWithQueryEncrypt.addSQLToken(new InsertValuesToken(27, 52)); ItemsToken itemsToken = new ItemsToken(34); itemsToken.getItems().add("order_id"); insertValuesStatementWithoutPlaceHolderWithQueryEncrypt.addSQLToken(itemsToken); @@ -184,8 +184,8 @@ private void initializeInsertSetWithPlaceHolder() { insertSetStatementWithPlaceHolder = new InsertStatement(); insertSetStatementWithPlaceHolder.getTables().add(new Table("t_order", null)); insertSetStatementWithPlaceHolder.setParametersIndex(0); - insertSetStatementWithPlaceHolder.addSQLToken(new TableToken(12, "t_order", QuoteCharacter.NONE, 0)); - insertSetStatementWithPlaceHolder.addSQLToken(new InsertSetToken(24)); + insertSetStatementWithPlaceHolder.addSQLToken(new TableToken(12, 18, "t_order", QuoteCharacter.NONE)); + insertSetStatementWithPlaceHolder.addSQLToken(new InsertSetToken(12, 20)); insertSetStatementWithPlaceHolder.getColumnNames().add("user_id"); insertSetStatementWithPlaceHolder.getColumnNames().add("status"); AndCondition andCondition = new AndCondition(); @@ -197,8 +197,8 @@ private void initializeInsertSetWithPlaceHolderWithQueryEncrypt() { insertSetStatementWithPlaceHolderWithQueryEncrypt = new InsertStatement(); insertSetStatementWithPlaceHolderWithQueryEncrypt.getTables().add(new Table("t_encrypt_query", null)); insertSetStatementWithPlaceHolderWithQueryEncrypt.setParametersIndex(0); - insertSetStatementWithPlaceHolderWithQueryEncrypt.addSQLToken(new TableToken(12, "t_encrypt_query", QuoteCharacter.NONE, 0)); - insertSetStatementWithPlaceHolderWithQueryEncrypt.addSQLToken(new InsertSetToken(24)); + insertSetStatementWithPlaceHolderWithQueryEncrypt.addSQLToken(new TableToken(12, 26, "t_encrypt_query", QuoteCharacter.NONE)); + insertSetStatementWithPlaceHolderWithQueryEncrypt.addSQLToken(new InsertSetToken(12, 20)); insertSetStatementWithPlaceHolderWithQueryEncrypt.getColumnNames().add("user_id"); insertSetStatementWithPlaceHolderWithQueryEncrypt.getColumnNames().add("status"); AndCondition andCondition = new AndCondition(); @@ -210,8 +210,8 @@ private void initializeInsertSetWithoutPlaceHolder() { insertSetStatementWithoutPlaceHolder = new InsertStatement(); insertSetStatementWithoutPlaceHolder.getTables().add(new Table("t_order", null)); insertSetStatementWithoutPlaceHolder.setParametersIndex(0); - insertSetStatementWithoutPlaceHolder.addSQLToken(new TableToken(12, "t_order", QuoteCharacter.NONE, 0)); - insertSetStatementWithoutPlaceHolder.addSQLToken(new InsertSetToken(24)); + insertSetStatementWithoutPlaceHolder.addSQLToken(new TableToken(12, 18, "t_order", QuoteCharacter.NONE)); + insertSetStatementWithoutPlaceHolder.addSQLToken(new InsertSetToken(12, 20)); insertSetStatementWithoutPlaceHolder.getColumnNames().add("user_id"); insertSetStatementWithoutPlaceHolder.getColumnNames().add("status"); AndCondition andCondition = new AndCondition(); @@ -223,8 +223,8 @@ private void initializeInsertSetWithoutPlaceHolderWithEncrypt() { insertSetStatementWithoutPlaceHolderWithEncrypt = new InsertStatement(); insertSetStatementWithoutPlaceHolderWithEncrypt.getTables().add(new Table("t_encrypt", null)); insertSetStatementWithoutPlaceHolderWithEncrypt.setParametersIndex(0); - insertSetStatementWithoutPlaceHolderWithEncrypt.addSQLToken(new TableToken(12, "t_encrypt", QuoteCharacter.NONE, 0)); - insertSetStatementWithoutPlaceHolderWithEncrypt.addSQLToken(new InsertSetToken(24)); + insertSetStatementWithoutPlaceHolderWithEncrypt.addSQLToken(new TableToken(12, 20, "t_encrypt", QuoteCharacter.NONE)); + insertSetStatementWithoutPlaceHolderWithEncrypt.addSQLToken(new InsertSetToken(12, 20)); insertSetStatementWithoutPlaceHolderWithEncrypt.getColumnNames().add("user_id"); insertSetStatementWithoutPlaceHolderWithEncrypt.getColumnNames().add("status"); AndCondition andCondition = new AndCondition(); diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/common/table/TableExtractor.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/common/table/TableExtractor.java index 37b02d1cafd7e..045099656fb9c 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/common/table/TableExtractor.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/common/table/TableExtractor.java @@ -54,7 +54,7 @@ public Optional extract(final ParserRuleContext ancestorNode, fina tableName = nodeText; owner = Optional.absent(); } - TableSegment result = new TableSegment(tableNameNode.get().getStart().getStartIndex(), tableName); + TableSegment result = new TableSegment(tableNameNode.get().getStart().getStartIndex(), tableNameNode.get().getStart().getStopIndex(), tableName); if (owner.isPresent()) { result.setOwner(owner.get()); } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/dml/select/LimitExtractor.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/dml/select/LimitExtractor.java index 0565b7111585f..f52f475caa02b 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/dml/select/LimitExtractor.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/dml/select/LimitExtractor.java @@ -35,6 +35,7 @@ * Limit extractor. * * @author duhongjun + * @author panjuan */ public final class LimitExtractor implements OptionalSQLSegmentExtractor { @@ -53,8 +54,12 @@ public Optional extract(final ParserRuleContext ancestorNode, fina } private LimitValueSegment createLimitValueSegment(final Map placeholderAndNodeIndexMap, final ParserRuleContext limitValueNode) { - return Symbol.QUESTION.getLiterals().equals(limitValueNode.getText()) - ? new PlaceholderLimitValueSegment(placeholderAndNodeIndexMap.get(limitValueNode.getChild(0)), ((ParserRuleContext) limitValueNode.getChild(0)).getStart().getStartIndex()) - : new LiteralLimitValueSegment(NumberUtil.getExactlyNumber(limitValueNode.getText(), 10).intValue(), limitValueNode.getStart().getStartIndex()); + if (Symbol.QUESTION.getLiterals().equals(limitValueNode.getText())) { + ParserRuleContext placeholderLimitValueNode = (ParserRuleContext) limitValueNode.getChild(0); + return new PlaceholderLimitValueSegment(placeholderAndNodeIndexMap.get(placeholderLimitValueNode), + placeholderLimitValueNode.getStart().getStartIndex(), placeholderLimitValueNode.getStart().getStopIndex()); + + } + return new LiteralLimitValueSegment(NumberUtil.getExactlyNumber(limitValueNode.getText(), 10).intValue(), limitValueNode.getStart().getStartIndex(), limitValueNode.getStart().getStopIndex()); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/common/dml/InsertColumnsFiller.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/common/dml/InsertColumnsFiller.java index 4ac3d787cec1a..1db64460b66e0 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/common/dml/InsertColumnsFiller.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/common/dml/InsertColumnsFiller.java @@ -46,7 +46,7 @@ public void fill(final InsertColumnsSegment sqlSegment, final SQLStatement sqlSt } else { fillFromSQL(sqlSegment, insertStatement); } - insertStatement.getSQLTokens().add(new InsertValuesToken(sqlSegment.getStartIndex())); + insertStatement.getSQLTokens().add(new InsertValuesToken(sqlSegment.getStartIndex(), sqlStatement.getLogicSQL().length() - 1)); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/common/dml/LimitFiller.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/common/dml/LimitFiller.java index 57bb95a983ad5..4d56820397c28 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/common/dml/LimitFiller.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/common/dml/LimitFiller.java @@ -50,7 +50,7 @@ private void setOffset(final LimitValueSegment offsetSegment, final SelectStatem if (offsetSegment instanceof LiteralLimitValueSegment) { int value = ((LiteralLimitValueSegment) offsetSegment).getValue(); selectStatement.getLimit().setOffset(new LimitValue(value, -1, false)); - selectStatement.getSQLTokens().add(new OffsetToken(offsetSegment.getStartIndex(), value)); + selectStatement.getSQLTokens().add(new OffsetToken(offsetSegment.getStartIndex(), offsetSegment.getStopIndex(), value)); } else { selectStatement.getLimit().setOffset(new LimitValue(-1, ((PlaceholderLimitValueSegment) offsetSegment).getParameterIndex(), false)); } @@ -60,7 +60,7 @@ private void setRowCount(final LimitValueSegment rowCountSegment, final SelectSt if (rowCountSegment instanceof LiteralLimitValueSegment) { int value = ((LiteralLimitValueSegment) rowCountSegment).getValue(); selectStatement.getLimit().setRowCount(new LimitValue(value, -1, false)); - selectStatement.getSQLTokens().add(new RowCountToken(rowCountSegment.getStartIndex(), value)); + selectStatement.getSQLTokens().add(new RowCountToken(rowCountSegment.getStartIndex(), rowCountSegment.getStopIndex(), value)); } else { selectStatement.getLimit().setRowCount(new LimitValue(-1, ((PlaceholderLimitValueSegment) rowCountSegment).getParameterIndex(), false)); } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/common/dml/SelectItemFiller.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/common/dml/SelectItemFiller.java index c7d59219add8b..5dba1ae0f6f43 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/common/dml/SelectItemFiller.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/common/dml/SelectItemFiller.java @@ -20,7 +20,6 @@ import com.google.common.base.Optional; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData; -import org.apache.shardingsphere.core.parse.antlr.constant.QuoteCharacter; import org.apache.shardingsphere.core.parse.antlr.filler.api.SQLSegmentFiller; import org.apache.shardingsphere.core.parse.antlr.sql.segment.SQLSegment; import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.expr.complex.SubquerySegment; @@ -86,7 +85,7 @@ private void fillShorthandSelectItemSegment(final ShorthandSelectItemSegment sel Optional table = selectStatement.getTables().find(owner.get()); if (table.isPresent() && !table.get().getAlias().isPresent() && shardingTableMetaData.containsTable(table.get().getName())) { // FIXME for QuoteCharacter.getQuoteCharacter(owner), if order by `xxx`.xx, has problem - selectStatement.addSQLToken(new TableToken(selectItemSegment.getStartIndex(), owner.get(), QuoteCharacter.getQuoteCharacter(owner.get()), 0)); + selectStatement.addSQLToken(new TableToken(selectItemSegment.getStartIndex(), selectItemSegment.getStopIndexOfOwner(), owner.get(), selectItemSegment.getOwnerQuoteCharacter())); } } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/encrypt/dml/EncryptSetAssignmentsFiller.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/encrypt/dml/EncryptSetAssignmentsFiller.java index 67750bc3e57ab..1aed168dfcaf6 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/encrypt/dml/EncryptSetAssignmentsFiller.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/encrypt/dml/EncryptSetAssignmentsFiller.java @@ -63,7 +63,7 @@ private void fillInsert(final SetAssignmentsSegment sqlSegment, final InsertStat InsertValue insertValue = getInsertValue(sqlSegment, insertStatement.getLogicSQL()); insertStatement.getValues().add(insertValue); insertStatement.setParametersIndex(insertValue.getParametersCount()); - insertStatement.getSQLTokens().add(new InsertSetToken(sqlSegment.getStartIndex())); + insertStatement.getSQLTokens().add(new InsertSetToken(sqlSegment.getStartIndex(), insertStatement.getLogicSQL().length() - 1)); } private InsertValue getInsertValue(final SetAssignmentsSegment sqlSegment, final String sql) { diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/sharding/ShardingColumnFiller.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/sharding/ShardingColumnFiller.java index ec53ecf14d88b..60138304d1b4a 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/sharding/ShardingColumnFiller.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/sharding/ShardingColumnFiller.java @@ -32,6 +32,7 @@ * * @author duhongjun * @author zhangliang + * @author panjuan */ @Setter public final class ShardingColumnFiller implements SQLSegmentFiller, ShardingTableMetaDataAwareFiller { @@ -45,7 +46,7 @@ public void fill(final ColumnSegment sqlSegment, final SQLStatement sqlStatement } Optional
logicTable = sqlStatement.getTables().find(sqlSegment.getOwner().get()); if (logicTable.isPresent() && !logicTable.get().getAlias().isPresent() && shardingTableMetaData.containsTable(logicTable.get().getName())) { - sqlStatement.addSQLToken(new TableToken(sqlSegment.getStartIndex(), logicTable.get().getName(), sqlSegment.getOwnerQuoteCharacter(), 0)); + sqlStatement.addSQLToken(new TableToken(sqlSegment.getStartIndex(), sqlSegment.getStopIndexOfOwner(), logicTable.get().getName(), sqlSegment.getOwnerQuoteCharacter())); } } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/sharding/ShardingTableFiller.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/sharding/ShardingTableFiller.java index 1a9eb1b42aecd..21aa801fc7d5f 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/sharding/ShardingTableFiller.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/sharding/ShardingTableFiller.java @@ -34,6 +34,7 @@ * * @author duhongjun * @author zhangliang + * @author panjuan */ @Setter public final class ShardingTableFiller implements SQLSegmentFiller, ShardingRuleAwareFiller { @@ -44,7 +45,7 @@ public final class ShardingTableFiller implements SQLSegmentFiller public void fill(final TableSegment sqlSegment, final SQLStatement sqlStatement) { if (isTableInShardingRule(sqlSegment.getName()) || !(sqlStatement instanceof SelectStatement)) { sqlStatement.getTables().add(new Table(sqlSegment.getName(), sqlSegment.getAlias().orNull())); - sqlStatement.getSQLTokens().add(new TableToken(sqlSegment.getStartIndex(), sqlSegment.getName(), sqlSegment.getQuoteCharacter(), sqlSegment.getOwnerLength())); + sqlStatement.getSQLTokens().add(new TableToken(sqlSegment.getStartIndex(), sqlSegment.getStopIndex(), sqlSegment.getName(), sqlSegment.getQuoteCharacter())); } if (sqlStatement instanceof DMLStatement && !sqlStatement.getTables().isSingleTable()) { throw new SQLParsingUnsupportedException("Cannot support Multiple-Table."); diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/sharding/dml/update/ShardingSetAssignmentsFiller.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/sharding/dml/update/ShardingSetAssignmentsFiller.java index c48861bffe93e..fa324e820c79b 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/sharding/dml/update/ShardingSetAssignmentsFiller.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/filler/sharding/dml/update/ShardingSetAssignmentsFiller.java @@ -84,7 +84,7 @@ private void fillInsert(final SetAssignmentsSegment sqlSegment, final InsertStat insertStatement.getValues().add(insertValue); insertStatement.getRouteConditions().getOrCondition().getAndConditions().add(andCondition); insertStatement.setParametersIndex(insertValue.getParametersCount()); - insertStatement.getSQLTokens().add(new InsertSetToken(sqlSegment.getStartIndex())); + insertStatement.getSQLTokens().add(new InsertSetToken(sqlSegment.getStartIndex(), insertStatement.getLogicSQL().length() - 1)); } private int getColumnCountExcludeAssistedQueryColumns(final InsertStatement insertStatement) { diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/common/TableSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/common/TableSegment.java index 0a5812acc4159..fcaa38cacbbb8 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/common/TableSegment.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/common/TableSegment.java @@ -38,6 +38,8 @@ public class TableSegment implements SQLSegment, OwnerAvailable, AliasAvailable private final int startIndex; + private final int stopIndex; + private final String name; private final QuoteCharacter quoteCharacter; @@ -48,12 +50,13 @@ public class TableSegment implements SQLSegment, OwnerAvailable, AliasAvailable private String alias; - public TableSegment(final int startIndex, final String name) { - this(startIndex, name, QuoteCharacter.getQuoteCharacter(name)); + public TableSegment(final int startIndex, final int stopIndex, final String name) { + this(startIndex, stopIndex, name, QuoteCharacter.getQuoteCharacter(name)); } - public TableSegment(final int startIndex, final String name, final QuoteCharacter quoteCharacter) { + private TableSegment(final int startIndex, final int stopIndex, final String name, final QuoteCharacter quoteCharacter) { this.startIndex = startIndex; + this.stopIndex = stopIndex; this.name = SQLUtil.getExactlyValue(name); this.quoteCharacter = quoteCharacter; } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/column/ColumnSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/column/ColumnSegment.java index aced11ad99185..c57e2880c5492 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/column/ColumnSegment.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/column/ColumnSegment.java @@ -33,12 +33,15 @@ * * @author duhongjun * @author zhangliang + * @author panjuan */ @Getter public class ColumnSegment implements SQLSegment, PredicateRightValue, OwnerAvailable { private final int startIndex; + private int stopIndexOfOwner; + private final String name; private String owner; @@ -67,6 +70,7 @@ public final Optional getOwner() { @Override public final void setOwner(final String owner) { + stopIndexOfOwner = startIndex + owner.length() - 1; this.owner = SQLUtil.getExactlyValue(owner); ownerQuoteCharacter = QuoteCharacter.getQuoteCharacter(owner); } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/item/ShorthandSelectItemSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/item/ShorthandSelectItemSegment.java index b6e4f32dfffc2..f0fb13589a8a2 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/item/ShorthandSelectItemSegment.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/item/ShorthandSelectItemSegment.java @@ -18,8 +18,11 @@ package org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.item; import com.google.common.base.Optional; +import lombok.AccessLevel; import lombok.Getter; import lombok.RequiredArgsConstructor; +import lombok.Setter; +import org.apache.shardingsphere.core.parse.antlr.constant.QuoteCharacter; import org.apache.shardingsphere.core.parse.antlr.sql.OwnerAvailable; import org.apache.shardingsphere.core.parse.util.SQLUtil; @@ -27,6 +30,7 @@ * Shorthand select item segment. * * @author zhangliang + * @author panjuan */ @RequiredArgsConstructor @Getter @@ -34,8 +38,13 @@ public final class ShorthandSelectItemSegment implements SelectItemSegment, Owne private final int startIndex; + private int stopIndexOfOwner; + private String owner; + @Setter(AccessLevel.PROTECTED) + private QuoteCharacter ownerQuoteCharacter = QuoteCharacter.NONE; + @Override public Optional getOwner() { return Optional.fromNullable(owner); @@ -43,6 +52,8 @@ public Optional getOwner() { @Override public void setOwner(final String owner) { + stopIndexOfOwner = startIndex + owner.length() - 1; this.owner = SQLUtil.getExactlyValue(owner); + ownerQuoteCharacter = QuoteCharacter.getQuoteCharacter(owner); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/limit/LimitValueSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/limit/LimitValueSegment.java index f0c1c5f4bf4e1..1fc1bee40ca7e 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/limit/LimitValueSegment.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/limit/LimitValueSegment.java @@ -25,10 +25,13 @@ * Limit value segment. * * @author zhangliang + * @author panjuan */ @RequiredArgsConstructor @Getter public abstract class LimitValueSegment implements SQLSegment { private final int startIndex; + + private final int stopIndex; } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/limit/LiteralLimitValueSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/limit/LiteralLimitValueSegment.java index c4ed1fc514ec0..9056c5f052665 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/limit/LiteralLimitValueSegment.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/limit/LiteralLimitValueSegment.java @@ -29,8 +29,8 @@ public final class LiteralLimitValueSegment extends LimitValueSegment { private final int value; - public LiteralLimitValueSegment(final int value, final int beginPosition) { - super(beginPosition); + public LiteralLimitValueSegment(final int value, final int startIndex, final int stopIndex) { + super(startIndex, stopIndex); this.value = value; } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/limit/PlaceholderLimitValueSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/limit/PlaceholderLimitValueSegment.java index da29d2ca8cb2d..179e6c63457b1 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/limit/PlaceholderLimitValueSegment.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/segment/dml/limit/PlaceholderLimitValueSegment.java @@ -29,8 +29,8 @@ public final class PlaceholderLimitValueSegment extends LimitValueSegment { private final int parameterIndex; - public PlaceholderLimitValueSegment(final int parameterIndex, final int beginPosition) { - super(beginPosition); + public PlaceholderLimitValueSegment(final int parameterIndex, final int startIndex, final int stopIndex) { + super(startIndex, stopIndex); this.parameterIndex = parameterIndex; } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/AggregationDistinctToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/AggregationDistinctToken.java index 8144e9f77c23a..5767ac73858c4 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/AggregationDistinctToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/AggregationDistinctToken.java @@ -28,15 +28,12 @@ @Getter public final class AggregationDistinctToken extends SQLToken { - private final int stopIndex; - private String columnName; private Optional alias; public AggregationDistinctToken(final int startIndex, final int stopIndex, final String columnName, final Optional alias) { - super(startIndex); - this.stopIndex = stopIndex; + super(startIndex, stopIndex); this.columnName = columnName; this.alias = alias; } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/EncryptColumnToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/EncryptColumnToken.java index 69d4fb47e5528..2e18cbd0c1b3c 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/EncryptColumnToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/EncryptColumnToken.java @@ -30,15 +30,12 @@ @ToString public final class EncryptColumnToken extends SQLToken { - private final int stopIndex; - private final Column column; private final boolean isInWhere; public EncryptColumnToken(final int startIndex, final int stopIndex, final Column column, final boolean isInWhere) { - super(startIndex); - this.stopIndex = stopIndex; + super(startIndex, stopIndex); this.column = column; this.isInWhere = isInWhere; } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/GeneratedKeyToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/GeneratedKeyToken.java deleted file mode 100644 index 1cb3de509df6e..0000000000000 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/GeneratedKeyToken.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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.core.parse.antlr.sql.token; - -/** - * Generated key token. - * - * @author zhangliang - */ -public final class GeneratedKeyToken extends SQLToken { - - public GeneratedKeyToken(final int startIndex) { - super(startIndex); - } -} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/IndexToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/IndexToken.java index e0b6327c84463..7ef44a4b0cf99 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/IndexToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/IndexToken.java @@ -26,19 +26,17 @@ * Index token. * * @author caohao + * @author panjuan */ @Getter @Setter @ToString public final class IndexToken extends SQLToken { - private final int stopIndex; - private String tableName; public IndexToken(final int startIndex, final int stopIndex) { - super(startIndex); - this.stopIndex = stopIndex; + super(startIndex, stopIndex); } public IndexToken(final int startIndex, final int stopIndex, final String tableName) { diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/InsertColumnToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/InsertColumnToken.java deleted file mode 100644 index beaaa6ffd7d3f..0000000000000 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/InsertColumnToken.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.core.parse.antlr.sql.token; - -import lombok.Getter; -import lombok.ToString; - -/** - * Symbol token. - * - * @author panjuan - */ -@Getter -@ToString -public final class InsertColumnToken extends SQLToken { - - private final String columnName; - - public InsertColumnToken(final int startIndex, final String columnName) { - super(startIndex); - this.columnName = columnName; - } -} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/InsertSetToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/InsertSetToken.java index e7584d1c08e72..22dcd4b042e5d 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/InsertSetToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/InsertSetToken.java @@ -21,10 +21,11 @@ * Insert set token. * * @author zhangliang + * @author panjuan */ public final class InsertSetToken extends SQLToken { - public InsertSetToken(final int startIndex) { - super(startIndex); + public InsertSetToken(final int startIndex, final int stopIndex) { + super(startIndex, stopIndex); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/InsertValuesToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/InsertValuesToken.java index 60061efe37d62..e83b2fd4b7a29 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/InsertValuesToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/InsertValuesToken.java @@ -25,7 +25,7 @@ */ public final class InsertValuesToken extends SQLToken { - public InsertValuesToken(final int startIndex) { - super(startIndex); + public InsertValuesToken(final int startIndex, final int stopIndex) { + super(startIndex, stopIndex); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/ItemsToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/ItemsToken.java index d45cb578d1606..a07d139503177 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/ItemsToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/ItemsToken.java @@ -42,6 +42,6 @@ public final class ItemsToken extends SQLToken { private final List items = new LinkedList<>(); public ItemsToken(final int startIndex) { - super(startIndex); + super(startIndex, startIndex - 1); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/OffsetToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/OffsetToken.java index 0d3e4564a92f0..c1d01974d2101 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/OffsetToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/OffsetToken.java @@ -23,14 +23,15 @@ * Offset token for limit. * * @author zhangliang + * @author panjuan */ @Getter public final class OffsetToken extends SQLToken { private final int offset; - public OffsetToken(final int startIndex, final int offset) { - super(startIndex); + public OffsetToken(final int startIndex, final int stopIndex, final int offset) { + super(startIndex, stopIndex); this.offset = offset; } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/OrderByToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/OrderByToken.java index 939c2f29c035e..e8b482638c3f5 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/OrderByToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/OrderByToken.java @@ -21,10 +21,11 @@ * Order by token. * * @author zhangliang + * @author panjuan */ public final class OrderByToken extends SQLToken { - public OrderByToken(final int startIndex) { - super(startIndex); + public OrderByToken(final int startIndex, final int stopIndex) { + super(startIndex, stopIndex); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/RemoveToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/RemoveToken.java index 2ef8725a078de..82c624071cb5b 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/RemoveToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/RemoveToken.java @@ -30,10 +30,7 @@ @ToString public final class RemoveToken extends SQLToken { - private final int stopIndex; - public RemoveToken(final int startIndex, final int stopIndex) { - super(startIndex); - this.stopIndex = stopIndex; + super(startIndex, stopIndex); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/RowCountToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/RowCountToken.java index 7f596a58fef2e..d4ad8190545ce 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/RowCountToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/RowCountToken.java @@ -29,8 +29,8 @@ public final class RowCountToken extends SQLToken { private final int rowCount; - public RowCountToken(final int startIndex, final int rowCount) { - super(startIndex); + public RowCountToken(final int startIndex, final int stopIndex, final int rowCount) { + super(startIndex, stopIndex); this.rowCount = rowCount; } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/SQLToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/SQLToken.java index caa8f5231b870..bc6217dfe8237 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/SQLToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/SQLToken.java @@ -34,6 +34,8 @@ public abstract class SQLToken implements Comparable { private final int startIndex; + private final int stopIndex; + @Override public final int compareTo(final SQLToken sqlToken) { return startIndex - sqlToken.getStartIndex(); diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/SchemaToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/SchemaToken.java index a87dcd09818fb..6ea64805831c8 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/SchemaToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/SchemaToken.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.core.parse.antlr.sql.token; -import lombok.Getter; import lombok.ToString; import org.apache.shardingsphere.core.parse.util.SQLUtil; @@ -25,18 +24,15 @@ * Schema token. * * @author zhangliang + * @author panjuan */ @ToString public final class SchemaToken extends SQLToken { - @Getter - private final int stopIndex; - private final String tableName; public SchemaToken(final int startIndex, final int stopIndex, final String tableName) { - super(startIndex); - this.stopIndex = stopIndex; + super(startIndex, stopIndex); this.tableName = tableName; } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/TableToken.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/TableToken.java index 1796d47269637..113f87b374637 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/TableToken.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/sql/token/TableToken.java @@ -37,21 +37,9 @@ public final class TableToken extends SQLToken { @Getter private final QuoteCharacter quoteCharacter; - private final int ownerLength; - - public TableToken(final int startIndex, final String tableName, final QuoteCharacter quoteCharacter, final int ownerLength) { - super(startIndex); + public TableToken(final int startIndex, final int stopIndex, final String tableName, final QuoteCharacter quoteCharacter) { + super(startIndex, stopIndex); this.tableName = SQLUtil.getExactlyValue(tableName); this.quoteCharacter = quoteCharacter; - this.ownerLength = ownerLength; - } - - /** - * Get table token length. - * - * @return table token length - */ - public int getLength() { - return ownerLength + tableName.length() + quoteCharacter.getStartDelimiter().length() + quoteCharacter.getEndDelimiter().length(); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/InsertSetClauseParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/InsertSetClauseParser.java index 704f36c112a08..bc7a0cda813da 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/InsertSetClauseParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/InsertSetClauseParser.java @@ -20,11 +20,8 @@ import com.google.common.base.Optional; import com.google.common.base.Preconditions; import org.apache.shardingsphere.core.parse.antlr.sql.statement.dml.InsertStatement; -import org.apache.shardingsphere.core.parse.antlr.sql.token.InsertColumnToken; import org.apache.shardingsphere.core.parse.antlr.sql.token.InsertSetToken; import org.apache.shardingsphere.core.parse.antlr.sql.token.InsertValuesToken; -import org.apache.shardingsphere.core.parse.antlr.sql.token.ItemsToken; -import org.apache.shardingsphere.core.parse.antlr.sql.token.SQLToken; import org.apache.shardingsphere.core.parse.old.lexer.LexerEngine; import org.apache.shardingsphere.core.parse.old.lexer.token.Keyword; import org.apache.shardingsphere.core.parse.old.lexer.token.Symbol; @@ -43,7 +40,6 @@ import org.apache.shardingsphere.core.parse.util.SQLUtil; import org.apache.shardingsphere.core.rule.ShardingRule; -import java.util.Iterator; import java.util.LinkedList; /** @@ -76,11 +72,10 @@ public void parse(final InsertStatement insertStatement) { if (!lexerEngine.skipIfEqual(getCustomizedInsertKeywords())) { return; } - removeUnnecessaryToken(insertStatement); Optional insertValuesToken = insertStatement.findSQLToken(InsertValuesToken.class); Preconditions.checkState(insertValuesToken.isPresent()); insertStatement.getSQLTokens().remove(insertValuesToken.get()); - insertStatement.addSQLToken(new InsertSetToken(insertValuesToken.get().getStartIndex())); + insertStatement.addSQLToken(new InsertSetToken(insertValuesToken.get().getStartIndex(), insertStatement.getLogicSQL().length() - 1)); do { SQLExpression left = basicExpressionParser.parse(insertStatement); Column column = null; @@ -105,15 +100,5 @@ public void parse(final InsertStatement insertStatement) { insertStatement.getValues().add(insertValue); } - private void removeUnnecessaryToken(final InsertStatement insertStatement) { - Iterator sqlTokens = insertStatement.getSQLTokens().iterator(); - while (sqlTokens.hasNext()) { - SQLToken sqlToken = sqlTokens.next(); - if (sqlToken instanceof InsertColumnToken || sqlToken instanceof ItemsToken) { - sqlTokens.remove(); - } - } - } - protected abstract Keyword[] getCustomizedInsertKeywords(); } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/InsertValuesClauseParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/InsertValuesClauseParser.java index 966ed7f119169..c9917b5527e17 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/InsertValuesClauseParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/InsertValuesClauseParser.java @@ -90,7 +90,7 @@ private void parseValues(final InsertStatement insertStatement) { Optional insertValuesToken = insertStatement.findSQLToken(InsertValuesToken.class); Preconditions.checkState(insertValuesToken.isPresent()); insertStatement.getSQLTokens().remove(insertValuesToken.get()); - insertStatement.addSQLToken(new InsertValuesToken(insertValuesToken.get().getStartIndex())); + insertStatement.addSQLToken(new InsertValuesToken(insertValuesToken.get().getStartIndex(), insertStatement.getLogicSQL().length() - 1)); do { lexerEngine.accept(Symbol.LEFT_PAREN); List sqlExpressions = new LinkedList<>(); diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/SelectListClauseParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/SelectListClauseParser.java index f2df2a892ae90..4c89951d20ba8 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/SelectListClauseParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/SelectListClauseParser.java @@ -164,8 +164,9 @@ private SelectItem parseStarSelectItem(final String owner) { } private SelectItem parseCommonOrStarSelectItem(final SelectStatement selectStatement) { + int endPosition = lexerEngine.getCurrentToken().getEndPosition(); String literals = lexerEngine.getCurrentToken().getLiterals(); - int position = lexerEngine.getCurrentToken().getEndPosition() - literals.length(); + int position = endPosition - literals.length(); StringBuilder result = new StringBuilder(); result.append(literals); lexerEngine.nextToken(); @@ -174,7 +175,7 @@ private SelectItem parseCommonOrStarSelectItem(final SelectStatement selectState } else if (lexerEngine.equalAny(Symbol.DOT)) { String tableName = SQLUtil.getExactlyValue(literals); if (shardingRule.findTableRule(tableName).isPresent() || shardingRule.isBroadcastTable(tableName) || shardingRule.findBindingTableRule(tableName).isPresent()) { - selectStatement.addSQLToken(new TableToken(position, literals, QuoteCharacter.getQuoteCharacter(literals), 0)); + selectStatement.addSQLToken(new TableToken(position, endPosition - 1, literals, QuoteCharacter.getQuoteCharacter(literals))); } result.append(lexerEngine.getCurrentToken().getLiterals()); lexerEngine.nextToken(); diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/TableReferencesClauseParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/TableReferencesClauseParser.java index 742c3f1db5be1..c39732e639a10 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/TableReferencesClauseParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/TableReferencesClauseParser.java @@ -91,15 +91,14 @@ protected void parseTableReference(final SQLStatement sqlStatement, final boolea } protected final void parseTableFactor(final SQLStatement sqlStatement, final boolean isSingleTableOnly) { - final int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length(); + int endPosition = lexerEngine.getCurrentToken().getEndPosition(); + int beginPosition = endPosition - lexerEngine.getCurrentToken().getLiterals().length(); String literals = lexerEngine.getCurrentToken().getLiterals(); - int skippedSchemaNameLength = 0; lexerEngine.nextToken(); if (sqlStatement instanceof InsertStatement) { - sqlStatement.addSQLToken(new InsertValuesToken(lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length())); + sqlStatement.addSQLToken(new InsertValuesToken(lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length(), sqlStatement.getLogicSQL().length() - 1)); } if (lexerEngine.skipIfEqual(Symbol.DOT)) { - skippedSchemaNameLength = literals.length() + Symbol.DOT.getLiterals().length(); literals = lexerEngine.getCurrentToken().getLiterals(); lexerEngine.nextToken(); } @@ -110,7 +109,7 @@ protected final void parseTableFactor(final SQLStatement sqlStatement, final boo if (isSingleTableOnly || shardingRule.findTableRule(tableName).isPresent() || shardingRule.isBroadcastTable(tableName) || shardingRule.findBindingTableRule(tableName).isPresent() || shardingRule.getShardingDataSourceNames().getDataSourceNames().contains(shardingRule.getShardingDataSourceNames().getDefaultDataSourceName())) { - sqlStatement.addSQLToken(new TableToken(beginPosition, literals, QuoteCharacter.getQuoteCharacter(literals), skippedSchemaNameLength)); + sqlStatement.addSQLToken(new TableToken(beginPosition, endPosition - 1, literals, QuoteCharacter.getQuoteCharacter(literals))); sqlStatement.getTables().add(new Table(tableName, aliasExpressionParser.parseTableAlias(sqlStatement, true, tableName).orNull())); } else { aliasExpressionParser.parseTableAlias(); @@ -190,16 +189,15 @@ private void parseJoinCondition(final SQLStatement sqlStatement) { * @param sqlStatement SQL statement */ public final void parseSingleTableWithoutAlias(final SQLStatement sqlStatement) { - int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length(); + int endPosition = lexerEngine.getCurrentToken().getEndPosition(); + int beginPosition = endPosition - lexerEngine.getCurrentToken().getLiterals().length(); String literals = lexerEngine.getCurrentToken().getLiterals(); - int skippedSchemaNameLength = 0; lexerEngine.nextToken(); if (lexerEngine.skipIfEqual(Symbol.DOT)) { - skippedSchemaNameLength = literals.length() + Symbol.DOT.getLiterals().length(); literals = lexerEngine.getCurrentToken().getLiterals(); lexerEngine.nextToken(); } - sqlStatement.addSQLToken(new TableToken(beginPosition, literals, QuoteCharacter.getQuoteCharacter(literals), skippedSchemaNameLength)); + sqlStatement.addSQLToken(new TableToken(beginPosition, endPosition - 1, literals, QuoteCharacter.getQuoteCharacter(literals))); sqlStatement.getTables().add(new Table(SQLUtil.getExactlyValue(literals), null)); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/UpdateSetItemsClauseParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/UpdateSetItemsClauseParser.java index 428813414fbf0..d16d0f11560d5 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/UpdateSetItemsClauseParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/UpdateSetItemsClauseParser.java @@ -73,7 +73,7 @@ private void parseSetColumn(final DMLStatement updateStatement) { lexerEngine.nextToken(); if (lexerEngine.skipIfEqual(Symbol.DOT)) { if (updateStatement.getTables().getSingleTableName().equalsIgnoreCase(SQLUtil.getExactlyValue(literals))) { - updateStatement.addSQLToken(new TableToken(beginPosition - literals.length(), literals, QuoteCharacter.getQuoteCharacter(literals), 0)); + updateStatement.addSQLToken(new TableToken(beginPosition - literals.length(), beginPosition - 1, literals, QuoteCharacter.getQuoteCharacter(literals))); } lexerEngine.nextToken(); } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/WhereClauseParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/WhereClauseParser.java index 5500fe987e8ce..b5c845eebbc16 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/WhereClauseParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/WhereClauseParser.java @@ -294,7 +294,7 @@ private void parseRowCountCondition(final SelectStatement selectStatement, final if (sqlExpression instanceof SQLNumberExpression) { int rowCount = ((SQLNumberExpression) sqlExpression).getNumber().intValue(); selectStatement.getLimit().setRowCount(new LimitValue(rowCount, -1, includeRowCount)); - selectStatement.addSQLToken(new RowCountToken(endPosition - String.valueOf(rowCount).length(), rowCount)); + selectStatement.addSQLToken(new RowCountToken(endPosition - String.valueOf(rowCount).length(), endPosition - 1, rowCount)); } else if (sqlExpression instanceof SQLParameterMarkerExpression) { selectStatement.getLimit().setRowCount(new LimitValue(-1, ((SQLParameterMarkerExpression) sqlExpression).getIndex(), includeRowCount)); } @@ -308,8 +308,8 @@ private void parseOffsetCondition(final SelectStatement selectStatement, final b if (sqlExpression instanceof SQLNumberExpression) { int offset = ((SQLNumberExpression) sqlExpression).getNumber().intValue(); selectStatement.getLimit().setOffset(new LimitValue(offset, -1, includeOffset)); - selectStatement.addSQLToken(new OffsetToken( - lexerEngine.getCurrentToken().getEndPosition() - String.valueOf(offset).length() - lexerEngine.getCurrentToken().getLiterals().length(), offset)); + selectStatement.addSQLToken(new OffsetToken(lexerEngine.getCurrentToken().getEndPosition() - String.valueOf(offset).length() - lexerEngine.getCurrentToken().getLiterals().length(), + lexerEngine.getCurrentToken().getEndPosition() - 1, offset)); } else if (sqlExpression instanceof SQLParameterMarkerExpression) { selectStatement.getLimit().setOffset(new LimitValue(-1, ((SQLParameterMarkerExpression) sqlExpression).getIndex(), includeOffset)); } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/expression/AliasExpressionParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/expression/AliasExpressionParser.java index 38c3dd62adaf0..a044cb45015fd 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/expression/AliasExpressionParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/expression/AliasExpressionParser.java @@ -68,7 +68,7 @@ private Optional parseAlias(final SQLStatement sqlStatement, final boole String literals = lexerEngine.getCurrentToken().getLiterals(); String alias = SQLUtil.getExactlyValue(literals); if (setTableToken && alias.equals(tableName)) { - sqlStatement.addSQLToken(new TableToken(beginPosition, literals, QuoteCharacter.getQuoteCharacter(literals), 0)); + sqlStatement.addSQLToken(new TableToken(beginPosition, lexerEngine.getCurrentToken().getEndPosition() - 1, literals, QuoteCharacter.getQuoteCharacter(literals))); } lexerEngine.nextToken(); return Optional.of(SQLUtil.getExactlyValue(literals)); diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/expression/BasicExpressionParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/expression/BasicExpressionParser.java index 9bd2398ef8ab7..821b78a74b605 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/expression/BasicExpressionParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/clause/expression/BasicExpressionParser.java @@ -133,7 +133,7 @@ private void skipRestCompositeExpression(final SQLStatement sqlStatement) { private void setTableToken(final SQLStatement sqlStatement, final int beginPosition, final SQLPropertyExpression propertyExpr) { String owner = propertyExpr.getOwner().getName(); if (sqlStatement.getTables().getTableNames().contains(SQLUtil.getExactlyValue(propertyExpr.getOwner().getName()))) { - sqlStatement.addSQLToken(new TableToken(beginPosition - owner.length(), owner, QuoteCharacter.getQuoteCharacter(owner), 0)); + sqlStatement.addSQLToken(new TableToken(beginPosition - owner.length(), beginPosition - 1, owner, QuoteCharacter.getQuoteCharacter(owner))); } } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/mysql/clause/MySQLLimitClauseParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/mysql/clause/MySQLLimitClauseParser.java index 67f415803ac0b..6645bed3bf318 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/mysql/clause/MySQLLimitClauseParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/mysql/clause/MySQLLimitClauseParser.java @@ -50,7 +50,8 @@ public void parse(final SelectStatement selectStatement) { return; } int valueIndex = -1; - int valueBeginPosition = lexerEngine.getCurrentToken().getEndPosition(); + int valueEndPosition = lexerEngine.getCurrentToken().getEndPosition(); + int valueBeginPosition = valueEndPosition; int value; boolean isParameterForValue = false; if (lexerEngine.equalAny(Literals.INT)) { @@ -66,25 +67,27 @@ public void parse(final SelectStatement selectStatement) { } lexerEngine.nextToken(); if (lexerEngine.skipIfEqual(Symbol.COMMA)) { - selectStatement.setLimit(getLimitWithComma(valueIndex, valueBeginPosition, value, isParameterForValue, selectStatement)); + selectStatement.setLimit(getLimitWithComma(valueIndex, valueBeginPosition, valueEndPosition, value, isParameterForValue, selectStatement)); return; } if (lexerEngine.skipIfEqual(MySQLKeyword.OFFSET)) { - selectStatement.setLimit(getLimitWithOffset(valueIndex, valueBeginPosition, value, isParameterForValue, selectStatement)); + selectStatement.setLimit(getLimitWithOffset(valueIndex, valueBeginPosition, valueEndPosition, value, isParameterForValue, selectStatement)); return; } if (isParameterForValue) { selectStatement.setParametersIndex(selectStatement.getParametersIndex() + 1); } else { - selectStatement.addSQLToken(new RowCountToken(valueBeginPosition, value)); + selectStatement.addSQLToken(new RowCountToken(valueBeginPosition, valueEndPosition - 1, value)); } Limit limit = new Limit(); limit.setRowCount(new LimitValue(value, valueIndex, false)); selectStatement.setLimit(limit); } - private Limit getLimitWithComma(final int index, final int valueBeginPosition, final int value, final boolean isParameterForValue, final SelectStatement selectStatement) { - int rowCountBeginPosition = lexerEngine.getCurrentToken().getEndPosition(); + private Limit getLimitWithComma(final int index, final int valueBeginPosition, + final int valueEndPosition, final int value, final boolean isParameterForValue, final SelectStatement selectStatement) { + int rowCountEndPosition = lexerEngine.getCurrentToken().getEndPosition(); + int rowCountBeginPosition = rowCountEndPosition; int rowCountValue; int rowCountIndex = -1; boolean isParameterForRowCount = false; @@ -103,12 +106,12 @@ private Limit getLimitWithComma(final int index, final int valueBeginPosition, f if (isParameterForValue) { selectStatement.setParametersIndex(selectStatement.getParametersIndex() + 1); } else { - selectStatement.addSQLToken(new OffsetToken(valueBeginPosition, value)); + selectStatement.addSQLToken(new OffsetToken(valueBeginPosition, valueEndPosition - 1, value)); } if (isParameterForRowCount) { selectStatement.setParametersIndex(selectStatement.getParametersIndex() + 1); } else { - selectStatement.addSQLToken(new RowCountToken(rowCountBeginPosition, rowCountValue)); + selectStatement.addSQLToken(new RowCountToken(rowCountBeginPosition, rowCountEndPosition - 1, rowCountValue)); } Limit result = new Limit(); result.setRowCount(new LimitValue(rowCountValue, rowCountIndex, false)); @@ -116,8 +119,10 @@ private Limit getLimitWithComma(final int index, final int valueBeginPosition, f return result; } - private Limit getLimitWithOffset(final int index, final int valueBeginPosition, final int value, final boolean isParameterForValue, final SelectStatement selectStatement) { - int offsetBeginPosition = lexerEngine.getCurrentToken().getEndPosition(); + private Limit getLimitWithOffset(final int index, final int valueBeginPosition, + final int valueEndPosition, final int value, final boolean isParameterForValue, final SelectStatement selectStatement) { + int offsetEndPosition = lexerEngine.getCurrentToken().getEndPosition(); + int offsetBeginPosition = offsetEndPosition; int offsetValue = -1; int offsetIndex = -1; boolean isParameterForOffset = false; @@ -135,12 +140,12 @@ private Limit getLimitWithOffset(final int index, final int valueBeginPosition, if (isParameterForOffset) { selectStatement.setParametersIndex(selectStatement.getParametersIndex() + 1); } else { - selectStatement.addSQLToken(new OffsetToken(offsetBeginPosition, offsetValue)); + selectStatement.addSQLToken(new OffsetToken(offsetBeginPosition, offsetEndPosition - 1, offsetValue)); } if (isParameterForValue) { selectStatement.setParametersIndex(selectStatement.getParametersIndex() + 1); } else { - selectStatement.addSQLToken(new RowCountToken(valueBeginPosition, value)); + selectStatement.addSQLToken(new RowCountToken(valueBeginPosition, valueEndPosition - 1, value)); } Limit result = new Limit(); result.setRowCount(new LimitValue(value, index, false)); diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/mysql/sql/MySQLInsertParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/mysql/sql/MySQLInsertParser.java index a5ee84848eb32..841ca163cd3b0 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/mysql/sql/MySQLInsertParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/mysql/sql/MySQLInsertParser.java @@ -31,7 +31,7 @@ */ public final class MySQLInsertParser extends AbstractInsertParser { - public MySQLInsertParser(final ShardingRule shardingRule, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { - super(shardingRule, shardingTableMetaData, lexerEngine, new MySQLInsertClauseParserFacade(shardingRule, lexerEngine)); + public MySQLInsertParser(final ShardingRule shardingRule, final String sql, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { + super(shardingRule, sql, shardingTableMetaData, lexerEngine, new MySQLInsertClauseParserFacade(shardingRule, lexerEngine)); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/mysql/sql/MySQLShowParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/mysql/sql/MySQLShowParser.java index df0b637e625c0..b79ca1ba88bea 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/mysql/sql/MySQLShowParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/mysql/sql/MySQLShowParser.java @@ -95,7 +95,7 @@ private DALStatement parseShowTableStatus() { if (lexerEngine.equalAny(DefaultKeyword.FROM, DefaultKeyword.IN)) { int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length(); lexerEngine.nextToken(); - result.addSQLToken(new RemoveToken(beginPosition, lexerEngine.getCurrentToken().getEndPosition())); + result.addSQLToken(new RemoveToken(beginPosition, lexerEngine.getCurrentToken().getEndPosition() - 1)); lexerEngine.nextToken(); } if (lexerEngine.skipIfEqual(DefaultKeyword.LIKE)) { @@ -109,7 +109,7 @@ private DALStatement parseShowTables() { if (lexerEngine.equalAny(DefaultKeyword.FROM, DefaultKeyword.IN)) { int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length(); lexerEngine.nextToken(); - result.addSQLToken(new RemoveToken(beginPosition, lexerEngine.getCurrentToken().getEndPosition())); + result.addSQLToken(new RemoveToken(beginPosition, lexerEngine.getCurrentToken().getEndPosition() - 1)); lexerEngine.nextToken(); } if (lexerEngine.skipIfEqual(DefaultKeyword.LIKE)) { @@ -150,7 +150,7 @@ private void parseLike(final DALStatement dalStatement) { int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length() - 1; String literals = lexerEngine.getCurrentToken().getLiterals(); if (shardingRule.findTableRule(literals).isPresent() || shardingRule.isBroadcastTable(literals)) { - dalStatement.addSQLToken(new TableToken(beginPosition, literals, QuoteCharacter.getQuoteCharacter(literals), 0)); + dalStatement.addSQLToken(new TableToken(beginPosition, lexerEngine.getCurrentToken().getEndPosition() - 1, literals, QuoteCharacter.getQuoteCharacter(literals))); dalStatement.getTables().add(new Table(SQLUtil.getExactlyValue(literals), null)); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/oracle/sql/OracleInsertParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/oracle/sql/OracleInsertParser.java index 264dc25e6708b..7a7fb9f0a03a2 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/oracle/sql/OracleInsertParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/oracle/sql/OracleInsertParser.java @@ -31,7 +31,7 @@ */ public final class OracleInsertParser extends AbstractInsertParser { - public OracleInsertParser(final ShardingRule shardingRule, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { - super(shardingRule, shardingTableMetaData, lexerEngine, new OracleInsertClauseParserFacade(shardingRule, lexerEngine)); + public OracleInsertParser(final ShardingRule shardingRule, final String sql, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { + super(shardingRule, sql, shardingTableMetaData, lexerEngine, new OracleInsertClauseParserFacade(shardingRule, lexerEngine)); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/postgresql/clause/PostgreSQLLimitClauseParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/postgresql/clause/PostgreSQLLimitClauseParser.java index a12fe4fa603fa..bf8f16555333f 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/postgresql/clause/PostgreSQLLimitClauseParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/postgresql/clause/PostgreSQLLimitClauseParser.java @@ -69,14 +69,15 @@ private Optional buildRowCount(final SelectStatement selectStatement int parameterIndex = selectStatement.getParametersIndex(); int rowCountValue = -1; int rowCountIndex = -1; - int valueBeginPosition = lexerEngine.getCurrentToken().getEndPosition(); + int valueEndPosition = lexerEngine.getCurrentToken().getEndPosition(); + int valueBeginPosition = valueEndPosition; if (lexerEngine.equalAny(DefaultKeyword.ALL)) { lexerEngine.nextToken(); } else { if (lexerEngine.equalAny(Literals.INT, Literals.FLOAT)) { rowCountValue = NumberUtil.roundHalfUp(lexerEngine.getCurrentToken().getLiterals()); valueBeginPosition = valueBeginPosition - (rowCountValue + "").length(); - selectStatement.addSQLToken(new RowCountToken(valueBeginPosition, rowCountValue)); + selectStatement.addSQLToken(new RowCountToken(valueBeginPosition, valueEndPosition - 1, rowCountValue)); } else if (lexerEngine.equalAny(Symbol.QUESTION)) { rowCountIndex = parameterIndex++; selectStatement.setParametersIndex(parameterIndex); @@ -97,7 +98,7 @@ private Optional buildOffset(final SelectStatement selectStatement) if (lexerEngine.equalAny(Literals.INT, Literals.FLOAT)) { offsetValue = NumberUtil.roundHalfUp(lexerEngine.getCurrentToken().getLiterals()); offsetBeginPosition = offsetBeginPosition - (offsetValue + "").length(); - selectStatement.addSQLToken(new OffsetToken(offsetBeginPosition, offsetValue)); + selectStatement.addSQLToken(new OffsetToken(offsetBeginPosition, lexerEngine.getCurrentToken().getEndPosition() - 1, offsetValue)); } else if (lexerEngine.equalAny(Symbol.QUESTION)) { offsetIndex = parameterIndex++; selectStatement.setParametersIndex(parameterIndex); diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/postgresql/sql/PostgreSQLInsertParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/postgresql/sql/PostgreSQLInsertParser.java index 023436c1df065..0e4bf42ebd173 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/postgresql/sql/PostgreSQLInsertParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/postgresql/sql/PostgreSQLInsertParser.java @@ -31,7 +31,7 @@ */ public final class PostgreSQLInsertParser extends AbstractInsertParser { - public PostgreSQLInsertParser(final ShardingRule shardingRule, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { - super(shardingRule, shardingTableMetaData, lexerEngine, new PostgreSQLInsertClauseParserFacade(shardingRule, lexerEngine)); + public PostgreSQLInsertParser(final ShardingRule shardingRule, final String sql, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { + super(shardingRule, sql, shardingTableMetaData, lexerEngine, new PostgreSQLInsertClauseParserFacade(shardingRule, lexerEngine)); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/sqlserver/clause/SQLServerTopClauseParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/sqlserver/clause/SQLServerTopClauseParser.java index fa899a06422ce..5aa0947400756 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/sqlserver/clause/SQLServerTopClauseParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/sqlserver/clause/SQLServerTopClauseParser.java @@ -58,7 +58,8 @@ public void parse(final SelectStatement selectStatement) { if (!lexerEngine.skipIfEqual(SQLServerKeyword.TOP)) { return; } - int beginPosition = lexerEngine.getCurrentToken().getEndPosition(); + int endPosition = lexerEngine.getCurrentToken().getEndPosition(); + int beginPosition = endPosition; if (!lexerEngine.skipIfEqual(Symbol.LEFT_PAREN)) { beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length(); } @@ -68,7 +69,7 @@ public void parse(final SelectStatement selectStatement) { if (sqlExpression instanceof SQLNumberExpression) { int rowCount = ((SQLNumberExpression) sqlExpression).getNumber().intValue(); rowCountValue = new LimitValue(rowCount, -1, false); - selectStatement.addSQLToken(new RowCountToken(beginPosition, rowCount)); + selectStatement.addSQLToken(new RowCountToken(beginPosition, endPosition - 1, rowCount)); } else if (sqlExpression instanceof SQLParameterMarkerExpression) { rowCountValue = new LimitValue(-1, ((SQLParameterMarkerExpression) sqlExpression).getIndex(), false); } else { diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/sqlserver/sql/SQLServerInsertParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/sqlserver/sql/SQLServerInsertParser.java index 3ceeb860dfb31..4cec2c9ff9a31 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/sqlserver/sql/SQLServerInsertParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/dialect/sqlserver/sql/SQLServerInsertParser.java @@ -31,7 +31,7 @@ */ public final class SQLServerInsertParser extends AbstractInsertParser { - public SQLServerInsertParser(final ShardingRule shardingRule, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { - super(shardingRule, shardingTableMetaData, lexerEngine, new SQLServerInsertClauseParserFacade(shardingRule, lexerEngine)); + public SQLServerInsertParser(final ShardingRule shardingRule, final String sql, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { + super(shardingRule, sql, shardingTableMetaData, lexerEngine, new SQLServerInsertClauseParserFacade(shardingRule, lexerEngine)); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/SQLParserFactory.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/SQLParserFactory.java index 53fb6f3d3f165..e9a9f308d8024 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/SQLParserFactory.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/SQLParserFactory.java @@ -80,7 +80,7 @@ public static SQLParser newInstance( if (DatabaseType.MySQL == dbType || DatabaseType.H2 == dbType) { return new AntlrParsingEngine(dbType, sql, shardingRule, shardingTableMetaData); } - return getDMLParser(dbType, tokenType, shardingRule, lexerEngine, shardingTableMetaData); + return getDMLParser(dbType, sql, tokenType, shardingRule, lexerEngine, shardingTableMetaData); } if (MySQLKeyword.REPLACE == tokenType) { if (DatabaseType.MySQL == dbType || DatabaseType.H2 == dbType) { @@ -134,10 +134,10 @@ private static SQLParser getDQLParser(final DatabaseType dbType, final ShardingR } private static SQLParser getDMLParser( - final DatabaseType dbType, final TokenType tokenType, final ShardingRule shardingRule, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { + final DatabaseType dbType, final String sql, final TokenType tokenType, final ShardingRule shardingRule, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { switch ((DefaultKeyword) tokenType) { case INSERT: - return InsertParserFactory.newInstance(dbType, shardingRule, lexerEngine, shardingTableMetaData); + return InsertParserFactory.newInstance(dbType, sql, shardingRule, lexerEngine, shardingTableMetaData); case UPDATE: return UpdateParserFactory.newInstance(dbType, shardingRule, lexerEngine); case DELETE: diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/dml/insert/AbstractInsertParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/dml/insert/AbstractInsertParser.java index 651b7b54ee5a4..e66c3ba54dd25 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/dml/insert/AbstractInsertParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/dml/insert/AbstractInsertParser.java @@ -49,18 +49,22 @@ public abstract class AbstractInsertParser implements SQLParser { private final AbstractInsertClauseParserFacade insertClauseParserFacade; - public AbstractInsertParser( - final ShardingRule shardingRule, final ShardingTableMetaData shardingTableMetaData, final LexerEngine lexerEngine, final AbstractInsertClauseParserFacade insertClauseParserFacade) { + private final String sql; + + public AbstractInsertParser(final ShardingRule shardingRule, + final String sql, final ShardingTableMetaData shardingTableMetaData, final LexerEngine lexerEngine, final AbstractInsertClauseParserFacade insertClauseParserFacade) { this.shardingRule = shardingRule; this.shardingTableMetaData = shardingTableMetaData; this.lexerEngine = lexerEngine; this.insertClauseParserFacade = insertClauseParserFacade; + this.sql = sql; } @Override public final DMLStatement parse() { lexerEngine.nextToken(); InsertStatement result = new InsertStatement(); + result.setLogicSQL(sql); insertClauseParserFacade.getInsertIntoClauseParser().parse(result); insertClauseParserFacade.getInsertColumnsClauseParser().parse(result, shardingTableMetaData); if (lexerEngine.equalAny(DefaultKeyword.SELECT, Symbol.LEFT_PAREN)) { diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/dml/insert/InsertParserFactory.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/dml/insert/InsertParserFactory.java index 82131acd461f5..fdc87843e7faa 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/dml/insert/InsertParserFactory.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/dml/insert/InsertParserFactory.java @@ -41,22 +41,24 @@ public final class InsertParserFactory { * Create insert parser instance. * * @param dbType database type + * @param sql sql * @param shardingRule databases and tables sharding rule * @param lexerEngine lexical analysis engine * @param shardingTableMetaData sharding meta data * @return insert parser instance */ - public static AbstractInsertParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { + public static AbstractInsertParser newInstance( + final DatabaseType dbType, final String sql, final ShardingRule shardingRule, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { switch (dbType) { case H2: case MySQL: - return new MySQLInsertParser(shardingRule, lexerEngine, shardingTableMetaData); + return new MySQLInsertParser(shardingRule, sql, lexerEngine, shardingTableMetaData); case Oracle: - return new OracleInsertParser(shardingRule, lexerEngine, shardingTableMetaData); + return new OracleInsertParser(shardingRule, sql, lexerEngine, shardingTableMetaData); case SQLServer: - return new SQLServerInsertParser(shardingRule, lexerEngine, shardingTableMetaData); + return new SQLServerInsertParser(shardingRule, sql, lexerEngine, shardingTableMetaData); case PostgreSQL: - return new PostgreSQLInsertParser(shardingRule, lexerEngine, shardingTableMetaData); + return new PostgreSQLInsertParser(shardingRule, sql, lexerEngine, shardingTableMetaData); default: throw new UnsupportedOperationException(String.format("Cannot support database [%s].", dbType)); } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/dml/select/AbstractSelectParser.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/dml/select/AbstractSelectParser.java index 764937a7ccd6d..31f1b0c7bdac9 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/dml/select/AbstractSelectParser.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/old/parser/sql/dml/select/AbstractSelectParser.java @@ -49,6 +49,7 @@ * Select parser. * * @author zhangliang + * @author panjuan */ @RequiredArgsConstructor @Getter(AccessLevel.PROTECTED) @@ -238,7 +239,7 @@ private boolean isSameQualifiedName(final SelectItem selectItem, final OrderItem private void appendDerivedOrderBy(final SelectStatement selectStatement) { if (!selectStatement.getGroupByItems().isEmpty() && selectStatement.getOrderByItems().isEmpty()) { selectStatement.getOrderByItems().addAll(selectStatement.getGroupByItems()); - selectStatement.addSQLToken(new OrderByToken(selectStatement.getGroupByLastIndex() + 1)); + selectStatement.addSQLToken(new OrderByToken(selectStatement.getGroupByLastIndex() + 1, selectStatement.getGroupByLastIndex() + 1)); } } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-mysql/src/main/java/org/apache/shardingsphere/core/parse/optimizer/MySQLSelectOptimizer.java b/sharding-core/sharding-core-parse/sharding-core-parse-mysql/src/main/java/org/apache/shardingsphere/core/parse/optimizer/MySQLSelectOptimizer.java index 55b0ef754e54d..43ac7b953d646 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-mysql/src/main/java/org/apache/shardingsphere/core/parse/optimizer/MySQLSelectOptimizer.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-mysql/src/main/java/org/apache/shardingsphere/core/parse/optimizer/MySQLSelectOptimizer.java @@ -42,6 +42,7 @@ * Select optimizer for MySQL. * * @author duhongjun + * @author panjuan */ public final class MySQLSelectOptimizer implements SQLStatementOptimizer { @@ -173,7 +174,7 @@ private boolean isSameQualifiedName(final SelectItem selectItem, final OrderItem private void appendDerivedOrderBy(final SelectStatement selectStatement) { if (!selectStatement.getGroupByItems().isEmpty() && selectStatement.getOrderByItems().isEmpty()) { selectStatement.getOrderByItems().addAll(selectStatement.getGroupByItems()); - selectStatement.addSQLToken(new OrderByToken(selectStatement.getGroupByLastIndex() + 1)); + selectStatement.addSQLToken(new OrderByToken(selectStatement.getGroupByLastIndex() + 1, selectStatement.getGroupByLastIndex() + 1)); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/token/GeneratedKeyTokenAssert.java b/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/token/GeneratedKeyTokenAssert.java deleted file mode 100644 index 21ecd631bf6d7..0000000000000 --- a/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/token/GeneratedKeyTokenAssert.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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.core.parse.integrate.asserts.token; - -import com.google.common.base.Optional; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.core.parse.antlr.sql.token.GeneratedKeyToken; -import org.apache.shardingsphere.core.parse.antlr.sql.token.SQLToken; -import org.apache.shardingsphere.core.parse.integrate.asserts.SQLStatementAssertMessage; -import org.apache.shardingsphere.core.parse.integrate.jaxb.token.ExpectedGeneratedKeyToken; -import org.apache.shardingsphere.core.parse.integrate.jaxb.token.ExpectedTokens; -import org.apache.shardingsphere.test.sql.SQLCaseType; - -import java.util.Collection; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; - -/** - * Generated key token assert. - * - * @author zhangliang - */ -@RequiredArgsConstructor -final class GeneratedKeyTokenAssert { - - private final SQLCaseType sqlCaseType; - - private final SQLStatementAssertMessage assertMessage; - - void assertGeneratedKeyToken(final Collection actual, final ExpectedTokens expected) { - Optional generatedKeyToken = getGeneratedKeyToken(actual); - if (generatedKeyToken.isPresent()) { - assertGeneratedKeyToken(generatedKeyToken.get(), expected.getGeneratedKeyToken()); - } else { - assertNull(assertMessage.getFullAssertMessage("Generated key token should not exist: "), expected.getGeneratedKeyToken()); - } - } - - private void assertGeneratedKeyToken(final GeneratedKeyToken actual, final ExpectedGeneratedKeyToken expected) { - if (SQLCaseType.Placeholder == sqlCaseType) { - assertThat(assertMessage.getFullAssertMessage("Generated key token begin position assertion error: "), actual.getStartIndex(), is(expected.getPlaceholderBeginPosition())); - } else { - assertThat(assertMessage.getFullAssertMessage("Generated key token begin position assertion error: "), actual.getStartIndex(), is(expected.getLiteralBeginPosition())); - } - } - - private Optional getGeneratedKeyToken(final Collection actual) { - for (SQLToken each : actual) { - if (each instanceof GeneratedKeyToken) { - return Optional.of((GeneratedKeyToken) each); - } - } - return Optional.absent(); - } -} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/token/TableTokenAssert.java b/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/token/TableTokenAssert.java index f9346b3575f8b..560e78e023acc 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/token/TableTokenAssert.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/token/TableTokenAssert.java @@ -55,7 +55,6 @@ void assertTableTokens(final Collection actual, final ExpectedTokens e private void assertTableToken(final TableToken actual, final ExpectedTableToken expected) { assertThat(assertMessage.getFullAssertMessage("Table tokens start index assertion error: "), actual.getStartIndex(), is(expected.getStartIndex())); - assertThat(assertMessage.getFullAssertMessage("Table tokens skipped schema name length assertion error: "), actual.getLength(), is(expected.getLength())); assertThat(assertMessage.getFullAssertMessage("Table tokens table name assertion error: "), actual.getTableName(), is(expected.getTableName())); assertThat(assertMessage.getFullAssertMessage("Table tokens left delimiter assertion error: "), actual.getQuoteCharacter(), is(QuoteCharacter.getQuoteCharacter(null == expected.getLeftDelimiter() ? "" : expected.getLeftDelimiter()))); diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/token/TokenAssert.java b/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/token/TokenAssert.java index ebb8c368b09ad..5f3b6afd0cb2d 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/token/TokenAssert.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/token/TokenAssert.java @@ -40,8 +40,6 @@ public final class TokenAssert { private final ItemsTokenAssert itemsTokenAssert; - private final GeneratedKeyTokenAssert generatedKeyTokenAssert; - private final InsertValuesTokenAssert insertValuesTokenAssert; private final OrderByTokenAssert orderByTokenAssert; @@ -61,7 +59,6 @@ public TokenAssert(final SQLCaseType sqlCaseType, final SQLStatementAssertMessag schemaTokenAssert = new SchemaTokenAssert(assertMessage); indexTokenAssert = new IndexTokenAssert(assertMessage); itemsTokenAssert = new ItemsTokenAssert(assertMessage); - generatedKeyTokenAssert = new GeneratedKeyTokenAssert(sqlCaseType, assertMessage); insertValuesTokenAssert = new InsertValuesTokenAssert(assertMessage); orderByTokenAssert = new OrderByTokenAssert(sqlCaseType, assertMessage); offsetTokenAssert = new OffsetTokenAssert(sqlCaseType, assertMessage); @@ -82,13 +79,12 @@ public void assertTokens(final Collection actual, final ExpectedTokens schemaTokenAssert.assertSchemaTokens(actual, expected); indexTokenAssert.assertIndexToken(actual, expected); itemsTokenAssert.assertItemsToken(actual, expected); - generatedKeyTokenAssert.assertGeneratedKeyToken(actual, expected); insertValuesTokenAssert.assertInsertValuesToken(actual, expected); orderByTokenAssert.assertOrderByToken(actual, expected); offsetTokenAssert.assertOffsetToken(actual, expected); rowCountTokenAssert.assertRowCountToken(actual, expected); aggregationDistinctTokenAssert.assertAggregationDistinctTokens(actual, expected); - if(DatabaseType.MySQL == databaseType) { + if (DatabaseType.MySQL == databaseType) { encryptColumnTokenAssert.assertEncryptColumnsToken(actual, expected); } } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/EncryptSQLRewriteEngine.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/EncryptSQLRewriteEngine.java index 35d2a1f381a88..b502bd83e25f0 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/EncryptSQLRewriteEngine.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/EncryptSQLRewriteEngine.java @@ -140,7 +140,7 @@ private void appendTokensAndPlaceholders(final SQLBuilder sqlBuilder) { } else if (each instanceof EncryptColumnToken) { appendEncryptColumnPlaceholder(sqlBuilder, (EncryptColumnToken) each, count); } else if (each instanceof RemoveToken) { - appendRest(sqlBuilder, count, ((RemoveToken) each).getStopIndex()); + appendRest(sqlBuilder, count, each.getStopIndex() + 1); } count++; } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngine.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngine.java index f821737b51675..cfceadab1c213 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngine.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngine.java @@ -203,13 +203,13 @@ private void appendTokensAndPlaceholders(final boolean isRewrite, final SQLBuild } else if (each instanceof OffsetToken) { appendLimitOffsetToken(sqlBuilder, (OffsetToken) each, count, isRewrite); } else if (each instanceof OrderByToken) { - appendOrderByToken(sqlBuilder, count, isRewrite); + appendOrderByToken(sqlBuilder, (OrderByToken) each, count, isRewrite); } else if (each instanceof AggregationDistinctToken) { appendAggregationDistinctPlaceholder(sqlBuilder, (AggregationDistinctToken) each, count, isRewrite); } else if (each instanceof EncryptColumnToken) { appendEncryptColumnPlaceholder(sqlBuilder, (EncryptColumnToken) each, count); } else if (each instanceof RemoveToken) { - appendRest(sqlBuilder, count, ((RemoveToken) each).getStopIndex()); + appendRest(sqlBuilder, count, each.getStopIndex() + 1); } count++; } @@ -217,8 +217,7 @@ private void appendTokensAndPlaceholders(final boolean isRewrite, final SQLBuild private void appendTablePlaceholder(final SQLBuilder sqlBuilder, final TableToken tableToken, final int count) { sqlBuilder.appendPlaceholder(new TablePlaceholder(tableToken.getTableName().toLowerCase(), tableToken.getQuoteCharacter())); - int startIndex = tableToken.getStartIndex() + tableToken.getLength(); - appendRest(sqlBuilder, count, startIndex); + appendRest(sqlBuilder, count, tableToken.getStopIndex() + 1); } private void appendSchemaPlaceholder(final SQLBuilder sqlBuilder, final SchemaToken schemaToken, final int count) { @@ -247,7 +246,7 @@ private void appendItemsToken(final SQLBuilder sqlBuilder, final ItemsToken item sqlBuilder.appendLiterals(SQLUtil.getOriginalValue(itemsToken.getItems().get(i), databaseType)); } } - appendRest(sqlBuilder, count, itemsToken.getStartIndex()); + appendRest(sqlBuilder, count, itemsToken.getStopIndex() + 1); } private void appendInsertValuesToken(final SQLBuilder sqlBuilder, final InsertValuesToken insertValuesToken, final int count, final InsertOptimizeResult insertOptimizeResult) { @@ -255,7 +254,7 @@ private void appendInsertValuesToken(final SQLBuilder sqlBuilder, final InsertVa encryptInsertOptimizeResultUnit(insertOptimizeResult.getColumnNames(), each); } sqlBuilder.appendPlaceholder(new InsertValuesPlaceholder(sqlStatement.getTables().getSingleTableName(), insertOptimizeResult.getColumnNames(), insertOptimizeResult.getUnits())); - appendRest(sqlBuilder, count, originalSQL.length()); + appendRest(sqlBuilder, count, insertValuesToken.getStopIndex() + 1); } private void appendInsertSetToken(final SQLBuilder sqlBuilder, final InsertSetToken insertSetToken, final int count, final InsertOptimizeResult insertOptimizeResult) { @@ -263,7 +262,7 @@ private void appendInsertSetToken(final SQLBuilder sqlBuilder, final InsertSetTo encryptInsertOptimizeResultUnit(insertOptimizeResult.getColumnNames(), each); } sqlBuilder.appendPlaceholder(new InsertSetPlaceholder(sqlStatement.getTables().getSingleTableName(), insertOptimizeResult.getColumnNames(), insertOptimizeResult.getUnits())); - appendRest(sqlBuilder, count, originalSQL.length()); + appendRest(sqlBuilder, count, insertSetToken.getStopIndex() + 1); } private void encryptInsertOptimizeResultUnit(final Collection columnNames, final InsertOptimizeResultUnit unit) { @@ -293,17 +292,15 @@ private void appendLimitRowCount(final SQLBuilder sqlBuilder, final RowCountToke } else { sqlBuilder.appendLiterals(String.valueOf(limit.isNeedRewriteRowCount(databaseType) ? rowCountToken.getRowCount() + limit.getOffsetValue() : rowCountToken.getRowCount())); } - int startIndex = rowCountToken.getStartIndex() + String.valueOf(rowCountToken.getRowCount()).length(); - appendRest(sqlBuilder, count, startIndex); + appendRest(sqlBuilder, count, rowCountToken.getStopIndex() + 1); } private void appendLimitOffsetToken(final SQLBuilder sqlBuilder, final OffsetToken offsetToken, final int count, final boolean isRewrite) { sqlBuilder.appendLiterals(isRewrite ? "0" : String.valueOf(offsetToken.getOffset())); - int startIndex = offsetToken.getStartIndex() + String.valueOf(offsetToken.getOffset()).length(); - appendRest(sqlBuilder, count, startIndex); + appendRest(sqlBuilder, count, offsetToken.getStopIndex() + 1); } - private void appendOrderByToken(final SQLBuilder sqlBuilder, final int count, final boolean isRewrite) { + private void appendOrderByToken(final SQLBuilder sqlBuilder, final OrderByToken orderByToken, final int count, final boolean isRewrite) { SelectStatement selectStatement = (SelectStatement) sqlStatement; if (isRewrite) { StringBuilder orderByLiterals = new StringBuilder(); @@ -322,7 +319,7 @@ private void appendOrderByToken(final SQLBuilder sqlBuilder, final int count, fi orderByLiterals.append(" "); sqlBuilder.appendLiterals(orderByLiterals.toString()); } - appendRest(sqlBuilder, count, selectStatement.getGroupByLastIndex() + 1); + appendRest(sqlBuilder, count, orderByToken.getStopIndex() + 1); } private void appendAggregationDistinctPlaceholder(final SQLBuilder sqlBuilder, final AggregationDistinctToken distinctToken, final int count, final boolean isRewrite) { @@ -500,7 +497,7 @@ private String getEncryptAssistedColumnName(final EncryptColumnToken encryptColu private void appendRest(final SQLBuilder sqlBuilder, final int count, final int startIndex) { int stopPosition = sqlTokens.size() - 1 == count ? originalSQL.length() : sqlTokens.get(count + 1).getStartIndex(); - sqlBuilder.appendLiterals(originalSQL.substring(startIndex, stopPosition)); + sqlBuilder.appendLiterals(originalSQL.substring(startIndex > originalSQL.length() ? originalSQL.length() : startIndex, stopPosition)); } /** diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngineTest.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngineTest.java index 7b4b687b8681d..279be8a7e0859 100644 --- a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngineTest.java +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngineTest.java @@ -140,9 +140,9 @@ public void assertRewriteForTableName() { List parameters = new ArrayList<>(2); parameters.add(1); parameters.add("x"); - selectStatement.addSQLToken(new TableToken(7, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new TableToken(31, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new TableToken(47, "table_x", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(7, 13, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new TableToken(31, 37, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new TableToken(47, 53, "table_x", QuoteCharacter.NONE)); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, @@ -152,7 +152,7 @@ public void assertRewriteForTableName() { @Test public void assertRewriteForOrderByAndGroupByDerivedColumns() { - selectStatement.addSQLToken(new TableToken(18, "table_x", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(18, 24, "table_x", QuoteCharacter.NONE)); ItemsToken itemsToken = new ItemsToken(12); itemsToken.getItems().addAll(Arrays.asList("x.id as GROUP_BY_DERIVED_0", "x.name as ORDER_BY_DERIVED_0")); selectStatement.addSQLToken(itemsToken); @@ -166,7 +166,7 @@ public void assertRewriteForOrderByAndGroupByDerivedColumns() { @Test public void assertRewriteForAggregationDerivedColumns() { - selectStatement.addSQLToken(new TableToken(23, "table_x", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(23, 29, "table_x", QuoteCharacter.NONE)); ItemsToken itemsToken = new ItemsToken(17); itemsToken.getItems().addAll(Arrays.asList("COUNT(x.age) as AVG_DERIVED_COUNT_0", "SUM(x.age) as AVG_DERIVED_SUM_0")); selectStatement.addSQLToken(itemsToken); @@ -181,8 +181,8 @@ public void assertRewriteForAggregationDerivedColumns() { public void assertRewriteForAutoGeneratedKeyColumn() { insertStatement.getTables().add(new Table("table_x", null)); insertStatement.setParametersIndex(2); - insertStatement.addSQLToken(new TableToken(12, "table_x", QuoteCharacter.NONE, 0)); - insertStatement.addSQLToken(new InsertValuesToken(19)); + insertStatement.addSQLToken(new TableToken(12, 18, "table_x", QuoteCharacter.NONE)); + insertStatement.addSQLToken(new InsertValuesToken(19, 44)); InsertOptimizeResult insertOptimizeResult = new InsertOptimizeResult(InsertType.VALUES, Arrays.asList("name", "age", "id")); Object[] parameters = {"x", 1, 1}; SQLExpression[] sqlExpressions = {new SQLParameterMarkerExpression(0), new SQLParameterMarkerExpression(1), new SQLParameterMarkerExpression(2)}; @@ -202,8 +202,8 @@ public void assertRewriteForAutoGeneratedKeyColumnWithoutColumnsWithParameter() insertStatement.getColumnNames().add("id"); insertStatement.getTables().add(new Table("table_x", null)); insertStatement.setParametersIndex(1); - insertStatement.addSQLToken(new TableToken(12, "`table_x`", QuoteCharacter.BACK_QUOTE, 0)); - insertStatement.addSQLToken(new InsertValuesToken(21)); + insertStatement.addSQLToken(new TableToken(12, 20, "`table_x`", QuoteCharacter.BACK_QUOTE)); + insertStatement.addSQLToken(new InsertValuesToken(21, 31)); InsertOptimizeResult insertOptimizeResult = new InsertOptimizeResult(InsertType.VALUES, Arrays.asList("name", "id")); Object[] parameters = {"Bill", 1}; SQLExpression[] sqlExpressions = {new SQLParameterMarkerExpression(0), new SQLParameterMarkerExpression(1)}; @@ -222,8 +222,8 @@ public void assertRewriteForAutoGeneratedKeyColumnWithoutColumnsWithoutParameter insertStatement.getColumnNames().add("name"); insertStatement.getColumnNames().add("id"); insertStatement.getTables().add(new Table("table_x", null)); - insertStatement.addSQLToken(new TableToken(12, "`table_x`", QuoteCharacter.BACK_QUOTE, 0)); - insertStatement.addSQLToken(new InsertValuesToken(21)); + insertStatement.addSQLToken(new TableToken(12, 20, "`table_x`", QuoteCharacter.BACK_QUOTE)); + insertStatement.addSQLToken(new InsertValuesToken(21, 32)); InsertOptimizeResult insertOptimizeResult = new InsertOptimizeResult(InsertType.VALUES, Arrays.asList("name", "id")); SQLExpression[] sqlExpressions = {new SQLNumberExpression(10), new SQLNumberExpression(1)}; insertOptimizeResult.addUnit(sqlExpressions, new Object[0]); @@ -244,8 +244,8 @@ public void assertRewriteColumnWithoutColumnsWithoutParameter() { List parameters = new ArrayList<>(2); parameters.add("x"); parameters.add(1); - insertStatement.addSQLToken(new TableToken(12, "`table_x`", QuoteCharacter.BACK_QUOTE, 0)); - insertStatement.addSQLToken(new InsertValuesToken(21)); + insertStatement.addSQLToken(new TableToken(12, 20, "`table_x`", QuoteCharacter.BACK_QUOTE)); + insertStatement.addSQLToken(new InsertValuesToken(21, 35)); InsertOptimizeResult insertOptimizeResult = new InsertOptimizeResult(InsertType.VALUES, Arrays.asList("name", "id")); SQLExpression[] sqlExpressions = {new SQLNumberExpression(10), new SQLNumberExpression(1)}; insertOptimizeResult.addUnit(sqlExpressions, new Object[0]); @@ -263,8 +263,8 @@ public void assertRewriteColumnWithoutColumnsWithParameter() { insertStatement.getColumnNames().add("name"); insertStatement.getColumnNames().add("id"); insertStatement.getTables().add(new Table("table_x", null)); - insertStatement.addSQLToken(new TableToken(12, "`table_x`", QuoteCharacter.BACK_QUOTE, 0)); - insertStatement.addSQLToken(new InsertValuesToken(21)); + insertStatement.addSQLToken(new TableToken(12, 20, "`table_x`", QuoteCharacter.BACK_QUOTE)); + insertStatement.addSQLToken(new InsertValuesToken(21, 34)); InsertOptimizeResult insertOptimizeResult = new InsertOptimizeResult(InsertType.VALUES, Arrays.asList("name", "id")); SQLExpression[] sqlExpressions = {new SQLParameterMarkerExpression(0), new SQLParameterMarkerExpression(1)}; Object[] parameters = {"x", 1}; @@ -283,9 +283,9 @@ public void assertRewriteForLimit() { selectStatement.setLimit(new Limit()); selectStatement.getLimit().setOffset(new LimitValue(2, -1, true)); selectStatement.getLimit().setRowCount(new LimitValue(2, -1, false)); - selectStatement.addSQLToken(new TableToken(17, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new OffsetToken(33, 2)); - selectStatement.addSQLToken(new RowCountToken(36, 2)); + selectStatement.addSQLToken(new TableToken(17, 23, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new OffsetToken(33, 33, 2)); + selectStatement.addSQLToken(new RowCountToken(36, 36, 2)); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id FROM table_x x LIMIT 2, 2", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); @@ -297,9 +297,9 @@ public void assertRewriteForRowNum() { selectStatement.setLimit(new Limit()); selectStatement.getLimit().setOffset(new LimitValue(2, -1, true)); selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false)); - selectStatement.addSQLToken(new TableToken(68, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new OffsetToken(119, 2)); - selectStatement.addSQLToken(new RowCountToken(98, 4)); + selectStatement.addSQLToken(new TableToken(68, 74, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new OffsetToken(119, 119, 2)); + selectStatement.addSQLToken(new RowCountToken(98, 98, 4)); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2", @@ -313,9 +313,9 @@ public void assertRewriteForTopAndRowNumber() { selectStatement.setLimit(new Limit()); selectStatement.getLimit().setOffset(new LimitValue(2, -1, true)); selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false)); - selectStatement.addSQLToken(new TableToken(85, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new OffsetToken(123, 2)); - selectStatement.addSQLToken(new RowCountToken(26, 4)); + selectStatement.addSQLToken(new TableToken(85, 91, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new OffsetToken(123, 123, 2)); + selectStatement.addSQLToken(new RowCountToken(26, 26, 4)); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, @@ -332,9 +332,9 @@ public void assertRewriteForLimitForMemoryGroupBy() { selectStatement.getLimit().setRowCount(new LimitValue(2, -1, false)); selectStatement.getOrderByItems().add(new OrderItem("x", "id", OrderDirection.ASC, OrderDirection.ASC)); selectStatement.getGroupByItems().add(new OrderItem("x", "id", OrderDirection.DESC, OrderDirection.ASC)); - selectStatement.addSQLToken(new TableToken(17, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new OffsetToken(33, 2)); - selectStatement.addSQLToken(new RowCountToken(36, 2)); + selectStatement.addSQLToken(new TableToken(17, 23, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new OffsetToken(33, 33, 2)); + selectStatement.addSQLToken(new RowCountToken(36, 36, 2)); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id FROM table_x x LIMIT 2, 2", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); @@ -346,9 +346,9 @@ public void assertRewriteForRowNumForMemoryGroupBy() { selectStatement.setLimit(new Limit()); selectStatement.getLimit().setOffset(new LimitValue(2, -1, true)); selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false)); - selectStatement.addSQLToken(new TableToken(68, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new OffsetToken(119, 2)); - selectStatement.addSQLToken(new RowCountToken(98, 4)); + selectStatement.addSQLToken(new TableToken(68, 74, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new OffsetToken(119, 119, 2)); + selectStatement.addSQLToken(new RowCountToken(98, 98, 4)); selectStatement.getOrderByItems().add(new OrderItem("x", "id", OrderDirection.ASC, OrderDirection.ASC)); selectStatement.getGroupByItems().add(new OrderItem("x", "id", OrderDirection.DESC, OrderDirection.ASC)); routeResult = new SQLRouteResult(selectStatement); @@ -364,9 +364,9 @@ public void assertRewriteForTopAndRowNumberForMemoryGroupBy() { selectStatement.setLimit(new Limit()); selectStatement.getLimit().setOffset(new LimitValue(2, -1, true)); selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false)); - selectStatement.addSQLToken(new TableToken(85, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new OffsetToken(123, 2)); - selectStatement.addSQLToken(new RowCountToken(26, 4)); + selectStatement.addSQLToken(new TableToken(85, 91, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new OffsetToken(123, 123, 2)); + selectStatement.addSQLToken(new RowCountToken(26, 26, 4)); selectStatement.getOrderByItems().add(new OrderItem("x", "id", OrderDirection.ASC, OrderDirection.ASC)); selectStatement.getGroupByItems().add(new OrderItem("x", "id", OrderDirection.DESC, OrderDirection.ASC)); routeResult = new SQLRouteResult(selectStatement); @@ -383,9 +383,9 @@ public void assertRewriteForLimitForNotRewriteLimit() { selectStatement.setLimit(new Limit()); selectStatement.getLimit().setOffset(new LimitValue(2, -1, true)); selectStatement.getLimit().setRowCount(new LimitValue(2, -1, false)); - selectStatement.addSQLToken(new TableToken(17, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new OffsetToken(33, 2)); - selectStatement.addSQLToken(new RowCountToken(36, 2)); + selectStatement.addSQLToken(new TableToken(17, 23, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new OffsetToken(33, 33, 2)); + selectStatement.addSQLToken(new RowCountToken(36, 36, 2)); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id FROM table_x x LIMIT 2, 2", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); @@ -397,9 +397,9 @@ public void assertRewriteForRowNumForNotRewriteLimit() { selectStatement.setLimit(new Limit()); selectStatement.getLimit().setOffset(new LimitValue(2, -1, true)); selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false)); - selectStatement.addSQLToken(new TableToken(68, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new OffsetToken(119, 2)); - selectStatement.addSQLToken(new RowCountToken(98, 4)); + selectStatement.addSQLToken(new TableToken(68, 74, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new OffsetToken(119, 119, 2)); + selectStatement.addSQLToken(new RowCountToken(98, 98, 4)); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2", @@ -413,9 +413,9 @@ public void assertRewriteForTopAndRowNumberForNotRewriteLimit() { selectStatement.setLimit(new Limit()); selectStatement.getLimit().setOffset(new LimitValue(2, -1, true)); selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false)); - selectStatement.addSQLToken(new TableToken(85, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new OffsetToken(123, 2)); - selectStatement.addSQLToken(new RowCountToken(26, 4)); + selectStatement.addSQLToken(new TableToken(85, 91, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new OffsetToken(123, 123, 2)); + selectStatement.addSQLToken(new RowCountToken(26, 26, 4)); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, @@ -430,8 +430,8 @@ public void assertRewriteForDerivedOrderBy() { selectStatement.setGroupByLastIndex(60); selectStatement.getOrderByItems().add(new OrderItem("x", "id", OrderDirection.ASC, OrderDirection.ASC)); selectStatement.getOrderByItems().add(new OrderItem("x", "name", OrderDirection.DESC, OrderDirection.ASC)); - selectStatement.addSQLToken(new TableToken(25, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new OrderByToken(61)); + selectStatement.addSQLToken(new TableToken(25, 31, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new OrderByToken(61, 61)); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, @@ -445,9 +445,9 @@ public void assertGenerateSQL() { List parameters = new ArrayList<>(2); parameters.add(1); parameters.add("x"); - selectStatement.addSQLToken(new TableToken(7, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new TableToken(31, "table_x", QuoteCharacter.NONE, 0)); - selectStatement.addSQLToken(new TableToken(58, "table_x", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(7, 13, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new TableToken(31, 37, "table_x", QuoteCharacter.NONE)); + selectStatement.addSQLToken(new TableToken(58, 64, "table_x", QuoteCharacter.NONE)); selectStatement.getTables().add(new Table("table_x", "x")); selectStatement.getTables().add(new Table("table_y", "y")); routeResult = new SQLRouteResult(selectStatement); @@ -464,7 +464,7 @@ public void assertGenerateSQL() { public void assertSchemaTokenRewriteForTableName() { tableTokens = new HashMap<>(1, 1); tableTokens.put("table_x", "table_y"); - selectStatement.addSQLToken(new TableToken(18, "table_x", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(18, 24, "table_x", QuoteCharacter.NONE)); selectStatement.addSQLToken(new SchemaToken(29, 35, "table_x")); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); @@ -475,7 +475,7 @@ public void assertSchemaTokenRewriteForTableName() { @Test public void assertIndexTokenForIndexNameTableName() { selectStatement.addSQLToken(new IndexToken(13, 22, "table_x")); - selectStatement.addSQLToken(new TableToken(27, "table_x", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(27, 33, "table_x", QuoteCharacter.NONE)); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "CREATE INDEX index_name ON table_x ('column')", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); @@ -485,7 +485,7 @@ public void assertIndexTokenForIndexNameTableName() { @Test public void assertIndexTokenForIndexNameTableNameWithoutLogicTableName() { selectStatement.addSQLToken(new IndexToken(13, 23, "")); - selectStatement.addSQLToken(new TableToken(28, "table_x", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(28, 34, "table_x", QuoteCharacter.NONE)); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "CREATE INDEX logic_index ON table_x ('column')", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); @@ -494,7 +494,7 @@ public void assertIndexTokenForIndexNameTableNameWithoutLogicTableName() { @Test public void assertTableTokenWithoutBackQuoteForShow() { - showTablesStatement.addSQLToken(new TableToken(18, "table_x", QuoteCharacter.NONE, 0)); + showTablesStatement.addSQLToken(new TableToken(18, 24, "table_x", QuoteCharacter.NONE)); routeResult = new SQLRouteResult(showTablesStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM table_x", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule, shardingDataSourceMetaData).getSql(), is("SHOW COLUMNS FROM table_1")); @@ -502,7 +502,7 @@ public void assertTableTokenWithoutBackQuoteForShow() { @Test public void assertTableTokenWithoutBackQuoteFromSchemaForShow() { - showTablesStatement.addSQLToken(new TableToken(18, "table_x", QuoteCharacter.NONE, 0)); + showTablesStatement.addSQLToken(new TableToken(18, 24, "table_x", QuoteCharacter.NONE)); showTablesStatement.addSQLToken(new SchemaToken(31, 43, "table_x")); routeResult = new SQLRouteResult(showTablesStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM table_x FROM 'sharding_db'", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); @@ -513,7 +513,7 @@ public void assertTableTokenWithoutBackQuoteFromSchemaForShow() { @Test public void assertTableTokenWithBackQuoteForShow() { - showTablesStatement.addSQLToken(new TableToken(18, "`table_x`", QuoteCharacter.BACK_QUOTE, 0)); + showTablesStatement.addSQLToken(new TableToken(18, 26, "`table_x`", QuoteCharacter.BACK_QUOTE)); routeResult = new SQLRouteResult(showTablesStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM `table_x`", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule, shardingDataSourceMetaData).getSql(), is("SHOW COLUMNS FROM `table_1`")); @@ -521,7 +521,7 @@ public void assertTableTokenWithBackQuoteForShow() { @Test public void assertTableTokenWithBackQuoteFromSchemaForShow() { - showTablesStatement.addSQLToken(new TableToken(18, "`table_x`", QuoteCharacter.BACK_QUOTE, 0)); + showTablesStatement.addSQLToken(new TableToken(18, 26, "`table_x`", QuoteCharacter.BACK_QUOTE)); routeResult = new SQLRouteResult(showTablesStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM `table_x` FROM 'sharding_db'", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule, shardingDataSourceMetaData).getSql(), is("SHOW COLUMNS FROM `table_1` FROM 'sharding_db'")); @@ -529,7 +529,7 @@ public void assertTableTokenWithBackQuoteFromSchemaForShow() { @Test public void assertTableTokenWithSchemaForShow() { - showTablesStatement.addSQLToken(new TableToken(18, "table_x", QuoteCharacter.NONE, "sharding_db".length() + 1)); + showTablesStatement.addSQLToken(new TableToken(18, 36, "table_x", QuoteCharacter.NONE)); routeResult = new SQLRouteResult(showTablesStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM sharding_db.table_x", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule, shardingDataSourceMetaData).getSql(), is("SHOW COLUMNS FROM table_1")); @@ -537,7 +537,7 @@ public void assertTableTokenWithSchemaForShow() { @Test public void assertTableTokenWithSchemaFromSchemaForShow() { - showTablesStatement.addSQLToken(new TableToken(18, "table_x", QuoteCharacter.NONE, "sharding_db".length() + 1)); + showTablesStatement.addSQLToken(new TableToken(18, 36, "table_x", QuoteCharacter.NONE)); routeResult = new SQLRouteResult(showTablesStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM sharding_db.table_x FROM sharding_db", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); @@ -546,7 +546,7 @@ public void assertTableTokenWithSchemaFromSchemaForShow() { @Test public void assertTableTokenWithBackQuoteWithSchemaForShow() { - showTablesStatement.addSQLToken(new TableToken(18, "`table_x`", QuoteCharacter.BACK_QUOTE, "sharding_db".length() + 1)); + showTablesStatement.addSQLToken(new TableToken(18, 38, "`table_x`", QuoteCharacter.BACK_QUOTE)); routeResult = new SQLRouteResult(showTablesStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM sharding_db.`table_x`", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule, shardingDataSourceMetaData).getSql(), is("SHOW COLUMNS FROM `table_1`")); @@ -554,7 +554,7 @@ public void assertTableTokenWithBackQuoteWithSchemaForShow() { @Test public void assertTableTokenWithBackQuoteWithSchemaFromSchemaForShow() { - showTablesStatement.addSQLToken(new TableToken(18, "`table_x`", QuoteCharacter.BACK_QUOTE, "sharding_db".length() + 1)); + showTablesStatement.addSQLToken(new TableToken(18, 38, "`table_x`", QuoteCharacter.BACK_QUOTE)); routeResult = new SQLRouteResult(showTablesStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM sharding_db.`table_x` FROM sharding_db", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); @@ -563,7 +563,7 @@ public void assertTableTokenWithBackQuoteWithSchemaFromSchemaForShow() { @Test public void assertTableTokenWithSchemaWithBackQuoteForShow() { - showTablesStatement.addSQLToken(new TableToken(18, "`table_x`", QuoteCharacter.BACK_QUOTE, "`sharding_db`".length() + 1)); + showTablesStatement.addSQLToken(new TableToken(18, 40, "`table_x`", QuoteCharacter.BACK_QUOTE)); routeResult = new SQLRouteResult(showTablesStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM `sharding_db`.`table_x`", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule, shardingDataSourceMetaData).getSql(), is("SHOW COLUMNS FROM `table_1`")); @@ -571,7 +571,7 @@ public void assertTableTokenWithSchemaWithBackQuoteForShow() { @Test public void assertTableTokenWithSchemaWithBackQuoteFromSchemaForShow() { - showTablesStatement.addSQLToken(new TableToken(18, "`table_x`", QuoteCharacter.BACK_QUOTE, "`sharding_db`".length() + 1)); + showTablesStatement.addSQLToken(new TableToken(18, 40, "`table_x`", QuoteCharacter.BACK_QUOTE)); routeResult = new SQLRouteResult(showTablesStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM `sharding_db`.`table_x` FROM sharding_db", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); @@ -580,7 +580,7 @@ public void assertTableTokenWithSchemaWithBackQuoteFromSchemaForShow() { @Test public void assertTableTokenWithSchemaForSelect() { - selectStatement.addSQLToken(new TableToken(14, "table_x", QuoteCharacter.NONE, "sharding_db".length() + 1)); + selectStatement.addSQLToken(new TableToken(14, 32, "table_x", QuoteCharacter.NONE)); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(selectStatement.getLimit()); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM sharding_db.table_x", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); @@ -589,7 +589,7 @@ public void assertTableTokenWithSchemaForSelect() { @Test public void assertTableTokenWithSchemaForInsert() { - insertStatement.addSQLToken(new TableToken(12, "table_x", QuoteCharacter.NONE, "sharding_db".length() + 1)); + insertStatement.addSQLToken(new TableToken(12, 30, "table_x", QuoteCharacter.NONE)); routeResult = new SQLRouteResult(insertStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "INSERT INTO sharding_db.table_x (order_id, user_id, status) values (1, 1, 'OK')", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); @@ -599,7 +599,7 @@ public void assertTableTokenWithSchemaForInsert() { @Test public void assertTableTokenWithSchemaForUpdate() { - updateStatement.addSQLToken(new TableToken(7, "table_x", QuoteCharacter.NONE, "`sharding_db`".length() + 1)); + updateStatement.addSQLToken(new TableToken(7, 27, "table_x", QuoteCharacter.NONE)); routeResult = new SQLRouteResult(updateStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "UPDATE `sharding_db`.table_x SET user_id=1 WHERE order_id=1", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); @@ -608,7 +608,7 @@ public void assertTableTokenWithSchemaForUpdate() { @Test public void assertTableTokenWithSchemaForDelete() { - deleteStatement.addSQLToken(new TableToken(12, "`table_x`", QuoteCharacter.BACK_QUOTE, "`sharding_db`".length() + 1)); + deleteStatement.addSQLToken(new TableToken(12, 34, "`table_x`", QuoteCharacter.BACK_QUOTE)); routeResult = new SQLRouteResult(deleteStatement); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "DELETE FROM `sharding_db`.`table_x` WHERE user_id=1", DatabaseType.MySQL, routeResult, Collections.emptyList(), null); assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule, shardingDataSourceMetaData).getSql(), is("DELETE FROM `table_1` WHERE user_id=1")); @@ -620,7 +620,7 @@ public void assertSelectEqualWithShardingEncryptor() { parameters.add(1); parameters.add("x"); Column column = new Column("id", "table_z"); - selectStatement.addSQLToken(new TableToken(15, "table_z", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(15, 21, "table_z", QuoteCharacter.NONE)); selectStatement.addSQLToken(new EncryptColumnToken(29, 32, column, true)); selectStatement.getEncryptConditions().getOrCondition().getAndConditions().add(new AndCondition()); selectStatement.getEncryptConditions().getOrCondition().getAndConditions().get(0).getConditions().add(new Condition(column, new SQLParameterMarkerExpression(0))); @@ -635,7 +635,7 @@ public void assertSelectEqualWithShardingEncryptor() { @Test public void assertSelectBetweenWithShardingEncryptor() { Column column = new Column("id", "table_z"); - selectStatement.addSQLToken(new TableToken(15, "table_z", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(15, 21, "table_z", QuoteCharacter.NONE)); selectStatement.addSQLToken(new EncryptColumnToken(29, 46, column, true)); selectStatement.getEncryptConditions().getOrCondition().getAndConditions().add(new AndCondition()); selectStatement.getEncryptConditions().getOrCondition().getAndConditions().get(0).getConditions().add(new Condition(column, new SQLNumberExpression(3), new SQLNumberExpression(5))); @@ -650,7 +650,7 @@ public void assertSelectBetweenWithShardingEncryptor() { @Test public void assertSelectInWithShardingEncryptor() { Column column = new Column("id", "table_z"); - selectStatement.addSQLToken(new TableToken(15, "table_z", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(15, 21, "table_z", QuoteCharacter.NONE)); selectStatement.addSQLToken(new EncryptColumnToken(29, 39, column, true)); selectStatement.getEncryptConditions().getOrCondition().getAndConditions().add(new AndCondition()); List sqlExpressions = new LinkedList<>(); @@ -671,7 +671,7 @@ public void assertSelectInWithShardingEncryptorWithParameter() { parameters.add(1); parameters.add(2); Column column = new Column("id", "table_z"); - selectStatement.addSQLToken(new TableToken(15, "table_z", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(15, 21, "table_z", QuoteCharacter.NONE)); selectStatement.addSQLToken(new EncryptColumnToken(29, 40, column, true)); selectStatement.addSQLToken(new EncryptColumnToken(45, 50, column, true)); selectStatement.getEncryptConditions().getOrCondition().getAndConditions().add(new AndCondition()); @@ -696,7 +696,7 @@ public void assertSelectEqualWithQueryAssistedShardingEncryptor() { parameters.add(1); parameters.add("k"); Column column = new Column("id", "table_k"); - selectStatement.addSQLToken(new TableToken(15, "table_k", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(15, 21, "table_k", QuoteCharacter.NONE)); selectStatement.addSQLToken(new EncryptColumnToken(29, 32, column, true)); selectStatement.getEncryptConditions().getOrCondition().getAndConditions().add(new AndCondition()); selectStatement.getEncryptConditions().getOrCondition().getAndConditions().get(0).getConditions().add(new Condition(column, new SQLParameterMarkerExpression(0))); @@ -711,7 +711,7 @@ public void assertSelectEqualWithQueryAssistedShardingEncryptor() { @Test public void assertSelectInWithQueryAssistedShardingEncryptor() { Column column = new Column("id", "table_k"); - selectStatement.addSQLToken(new TableToken(15, "table_k", QuoteCharacter.NONE, 0)); + selectStatement.addSQLToken(new TableToken(15, 21, "table_k", QuoteCharacter.NONE)); selectStatement.addSQLToken(new EncryptColumnToken(29, 39, column, true)); selectStatement.getEncryptConditions().getOrCondition().getAndConditions().add(new AndCondition()); List sqlExpressions = new LinkedList<>(); @@ -729,7 +729,7 @@ public void assertSelectInWithQueryAssistedShardingEncryptor() { @Test public void assertUpdateWithShardingEncryptor() { Column column = new Column("id", "table_z"); - updateStatement.addSQLToken(new TableToken(7, "table_z", QuoteCharacter.NONE, 0)); + updateStatement.addSQLToken(new TableToken(7, 13, "table_z", QuoteCharacter.NONE)); updateStatement.addSQLToken(new EncryptColumnToken(19, 24, column, false)); updateStatement.getAssignments().put(column, new SQLNumberExpression(1)); updateStatement.addSQLToken(new EncryptColumnToken(32, 37, column, true)); @@ -748,7 +748,7 @@ public void assertUpdateWithQueryAssistedShardingEncryptor() { parameters.add(1); parameters.add(5); Column column = new Column("id", "table_k"); - updateStatement.addSQLToken(new TableToken(7, "table_k", QuoteCharacter.NONE, 0)); + updateStatement.addSQLToken(new TableToken(7, 13, "table_k", QuoteCharacter.NONE)); updateStatement.addSQLToken(new EncryptColumnToken(19, 24, column, false)); updateStatement.getAssignments().put(column, new SQLParameterMarkerExpression(0)); updateStatement.addSQLToken(new EncryptColumnToken(32, 49, column, true));