Search in sources :

Example 1 with Condition

use of io.shardingjdbc.core.parsing.parser.context.condition.Condition in project sharding-jdbc by shardingjdbc.

the class InsertSetClauseParser method parse.

/**
 * Parse insert set.
 *
 * @param insertStatement insert statement
 */
public void parse(final InsertStatement insertStatement) {
    if (!lexerEngine.skipIfEqual(getCustomizedInsertKeywords())) {
        return;
    }
    do {
        Column column = new Column(SQLUtil.getExactlyValue(lexerEngine.getCurrentToken().getLiterals()), insertStatement.getTables().getSingleTableName());
        lexerEngine.nextToken();
        lexerEngine.accept(Symbol.EQ);
        SQLExpression sqlExpression;
        if (lexerEngine.equalAny(Literals.INT)) {
            sqlExpression = new SQLNumberExpression(Integer.parseInt(lexerEngine.getCurrentToken().getLiterals()));
        } else if (lexerEngine.equalAny(Literals.FLOAT)) {
            sqlExpression = new SQLNumberExpression(Double.parseDouble(lexerEngine.getCurrentToken().getLiterals()));
        } else if (lexerEngine.equalAny(Literals.CHARS)) {
            sqlExpression = new SQLTextExpression(lexerEngine.getCurrentToken().getLiterals());
        } else if (lexerEngine.equalAny(DefaultKeyword.NULL)) {
            sqlExpression = new SQLIgnoreExpression(DefaultKeyword.NULL.name());
        } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
            sqlExpression = new SQLPlaceholderExpression(insertStatement.getParametersIndex());
            insertStatement.increaseParametersIndex();
        } else {
            throw new UnsupportedOperationException("");
        }
        lexerEngine.nextToken();
        if (lexerEngine.equalAny(Symbol.COMMA, DefaultKeyword.ON, Assist.END)) {
            insertStatement.getConditions().add(new Condition(column, sqlExpression), shardingRule);
        } else {
            lexerEngine.skipUntil(Symbol.COMMA, DefaultKeyword.ON);
        }
    } while (lexerEngine.skipIfEqual(Symbol.COMMA));
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) SQLTextExpression(io.shardingjdbc.core.parsing.parser.expression.SQLTextExpression) SQLPlaceholderExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression) SQLIgnoreExpression(io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression)

Example 2 with Condition

use of io.shardingjdbc.core.parsing.parser.context.condition.Condition in project sharding-jdbc by shardingjdbc.

the class InsertValuesClauseParser method parseValues.

private void parseValues(final InsertStatement insertStatement) {
    lexerEngine.accept(Symbol.LEFT_PAREN);
    List<SQLExpression> sqlExpressions = new LinkedList<>();
    do {
        sqlExpressions.add(basicExpressionParser.parse(insertStatement));
    } while (lexerEngine.skipIfEqual(Symbol.COMMA));
    insertStatement.setValuesListLastPosition(lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length());
    int count = 0;
    for (Column each : insertStatement.getColumns()) {
        SQLExpression sqlExpression = sqlExpressions.get(count);
        insertStatement.getConditions().add(new Condition(each, sqlExpression), shardingRule);
        if (insertStatement.getGenerateKeyColumnIndex() == count) {
            insertStatement.setGeneratedKey(createGeneratedKey(each, sqlExpression));
        }
        count++;
    }
    lexerEngine.accept(Symbol.RIGHT_PAREN);
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) LinkedList(java.util.LinkedList)

Example 3 with Condition

use of io.shardingjdbc.core.parsing.parser.context.condition.Condition in project sharding-jdbc by shardingjdbc.

the class WhereClauseParser method parseBetweenCondition.

private void parseBetweenCondition(final ShardingRule shardingRule, final SQLStatement sqlStatement, final SQLExpression left) {
    List<SQLExpression> rights = new LinkedList<>();
    rights.add(basicExpressionParser.parse(sqlStatement));
    lexerEngine.accept(DefaultKeyword.AND);
    rights.add(basicExpressionParser.parse(sqlStatement));
    Optional<Column> column = find(sqlStatement.getTables(), left);
    if (column.isPresent()) {
        sqlStatement.getConditions().add(new Condition(column.get(), rights.get(0), rights.get(1)), shardingRule);
    }
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) LinkedList(java.util.LinkedList)

