ShardingRouteEngineFactory { public static ShardingRouteEngine newInstance(final ShardingRule shardingRule, final ShardingSphereMetaData metaData, final SQLStatementContext sqlStatementContext, final ShardingConditions shardingConditions, final ConfigurationProperties props) { SQLStatement sqlStatement = sqlStatementContext.getSqlStatement(); Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames(); if (sqlStatement instanceof TCLStatement) { return new ShardingDatabaseBroadcastRoutingEngine(); } if (sqlStatement instanceof DDLStatement) { return new ShardingTableBroadcastRoutingEngine(metaData.getRuleSchemaMetaData().getConfiguredSchemaMetaData(), sqlStatementContext); } if (sqlStatement instanceof DALStatement) { return getDALRoutingEngine(shardingRule, metaData.getRuleSchemaMetaData().getUnconfiguredSchemaMetaDataMap(), sqlStatement, tableNames); } if (sqlStatement instanceof DCLStatement) { return getDCLRoutingEngine(sqlStatementContext, metaData); } if (shardingRule.isAllBroadcastTables(tableNames)) { return sqlStatement instanceof SelectStatement ? new ShardingUnicastRoutingEngine(tableNames) : new ShardingDatabaseBroadcastRoutingEngine(); } if (sqlStatementContext.getSqlStatement() instanceof DMLStatement && shardingConditions.isAlwaysFalse() || tableNames.isEmpty()) { return new ShardingUnicastRoutingEngine(tableNames); } if (!shardingRule.tableRuleExists(tableNames)) { return new ShardingUnconfiguredTablesRoutingEngine(tableNames, metaData.getRuleSchemaMetaData().getUnconfiguredSchemaMetaDataMap()); } return getShardingRoutingEngine(shardingRule, shardingConditions, tableNames, props); }  static ShardingRouteEngine newInstance(final ShardingRule shardingRule, final ShardingSphereMetaData metaData, 
                                                  final SQLStatementContext sqlStatementContext, final ShardingConditions shardingConditions, final ConfigurationProperties props);  }
