This is an automated email from the ASF dual-hosted git repository. zhangliang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push: new 47a24c66de3 Refactor TableAvailableSQLStatementContext (#35710) 47a24c66de3 is described below commit 47a24c66de3579870b051672738a8516e1fcce4b Author: Liang Zhang <zhangli...@apache.org> AuthorDate: Sat Jun 14 21:36:58 2025 +0800 Refactor TableAvailableSQLStatementContext (#35710) --- .../sql/ddl/ShardingCreateFunctionSupportedCheckerTest.java | 9 +++++---- .../checker/sql/dml/ShardingCopySupportedCheckerTest.java | 3 ++- .../checker/sql/dml/ShardingLoadDataSupportedCheckerTest.java | 6 ++++-- .../checker/sql/dml/ShardingLoadXmlSupportedCheckerTest.java | 6 ++++-- .../checker/ddl/ShardingDropTableRouteContextCheckerTest.java | 8 ++++---- .../route/engine/type/ShardingRouteEngineFactoryTest.java | 2 +- .../context/statement/TableAvailableSQLStatementContext.java | 5 ----- .../context/statement/TableAvailableSQLStatementContextTest.java | 2 +- 8 files changed, 21 insertions(+), 20 deletions(-) diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedCheckerTest.java index cd75e5116ad..b56b4cd023b 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedCheckerTest.java @@ -37,6 +37,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Collections; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -67,7 +68,7 @@ class ShardingCreateFunctionSupportedCheckerTest { routineBody.getValidStatements().add(selectValidStatementSegment); CreateFunctionStatement sqlStatement = mock(CreateFunctionStatement.class); when(sqlStatement.getRoutineBody()).thenReturn(Optional.of(routineBody)); - TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), sqlStatement, fromTable); + TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(fromTable)); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); ShardingSphereSchema schema = mock(ShardingSphereSchema.class); when(schema.containsTable("bar_tbl")).thenReturn(true); @@ -85,7 +86,7 @@ class ShardingCreateFunctionSupportedCheckerTest { routineBody.getValidStatements().add(validStatementSegment); CreateFunctionStatement sqlStatement = mock(CreateFunctionStatement.class); when(sqlStatement.getRoutineBody()).thenReturn(Optional.of(routineBody)); - TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), sqlStatement, fromTable); + TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(fromTable)); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(rule.isShardingTable("foo_tbl")).thenReturn(true); assertThrows(UnsupportedShardingOperationException.class, () -> new ShardingCreateFunctionSupportedChecker().check(rule, database, mock(), sqlStatementContext)); @@ -102,7 +103,7 @@ class ShardingCreateFunctionSupportedCheckerTest { routineBody.getValidStatements().add(validStatementSegment); CreateFunctionStatement sqlStatement = mock(CreateFunctionStatement.class); when(sqlStatement.getRoutineBody()).thenReturn(Optional.of(routineBody)); - TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), sqlStatement, fromTable); + TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(fromTable)); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); assertThrows(NoSuchTableException.class, () -> new ShardingCreateFunctionSupportedChecker().check(rule, database, mock(), sqlStatementContext)); } @@ -118,7 +119,7 @@ class ShardingCreateFunctionSupportedCheckerTest { routineBody.getValidStatements().add(validStatementSegment); CreateFunctionStatement sqlStatement = mock(CreateFunctionStatement.class); when(sqlStatement.getRoutineBody()).thenReturn(Optional.of(routineBody)); - TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), sqlStatement, table); + TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(table)); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); ShardingSphereSchema schema = mock(ShardingSphereSchema.class); when(schema.containsTable("foo_tbl")).thenReturn(true); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingCopySupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingCopySupportedCheckerTest.java index ec7dcda90ff..9b87a1cd193 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingCopySupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingCopySupportedCheckerTest.java @@ -29,6 +29,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Collections; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -57,6 +58,6 @@ class ShardingCopySupportedCheckerTest { CopyStatement sqlStatement = mock(CopyStatement.class); SimpleTableSegment table = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl"))); when(sqlStatement.getTable()).thenReturn(Optional.of(table)); - return new TableAvailableSQLStatementContext(mock(), sqlStatement, table); + return new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(table)); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedCheckerTest.java index 9715a64686b..7015d2e2216 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedCheckerTest.java @@ -29,6 +29,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Collections; + import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; @@ -45,7 +47,7 @@ class ShardingLoadDataSupportedCheckerTest { LoadDataStatement sqlStatement = mock(LoadDataStatement.class); SimpleTableSegment table = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl"))); when(sqlStatement.getTable()).thenReturn(table); - assertDoesNotThrow(() -> new ShardingLoadDataSupportedChecker().check(rule, mock(), mock(), new TableAvailableSQLStatementContext(mock(), sqlStatement, table))); + assertDoesNotThrow(() -> new ShardingLoadDataSupportedChecker().check(rule, mock(), mock(), new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(table)))); } @Test @@ -55,6 +57,6 @@ class ShardingLoadDataSupportedCheckerTest { when(sqlStatement.getTable()).thenReturn(table); when(rule.isShardingTable("foo_tbl")).thenReturn(true); assertThrows(UnsupportedShardingOperationException.class, - () -> new ShardingLoadDataSupportedChecker().check(rule, mock(), mock(), new TableAvailableSQLStatementContext(mock(), sqlStatement, table))); + () -> new ShardingLoadDataSupportedChecker().check(rule, mock(), mock(), new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(table)))); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadXmlSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadXmlSupportedCheckerTest.java index 468a6fcaff9..bab930e7103 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadXmlSupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadXmlSupportedCheckerTest.java @@ -29,6 +29,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Collections; + import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; @@ -45,7 +47,7 @@ class ShardingLoadXmlSupportedCheckerTest { LoadXMLStatement sqlStatement = mock(LoadXMLStatement.class); SimpleTableSegment table = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl"))); when(sqlStatement.getTable()).thenReturn(table); - assertDoesNotThrow(() -> new ShardingLoadXmlSupportedChecker().check(rule, mock(), mock(), new TableAvailableSQLStatementContext(mock(), sqlStatement, table))); + assertDoesNotThrow(() -> new ShardingLoadXmlSupportedChecker().check(rule, mock(), mock(), new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(table)))); } @Test @@ -55,6 +57,6 @@ class ShardingLoadXmlSupportedCheckerTest { when(sqlStatement.getTable()).thenReturn(table); when(rule.isShardingTable("foo_tbl")).thenReturn(true); assertThrows(UnsupportedShardingOperationException.class, - () -> new ShardingLoadXmlSupportedChecker().check(rule, mock(), mock(), new TableAvailableSQLStatementContext(mock(), sqlStatement, table))); + () -> new ShardingLoadXmlSupportedChecker().check(rule, mock(), mock(), new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(table)))); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/checker/ddl/ShardingDropTableRouteContextCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/checker/ddl/ShardingDropTableRouteContextCheckerTest.java index df0ff77fc8b..f4e0c7092fc 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/checker/ddl/ShardingDropTableRouteContextCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/checker/ddl/ShardingDropTableRouteContextCheckerTest.java @@ -94,7 +94,7 @@ class ShardingDropTableRouteContextCheckerTest { routeUnits.add(routeUnit); RouteContext routeContext = mock(RouteContext.class); when(routeContext.getRouteUnits()).thenReturn(routeUnits); - when(queryContext.getSqlStatementContext()).thenReturn(new TableAvailableSQLStatementContext(mock(), sqlStatement, table)); + when(queryContext.getSqlStatementContext()).thenReturn(new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(table))); assertThrows(InUsedTablesException.class, () -> new ShardingDropTableRouteContextChecker().check(shardingRule, queryContext, database, mock(ConfigurationProperties.class), routeContext)); } @@ -128,7 +128,7 @@ class ShardingDropTableRouteContextCheckerTest { routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singleton(new RouteMapper("t_order", "t_order_0")))); routeUnits.add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.singleton(new RouteMapper("t_order", "t_order_0")))); when(routeContext.getRouteUnits()).thenReturn(routeUnits); - when(queryContext.getSqlStatementContext()).thenReturn(new TableAvailableSQLStatementContext(mock(), sqlStatement, table)); + when(queryContext.getSqlStatementContext()).thenReturn(new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(table))); assertDoesNotThrow(() -> new ShardingDropTableRouteContextChecker().check(shardingRule, queryContext, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), routeContext)); } @@ -143,7 +143,7 @@ class ShardingDropTableRouteContextCheckerTest { Collection<RouteUnit> routeUnits = new LinkedList<>(); routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singleton(new RouteMapper("t_order", "t_order_0")))); when(routeContext.getRouteUnits()).thenReturn(routeUnits); - when(queryContext.getSqlStatementContext()).thenReturn(new TableAvailableSQLStatementContext(mock(), sqlStatement, table)); + when(queryContext.getSqlStatementContext()).thenReturn(new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(table))); assertThrows(ShardingDDLRouteException.class, () -> new ShardingDropTableRouteContextChecker().check(shardingRule, queryContext, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), routeContext)); } @@ -158,7 +158,7 @@ class ShardingDropTableRouteContextCheckerTest { routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singleton(new RouteMapper("t_config", "t_config")))); routeUnits.add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.singleton(new RouteMapper("t_config", "t_config")))); when(routeContext.getRouteUnits()).thenReturn(routeUnits); - when(queryContext.getSqlStatementContext()).thenReturn(new TableAvailableSQLStatementContext(mock(), sqlStatement, table)); + when(queryContext.getSqlStatementContext()).thenReturn(new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(table))); assertDoesNotThrow(() -> new ShardingDropTableRouteContextChecker().check(shardingRule, queryContext, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), routeContext)); } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactoryTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactoryTest.java index 9f7847c99e9..a41f50f2aa7 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactoryTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactoryTest.java @@ -131,7 +131,7 @@ class ShardingRouteEngineFactoryTest { void assertNewInstanceForDCLForSingleTable() { GrantStatement sqlStatement = new GrantStatement(); TableAvailableSQLStatementContext sqlStatementContext = new TableAvailableSQLStatementContext(mock(), - sqlStatement, new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("tbl")))); + sqlStatement, Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("tbl"))))); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, Collections.singletonList("tbl"), props); assertThat(actual, instanceOf(ShardingTableBroadcastRouteEngine.class)); diff --git a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/TableAvailableSQLStatementContext.java b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/TableAvailableSQLStatementContext.java index ab48de48911..95ac707d289 100644 --- a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/TableAvailableSQLStatementContext.java +++ b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/TableAvailableSQLStatementContext.java @@ -34,11 +34,6 @@ public final class TableAvailableSQLStatementContext extends CommonSQLStatementC private final TablesContext tablesContext; - public TableAvailableSQLStatementContext(final DatabaseType databaseType, final SQLStatement sqlStatement, final SimpleTableSegment table) { - super(databaseType, sqlStatement); - tablesContext = new TablesContext(table); - } - public TableAvailableSQLStatementContext(final DatabaseType databaseType, final SQLStatement sqlStatement, final Collection<SimpleTableSegment> tables) { super(databaseType, sqlStatement); tablesContext = new TablesContext(tables); diff --git a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/TableAvailableSQLStatementContextTest.java b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/TableAvailableSQLStatementContextTest.java index 7d7b8d21265..3dae832bbd9 100644 --- a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/TableAvailableSQLStatementContextTest.java +++ b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/TableAvailableSQLStatementContextTest.java @@ -39,7 +39,7 @@ class TableAvailableSQLStatementContextTest { TableNameSegment tableNameSegment = new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")); tableNameSegment.setTableBoundInfo(new TableSegmentBoundInfo(new IdentifierValue("foo_db"), new IdentifierValue("foo_schema"))); SimpleTableSegment table = new SimpleTableSegment(tableNameSegment); - TableAvailableSQLStatementContext actual = new TableAvailableSQLStatementContext(mock(), sqlStatement, table); + TableAvailableSQLStatementContext actual = new TableAvailableSQLStatementContext(mock(), sqlStatement, Collections.singleton(table)); assertThat(actual.getSqlStatement(), is(sqlStatement)); assertThat(actual.getTablesContext().getSimpleTables().stream().map(each -> each.getTableName().getIdentifier().getValue()).collect(Collectors.toList()), is(Collections.singletonList("foo_tbl")));