未验证 提交 159438bd 编写于 作者: L Liang Zhang 提交者: GitHub

Merge pull request #2485 from tristaZero/dev

Move RowCountToken and OffsetToken from parsing engine
......@@ -47,8 +47,6 @@ public final class LimitDecoratorMergedResultTest {
private List<QueryResult> queryResults;
private SelectStatement selectStatement;
private SQLRouteResult routeResult;
@Before
......@@ -64,7 +62,7 @@ public final class LimitDecoratorMergedResultTest {
for (ResultSet each : resultSets) {
queryResults.add(new TestQueryResult(each));
}
selectStatement = new SelectStatement();
SelectStatement selectStatement = new SelectStatement();
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(selectStatement.getLimit());
}
......@@ -72,7 +70,7 @@ public final class LimitDecoratorMergedResultTest {
@Test
public void assertNextForSkipAll() throws SQLException {
Limit limit = new Limit();
limit.setOffset(new LimitValue(Integer.MAX_VALUE, -1, true));
limit.setOffset(new LimitValue(Integer.MAX_VALUE, -1, null, true));
routeResult.setLimit(limit);
mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -82,7 +80,7 @@ public final class LimitDecoratorMergedResultTest {
@Test
public void assertNextWithoutRowCount() throws SQLException {
Limit limit = new Limit();
limit.setOffset(new LimitValue(2, -1, true));
limit.setOffset(new LimitValue(2, -1, null, true));
routeResult.setLimit(limit);
mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -95,8 +93,8 @@ public final class LimitDecoratorMergedResultTest {
@Test
public void assertNextWithRowCount() throws SQLException {
Limit limit = new Limit();
limit.setOffset(new LimitValue(2, -1, true));
limit.setRowCount(new LimitValue(2, -1, false));
limit.setOffset(new LimitValue(2, -1, null, true));
limit.setRowCount(new LimitValue(2, -1, null, false));
routeResult.setLimit(limit);
mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......
......@@ -71,7 +71,7 @@ public final class RowNumberDecoratorMergedResultTest {
@Test
public void assertNextForSkipAll() throws SQLException {
Limit limit = new Limit();
limit.setOffset(new LimitValue(Integer.MAX_VALUE, -1, true));
limit.setOffset(new LimitValue(Integer.MAX_VALUE, -1, null, true));
routeResult.setLimit(limit);
mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -92,8 +92,8 @@ public final class RowNumberDecoratorMergedResultTest {
@Test
public void assertNextForRowCountBoundOpenedFalse() throws SQLException {
Limit limit = new Limit();
limit.setOffset(new LimitValue(2, -1, true));
limit.setRowCount(new LimitValue(4, -1, false));
limit.setOffset(new LimitValue(2, -1, null, true));
limit.setRowCount(new LimitValue(4, -1, null, false));
routeResult.setLimit(limit);
mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -105,8 +105,8 @@ public final class RowNumberDecoratorMergedResultTest {
@Test
public void assertNextForRowCountBoundOpenedTrue() throws SQLException {
Limit limit = new Limit();
limit.setOffset(new LimitValue(2, -1, true));
limit.setRowCount(new LimitValue(4, -1, true));
limit.setOffset(new LimitValue(2, -1, null, true));
limit.setRowCount(new LimitValue(4, -1, null, true));
routeResult.setLimit(limit);
mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......
......@@ -71,7 +71,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
@Test
public void assertNextForSkipAll() throws SQLException {
Limit limit = new Limit();
limit.setOffset(new LimitValue(Integer.MAX_VALUE, -1, true));
limit.setOffset(new LimitValue(Integer.MAX_VALUE, -1, null, true));
routeResult.setLimit(limit);
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -81,7 +81,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
@Test
public void assertNextWithoutOffsetWithRowCount() throws SQLException {
Limit limit = new Limit();
limit.setRowCount(new LimitValue(5, -1, false));
limit.setRowCount(new LimitValue(5, -1, null, false));
routeResult.setLimit(limit);
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -94,7 +94,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
@Test
public void assertNextWithOffsetWithoutRowCount() throws SQLException {
Limit limit = new Limit();
limit.setOffset(new LimitValue(2, -1, true));
limit.setOffset(new LimitValue(2, -1, null, true));
routeResult.setLimit(limit);
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -107,8 +107,8 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
@Test
public void assertNextWithOffsetBoundOpenedFalse() throws SQLException {
Limit limit = new Limit();
limit.setOffset(new LimitValue(2, -1, false));
limit.setRowCount(new LimitValue(4, -1, false));
limit.setOffset(new LimitValue(2, -1, null, false));
limit.setRowCount(new LimitValue(4, -1, null, false));
routeResult.setLimit(limit);
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -120,8 +120,8 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
@Test
public void assertNextWithOffsetBoundOpenedTrue() throws SQLException {
Limit limit = new Limit();
limit.setOffset(new LimitValue(2, -1, true));
limit.setRowCount(new LimitValue(4, -1, false));
limit.setOffset(new LimitValue(2, -1, null, true));
limit.setRowCount(new LimitValue(4, -1, null, false));
routeResult.setLimit(limit);
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......
......@@ -26,8 +26,6 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralL
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.ParameterMarkerLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.OffsetToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.RowCountToken;
/**
* Limit filler.
......@@ -51,20 +49,18 @@ public final class LimitFiller implements SQLSegmentFiller<LimitSegment> {
private void fillRowCount(final LimitValueSegment rowCountSegment, final SelectStatement selectStatement) {
if (rowCountSegment instanceof NumberLiteralLimitValueSegment) {
int value = ((NumberLiteralLimitValueSegment) rowCountSegment).getValue();
selectStatement.getLimit().setRowCount(new LimitValue(value, -1, false));
selectStatement.getSQLTokens().add(new RowCountToken(rowCountSegment.getStartIndex(), rowCountSegment.getStopIndex(), value));
selectStatement.getLimit().setRowCount(new LimitValue(value, -1, rowCountSegment, false));
} else {
selectStatement.getLimit().setRowCount(new LimitValue(-1, ((ParameterMarkerLimitValueSegment) rowCountSegment).getParameterIndex(), false));
selectStatement.getLimit().setRowCount(new LimitValue(-1, ((ParameterMarkerLimitValueSegment) rowCountSegment).getParameterIndex(), rowCountSegment, false));
}
}
private void fillOffset(final LimitValueSegment offsetSegment, final SelectStatement selectStatement) {
if (offsetSegment instanceof NumberLiteralLimitValueSegment) {
int value = ((NumberLiteralLimitValueSegment) offsetSegment).getValue();
selectStatement.getLimit().setOffset(new LimitValue(value, -1, false));
selectStatement.getSQLTokens().add(new OffsetToken(offsetSegment.getStartIndex(), offsetSegment.getStopIndex(), value));
selectStatement.getLimit().setOffset(new LimitValue(value, -1, offsetSegment, false));
} else {
selectStatement.getLimit().setOffset(new LimitValue(-1, ((ParameterMarkerLimitValueSegment) offsetSegment).getParameterIndex(), false));
selectStatement.getLimit().setOffset(new LimitValue(-1, ((ParameterMarkerLimitValueSegment) offsetSegment).getParameterIndex(), offsetSegment, false));
}
}
}
......@@ -40,7 +40,6 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.ParameterMarke
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.TopSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.RowCountToken;
/**
* Select item filler.
......@@ -117,14 +116,11 @@ public final class SelectItemFiller implements SQLSegmentFiller<SelectItemSegmen
Limit limit = new Limit();
limit.setRowCount(getTopValueSegment(topSegment.getTop()));
selectStatement.setLimit(limit);
if (-1 != limit.getRowCount().getValue()) {
selectStatement.addSQLToken(new RowCountToken(topSegment.getTop().getStartIndex(), topSegment.getTop().getStopIndex(), limit.getRowCount().getValue()));
}
selectStatement.getItems().add(new CommonSelectItem("rownum", Optional.of(topSegment.getRowNumberAlias())));
}
private LimitValue getTopValueSegment(final LimitValueSegment topValueSegment) {
return topValueSegment instanceof ParameterMarkerLimitValueSegment ? new LimitValue(-1, ((ParameterMarkerLimitValueSegment) topValueSegment).getParameterIndex(), false)
: new LimitValue(((NumberLiteralLimitValueSegment) topValueSegment).getValue(), -1, false);
return topValueSegment instanceof ParameterMarkerLimitValueSegment ? new LimitValue(-1, ((ParameterMarkerLimitValueSegment) topValueSegment).getParameterIndex(), topValueSegment, false)
: new LimitValue(((NumberLiteralLimitValueSegment) topValueSegment).getValue(), -1, topValueSegment, false);
}
}
......@@ -31,8 +31,6 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateS
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateCompareRightValue;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.OffsetToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.RowCountToken;
import java.util.Collection;
import java.util.LinkedList;
......@@ -104,27 +102,15 @@ public final class ShardingRowNumberPredicateFiller implements SQLSegmentFiller<
switch (((PredicateCompareRightValue) each.getRightValue()).getOperator()) {
case "<":
limit.setRowCount(createLimitValue(expression, false));
if (-1 != limit.getRowCount().getValue()) {
selectStatement.addSQLToken(new RowCountToken(expression.getStartIndex(), expression.getStopIndex(), limit.getRowCount().getValue()));
}
break;
case "<=":
limit.setRowCount(createLimitValue(expression, true));
if (-1 != limit.getRowCount().getValue()) {
selectStatement.addSQLToken(new RowCountToken(expression.getStartIndex(), expression.getStopIndex(), limit.getRowCount().getValue()));
}
break;
case ">":
limit.setOffset(createLimitValue(expression, false));
if (-1 != limit.getOffset().getValue()) {
selectStatement.addSQLToken(new OffsetToken(expression.getStartIndex(), expression.getStopIndex(), limit.getOffset().getValue()));
}
break;
case ">=":
limit.setOffset(createLimitValue(expression, true));
if (-1 != limit.getOffset().getValue()) {
selectStatement.addSQLToken(new OffsetToken(expression.getStartIndex(), expression.getStopIndex(), limit.getOffset().getValue()));
}
break;
default:
break;
......@@ -134,7 +120,7 @@ public final class ShardingRowNumberPredicateFiller implements SQLSegmentFiller<
}
private LimitValue createLimitValue(final ExpressionSegment expression, final boolean boundOpened) {
return expression instanceof ParameterMarkerExpressionSegment ? new LimitValue(-1, ((ParameterMarkerExpressionSegment) expression).getParameterMarkerIndex(), boundOpened)
: new LimitValue(((Number) ((LiteralExpressionSegment) expression).getLiterals()).intValue(), -1, boundOpened);
return expression instanceof ParameterMarkerExpressionSegment ? new LimitValue(-1, ((ParameterMarkerExpressionSegment) expression).getParameterMarkerIndex(), expression, boundOpened)
: new LimitValue(((Number) ((LiteralExpressionSegment) expression).getLiterals()).intValue(), -1, expression, boundOpened);
}
}
......@@ -21,6 +21,7 @@ import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment;
/**
* Limit value.
......@@ -37,5 +38,7 @@ public final class LimitValue {
private int index;
private SQLSegment sqlSegment;
private boolean boundOpened;
}
......@@ -24,13 +24,10 @@ import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.apache.shardingsphere.core.constant.SQLType;
import org.apache.shardingsphere.core.parse.sql.context.condition.Condition;
import org.apache.shardingsphere.core.parse.sql.context.condition.ParseCondition;
import org.apache.shardingsphere.core.parse.sql.context.table.Tables;
import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment;
import org.apache.shardingsphere.core.parse.sql.token.SQLToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.EncryptColumnToken;
import java.util.Collection;
import java.util.Collections;
......@@ -88,25 +85,6 @@ public abstract class AbstractSQLStatement implements SQLStatement {
return sqlTokens;
}
/**
* Get encrypt condition.
*
* @param encryptColumnToken encrypt column token
* @return encrypt condition
*/
public Optional<Condition> getEncryptCondition(final EncryptColumnToken encryptColumnToken) {
for (Condition each : encryptCondition.findConditions(encryptColumnToken.getColumn())) {
if (isSameIndexes(each.getPredicateSegment(), encryptColumnToken)) {
return Optional.of(each);
}
}
return Optional.absent();
}
private boolean isSameIndexes(final PredicateSegment predicateSegment, final EncryptColumnToken encryptColumnToken) {
return predicateSegment.getStartIndex() == encryptColumnToken.getStartIndex() && predicateSegment.getStopIndex() == encryptColumnToken.getStopIndex();
}
@Override
@SuppressWarnings("unchecked")
public final <T extends SQLSegment> Optional<T> findSQLSegment(final Class<T> sqlSegmentType) {
......
/*
* 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.integrate.asserts.SQLStatementAssertMessage;
import org.apache.shardingsphere.core.parse.integrate.jaxb.token.ExpectedOffsetToken;
import org.apache.shardingsphere.core.parse.integrate.jaxb.token.ExpectedTokens;
import org.apache.shardingsphere.core.parse.sql.token.SQLToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.OffsetToken;
import org.apache.shardingsphere.test.sql.SQLCaseType;
import java.util.Collection;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
/**
* Offset token assert.
*
* @author zhangliang
*/
@RequiredArgsConstructor
final class OffsetTokenAssert {
private final SQLCaseType sqlCaseType;
private final SQLStatementAssertMessage assertMessage;
void assertOffsetToken(final Collection<SQLToken> actual, final ExpectedTokens expected) {
Optional<OffsetToken> offsetToken = getOffsetToken(actual);
if (SQLCaseType.Placeholder == sqlCaseType) {
assertFalse(assertMessage.getFullAssertMessage("Offset token should not exist: "), offsetToken.isPresent());
return;
}
if (offsetToken.isPresent()) {
assertOffsetToken(offsetToken.get(), expected.getOffsetToken());
} else {
assertNull(assertMessage.getFullAssertMessage("Offset token should not exist: "), expected.getOffsetToken());
}
}
private void assertOffsetToken(final OffsetToken actual, final ExpectedOffsetToken expected) {
assertThat(assertMessage.getFullAssertMessage("Offset token begin position assertion error: "), actual.getStartIndex(), is(expected.getBeginPosition()));
assertThat(assertMessage.getFullAssertMessage("Offset token offset assertion error: "), actual.getOffset(), is(expected.getOffset()));
}
private Optional<OffsetToken> getOffsetToken(final Collection<SQLToken> actual) {
for (SQLToken each : actual) {
if (each instanceof OffsetToken) {
return Optional.of((OffsetToken) each);
}
}
return Optional.absent();
}
}
......@@ -33,14 +33,8 @@ public final class TokenAssert {
private final InsertValuesTokenAssert insertValuesTokenAssert;
private final OffsetTokenAssert offsetTokenAssert;
private final RowCountTokenAssert rowCountTokenAssert;
public TokenAssert(final SQLCaseType sqlCaseType, final SQLStatementAssertMessage assertMessage) {
insertValuesTokenAssert = new InsertValuesTokenAssert(assertMessage);
offsetTokenAssert = new OffsetTokenAssert(sqlCaseType, assertMessage);
rowCountTokenAssert = new RowCountTokenAssert(sqlCaseType, assertMessage);
}
/**
......@@ -52,7 +46,5 @@ public final class TokenAssert {
public void assertTokens(final Collection<SQLToken> actual, final ExpectedTokens expected) {
// TODO do not assert table token and index, will assert table segment in future
insertValuesTokenAssert.assertInsertValuesToken(actual, expected);
offsetTokenAssert.assertOffsetToken(actual, expected);
rowCountTokenAssert.assertRowCountToken(actual, expected);
}
}
/*
* 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.jaxb.token;
import lombok.Getter;
import lombok.Setter;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
public final class ExpectedOffsetToken {
@XmlAttribute(name = "begin-position")
private int beginPosition;
@XmlAttribute(name = "offset")
private int offset;
}
/*
* 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.jaxb.token;
import lombok.Getter;
import lombok.Setter;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
public final class ExpectedRowCountToken {
@XmlAttribute(name = "begin-position")
private int beginPosition;
@XmlAttribute(name = "row-count")
private int rowCount;
}
......@@ -46,12 +46,6 @@ public final class ExpectedTokens {
@XmlElement(name = "insert-set-token")
private ExpectedInsertSetToken insertSetToken;
@XmlElement(name = "offset-token")
private ExpectedOffsetToken offsetToken;
@XmlElement(name = "row-count-token")
private ExpectedRowCountToken rowCountToken;
@XmlElement(name = "insert-column-token")
private ExpectedInsertColumnToken insertColumnToken;
}
......@@ -180,7 +180,6 @@
</tables>
<tokens>
<table-token start-index="20" table-name="t_order" length="7" />
<row-count-token row-count="5" begin-position="68" />
</tokens>
<group-by-columns>
<group-by-column name="user_id" order-direction="ASC" />
......@@ -197,7 +196,6 @@
</tables>
<tokens>
<table-token start-index="49" table-name="t_order" length="7" />
<row-count-token row-count="5" begin-position="103" />
</tokens>
<select-items>
<aggregation-select-items>
......
......@@ -25,7 +25,6 @@
<tokens>
<table-token start-index="16" table-name="t_order" length="7" />
<table-token start-index="31" table-name="t_order_item" length="12" />
<offset-token begin-position="188" offset="5" />
</tokens>
<or-condition>
<and-condition>
......@@ -53,7 +52,6 @@
<tokens>
<table-token start-index="16" table-name="t_order" length="7" />
<table-token start-index="31" table-name="t_order_item" length="12" />
<row-count-token begin-position="187" row-count="5" />
</tokens>
<or-condition>
<and-condition>
......@@ -81,8 +79,6 @@
<tokens>
<table-token start-index="16" table-name="t_order" left-delimiter="`" right-delimiter="`" length="9" />
<table-token start-index="33" table-name="t_order_item" left-delimiter="`" right-delimiter="`" length="14" />
<offset-token begin-position="195" offset="5" />
<row-count-token begin-position="198" row-count="3" />
</tokens>
<or-condition>
<and-condition>
......@@ -110,7 +106,6 @@
<tokens>
<table-token start-index="167" table-name="t_order" length="7" />
<table-token start-index="182" table-name="t_order_item" length="12" />
<row-count-token begin-position="27" row-count="3" />
</tokens>
<or-condition>
<and-condition>
......@@ -138,8 +133,6 @@
<tokens>
<table-token start-index="16" table-name="t_order" length="7" />
<table-token start-index="31" table-name="t_order_item" length="12" />
<offset-token begin-position="188" offset="5" />
<row-count-token begin-position="196" row-count="3" />
</tokens>
<or-condition>
<and-condition>
......@@ -167,8 +160,6 @@
<tokens>
<table-token start-index="167" table-name="t_order" length="7" />
<table-token start-index="182" table-name="t_order_item" length="12" />
<offset-token begin-position="338" offset="6" />
<row-count-token begin-position="27" row-count="3" />
</tokens>
<or-condition>
<and-condition>
......@@ -196,8 +187,6 @@
<tokens>
<table-token start-index="167" table-name="t_order" length="7" />
<table-token start-index="182" table-name="t_order_item" length="12" />
<offset-token begin-position="339" offset="6" />
<row-count-token begin-position="27" row-count="3" />
</tokens>
<or-condition>
<and-condition>
......@@ -225,7 +214,6 @@
<tokens>
<table-token start-index="146" table-name="t_order" length="7" />
<table-token start-index="167" table-name="t_order_item" length="12" />
<row-count-token begin-position="363" row-count="3"/>
</tokens>
<or-condition>
<and-condition>
......@@ -253,8 +241,6 @@
<tokens>
<table-token start-index="146" table-name="t_order" length="7" />
<table-token start-index="167" table-name="t_order_item" length="12" />
<offset-token begin-position="386" offset="3" />
<row-count-token begin-position="363" row-count="5" />
</tokens>
<or-condition>
<and-condition>
......@@ -282,8 +268,6 @@
<tokens>
<table-token start-index="146" table-name="t_order" length="7" />
<table-token start-index="167" table-name="t_order_item" length="12" />
<offset-token begin-position="387" offset="3" />
<row-count-token begin-position="363" row-count="5" />
</tokens>
<or-condition>
<and-condition>
......@@ -309,7 +293,6 @@
</tables>
<tokens>
<table-token start-index="14" table-name="t_order" length="7" />
<row-count-token begin-position="38" row-count="20" />
</tokens>
<order-by-columns>
<order-by-column name="order_id" order-direction="ASC" />
......
......@@ -25,8 +25,6 @@
<tokens>
<table-token start-index="22" table-name="t_order" length="7" />
<table-token start-index="37" table-name="t_order_item" length="12" />
<offset-token begin-position="212" offset="5" />
<row-count-token begin-position="215" row-count="3" />
</tokens>
<or-condition>
<and-condition>
......@@ -57,8 +55,6 @@
<tokens>
<table-token start-index="22" table-name="t_order" length="7" />
<table-token start-index="37" table-name="t_order_item" length="12" />
<offset-token begin-position="212" offset="5" />
<row-count-token begin-position="215" row-count="3" />
</tokens>
<or-condition>
<and-condition>
......@@ -89,8 +85,6 @@
<tokens>
<table-token start-index="165" table-name="t_order" length="7" />
<table-token start-index="180" table-name="t_order_item" length="12" />
<offset-token begin-position="355" offset="6" />
<row-count-token begin-position="26" row-count="3" />
</tokens>
<or-condition>
<and-condition>
......@@ -121,8 +115,6 @@
<tokens>
<table-token start-index="167" table-name="t_order" length="7" />
<table-token start-index="182" table-name="t_order_item" length="12" />
<offset-token begin-position="357" offset="6" />
<row-count-token begin-position="27" row-count="3" />
</tokens>
<or-condition>
<and-condition>
......@@ -153,8 +145,6 @@
<tokens>
<table-token start-index="165" table-name="t_order" length="7" />
<table-token start-index="180" table-name="t_order_item" length="12" />
<offset-token begin-position="356" offset="6" />
<row-count-token begin-position="26" row-count="3" />
</tokens>
<or-condition>
<and-condition>
......@@ -185,8 +175,6 @@
<tokens>
<table-token start-index="167" table-name="t_order" length="7" />
<table-token start-index="182" table-name="t_order_item" length="12" />
<offset-token begin-position="358" offset="6" />
<row-count-token begin-position="27" row-count="3" />
</tokens>
<or-condition>
<and-condition>
......@@ -217,8 +205,6 @@
<tokens>
<table-token start-index="146" table-name="t_order" length="7" />
<table-token start-index="167" table-name="t_order_item" length="12" />
<offset-token begin-position="405" offset="3" />
<row-count-token begin-position="382" row-count="5" />
</tokens>
<or-condition>
<and-condition>
......@@ -249,8 +235,6 @@
<tokens>
<table-token start-index="146" table-name="t_order" length="7" />
<table-token start-index="167" table-name="t_order_item" length="12" />
<offset-token begin-position="405" offset="3" />
<row-count-token begin-position="382" row-count="5" />
</tokens>
<or-condition>
<and-condition>
......
......@@ -23,13 +23,14 @@ import org.apache.shardingsphere.core.optimize.result.OptimizeResult;
import org.apache.shardingsphere.core.optimize.result.insert.InsertOptimizeResult;
import org.apache.shardingsphere.core.optimize.result.insert.InsertOptimizeResultUnit;
import org.apache.shardingsphere.core.parse.sql.context.condition.Condition;
import org.apache.shardingsphere.core.parse.sql.context.condition.ParseCondition;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.statement.AbstractSQLStatement;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.UpdateStatement;
import org.apache.shardingsphere.core.parse.sql.token.SQLToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.EncryptColumnToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.EncryptColumnToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertSetAddItemsToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertSetEncryptValueToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertValuesToken;
......@@ -144,13 +145,26 @@ public final class EncryptSQLRewriter implements SQLRewriter {
}
private void appendEncryptColumnPlaceholder(final SQLBuilder sqlBuilder, final EncryptColumnToken encryptColumnToken, final ParameterBuilder parameterBuilder) {
Optional<Condition> encryptCondition = ((AbstractSQLStatement) sqlStatement).getEncryptCondition(encryptColumnToken);
Optional<Condition> encryptCondition = getEncryptCondition(sqlStatement.getEncryptCondition(), encryptColumnToken);
Preconditions.checkArgument(!encryptColumnToken.isInWhere() || encryptCondition.isPresent(), "Can not find encrypt condition");
ShardingPlaceholder result = encryptColumnToken.isInWhere() ? getEncryptColumnPlaceholderFromConditions(encryptColumnToken, encryptCondition.get(), parameterBuilder)
: getEncryptColumnPlaceholderFromUpdateItem(encryptColumnToken, parameterBuilder);
sqlBuilder.appendPlaceholder(result);
}
private Optional<Condition> getEncryptCondition(final ParseCondition encryptCondition, final EncryptColumnToken encryptColumnToken) {
for (Condition each : encryptCondition.findConditions(encryptColumnToken.getColumn())) {
if (isSameIndexes(each.getPredicateSegment(), encryptColumnToken)) {
return Optional.of(each);
}
}
return Optional.absent();
}
private boolean isSameIndexes(final PredicateSegment predicateSegment, final EncryptColumnToken encryptColumnToken) {
return predicateSegment.getStartIndex() == encryptColumnToken.getStartIndex() && predicateSegment.getStopIndex() == encryptColumnToken.getStopIndex();
}
private WhereEncryptColumnPlaceholder getEncryptColumnPlaceholderFromConditions(
final EncryptColumnToken encryptColumnToken, final Condition encryptCondition, final ParameterBuilder parameterBuilder) {
ColumnNode columnNode = new ColumnNode(encryptColumnToken.getColumn().getTableName(), encryptColumnToken.getColumn().getName());
......
......@@ -25,7 +25,7 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.OrderByIt
import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.TextOrderByItemSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.parse.sql.token.SQLToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.OffsetToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.OffsetToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.RowCountToken;
import org.apache.shardingsphere.core.rewrite.builder.ParameterBuilder;
import org.apache.shardingsphere.core.rewrite.builder.SQLBuilder;
......
......@@ -19,8 +19,10 @@ package org.apache.shardingsphere.core.rewrite.token;
import org.apache.shardingsphere.core.rewrite.token.generator.AggregationDistinctTokenGenerator;
import org.apache.shardingsphere.core.rewrite.token.generator.IndexTokenGenerator;
import org.apache.shardingsphere.core.rewrite.token.generator.OffsetTokenGenerator;
import org.apache.shardingsphere.core.rewrite.token.generator.OrderByTokenGenerator;
import org.apache.shardingsphere.core.rewrite.token.generator.RemoveTokenGenerator;
import org.apache.shardingsphere.core.rewrite.token.generator.RowCountTokenGenerator;
import org.apache.shardingsphere.core.rewrite.token.generator.SQLTokenGenerator;
import org.apache.shardingsphere.core.rewrite.token.generator.SelectItemPrefixTokenGenerator;
import org.apache.shardingsphere.core.rewrite.token.generator.SelectItemsTokenGenerator;
......@@ -47,6 +49,8 @@ public final class ShardingTokenGenerateEngine extends SQLTokenGenerateEngine<Sh
SQL_TOKEN_GENERATORS.add(new AggregationDistinctTokenGenerator());
SQL_TOKEN_GENERATORS.add(new IndexTokenGenerator());
SQL_TOKEN_GENERATORS.add(new RemoveTokenGenerator());
SQL_TOKEN_GENERATORS.add(new OffsetTokenGenerator());
SQL_TOKEN_GENERATORS.add(new RowCountTokenGenerator());
}
@Override
......
......@@ -24,7 +24,7 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.Assignmen
import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.SetAssignmentsSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.EncryptColumnToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.EncryptColumnToken;
import org.apache.shardingsphere.core.rule.EncryptRule;
import java.util.Collection;
......
/*
* 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.rewrite.token.generator;
import com.google.common.base.Optional;
import org.apache.shardingsphere.core.parse.sql.context.limit.Limit;
import org.apache.shardingsphere.core.parse.sql.context.limit.LimitValue;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.rewrite.token.pojo.OffsetToken;
import org.apache.shardingsphere.core.rule.ShardingRule;
/**
* Offset token generator.
*
* @author panjuan
*/
public final class OffsetTokenGenerator implements OptionalSQLTokenGenerator<ShardingRule> {
@Override
public Optional<OffsetToken> generateSQLToken(final SQLStatement sqlStatement, final ShardingRule shardingRule) {
if (!(sqlStatement instanceof SelectStatement)) {
return Optional.absent();
}
if (isExistedOfNumberOffset(sqlStatement)) {
LimitValueSegment offset = sqlStatement.findSQLSegment(LimitSegment.class).get().getOffset().get();
return Optional.of(new OffsetToken(offset.getStartIndex(), offset.getStopIndex(), ((NumberLiteralLimitValueSegment) offset).getValue()));
}
if (isExistedOfLimitRowNum((SelectStatement) sqlStatement)) {
LimitValue offset = ((SelectStatement) sqlStatement).getLimit().getOffset();
return Optional.of(new OffsetToken(offset.getSqlSegment().getStartIndex(), offset.getSqlSegment().getStopIndex(), offset.getValue()));
}
return Optional.absent();
}
private boolean isExistedOfLimitRowNum(final SelectStatement sqlStatement) {
Limit limit = sqlStatement.getLimit();
return null != limit && null != limit.getOffset() && -1 != limit.getOffset().getValue();
}
private boolean isExistedOfNumberOffset(final SQLStatement sqlStatement) {
Optional<LimitSegment> limitSegment = sqlStatement.findSQLSegment(LimitSegment.class);
return limitSegment.isPresent() && limitSegment.get().getOffset().isPresent() && limitSegment.get().getOffset().get() instanceof NumberLiteralLimitValueSegment;
}
}
......@@ -15,60 +15,49 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.parse.integrate.asserts.token;
package org.apache.shardingsphere.core.rewrite.token.generator;
import com.google.common.base.Optional;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.parse.integrate.asserts.SQLStatementAssertMessage;
import org.apache.shardingsphere.core.parse.integrate.jaxb.token.ExpectedRowCountToken;
import org.apache.shardingsphere.core.parse.integrate.jaxb.token.ExpectedTokens;
import org.apache.shardingsphere.core.parse.sql.token.SQLToken;
import org.apache.shardingsphere.core.parse.sql.context.limit.Limit;
import org.apache.shardingsphere.core.parse.sql.context.limit.LimitValue;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.RowCountToken;
import org.apache.shardingsphere.test.sql.SQLCaseType;
import java.util.Collection;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import org.apache.shardingsphere.core.rule.ShardingRule;
/**
* Row count token assert.
* Row count token generator.
*
* @author zhangliang
* @author panjuan
*/
@RequiredArgsConstructor
final class RowCountTokenAssert {
private final SQLCaseType sqlCaseType;
private final SQLStatementAssertMessage assertMessage;
public final class RowCountTokenGenerator implements OptionalSQLTokenGenerator<ShardingRule> {
void assertRowCountToken(final Collection<SQLToken> actual, final ExpectedTokens expected) {
Optional<RowCountToken> rowCountToken = getRowCountToken(actual);
if (SQLCaseType.Placeholder == sqlCaseType) {
assertFalse(assertMessage.getFullAssertMessage("Row count token should not exist: "), rowCountToken.isPresent());
return;
@Override
public Optional<RowCountToken> generateSQLToken(final SQLStatement sqlStatement, final ShardingRule shardingRule) {
if (!(sqlStatement instanceof SelectStatement)) {
return Optional.absent();
}
if (rowCountToken.isPresent()) {
assertRowCountToken(rowCountToken.get(), expected.getRowCountToken());
} else {
assertNull(assertMessage.getFullAssertMessage("Row count token should not exist: "), expected.getRowCountToken());
if (isExistedOfNumberRowCount(sqlStatement)) {
LimitValueSegment rowCount = sqlStatement.findSQLSegment(LimitSegment.class).get().getRowCount().get();
return Optional.of(new RowCountToken(rowCount.getStartIndex(), rowCount.getStopIndex(), ((NumberLiteralLimitValueSegment) rowCount).getValue()));
}
if (isExistedOfLimitRowNum((SelectStatement) sqlStatement)) {
LimitValue rowCount = ((SelectStatement) sqlStatement).getLimit().getRowCount();
return Optional.of(new RowCountToken(rowCount.getSqlSegment().getStartIndex(), rowCount.getSqlSegment().getStopIndex(), rowCount.getValue()));
}
return Optional.absent();
}
private void assertRowCountToken(final RowCountToken actual, final ExpectedRowCountToken expected) {
assertThat(assertMessage.getFullAssertMessage("Row count token begin position assertion error: "), actual.getStartIndex(), is(expected.getBeginPosition()));
assertThat(assertMessage.getFullAssertMessage("Row count token row count assertion error: "), actual.getRowCount(), is(expected.getRowCount()));
private boolean isExistedOfLimitRowNum(final SelectStatement sqlStatement) {
Limit limit = sqlStatement.getLimit();
return null != limit && null != limit.getRowCount() && -1 != limit.getRowCount().getValue();
}
private Optional<RowCountToken> getRowCountToken(final Collection<SQLToken> actual) {
for (SQLToken each : actual) {
if (each instanceof RowCountToken) {
return Optional.of((RowCountToken) each);
}
}
return Optional.absent();
private boolean isExistedOfNumberRowCount(final SQLStatement sqlStatement) {
Optional<LimitSegment> limitSegment = sqlStatement.findSQLSegment(LimitSegment.class);
return limitSegment.isPresent() && limitSegment.get().getRowCount().isPresent() && limitSegment.get().getRowCount().get() instanceof NumberLiteralLimitValueSegment;
}
}
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.parse.sql.token.impl;
package org.apache.shardingsphere.core.rewrite.token.pojo;
import lombok.Getter;
import lombok.ToString;
......
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.parse.sql.token.impl;
package org.apache.shardingsphere.core.rewrite.token.pojo;
import lombok.Getter;
import org.apache.shardingsphere.core.parse.sql.token.SQLToken;
......
......@@ -31,6 +31,7 @@ import org.apache.shardingsphere.core.parse.sql.context.condition.Condition;
import org.apache.shardingsphere.core.parse.sql.context.limit.Limit;
import org.apache.shardingsphere.core.parse.sql.context.limit.LimitValue;
import org.apache.shardingsphere.core.parse.sql.context.table.Table;
import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment;
import org.apache.shardingsphere.core.parse.sql.segment.common.TableSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.SelectItemsSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment;
......@@ -49,14 +50,12 @@ import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.UpdateStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertColumnsToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertValuesToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.OffsetToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.RemoveToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.RowCountToken;
import org.apache.shardingsphere.core.rewrite.SQLRewriteEngine;
import org.apache.shardingsphere.core.rewrite.builder.ParameterBuilder;
import org.apache.shardingsphere.core.rewrite.builder.SQLBuilder;
import org.apache.shardingsphere.core.rewrite.token.pojo.IndexToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.OrderByToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.RemoveToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.SelectItemsToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.TableToken;
import org.apache.shardingsphere.core.route.SQLRouteResult;
......@@ -86,6 +85,8 @@ import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public final class ShardingSQLRewriterTest {
......@@ -351,12 +352,16 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForLimit() {
SQLSegment offsetSQLSegment = mock(SQLSegment.class);
when(offsetSQLSegment.getStartIndex()).thenReturn(33);
when(offsetSQLSegment.getStopIndex()).thenReturn(33);
SQLSegment rowCountSQLSegment = mock(SQLSegment.class);
when(rowCountSQLSegment.getStartIndex()).thenReturn(36);
when(rowCountSQLSegment.getStopIndex()).thenReturn(36);
selectStatement.setLimit(new Limit());
selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
selectStatement.getLimit().setRowCount(new LimitValue(2, -1, false));
selectStatement.getLimit().setOffset(new LimitValue(2, -1, offsetSQLSegment, true));
selectStatement.getLimit().setRowCount(new LimitValue(2, -1, rowCountSQLSegment, false));
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());
routeResult.setRoutingResult(new RoutingResult());
......@@ -367,12 +372,16 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForRowNum() {
SQLSegment offsetSQLSegment = mock(SQLSegment.class);
when(offsetSQLSegment.getStartIndex()).thenReturn(119);
when(offsetSQLSegment.getStopIndex()).thenReturn(119);
SQLSegment rowCountSQLSegment = mock(SQLSegment.class);
when(rowCountSQLSegment.getStartIndex()).thenReturn(98);
when(rowCountSQLSegment.getStopIndex()).thenReturn(98);
selectStatement.setLimit(new Limit());
selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
selectStatement.getLimit().setOffset(new LimitValue(2, -1, offsetSQLSegment, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, rowCountSQLSegment, false));
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());
routeResult.setRoutingResult(new RoutingResult());
......@@ -384,12 +393,16 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForTopAndRowNumber() {
SQLSegment offsetSQLSegment = mock(SQLSegment.class);
when(offsetSQLSegment.getStartIndex()).thenReturn(123);
when(offsetSQLSegment.getStopIndex()).thenReturn(123);
SQLSegment rowCountSQLSegment = mock(SQLSegment.class);
when(rowCountSQLSegment.getStartIndex()).thenReturn(26);
when(rowCountSQLSegment.getStopIndex()).thenReturn(26);
selectStatement.setLimit(new Limit());
selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
selectStatement.getLimit().setOffset(new LimitValue(2, -1, offsetSQLSegment, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, rowCountSQLSegment, false));
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());
routeResult.setRoutingResult(new RoutingResult());
......@@ -401,16 +414,20 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForLimitForMemoryGroupBy() {
SQLSegment offsetSQLSegment = mock(SQLSegment.class);
when(offsetSQLSegment.getStartIndex()).thenReturn(33);
when(offsetSQLSegment.getStopIndex()).thenReturn(33);
SQLSegment rowCountSQLSegment = mock(SQLSegment.class);
when(rowCountSQLSegment.getStartIndex()).thenReturn(36);
when(rowCountSQLSegment.getStopIndex()).thenReturn(36);
selectStatement.setLimit(new Limit());
selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
selectStatement.getLimit().setRowCount(new LimitValue(2, -1, false));
selectStatement.getLimit().setOffset(new LimitValue(2, -1, offsetSQLSegment, true));
selectStatement.getLimit().setRowCount(new LimitValue(2, -1, rowCountSQLSegment, false));
ColumnSegment columnSegment = new ColumnSegment(0, 0, "id");
columnSegment.setOwner(new TableSegment(0, 0, "x"));
selectStatement.getOrderByItems().add(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.ASC, OrderDirection.ASC));
selectStatement.getGroupByItems().add(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.DESC, OrderDirection.ASC));
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());
routeResult.setRoutingResult(new RoutingResult());
......@@ -421,12 +438,16 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForRowNumForMemoryGroupBy() {
SQLSegment offsetSQLSegment = mock(SQLSegment.class);
when(offsetSQLSegment.getStartIndex()).thenReturn(119);
when(offsetSQLSegment.getStopIndex()).thenReturn(119);
SQLSegment rowCountSQLSegment = mock(SQLSegment.class);
when(rowCountSQLSegment.getStartIndex()).thenReturn(98);
when(rowCountSQLSegment.getStopIndex()).thenReturn(98);
selectStatement.setLimit(new Limit());
selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
selectStatement.getLimit().setOffset(new LimitValue(2, -1, offsetSQLSegment, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, rowCountSQLSegment, false));
selectStatement.addSQLToken(new TableToken(68, 74, "table_x", QuoteCharacter.NONE));
selectStatement.addSQLToken(new OffsetToken(119, 119, 2));
selectStatement.addSQLToken(new RowCountToken(98, 98, 4));
ColumnSegment columnSegment = new ColumnSegment(0, 0, "id");
columnSegment.setOwner(new TableSegment(0, 0, "x"));
selectStatement.getOrderByItems().add(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.ASC, OrderDirection.ASC));
......@@ -442,12 +463,16 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForTopAndRowNumberForMemoryGroupBy() {
SQLSegment offsetSQLSegment = mock(SQLSegment.class);
when(offsetSQLSegment.getStartIndex()).thenReturn(123);
when(offsetSQLSegment.getStopIndex()).thenReturn(123);
SQLSegment rowCountSQLSegment = mock(SQLSegment.class);
when(rowCountSQLSegment.getStartIndex()).thenReturn(26);
when(rowCountSQLSegment.getStopIndex()).thenReturn(26);
selectStatement.setLimit(new Limit());
selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
selectStatement.getLimit().setOffset(new LimitValue(2, -1, offsetSQLSegment, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, rowCountSQLSegment, false));
selectStatement.addSQLToken(new TableToken(85, 91, "table_x", QuoteCharacter.NONE));
selectStatement.addSQLToken(new OffsetToken(123, 123, 2));
selectStatement.addSQLToken(new RowCountToken(26, 26, 4));
ColumnSegment columnSegment = new ColumnSegment(0, 0, "id");
columnSegment.setOwner(new TableSegment(0, 0, "x"));
selectStatement.getOrderByItems().add(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.ASC, OrderDirection.ASC));
......@@ -463,12 +488,16 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForLimitForNotRewriteLimit() {
SQLSegment offsetSQLSegment = mock(SQLSegment.class);
when(offsetSQLSegment.getStartIndex()).thenReturn(33);
when(offsetSQLSegment.getStopIndex()).thenReturn(33);
SQLSegment rowCountSQLSegment = mock(SQLSegment.class);
when(rowCountSQLSegment.getStartIndex()).thenReturn(36);
when(rowCountSQLSegment.getStopIndex()).thenReturn(36);
selectStatement.setLimit(new Limit());
selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
selectStatement.getLimit().setRowCount(new LimitValue(2, -1, false));
selectStatement.getLimit().setOffset(new LimitValue(2, -1, offsetSQLSegment, true));
selectStatement.getLimit().setRowCount(new LimitValue(2, -1, rowCountSQLSegment, false));
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());
RoutingResult routingResult = new RoutingResult();
......@@ -481,12 +510,16 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForRowNumForNotRewriteLimit() {
SQLSegment offsetSQLSegment = mock(SQLSegment.class);
when(offsetSQLSegment.getStartIndex()).thenReturn(119);
when(offsetSQLSegment.getStopIndex()).thenReturn(119);
SQLSegment rowCountSQLSegment = mock(SQLSegment.class);
when(rowCountSQLSegment.getStartIndex()).thenReturn(98);
when(rowCountSQLSegment.getStopIndex()).thenReturn(98);
selectStatement.setLimit(new Limit());
selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
selectStatement.getLimit().setOffset(new LimitValue(2, -1, offsetSQLSegment, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, rowCountSQLSegment, false));
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());
RoutingResult routingResult = new RoutingResult();
......@@ -500,12 +533,16 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForTopAndRowNumberForNotRewriteLimit() {
SQLSegment offsetSQLSegment = mock(SQLSegment.class);
when(offsetSQLSegment.getStartIndex()).thenReturn(123);
when(offsetSQLSegment.getStopIndex()).thenReturn(123);
SQLSegment rowCountSQLSegment = mock(SQLSegment.class);
when(rowCountSQLSegment.getStartIndex()).thenReturn(26);
when(rowCountSQLSegment.getStopIndex()).thenReturn(26);
selectStatement.setLimit(new Limit());
selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
selectStatement.getLimit().setOffset(new LimitValue(2, -1, offsetSQLSegment, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, rowCountSQLSegment, false));
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());
RoutingResult routingResult = new RoutingResult();
......
......@@ -195,10 +195,10 @@ public final class ParsingSQLRouter implements ShardingRouter {
private Limit cloneLimit(final Limit limit) {
Limit result = new Limit();
if (null != limit.getOffset()) {
result.setOffset(new LimitValue(limit.getOffset().getValue(), limit.getOffset().getIndex(), limit.getOffset().isBoundOpened()));
result.setOffset(new LimitValue(limit.getOffset().getValue(), limit.getOffset().getIndex(), limit.getOffset().getSqlSegment(), limit.getOffset().isBoundOpened()));
}
if (null != limit.getRowCount()) {
result.setRowCount(new LimitValue(limit.getRowCount().getValue(), limit.getRowCount().getIndex(), limit.getRowCount().isBoundOpened()));
result.setRowCount(new LimitValue(limit.getRowCount().getValue(), limit.getRowCount().getIndex(), limit.getRowCount().getSqlSegment(), limit.getRowCount().isBoundOpened()));
}
return result;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册