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 71861fd13fe Merge PrepareStatementContext and
TableAvailableSQLStatementContext (#35696)
71861fd13fe is described below
commit 71861fd13fe7a47f3c33cd718f9b9a0a040013a0
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Jun 13 17:25:16 2025 +0800
Merge PrepareStatementContext and TableAvailableSQLStatementContext (#35696)
* Merge PrepareStatementContext and TableAvailableSQLStatementContext
* Merge PrepareStatementContext and TableAvailableSQLStatementContext
---
.../statement/SQLStatementContextFactory.java | 3 +-
.../type/ddl/PrepareStatementContext.java | 58 --------------
.../type/ddl/PrepareStatementContextTest.java | 92 ----------------------
.../core/statement/ddl/PrepareStatement.java | 18 +++++
4 files changed, 19 insertions(+), 152 deletions(-)
diff --git
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/SQLStatementContextFactory.java
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/SQLStatementContextFactory.java
index 7288ef2987a..6ab074901c9 100644
---
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/SQLStatementContextFactory.java
+++
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/SQLStatementContextFactory.java
@@ -42,7 +42,6 @@ import
org.apache.shardingsphere.infra.binder.context.statement.type.ddl.DropTab
import
org.apache.shardingsphere.infra.binder.context.statement.type.ddl.DropViewStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.type.ddl.FetchStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.type.ddl.MoveStatementContext;
-import
org.apache.shardingsphere.infra.binder.context.statement.type.ddl.PrepareStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.type.dml.CopyStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.type.dml.DeleteStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.type.dml.InsertStatementContext;
@@ -218,7 +217,7 @@ public final class SQLStatementContextFactory {
return new DropViewStatementContext(databaseType,
(DropViewStatement) sqlStatement);
}
if (sqlStatement instanceof PrepareStatement) {
- return new PrepareStatementContext(databaseType,
(PrepareStatement) sqlStatement);
+ return new TableAvailableSQLStatementContext(databaseType,
sqlStatement, ((PrepareStatement) sqlStatement).getTables());
}
if (sqlStatement instanceof CommentStatement) {
return new CommentStatementContext(databaseType,
(CommentStatement) sqlStatement);
diff --git
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/PrepareStatementContext.java
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/PrepareStatementContext.java
deleted file mode 100644
index cef982d79c5..00000000000
---
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/PrepareStatementContext.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.infra.binder.context.statement.type.ddl;
-
-import lombok.Getter;
-import
org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
-import
org.apache.shardingsphere.infra.binder.context.statement.CommonSQLStatementContext;
-import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import
org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.PrepareStatement;
-
-import java.util.Collection;
-import java.util.LinkedList;
-
-/**
- * Prepare statement context.
- */
-@Getter
-public final class PrepareStatementContext extends CommonSQLStatementContext
implements TableAvailable {
-
- private final TablesContext tablesContext;
-
- public PrepareStatementContext(final DatabaseType databaseType, final
PrepareStatement sqlStatement) {
- super(databaseType, sqlStatement);
- tablesContext = new
TablesContext(extractTablesFromPreparedStatement(sqlStatement));
- }
-
- private Collection<SimpleTableSegment>
extractTablesFromPreparedStatement(final PrepareStatement sqlStatement) {
- TableExtractor tableExtractor = new TableExtractor();
-
sqlStatement.getSelect().ifPresent(tableExtractor::extractTablesFromSelect);
-
sqlStatement.getInsert().ifPresent(tableExtractor::extractTablesFromInsert);
-
sqlStatement.getUpdate().ifPresent(tableExtractor::extractTablesFromUpdate);
-
sqlStatement.getDelete().ifPresent(tableExtractor::extractTablesFromDelete);
- return new LinkedList<>(tableExtractor.getRewriteTables());
- }
-
- @Override
- public PrepareStatement getSqlStatement() {
- return (PrepareStatement) super.getSqlStatement();
- }
-}
diff --git
a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/PrepareStatementContextTest.java
b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/PrepareStatementContextTest.java
deleted file mode 100644
index eb23d5bf53f..00000000000
---
a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/type/ddl/PrepareStatementContextTest.java
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.infra.binder.context.statement.type.ddl;
-
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.SetAssignmentSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.TableSegmentBoundInfo;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.PrepareStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.UpdateStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
-import org.junit.jupiter.api.Test;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Optional;
-import java.util.stream.Collectors;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-class PrepareStatementContextTest {
-
- @Test
- void assertNewInstance() {
- PrepareStatement sqlStatement = mock(PrepareStatement.class);
- SimpleTableSegment table = new
SimpleTableSegment(createTableNameSegment());
-
when(sqlStatement.getSelect()).thenReturn(Optional.of(getSelect(table)));
-
when(sqlStatement.getInsert()).thenReturn(Optional.of(getInsert(table)));
-
when(sqlStatement.getUpdate()).thenReturn(Optional.of(getUpdate(table)));
-
when(sqlStatement.getDelete()).thenReturn(Optional.of(getDelete(table)));
- PrepareStatementContext actual = new PrepareStatementContext(mock(),
sqlStatement);
- assertThat(actual.getSqlStatement(), is(sqlStatement));
-
assertThat(actual.getTablesContext().getSimpleTables().stream().map(each ->
each.getTableName().getIdentifier().getValue()).collect(Collectors.toList()),
- is(Arrays.asList("foo_tbl", "foo_tbl", "foo_tbl", "foo_tbl")));
- }
-
- private static TableNameSegment createTableNameSegment() {
- TableNameSegment result = new TableNameSegment(0, 0, new
IdentifierValue("foo_tbl"));
- result.setTableBoundInfo(new TableSegmentBoundInfo(new
IdentifierValue("foo_db"), new IdentifierValue("foo_schema")));
- return result;
- }
-
- private SelectStatement getSelect(final SimpleTableSegment table) {
- SelectStatement result = new SelectStatement();
- result.setFrom(table);
- return result;
- }
-
- private InsertStatement getInsert(final SimpleTableSegment table) {
- InsertStatement result = new InsertStatement();
- result.setTable(table);
- return result;
- }
-
- private UpdateStatement getUpdate(final SimpleTableSegment table) {
- UpdateStatement result = new UpdateStatement();
- result.setTable(table);
- ColumnSegment column = new ColumnSegment(0, 0, new
IdentifierValue("foo_col"));
- SetAssignmentSegment setAssignmentSegment = new
SetAssignmentSegment(0, 0, Collections.singletonList(new
ColumnAssignmentSegment(0, 0, Collections.singletonList(column), column)));
- result.setSetAssignment(setAssignmentSegment);
- return result;
- }
-
- private DeleteStatement getDelete(final SimpleTableSegment table) {
- DeleteStatement result = new DeleteStatement();
- result.setTable(table);
- return result;
- }
-}
diff --git
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/PrepareStatement.java
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/PrepareStatement.java
index 5812a30f431..37e79486920 100644
---
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/PrepareStatement.java
+++
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/ddl/PrepareStatement.java
@@ -19,12 +19,16 @@ package
org.apache.shardingsphere.sql.parser.statement.core.statement.ddl;
import lombok.Getter;
import lombok.Setter;
+import
org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.UpdateStatement;
+import java.util.Collection;
+import java.util.LinkedList;
import java.util.Optional;
/**
@@ -77,4 +81,18 @@ public final class PrepareStatement extends
AbstractSQLStatement implements DDLS
public Optional<DeleteStatement> getDelete() {
return Optional.ofNullable(delete);
}
+
+ /**
+ * Get tables.
+ *
+ * @return tables
+ */
+ public Collection<SimpleTableSegment> getTables() {
+ TableExtractor tableExtractor = new TableExtractor();
+
Optional.ofNullable(select).ifPresent(tableExtractor::extractTablesFromSelect);
+
Optional.ofNullable(insert).ifPresent(tableExtractor::extractTablesFromInsert);
+
Optional.ofNullable(update).ifPresent(tableExtractor::extractTablesFromUpdate);
+
Optional.ofNullable(delete).ifPresent(tableExtractor::extractTablesFromDelete);
+ return new LinkedList<>(tableExtractor.getRewriteTables());
+ }
}