Search in sources :

Example 11 with ShardingRule

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

the class InsertStatementParserTest method assertParseWithoutParameter.

@Test
public void assertParseWithoutParameter() {
    ShardingRule shardingRule = createShardingRule();
    SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, "INSERT INTO `TABLE_XXX` (`field1`, `field2`) VALUES (10, 1)", shardingRule);
    InsertStatement insertStatement = (InsertStatement) statementParser.parse();
    assertInsertStatementWithoutParameter(insertStatement);
}
Also used : ShardingRule(io.shardingjdbc.core.rule.ShardingRule) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) Test(org.junit.Test)

Example 12 with ShardingRule

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

the class InsertStatementParserTest method assertParseWithParameter.

@Test
public void assertParseWithParameter() {
    ShardingRule shardingRule = createShardingRule();
    SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, "INSERT INTO TABLE_XXX (field1, field2) VALUES (?, ?)", shardingRule);
    InsertStatement insertStatement = (InsertStatement) statementParser.parse();
    assertInsertStatementWithParameter(insertStatement);
}
Also used : ShardingRule(io.shardingjdbc.core.rule.ShardingRule) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) Test(org.junit.Test)

Example 13 with ShardingRule

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

the class InsertStatementParserTest method assertParseWithGenerateKeyColumnsWithParameter.

@Test
public void assertParseWithGenerateKeyColumnsWithParameter() {
    ShardingRule shardingRule = createShardingRuleWithGenerateKeyColumns();
    SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, "INSERT INTO `TABLE_XXX` (`field1`) VALUES (?)", shardingRule);
    InsertStatement insertStatement = (InsertStatement) statementParser.parse();
    assertInsertStatementWithParameter(insertStatement);
}
Also used : ShardingRule(io.shardingjdbc.core.rule.ShardingRule) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) Test(org.junit.Test)

Example 14 with ShardingRule

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

the class InsertStatementParserTest method createShardingRuleWithGenerateKeyColumns.

private ShardingRule createShardingRuleWithGenerateKeyColumns() {
    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);
    }
    final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
    tableRuleConfig.setLogicTable("TABLE_XXX");
    tableRuleConfig.setActualDataNodes("ds.table_${0..2}");
    tableRuleConfig.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration("field1", new TestComplexKeysShardingAlgorithm()));
    tableRuleConfig.setKeyGeneratorColumnName("field2");
    shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
    shardingRuleConfig.setDefaultKeyGenerator(new IncrementKeyGenerator());
    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) IncrementKeyGenerator(io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator) DatabaseMetaData(java.sql.DatabaseMetaData) TestComplexKeysShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) DataSource(javax.sql.DataSource)

Example 15 with ShardingRule

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

the class ShardingNamespaceTest method assertShardingRuleWithAttributesDataSource.

@Test
public void assertShardingRuleWithAttributesDataSource() {
    Map<String, DataSource> dataSourceMap = getDataSourceMap("shardingRuleWithAttributesDataSource");
    assertNotNull(dataSourceMap.get("dbtbl_0"));
    assertNotNull(dataSourceMap.get("dbtbl_1"));
    ShardingRule shardingRule = getShardingRule("shardingRuleWithAttributesDataSource");
    assertThat(shardingRule.getDefaultDataSourceName(), is("dbtbl_0"));
    assertTrue(Arrays.equals(shardingRule.getDefaultDatabaseShardingStrategy().getShardingColumns().toArray(new String[] {}), new String[] { this.applicationContext.getBean("standardStrategy", StandardShardingStrategyConfiguration.class).getShardingColumn() }));
    assertTrue(Arrays.equals(shardingRule.getDefaultTableShardingStrategy().getShardingColumns().toArray(new String[] {}), new String[] { this.applicationContext.getBean("inlineStrategy", InlineShardingStrategyConfiguration.class).getShardingColumn() }));
    assertThat(shardingRule.getDefaultKeyGenerator().getClass().getName(), is("io.shardingjdbc.spring.fixture.IncrementKeyGenerator"));
}
Also used : StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) InlineShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration) DataSource(javax.sql.DataSource) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) SpringShardingDataSource(io.shardingjdbc.spring.datasource.SpringShardingDataSource) Test(org.junit.Test)

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