Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ShardingSphereMetaData's constructor with ShardingSphereDatabase collection #33900

Merged
merged 9 commits into from
Dec 3, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,13 @@ public static EncryptRule createEncryptRule() {
* @return created insert statement context
*/
public static InsertStatementContext createInsertStatementContext(final List<Object> params) {
InsertStatement insertStatement = createInsertStatement();
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(schema.getAllColumnNames("tbl")).thenReturn(Arrays.asList("id", "name", "status", "pwd"));
when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema);
ShardingSphereMetaData metaData = new ShardingSphereMetaData(
Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class));
return new InsertStatementContext(metaData, params, insertStatement, DefaultDatabase.LOGIC_NAME);
ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class));
return new InsertStatementContext(metaData, params, createInsertStatement(), DefaultDatabase.LOGIC_NAME);
}

private static InsertStatement createInsertStatement() {
Expand Down Expand Up @@ -210,13 +209,12 @@ private static List<ExpressionSegment> createValueExpressions() {
* @return created insert select statement context
*/
public static InsertStatementContext createInsertSelectStatementContext(final List<Object> params, final boolean containsInsertColumns) {
InsertStatement insertStatement = createInsertSelectStatement(containsInsertColumns);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(schema.getAllColumnNames("t_user")).thenReturn(Arrays.asList("user_id", "user_name", "pwd"));
when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema);
ShardingSphereMetaData metaData = new ShardingSphereMetaData(
Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class));
return new InsertStatementContext(metaData, params, insertStatement, DefaultDatabase.LOGIC_NAME);
ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class));
return new InsertStatementContext(metaData, params, createInsertSelectStatement(containsInsertColumns), DefaultDatabase.LOGIC_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ private ShardingSphereDatabase createDatabase(final ShardingRule shardingRule, f
}

private QueryContext createQueryContext(final ShardingSphereDatabase database, final String sql, final List<Object> params) {
SQLStatementContext sqlStatementContext = new SQLBindEngine(createShardingSphereMetaData(database), DATABASE_NAME, new HintValueContext()).bind(parse(sql), params);
SQLStatementContext sqlStatementContext = new SQLBindEngine(
new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)),
DATABASE_NAME, new HintValueContext()).bind(parse(sql), params);
return new QueryContext(sqlStatementContext, sql, params, new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class));
}

Expand All @@ -158,11 +160,6 @@ private ConnectionContext mockConnectionContext() {
return result;
}

private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) {
return new ShardingSphereMetaData(Collections.singletonMap(DATABASE_NAME, database), mock(ResourceMetaData.class),
mock(RuleMetaData.class), mock(ConfigurationProperties.class));
}

private SQLStatement parse(final String sql) {
CacheOption cacheOption = new CacheOption(0, 0L);
return new SQLStatementParserEngine(databaseType, cacheOption, cacheOption).parse(sql, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
class ShardingInsertSupportedCheckerTest {

@Mock
private ShardingRule shardingRule;
private ShardingRule rule;

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ShardingSphereDatabase database;
Expand All @@ -77,57 +77,57 @@ class ShardingInsertSupportedCheckerTest {
void assertCheckWhenInsertMultiTables() {
InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertStatement());
Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames();
when(shardingRule.isAllShardingTables(tableNames)).thenReturn(false);
when(shardingRule.containsShardingTable(tableNames)).thenReturn(true);
assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
when(rule.isAllShardingTables(tableNames)).thenReturn(false);
when(rule.containsShardingTable(tableNames)).thenReturn(true);
assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingInsertSupportedChecker().check(rule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}

private InsertStatementContext createInsertStatementContext(final List<Object> params, final InsertStatement insertStatement) {
when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class));
ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class),
mock(RuleMetaData.class), mock(ConfigurationProperties.class));
ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class));
return new InsertStatementContext(metaData, params, insertStatement, DefaultDatabase.LOGIC_NAME);
}

