Search in sources :

Example 6 with RangeShardingValue

use of io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue in project sharding-jdbc by shardingjdbc.

the class HintManagerTest method assertAddTableShardingValueForBetween.

@Test
public void assertAddTableShardingValueForBetween() {
    try (HintManager hintManager = HintManager.getInstance()) {
        hintManager.addTableShardingValue("logicTable", "shardingColumn", ShardingOperator.BETWEEN, 1, 10);
        ShardingKey shardingKey = new ShardingKey("logicTable", "shardingColumn");
        assertTrue(HintManagerHolder.getTableShardingValue(shardingKey).isPresent());
        assertThat(hintManager.getTableShardingValue(shardingKey).getColumnName(), is("shardingColumn"));
        assertThat(((RangeShardingValue) hintManager.getTableShardingValue(shardingKey)).getValueRange().lowerEndpoint(), is((Comparable) 1));
        assertThat(((RangeShardingValue) hintManager.getTableShardingValue(shardingKey)).getValueRange().upperEndpoint(), is((Comparable) 10));
    }
}
Also used : RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) ShardingKey(io.shardingjdbc.core.hint.ShardingKey) Test(org.junit.Test)

Example 7 with RangeShardingValue

use of io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue in project sharding-jdbc by shardingjdbc.

the class HintManagerTest method assertAddDatabaseShardingValueForBetween.

@Test
public void assertAddDatabaseShardingValueForBetween() {
    try (HintManager hintManager = HintManager.getInstance()) {
        hintManager.addDatabaseShardingValue("logicTable", "shardingColumn", ShardingOperator.BETWEEN, 1, 10);
        ShardingKey shardingKey = new ShardingKey("logicTable", "shardingColumn");
        assertTrue(HintManagerHolder.getDatabaseShardingValue(shardingKey).isPresent());
        assertThat(hintManager.getDatabaseShardingValue(shardingKey).getColumnName(), is("shardingColumn"));
        assertThat(((RangeShardingValue) hintManager.getDatabaseShardingValue(shardingKey)).getValueRange().lowerEndpoint(), is((Comparable) 1));
        assertThat(((RangeShardingValue) hintManager.getDatabaseShardingValue(shardingKey)).getValueRange().upperEndpoint(), is((Comparable) 10));
    }
}
Also used : RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) ShardingKey(io.shardingjdbc.core.hint.ShardingKey) Test(org.junit.Test)

Example 8 with RangeShardingValue

use of io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue in project sharding-jdbc by shardingjdbc.

the class ShardingStrategyTest method assertDoShardingForBetweenSingleKey.

@Test
public void assertDoShardingForBetweenSingleKey() {
    StandardShardingStrategy strategy = new StandardShardingStrategy(new StandardShardingStrategyConfiguration("column", new TestPreciseShardingAlgorithm(), new TestRangeShardingAlgorithm()));
    assertThat(strategy.doSharding(targets, Collections.<ShardingValue>singletonList(new RangeShardingValue<>("logicTable", "column", Range.open("1", "3")))), is((Collection<String>) Sets.newHashSet("1", "2", "3")));
}
Also used : StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) TestPreciseShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestPreciseShardingAlgorithm) TestRangeShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestRangeShardingAlgorithm) Collection(java.util.Collection) PreciseShardingValue(io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) StandardShardingStrategy(io.shardingjdbc.core.routing.strategy.standard.StandardShardingStrategy) Test(org.junit.Test)

Example 9 with RangeShardingValue

use of io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue 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)

Aggregations

RangeShardingValue (io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue)9 Range (com.google.common.collect.Range)4 Column (io.shardingjdbc.core.parsing.parser.context.condition.Column)4 Condition (io.shardingjdbc.core.parsing.parser.context.condition.Condition)4 Test (org.junit.Test)4 ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)3 ListShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue)2 PreciseShardingValue (io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue)2 ShardingKey (io.shardingjdbc.core.hint.ShardingKey)2 TestPreciseShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestPreciseShardingAlgorithm)1 TestRangeShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestRangeShardingAlgorithm)1 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)1 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)1 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)1 StandardShardingStrategy (io.shardingjdbc.core.routing.strategy.standard.StandardShardingStrategy)1 Collection (java.util.Collection)1 TreeSet (java.util.TreeSet)1