use of io.shardingjdbc.core.parsing.parser.expression.SQLExpression in project sharding-jdbc by shardingjdbc.
the class WhereClauseParser method parseOffsetCondition.
private void parseOffsetCondition(final SelectStatement selectStatement, final boolean includeOffset) {
SQLExpression sqlExpression = basicExpressionParser.parse(selectStatement);
if (null == selectStatement.getLimit()) {
selectStatement.setLimit(new Limit(databaseType));
}
if (sqlExpression instanceof SQLNumberExpression) {
int offset = ((SQLNumberExpression) sqlExpression).getNumber().intValue();
selectStatement.getLimit().setOffset(new LimitValue(offset, -1, includeOffset));
selectStatement.getSqlTokens().add(new OffsetToken(lexerEngine.getCurrentToken().getEndPosition() - String.valueOf(offset).length() - lexerEngine.getCurrentToken().getLiterals().length(), offset));
} else if (sqlExpression instanceof SQLPlaceholderExpression) {
selectStatement.getLimit().setOffset(new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex(), includeOffset));
}
}
use of io.shardingjdbc.core.parsing.parser.expression.SQLExpression in project sharding-jdbc by shardingjdbc.
the class WhereClauseParser method parseComparisonCondition.
private void parseComparisonCondition(final ShardingRule shardingRule, final SQLStatement sqlStatement, final List<SelectItem> items) {
lexerEngine.skipIfEqual(Symbol.LEFT_PAREN);
SQLExpression left = basicExpressionParser.parse(sqlStatement);
if (lexerEngine.skipIfEqual(Symbol.EQ)) {
parseEqualCondition(shardingRule, sqlStatement, left);
lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
return;
}
if (lexerEngine.skipIfEqual(DefaultKeyword.IN)) {
parseInCondition(shardingRule, sqlStatement, left);
lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
return;
}
if (lexerEngine.skipIfEqual(DefaultKeyword.BETWEEN)) {
parseBetweenCondition(shardingRule, sqlStatement, left);
lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
return;
}
if (sqlStatement instanceof SelectStatement && isRowNumberCondition(items, left)) {
if (lexerEngine.skipIfEqual(Symbol.LT)) {
parseRowCountCondition((SelectStatement) sqlStatement, false);
return;
}
if (lexerEngine.skipIfEqual(Symbol.LT_EQ)) {
parseRowCountCondition((SelectStatement) sqlStatement, true);
return;
}
if (lexerEngine.skipIfEqual(Symbol.GT)) {
parseOffsetCondition((SelectStatement) sqlStatement, false);
return;
}
if (lexerEngine.skipIfEqual(Symbol.GT_EQ)) {
parseOffsetCondition((SelectStatement) sqlStatement, true);
return;
}
}
List<Keyword> otherConditionOperators = new LinkedList<>(Arrays.asList(getCustomizedOtherConditionOperators()));
otherConditionOperators.addAll(Arrays.asList(Symbol.LT, Symbol.LT_EQ, Symbol.GT, Symbol.GT_EQ, Symbol.LT_GT, Symbol.BANG_EQ, Symbol.BANG_GT, Symbol.BANG_LT, DefaultKeyword.LIKE, DefaultKeyword.IS));
if (lexerEngine.skipIfEqual(otherConditionOperators.toArray(new Keyword[otherConditionOperators.size()]))) {
lexerEngine.skipIfEqual(DefaultKeyword.NOT);
parseOtherCondition(sqlStatement);
}
if (lexerEngine.skipIfEqual(DefaultKeyword.NOT)) {
lexerEngine.nextToken();
lexerEngine.skipIfEqual(Symbol.LEFT_PAREN);
parseOtherCondition(sqlStatement);
lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
}
lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
}
use of io.shardingjdbc.core.parsing.parser.expression.SQLExpression in project sharding-jdbc by shardingjdbc.
the class BasicExpressionParser method parseExpression.
// TODO complete more expression parse
private SQLExpression parseExpression(final SQLStatement sqlStatement) {
String literals = lexerEngine.getCurrentToken().getLiterals();
final int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - literals.length();
final SQLExpression expression = getExpression(literals, sqlStatement);
lexerEngine.nextToken();
if (lexerEngine.skipIfEqual(Symbol.DOT)) {
String property = lexerEngine.getCurrentToken().getLiterals();
lexerEngine.nextToken();
return skipIfCompositeExpression(sqlStatement) ? new SQLIgnoreExpression(lexerEngine.getInput().substring(beginPosition, lexerEngine.getCurrentToken().getEndPosition())) : new SQLPropertyExpression(new SQLIdentifierExpression(literals), property);
}
if (lexerEngine.equalAny(Symbol.LEFT_PAREN)) {
lexerEngine.skipParentheses(sqlStatement);
skipRestCompositeExpression(sqlStatement);
return new SQLIgnoreExpression(lexerEngine.getInput().substring(beginPosition, lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length()).trim());
}
return skipIfCompositeExpression(sqlStatement) ? new SQLIgnoreExpression(lexerEngine.getInput().substring(beginPosition, lexerEngine.getCurrentToken().getEndPosition())) : expression;
}
use of io.shardingjdbc.core.parsing.parser.expression.SQLExpression in project sharding-jdbc by shardingjdbc.
the class BasicExpressionParser method parse.
/**
* Parse expression.
*
* @param sqlStatement SQL statement
* @return expression
*/
public SQLExpression parse(final SQLStatement sqlStatement) {
int beginPosition = lexerEngine.getCurrentToken().getEndPosition();
SQLExpression result = parseExpression(sqlStatement);
if (result instanceof SQLPropertyExpression) {
setTableToken(sqlStatement, beginPosition, (SQLPropertyExpression) result);
}
return result;
}
use of io.shardingjdbc.core.parsing.parser.expression.SQLExpression in project sharding-jdbc by shardingjdbc.
the class ConditionTest method assertGetShardingValue.
@Test
public void assertGetShardingValue() {
Condition condition = new Condition(new Column("test", "test"), new SQLNumberExpression(1));
ShardingValue shardingValue = condition.getShardingValue(Collections.emptyList());
assertThat((Integer) ((ListShardingValue) shardingValue).getValues().iterator().next(), is(1));
condition = new Condition(new Column("test", "test"), Arrays.<SQLExpression>asList(new SQLNumberExpression(1), new SQLNumberExpression(2)));
shardingValue = condition.getShardingValue(Collections.emptyList());
Iterator<?> iterator = ((ListShardingValue) shardingValue).getValues().iterator();
assertThat((Integer) iterator.next(), is(1));
assertThat((Integer) iterator.next(), is(2));
condition = new Condition(new Column("test", "test"), new SQLNumberExpression(1), new SQLNumberExpression(2));
shardingValue = condition.getShardingValue(Collections.emptyList());
assertThat((Integer) ((RangeShardingValue) shardingValue).getValueRange().lowerEndpoint(), is(1));
assertThat((Integer) ((RangeShardingValue) shardingValue).getValueRange().upperEndpoint(), is(2));
}
Aggregations