@Test public void assertNewInstanceForTCL() { TCLStatement tclStatement = mock(TCLStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(tclStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingDatabaseBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForDDL() { when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DDLStatement.class)); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingTableBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForDALWithTables() { tableNames.add("tbl"); when(shardingRule.tableRuleExists(tableNames)).thenReturn(true); DALStatement dalStatement = mock(DALStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingUnicastRoutingEngine.class)); }
@Test public void assertNewInstanceForDALWithoutTables() { DALStatement dalStatement = mock(DALStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingDataSourceGroupBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForDALShow() { DALStatement dalStatement = mock(MySQLShowDatabasesStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingDatabaseBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForMySQLDALSet() { DALStatement dalStatement = mock(MySQLSetStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingDatabaseBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForPostgreSQLDALSet() { DALStatement dalStatement = mock(PostgreSQLSetStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingDatabaseBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForMySQLDCLForSingleTable() { MySQLGrantStatement grantStatement = new MySQLGrantStatement(); grantStatement.getTables().add(new SimpleTableSegment(0, 0, new IdentifierValue("tbl"))); GrantStatementContext sqlStatementContext = new GrantStatementContext(grantStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingTableBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForOracleDCLForSingleTable() { OracleGrantStatement grantStatement = new OracleGrantStatement(); grantStatement.getTables().add(new SimpleTableSegment(0, 0, new IdentifierValue("tbl"))); GrantStatementContext sqlStatementContext = new GrantStatementContext(grantStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingTableBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForPostgreSQLDCLForSingleTable() { PostgreSQLGrantStatement grantStatement = new PostgreSQLGrantStatement(); grantStatement.getTables().add(new SimpleTableSegment(0, 0, new IdentifierValue("tbl"))); GrantStatementContext sqlStatementContext = new GrantStatementContext(grantStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingTableBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForSQLServerDCLForSingleTable() { SQLServerGrantStatement grantStatement = new SQLServerGrantStatement(); grantStatement.getTables().add(new SimpleTableSegment(0, 0, new IdentifierValue("tbl"))); GrantStatementContext sqlStatementContext = new GrantStatementContext(grantStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingTableBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForSQL92DCLForSingleTable() { SQL92GrantStatement grantStatement = new SQL92GrantStatement(); grantStatement.getTables().add(new SimpleTableSegment(0, 0, new IdentifierValue("tbl"))); GrantStatementContext sqlStatementContext = new GrantStatementContext(grantStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingTableBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForDCLForNoSingleTable() { DCLStatement dclStatement = mock(DCLStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dclStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingInstanceBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForUnconfiguredTables() { tableNames.add("tbl"); when(shardingRule.tableRuleExists(tableNames)).thenReturn(false); SQLStatement sqlStatement = mock(SQLStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingUnconfiguredTablesRoutingEngine.class)); }
@Test public void assertNewInstanceForSelectWithUnconfiguredTables() { tableNames.add("tbl"); when(shardingRule.tableRuleExists(tableNames)).thenReturn(false); SQLStatement sqlStatement = mock(MySQLSelectStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingUnconfiguredTablesRoutingEngine.class)); }
@Test public void assertNewInstanceForSelectWithoutUnconfiguredTables() { SQLStatement sqlStatement = mock(MySQLSelectStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingUnicastRoutingEngine.class)); }
@Test public void assertNewInstanceForInsertBroadcastTable() { when(shardingRule.isAllBroadcastTables(tableNames)).thenReturn(true); SQLStatement sqlStatement = mock(InsertStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingDatabaseBroadcastRoutingEngine.class)); }
@Test public void assertNewInstanceForSelectBroadcastTable() { when(shardingRule.isAllBroadcastTables(tableNames)).thenReturn(true); SQLStatement sqlStatement = mock(MySQLSelectStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingUnicastRoutingEngine.class)); }
@Test public void assertNewInstanceForAlwaysFalse() { SQLStatement sqlStatement = mock(SQLStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingUnicastRoutingEngine.class)); }
@Test public void assertNewInstanceForStandard() { SQLStatement sqlStatement = mock(SQLStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); tableNames.add(""); when(shardingRule.getShardingLogicTableNames(tableNames)).thenReturn(tableNames); when(shardingRule.tableRuleExists(tableNames)).thenReturn(true); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingStandardRoutingEngine.class)); }
@Test public void assertNewInstanceForComplex() { SQLStatement sqlStatement = mock(SQLStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); tableNames.add("1"); tableNames.add("2"); when(shardingRule.getShardingLogicTableNames(tableNames)).thenReturn(tableNames); when(shardingRule.tableRuleExists(tableNames)).thenReturn(true); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingComplexRoutingEngine.class)); }
@Test public void assertNewInstanceForShowCreateTableWithTableRule() { DALStatement dalStatement = mock(MySQLShowCreateTableStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); when(shardingRule.tableRuleExists(tableNames)).thenReturn(true); tableNames.add("table_1"); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingUnicastRoutingEngine.class)); }
@Test public void assertNewInstanceForShowCreateTableWithUnconfiguredTables() { DALStatement dalStatement = mock(MySQLShowCreateTableStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); when(shardingRule.tableRuleExists(tableNames)).thenReturn(false); tableNames.add("table_1"); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingUnconfiguredTablesRoutingEngine.class)); }
@Test public void assertNewInstanceForShowColumnsWithTableRule() { DALStatement dalStatement = mock(MySQLShowColumnsStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); when(shardingRule.tableRuleExists(tableNames)).thenReturn(true); tableNames.add("table_1"); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingUnicastRoutingEngine.class)); }
@Test public void assertNewInstanceForShowColumnsWithUnconfiguredTables() { DALStatement dalStatement = mock(MySQLShowColumnsStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); when(shardingRule.tableRuleExists(tableNames)).thenReturn(false); tableNames.add("table_1"); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions, props); assertThat(actual, instanceOf(ShardingUnconfiguredTablesRoutingEngine.class)); }