Skip to content

Commit

Permalink
Fixes #3518 (#3558)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaigorJiang authored and terrymanu committed Nov 19, 2019
1 parent 5266062 commit 4bd1c3d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Expand Up @@ -102,6 +102,9 @@ private static RoutingEngine getDALRoutingEngine(final ShardingRule shardingRule
if (sqlStatement instanceof SetStatement || sqlStatement instanceof ResetParameterStatement || sqlStatement instanceof ShowDatabasesStatement) {
return new DatabaseBroadcastRoutingEngine(shardingRule);
}
if (!tableNames.isEmpty() && !shardingRule.tableRuleExists(tableNames) && shardingRule.hasDefaultDataSourceName()) {
return new DefaultDatabaseRoutingEngine(shardingRule, tableNames);
}
if (!tableNames.isEmpty()) {
return new UnicastRoutingEngine(shardingRule, tableNames);
}
Expand Down
Expand Up @@ -22,6 +22,8 @@
import org.apache.shardingsphere.core.preprocessor.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.DALStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowColumnsStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowCreateTableStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowDatabasesStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.postgresql.SetStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dcl.DCLStatement;
Expand Down Expand Up @@ -237,4 +239,47 @@ public void assertNewInstanceForTableRuleNotExists() {
RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions);
assertThat(actual, instanceOf(UnicastRoutingEngine.class));
}

@Test
public void assertNewInstanceForShowCreateTableWithTableRule() {
DALStatement dalStatement = mock(ShowCreateTableStatement.class);
when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement);
when(shardingRule.tableRuleExists(tableNames)).thenReturn(true);
tableNames.add("table_1");
RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions);
assertThat(actual, instanceOf(UnicastRoutingEngine.class));
}

@Test
public void assertNewInstanceForShowCreateTableWithDefaultDataSource() {
DALStatement dalStatement = mock(ShowCreateTableStatement.class);
when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement);
when(shardingRule.tableRuleExists(tableNames)).thenReturn(false);
when(shardingRule.hasDefaultDataSourceName()).thenReturn(true);
tableNames.add("table_1");
RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions);
assertThat(actual, instanceOf(DefaultDatabaseRoutingEngine.class));
}

@Test
public void assertNewInstanceForShowColumnsWithTableRule() {
DALStatement dalStatement = mock(ShowColumnsStatement.class);
when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement);
when(shardingRule.tableRuleExists(tableNames)).thenReturn(true);
tableNames.add("table_1");
RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions);
assertThat(actual, instanceOf(UnicastRoutingEngine.class));
}

@Test
public void assertNewInstanceForShowColumnsWithDefaultDataSource() {
DALStatement dalStatement = mock(ShowColumnsStatement.class);
when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement);
when(shardingRule.tableRuleExists(tableNames)).thenReturn(false);
when(shardingRule.hasDefaultDataSourceName()).thenReturn(true);
tableNames.add("table_1");
RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions);
assertThat(actual, instanceOf(DefaultDatabaseRoutingEngine.class));
}

}

0 comments on commit 4bd1c3d

Please sign in to comment.