Example 4 with Condition

use of io.shardingjdbc.core.parsing.parser.context.condition.Condition in project sharding-jdbc by shardingjdbc.

the class WhereClauseParser method parseInCondition.

private void parseInCondition(final ShardingRule shardingRule, final SQLStatement sqlStatement, final SQLExpression left) {
    lexerEngine.accept(Symbol.LEFT_PAREN);
    List<SQLExpression> rights = new LinkedList<>();
    do {
        lexerEngine.skipIfEqual(Symbol.COMMA);
        rights.add(basicExpressionParser.parse(sqlStatement));
    } while (!lexerEngine.equalAny(Symbol.RIGHT_PAREN));
    Optional<Column> column = find(sqlStatement.getTables(), left);
    if (column.isPresent()) {
        sqlStatement.getConditions().add(new Condition(column.get(), rights), shardingRule);
    }
    lexerEngine.nextToken();
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) LinkedList(java.util.LinkedList)

Example 5 with Condition

use of io.shardingjdbc.core.parsing.parser.context.condition.Condition in project sharding-jdbc by shardingjdbc.

the class UpdateStatementParserTest method assertUpdateStatementWithoutParameter.

private void assertUpdateStatementWithoutParameter(final DMLStatement updateStatement) {
    assertThat(updateStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX"));
    assertThat(updateStatement.getTables().find("TABLE_XXX").get().getAlias().get(), is("xxx"));
    Condition condition1 = updateStatement.getConditions().find(new Column("field1", "TABLE_XXX")).get();
    assertThat(condition1.getOperator(), is(ShardingOperator.EQUAL));
    assertThat(((ListShardingValue<? extends Comparable>) condition1.getShardingValue(Collections.emptyList())).getValues().iterator().next(), is((Comparable) 1));
    Condition condition2 = updateStatement.getConditions().find(new Column("field2", "TABLE_XXX")).get();
    assertThat(condition2.getOperator(), is(ShardingOperator.IN));
    Iterator<?> shardingValues2 = ((ListShardingValue) condition2.getShardingValue(Collections.emptyList())).getValues().iterator();
    assertThat(shardingValues2.next(), is((Object) 1));
    assertThat(shardingValues2.next(), is((Object) 3));
    assertFalse(shardingValues2.hasNext());
    Condition condition3 = updateStatement.getConditions().find(new Column("field3", "TABLE_XXX")).get();
    Range shardingValues3 = ((RangeShardingValue) condition3.getShardingValue(Collections.emptyList())).getValueRange();
    assertThat(condition3.getOperator(), is(ShardingOperator.BETWEEN));
    assertThat(shardingValues3.lowerEndpoint(), is((Comparable) 5));
    assertThat(shardingValues3.upperEndpoint(), is((Comparable) 20));
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) Range(com.google.common.collect.Range)

Aggregations

Column (io.shardingjdbc.core.parsing.parser.context.condition.Column)15 Condition (io.shardingjdbc.core.parsing.parser.context.condition.Condition)15 Range (com.google.common.collect.Range)4 RangeShardingValue (io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue)4 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)4 SQLParsingEngine (io.shardingjdbc.core.parsing.SQLParsingEngine)3 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)3 LinkedList (java.util.LinkedList)3 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)2 SQLPlaceholderExpression (io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression)2 DMLStatement (io.shardingjdbc.core.parsing.parser.sql.dml.DMLStatement)2 ExpectedCondition (io.shardingjdbc.core.parsing.integrate.jaxb.condition.ExpectedCondition)1 GeneratedKey (io.shardingjdbc.core.parsing.parser.context.GeneratedKey)1 SQLIgnoreExpression (io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression)1 SQLTextExpression (io.shardingjdbc.core.parsing.parser.expression.SQLTextExpression)1 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)1