@Test
void assertCheckWhenInsertSelectWithoutKeyGenerateColumn() {
when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(false);
when(rule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
when(rule.isGenerateKeyColumn("id", "user")).thenReturn(false);
InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTableNames().addAll(createSingleTablesContext().getTableNames());
assertThrows(MissingGenerateKeyColumnWithInsertSelectException.class,
() -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
() -> new ShardingInsertSupportedChecker().check(rule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}

@Test
void assertCheckWhenInsertSelectWithKeyGenerateColumn() {
when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
when(rule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
when(rule.isGenerateKeyColumn("id", "user")).thenReturn(true);
InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTableNames().addAll(createSingleTablesContext().getTableNames());
assertDoesNotThrow(() -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
assertDoesNotThrow(() -> new ShardingInsertSupportedChecker().check(rule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}

@Test
void assertCheckWhenInsertSelectWithoutBindingTables() {
when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
when(rule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
when(rule.isGenerateKeyColumn("id", "user")).thenReturn(true);
TablesContext multiTablesContext = createMultiTablesContext();
when(shardingRule.isAllBindingTables(multiTablesContext.getTableNames())).thenReturn(false);
when(shardingRule.containsShardingTable(multiTablesContext.getTableNames())).thenReturn(true);
when(rule.isAllBindingTables(multiTablesContext.getTableNames())).thenReturn(false);
when(rule.containsShardingTable(multiTablesContext.getTableNames())).thenReturn(true);
InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTableNames().addAll(multiTablesContext.getTableNames());
assertThrows(InsertSelectTableViolationException.class, () -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
assertThrows(InsertSelectTableViolationException.class, () -> new ShardingInsertSupportedChecker().check(rule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}

@Test
void assertCheckWhenInsertSelectWithBindingTables() {
when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
when(rule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
when(rule.isGenerateKeyColumn("id", "user")).thenReturn(true);
TablesContext multiTablesContext = createMultiTablesContext();
InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTableNames().addAll(multiTablesContext.getTableNames());
assertDoesNotThrow(() -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
assertDoesNotThrow(() -> new ShardingInsertSupportedChecker().check(rule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}

private InsertStatement createInsertStatement() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.apache.shardingsphere.infra.merge.engine.merger.impl.TransparentResultMerger;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sharding.merge.dal.ShardingDALResultMerger;
Expand Down Expand Up @@ -62,6 +60,8 @@

class ShardingResultMergerEngineTest {

private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "MySQL");

@Test
void assertNewInstanceWithSelectStatementForMySQL() {
assertNewInstanceWithSelectStatement(new MySQLSelectStatement());
Expand All @@ -88,27 +88,21 @@ void assertNewInstanceWithSelectStatementForSQLServer() {
}

private void assertNewInstanceWithSelectStatement(final SelectStatement selectStatement) {
ConfigurationProperties props = new ConfigurationProperties(new Properties());
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class));
selectStatement.setProjections(new ProjectionsSegment(0, 0));
SelectStatementContext sqlStatementContext = new SelectStatementContext(createShardingSphereMetaData(database),
SelectStatementContext sqlStatementContext = new SelectStatementContext(new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()),
Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList());
assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "MySQL"), null, props,
sqlStatementContext), instanceOf(ShardingDQLResultMerger.class));
}

private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) {
return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class),
mock(RuleMetaData.class), mock(ConfigurationProperties.class));
assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, databaseType, null, new ConfigurationProperties(new Properties()), sqlStatementContext),
instanceOf(ShardingDQLResultMerger.class));
}

@Test
void assertNewInstanceWithDALStatement() {
ConfigurationProperties props = new ConfigurationProperties(new Properties());
UnknownSQLStatementContext sqlStatementContext = new UnknownSQLStatementContext(new PostgreSQLShowStatement(""));
assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "MySQL"), null, props,
sqlStatementContext), instanceOf(ShardingDALResultMerger.class));
assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, databaseType, null, props, sqlStatementContext), instanceOf(ShardingDALResultMerger.class));
}

@Test
Expand All @@ -119,21 +113,20 @@ void assertNewInstanceWithOtherStatement() {
insertStatement.setInsertColumns(insertColumnsSegment);
InsertStatementContext sqlStatementContext = createInsertStatementContext(insertStatement);
ConfigurationProperties props = new ConfigurationProperties(new Properties());
assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "MySQL"), null, props,
sqlStatementContext), instanceOf(TransparentResultMerger.class));
assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, databaseType, null, props, sqlStatementContext), instanceOf(TransparentResultMerger.class));
}

private InsertStatementContext createInsertStatementContext(final InsertStatement insertStatement) {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class));
return new InsertStatementContext(createShardingSphereMetaData(database), Collections.emptyList(), insertStatement, DefaultDatabase.LOGIC_NAME);
return new InsertStatementContext(new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()), Collections.emptyList(), insertStatement, DefaultDatabase.LOGIC_NAME);
}

@Test
void assertNewInstanceWithDDLStatement() {
ConfigurationProperties props = new ConfigurationProperties(new Properties());
UnknownSQLStatementContext sqlStatementContext = new UnknownSQLStatementContext(new OpenGaussFetchStatement());
assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "MySQL"), null, props,
sqlStatementContext), instanceOf(ShardingDDLResultMerger.class));
assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, databaseType, null, props, sqlStatementContext), instanceOf(ShardingDDLResultMerger.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ void setUp() {
}

private ShardingSphereMetaData createShardingSphereMetaData() {
return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)), mock(ResourceMetaData.class),
mock(RuleMetaData.class), mock(ConfigurationProperties.class));
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
return new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class));
}

@Test
Expand Down
Loading
Loading