Search in sources :

Example 6 with SQLExpression

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));
    }
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) SQLPlaceholderExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)

Example 7 with SQLExpression

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);
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) DefaultKeyword(io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword) Keyword(io.shardingjdbc.core.parsing.lexer.token.Keyword) LinkedList(java.util.LinkedList)

Example 8 with SQLExpression

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;
}
Also used : SQLPropertyExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPropertyExpression) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) SQLIdentifierExpression(io.shardingjdbc.core.parsing.parser.expression.SQLIdentifierExpression) SQLIgnoreExpression(io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression)

Example 9 with SQLExpression

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;
}
Also used : SQLPropertyExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPropertyExpression) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression)

Example 10 with SQLExpression

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));
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) ListShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue) RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) ListShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue) Test(org.junit.Test)

Aggregations

SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)12 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)6 Column (io.shardingjdbc.core.parsing.parser.context.condition.Column)4 Condition (io.shardingjdbc.core.parsing.parser.context.condition.Condition)4 SQLPlaceholderExpression (io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression)4 LinkedList (java.util.LinkedList)4 Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)3 LimitValue (io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)3 SQLIgnoreExpression (io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression)3 SQLPropertyExpression (io.shardingjdbc.core.parsing.parser.expression.SQLPropertyExpression)3 SQLParsingException (io.shardingjdbc.core.parsing.parser.exception.SQLParsingException)2 SQLIdentifierExpression (io.shardingjdbc.core.parsing.parser.expression.SQLIdentifierExpression)2 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)2 ListShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue)1 RangeShardingValue (io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue)1 ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)1 OrderDirection (io.shardingjdbc.core.constant.OrderDirection)1 DefaultKeyword (io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword)1 Keyword (io.shardingjdbc.core.parsing.lexer.token.Keyword)1 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)1