use of io.shardingjdbc.core.parsing.parser.token.RowCountToken in project sharding-jdbc by shardingjdbc.
the class WhereClauseParser method parseRowCountCondition.
private void parseRowCountCondition(final SelectStatement selectStatement, final boolean includeRowCount) {
SQLExpression sqlExpression = basicExpressionParser.parse(selectStatement);
if (null == selectStatement.getLimit()) {
selectStatement.setLimit(new Limit(databaseType));
}
if (sqlExpression instanceof SQLNumberExpression) {
int rowCount = ((SQLNumberExpression) sqlExpression).getNumber().intValue();
selectStatement.getLimit().setRowCount(new LimitValue(rowCount, -1, includeRowCount));
selectStatement.getSqlTokens().add(new RowCountToken(lexerEngine.getCurrentToken().getEndPosition() - String.valueOf(rowCount).length() - lexerEngine.getCurrentToken().getLiterals().length(), rowCount));
} else if (sqlExpression instanceof SQLPlaceholderExpression) {
selectStatement.getLimit().setRowCount(new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex(), includeRowCount));
}
}
use of io.shardingjdbc.core.parsing.parser.token.RowCountToken in project sharding-jdbc by shardingjdbc.
the class MySQLLimitClauseParser method getLimitWithComma.
private Limit getLimitWithComma(final int index, final int valueBeginPosition, final int value, final boolean isParameterForValue, final SelectStatement selectStatement) {
int rowCountBeginPosition = lexerEngine.getCurrentToken().getEndPosition();
int rowCountValue;
int rowCountIndex = -1;
boolean isParameterForRowCount = false;
if (lexerEngine.equalAny(Literals.INT)) {
rowCountValue = Integer.parseInt(lexerEngine.getCurrentToken().getLiterals());
rowCountBeginPosition = rowCountBeginPosition - (rowCountValue + "").length();
} else if (lexerEngine.equalAny(Symbol.QUESTION)) {
rowCountIndex = -1 == index ? selectStatement.getParametersIndex() : index + 1;
rowCountValue = -1;
rowCountBeginPosition--;
isParameterForRowCount = true;
} else {
throw new SQLParsingException(lexerEngine);
}
lexerEngine.nextToken();
if (isParameterForValue) {
selectStatement.increaseParametersIndex();
} else {
selectStatement.getSqlTokens().add(new OffsetToken(valueBeginPosition, value));
}
if (isParameterForRowCount) {
selectStatement.increaseParametersIndex();
} else {
selectStatement.getSqlTokens().add(new RowCountToken(rowCountBeginPosition, rowCountValue));
}
Limit result = new Limit(DatabaseType.MySQL);
result.setRowCount(new LimitValue(rowCountValue, rowCountIndex, false));
result.setOffset(new LimitValue(value, index, true));
return result;
}
use of io.shardingjdbc.core.parsing.parser.token.RowCountToken in project sharding-jdbc by shardingjdbc.
the class MySQLLimitClauseParser method parse.
/**
* Parse limit.
*
* @param selectStatement select statement
*/
public void parse(final SelectStatement selectStatement) {
if (!lexerEngine.skipIfEqual(MySQLKeyword.LIMIT)) {
return;
}
int valueIndex = -1;
int valueBeginPosition = lexerEngine.getCurrentToken().getEndPosition();
int value;
boolean isParameterForValue = false;
if (lexerEngine.equalAny(Literals.INT)) {
value = Integer.parseInt(lexerEngine.getCurrentToken().getLiterals());
valueBeginPosition = valueBeginPosition - (value + "").length();
} else if (lexerEngine.equalAny(Symbol.QUESTION)) {
valueIndex = selectStatement.getParametersIndex();
value = -1;
valueBeginPosition--;
isParameterForValue = true;
} else {
throw new SQLParsingException(lexerEngine);
}
lexerEngine.nextToken();
if (lexerEngine.skipIfEqual(Symbol.COMMA)) {
selectStatement.setLimit(getLimitWithComma(valueIndex, valueBeginPosition, value, isParameterForValue, selectStatement));
return;
}
if (lexerEngine.skipIfEqual(MySQLKeyword.OFFSET)) {
selectStatement.setLimit(getLimitWithOffset(valueIndex, valueBeginPosition, value, isParameterForValue, selectStatement));
return;
}
if (isParameterForValue) {
selectStatement.increaseParametersIndex();
} else {
selectStatement.getSqlTokens().add(new RowCountToken(valueBeginPosition, value));
}
Limit limit = new Limit(DatabaseType.MySQL);
limit.setRowCount(new LimitValue(value, valueIndex, false));
selectStatement.setLimit(limit);
}
use of io.shardingjdbc.core.parsing.parser.token.RowCountToken in project sharding-jdbc by shardingjdbc.
the class PostgreSQLLimitClauseParser method buildRowCount.
private Optional<LimitValue> buildRowCount(final SelectStatement selectStatement) {
int parameterIndex = selectStatement.getParametersIndex();
int rowCountValue = -1;
int rowCountIndex = -1;
int valueBeginPosition = lexerEngine.getCurrentToken().getEndPosition();
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.getSqlTokens().add(new RowCountToken(valueBeginPosition, rowCountValue));
} else if (lexerEngine.equalAny(Symbol.QUESTION)) {
rowCountIndex = parameterIndex++;
selectStatement.setParametersIndex(parameterIndex);
rowCountValue = -1;
} else {
throw new SQLParsingException(lexerEngine);
}
lexerEngine.nextToken();
}
return Optional.of(new LimitValue(rowCountValue, rowCountIndex, false));
}
use of io.shardingjdbc.core.parsing.parser.token.RowCountToken in project sharding-jdbc by shardingjdbc.
the class SQLRewriteEngineTest method assertRewriteForTopAndRowNumberForNotRewriteLimit.
@Test
public void assertRewriteForTopAndRowNumberForNotRewriteLimit() {
selectStatement.setLimit(new Limit(DatabaseType.SQLServer));
selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
selectStatement.getSqlTokens().add(new TableToken(85, "table_x"));
selectStatement.getSqlTokens().add(new OffsetToken(123, 2));
selectStatement.getSqlTokens().add(new RowCountToken(26, 4));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2", DatabaseType.SQLServer, selectStatement);
assertThat(rewriteEngine.rewrite(false).toSQL(tableTokens, null), is("SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>2"));
}
Aggregations