Search in sources :

Example 11 with Condition

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

the class ConditionAssert method assertConditions.

/**
 * Assert conditions.
 *
 * @param actual actual conditions
 * @param expected expected conditions
 */
public void assertConditions(final Conditions actual, final List<ExpectedCondition> expected) {
    assertThat(assertMessage.getFullAssertMessage("Conditions size assertion error: "), actual.getConditions().size(), is(expected.size()));
    for (ExpectedCondition each : expected) {
        Optional<Condition> condition = actual.find(new Column(each.getColumnName(), each.getTableName()));
        assertTrue(assertMessage.getFullAssertMessage("Table should exist: "), condition.isPresent());
        assertCondition(condition.get(), each);
    }
}
Also used : ExpectedCondition(io.shardingjdbc.core.parsing.integrate.jaxb.condition.ExpectedCondition) Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) ExpectedCondition(io.shardingjdbc.core.parsing.integrate.jaxb.condition.ExpectedCondition)

Example 12 with Condition

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

the class DeleteStatementParserTest method parseWithSpecialSyntax.

private void parseWithSpecialSyntax(final DatabaseType dbType, final String actualSQL) {
    ShardingRule shardingRule = createShardingRule();
    DMLStatement deleteStatement = (DMLStatement) new SQLParsingEngine(dbType, actualSQL, shardingRule).parse();
    assertThat(deleteStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX"));
    assertFalse(deleteStatement.getTables().find("TABLE_XXX").get().getAlias().isPresent());
    Condition condition = deleteStatement.getConditions().find(new Column("field1", "TABLE_XXX")).get();
    assertThat(condition.getOperator(), is(ShardingOperator.EQUAL));
    assertThat(((ListShardingValue<? extends Comparable>) condition.getShardingValue(Collections.emptyList())).getValues().iterator().next(), is((Comparable) 1));
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) DMLStatement(io.shardingjdbc.core.parsing.parser.sql.dml.DMLStatement) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine)

Example 13 with Condition

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

the class DeleteStatementParserTest method assertDeleteStatementWithoutParameter.

private void assertDeleteStatementWithoutParameter(final DMLStatement deleteStatement) {
    assertThat(deleteStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX"));
    assertThat(deleteStatement.getTables().find("xxx").get().getAlias().get(), is("xxx"));
    Condition condition1 = deleteStatement.getConditions().find(new Column("field1", "TABLE_XXX")).get();
    assertThat(condition1.getOperator(), is(ShardingOperator.EQUAL));
    assertThat(((ListShardingValue) condition1.getShardingValue(Collections.emptyList())).getValues().iterator().next(), is((Object) 1));
    Condition condition2 = deleteStatement.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 = deleteStatement.getConditions().find(new Column("field3", "TABLE_XXX")).get();
    assertThat(condition3.getOperator(), is(ShardingOperator.BETWEEN));
    Range shardingValues3 = ((RangeShardingValue) condition3.getShardingValue(Collections.emptyList())).getValueRange();
    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)

Example 14 with Condition

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

the class InsertStatementParserTest method assertInsertStatementWithParameter.

private void assertInsertStatementWithParameter(final InsertStatement insertStatement) {
    assertThat(insertStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX"));
    Condition condition = insertStatement.getConditions().find(new Column("field1", "TABLE_XXX")).get();
    assertThat(condition.getOperator(), is(ShardingOperator.EQUAL));
    assertThat(((ListShardingValue<? extends Comparable>) condition.getShardingValue(Collections.<Object>singletonList(0))).getValues().iterator().next(), is((Comparable) 0));
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column)

Example 15 with Condition

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

the class InsertStatement method appendGenerateKeyToken.

private void appendGenerateKeyToken(final ShardingRule shardingRule, final TableRule tableRule, final ItemsToken valuesToken) {
    if (0 == getParametersIndex()) {
        Number generatedKey = shardingRule.generateKey(tableRule.getLogicTable());
        valuesToken.getItems().add(generatedKey.toString());
        getConditions().add(new Condition(new Column(tableRule.getGenerateKeyColumn(), tableRule.getLogicTable()), new SQLNumberExpression(generatedKey)), shardingRule);
        this.generatedKey = new GeneratedKey(tableRule.getLogicTable(), -1, generatedKey);
    } else {
        valuesToken.getItems().add(Symbol.QUESTION.getLiterals());
        getConditions().add(new Condition(new Column(tableRule.getGenerateKeyColumn(), tableRule.getLogicTable()), new SQLPlaceholderExpression(getParametersIndex())), shardingRule);
        generatedKey = new GeneratedKey(tableRule.getGenerateKeyColumn(), getParametersIndex(), null);
    }
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) GeneratedKey(io.shardingjdbc.core.parsing.parser.context.GeneratedKey) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) SQLPlaceholderExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression)

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