Search in sources :

Example 41 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class ShowTablesMergedResultTest method setUp.

@Before
public void setUp() throws SQLException {
    TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
    tableRuleConfig.setLogicTable("table");
    tableRuleConfig.setActualDataNodes("ds.table_${0..2}");
    tableRuleConfig.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration("field1, field2, field3", new TestComplexKeysShardingAlgorithm()));
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
    shardingRule = new ShardingRule(shardingRuleConfig, Lists.newArrayList("ds"));
    resultSet = mock(ResultSet.class);
    ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class);
    when(resultSet.getMetaData()).thenReturn(resultSetMetaData);
    when(resultSetMetaData.getColumnCount()).thenReturn(1);
    List<ResultSet> resultSets = Lists.newArrayList(resultSet);
    for (ResultSet each : resultSets) {
        when(each.next()).thenReturn(true, false);
    }
    queryResults = new ArrayList<>(resultSets.size());
    for (ResultSet each : resultSets) {
        queryResults.add(new TestQueryResult(each));
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) TestQueryResult(io.shardingjdbc.core.merger.fixture.TestQueryResult) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) ResultSet(java.sql.ResultSet) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) TestComplexKeysShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) Before(org.junit.Before)

Example 42 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class UpdateStatementParserTest method parseWithoutParameter.

@Test
public void parseWithoutParameter() {
    ShardingRule shardingRule = createShardingRule();
    SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, "UPDATE TABLE_XXX xxx SET TABLE_XXX.field1=field1+1,xxx.field2=2 WHERE TABLE_XXX.field4<10 AND" + " TABLE_XXX.field1=1 AND xxx.field5>10 AND TABLE_XXX.field2 IN (1,3) AND xxx.field6<=10 AND TABLE_XXX.field3 BETWEEN 5 AND 20 AND xxx.field7>=10", shardingRule);
    DMLStatement updateStatement = (DMLStatement) statementParser.parse();
    assertUpdateStatementWithoutParameter(updateStatement);
}
Also used : ShardingRule(io.shardingjdbc.core.rule.ShardingRule) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) DMLStatement(io.shardingjdbc.core.parsing.parser.sql.dml.DMLStatement) Test(org.junit.Test)

Example 43 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class UpdateStatementParserTest method parseWithParameter.

@Test
public void parseWithParameter() {
    String sql = "UPDATE TABLE_XXX AS xxx SET field1=field1+? WHERE field4<? AND xxx.field1=? AND field5>? AND xxx.field2 IN (?, ?) AND field6<=? AND xxx.field3 BETWEEN ? AND ? AND field7>=?";
    ShardingRule shardingRule = createShardingRule();
    SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, sql, shardingRule);
    DMLStatement updateStatement = (DMLStatement) statementParser.parse();
    assertUpdateStatementWitParameter(updateStatement);
}
Also used : ShardingRule(io.shardingjdbc.core.rule.ShardingRule) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) DMLStatement(io.shardingjdbc.core.parsing.parser.sql.dml.DMLStatement) Test(org.junit.Test)

Example 44 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class AbstractStatementParserTest method createShardingRule.

protected final ShardingRule createShardingRule() {
    DataSource dataSource = mock(DataSource.class);
    Connection connection = mock(Connection.class);
    DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
    try {
        when(dataSource.getConnection()).thenReturn(connection);
        when(connection.getMetaData()).thenReturn(databaseMetaData);
        when(databaseMetaData.getDatabaseProductName()).thenReturn("H2");
    } catch (final SQLException ex) {
        throw new RuntimeException(ex);
    }
    TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
    tableRuleConfig.setLogicTable("TABLE_XXX");
    tableRuleConfig.setActualDataNodes("ds.table_${0..2}");
    tableRuleConfig.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration("field1, field2, field3, field4, field5, field6, field7", new TestComplexKeysShardingAlgorithm()));
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
    return new ShardingRule(shardingRuleConfig, Lists.newArrayList("ds"));
}
Also used : SQLException(java.sql.SQLException) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) Connection(java.sql.Connection) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) DatabaseMetaData(java.sql.DatabaseMetaData) TestComplexKeysShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) DataSource(javax.sql.DataSource)

Example 45 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule 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)

Aggregations

ShardingRule (io.shardingjdbc.core.rule.ShardingRule)60 Test (org.junit.Test)35 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)20 SQLParsingEngine (io.shardingjdbc.core.parsing.SQLParsingEngine)19 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)16 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)14 DataSource (javax.sql.DataSource)14 BindingTableRule (io.shardingjdbc.core.rule.BindingTableRule)10 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)8 DMLStatement (io.shardingjdbc.core.parsing.parser.sql.dml.DMLStatement)8 Map (java.util.Map)8 ComplexShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration)7 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)7 TableRule (io.shardingjdbc.core.rule.TableRule)6 SQLShardingRule (io.shardingjdbc.core.integrate.jaxb.SQLShardingRule)5 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)5 HashMap (java.util.HashMap)5 Before (org.junit.Before)5 TestComplexKeysShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm)4 InlineShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration)4