diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index fb6024298eb20..ea7a34a560bcf 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -119,6 +119,7 @@ 1. SQL Binder: Support AnalyzeTable statement SQL bind - [#35954](https://github.com/apache/shardingsphere/pull/35954) 1. SQL Binder: Support Comment statement SQL bind - [#36012](https://github.com/apache/shardingsphere/pull/36012) 1. SQL Binder: Support Prepare statement SQL bind - [#36064](https://github.com/apache/shardingsphere/pull/36064) +1. SQL Binder: Support Flush statement SQL bind - [#36036](https://github.com/apache/shardingsphere/pull/36036) 1. SQL Binder: Support Revoke statement SQL bind - [#36124](https://github.com/apache/shardingsphere/pull/36124) 1. SQL Binder: Add alter table metadata check - [#35877](https://github.com/apache/shardingsphere/pull/35877) 1. SQL Router: Add check for select with union all routing to multi data sources - [#35037](https://github.com/apache/shardingsphere/pull/35037) diff --git a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/FlushStatementBinder.java b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/FlushStatementBinder.java new file mode 100644 index 0000000000000..089b88c7cbb41 --- /dev/null +++ b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/FlushStatementBinder.java @@ -0,0 +1,68 @@ +/* + * 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.engine.statement.dal; + +import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString; +import com.google.common.collect.LinkedHashMultimap; +import com.google.common.collect.Multimap; +import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.TableSegmentBinderContext; +import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.type.SimpleTableSegmentBinder; +import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinder; +import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext; +import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementCopyUtils; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; +import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.FlushStatement; + +import java.util.ArrayList; +import java.util.Collection; + +/** + * Flush statement binder. + */ +public final class FlushStatementBinder implements SQLStatementBinder { + + @Override + public FlushStatement bind(final FlushStatement sqlStatement, final SQLStatementBinderContext binderContext) { + Collection tables = sqlStatement.getTables(); + if (tables.isEmpty()) { + return sqlStatement; + } + Multimap tableBinderContexts = LinkedHashMultimap.create(); + Collection boundTables = new ArrayList<>(); + for (SimpleTableSegment each : tables) { + boundTables.add(SimpleTableSegmentBinder.bind(each, binderContext, tableBinderContexts)); + } + return copyFlushStatement(sqlStatement, boundTables); + } + + private FlushStatement copyFlushStatement(final FlushStatement sqlStatement, final Collection tables) { + if (tables.equals(sqlStatement.getTables())) { + return sqlStatement; + } + try { + FlushStatement result = sqlStatement.getClass() + .getDeclaredConstructor(DatabaseType.class, Collection.class, boolean.class) + .newInstance(sqlStatement.getDatabaseType(), tables, sqlStatement.isFlushTable()); + SQLStatementCopyUtils.copyAttributes(sqlStatement, result); + return result; + } catch (final ReflectiveOperationException ex) { + return sqlStatement; + } + } +} diff --git a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java index b01aa7c036db1..9569f2b313f3f 100644 --- a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java +++ b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java @@ -20,9 +20,11 @@ import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext; import org.apache.shardingsphere.infra.binder.engine.statement.dal.AnalyzeTableStatementBinder; import org.apache.shardingsphere.infra.binder.engine.statement.dal.ExplainStatementBinder; +import org.apache.shardingsphere.infra.binder.engine.statement.dal.FlushStatementBinder; import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.AnalyzeTableStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.DALStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.ExplainStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.FlushStatement; /** * DAL statement bind engine. @@ -43,6 +45,9 @@ public DALStatement bind(final DALStatement statement, final SQLStatementBinderC if (statement instanceof ExplainStatement) { return new ExplainStatementBinder().bind((ExplainStatement) statement, binderContext); } + if (statement instanceof FlushStatement) { + return new FlushStatementBinder().bind((FlushStatement) statement, binderContext); + } return statement; } } diff --git a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/type/dal/FlushStatement.java b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/type/dal/FlushStatement.java new file mode 100644 index 0000000000000..7dff922a2bb89 --- /dev/null +++ b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/type/dal/FlushStatement.java @@ -0,0 +1,47 @@ +/* + * 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.sql.parser.statement.core.statement.type.dal; + +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; + +import java.util.Collection; + +/** + * Flush statement. + */ +public abstract class FlushStatement extends DALStatement { + + public FlushStatement(final DatabaseType databaseType) { + super(databaseType); + } + + /** + * Get tables. + * + * @return tables + */ + public abstract Collection getTables(); + + /** + * Is flush table. + * + * @return true if flush table, false otherwise + */ + public abstract boolean isFlushTable(); +} diff --git a/parser/sql/statement/type/mysql/src/main/java/org/apache/shardingsphere/sql/parser/statement/mysql/dal/MySQLFlushStatement.java b/parser/sql/statement/type/mysql/src/main/java/org/apache/shardingsphere/sql/parser/statement/mysql/dal/MySQLFlushStatement.java index c56c484cac476..56a00cdfcbbaa 100644 --- a/parser/sql/statement/type/mysql/src/main/java/org/apache/shardingsphere/sql/parser/statement/mysql/dal/MySQLFlushStatement.java +++ b/parser/sql/statement/type/mysql/src/main/java/org/apache/shardingsphere/sql/parser/statement/mysql/dal/MySQLFlushStatement.java @@ -22,7 +22,7 @@ import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; import org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes; import org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.TableSQLStatementAttribute; -import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.DALStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.FlushStatement; import java.util.Collection; @@ -30,7 +30,7 @@ * Flush statement for MySQL. */ @Getter -public final class MySQLFlushStatement extends DALStatement { +public final class MySQLFlushStatement extends FlushStatement { private final Collection tables; diff --git a/test/it/binder/src/test/resources/cases/dal/flush.xml b/test/it/binder/src/test/resources/cases/dal/flush.xml new file mode 100644 index 0000000000000..a9a8fb224b5a6 --- /dev/null +++ b/test/it/binder/src/test/resources/cases/dal/flush.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + +
+
+ + + + + + + +
+ + + + + +
+
+ + + + + + + + +
+
+ + + + +
diff --git a/test/it/binder/src/test/resources/sqls/dal/flush.xml b/test/it/binder/src/test/resources/sqls/dal/flush.xml new file mode 100644 index 0000000000000..240df68dcef16 --- /dev/null +++ b/test/it/binder/src/test/resources/sqls/dal/flush.xml @@ -0,0 +1,26 @@ + + + + + + + + + + +