-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ShardingValidator preValidate logic to ShardingSupportedChecker (#…
…33832) * Move ShardingValidator preValidate logic to ShardingSupportedChecker * fix unit test * fix unit test * fix unit test
- Loading branch information
1 parent
e0a7bd1
commit 0825ea8
Showing
77 changed files
with
2,067 additions
and
1,242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...a/org/apache/shardingsphere/sharding/checker/sql/ShardingSupportedSQLCheckersBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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.sharding.checker.sql; | ||
|
||
import org.apache.shardingsphere.infra.checker.SupportedSQLChecker; | ||
import org.apache.shardingsphere.infra.checker.SupportedSQLCheckersBuilder; | ||
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingAlterIndexSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingAlterTableSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingAlterViewSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateFunctionSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateIndexSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateProcedureSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateTableSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateViewSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingDropIndexSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingDropTableSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingRenameTableSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingCopySupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingDeleteSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingInsertSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingLoadDataSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingLoadXmlSupportedChecker; | ||
import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingUpdateSupportedChecker; | ||
import org.apache.shardingsphere.sharding.constant.ShardingOrder; | ||
import org.apache.shardingsphere.sharding.rule.ShardingRule; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
/** | ||
* Sharding SQL supported checker factory. | ||
*/ | ||
public final class ShardingSupportedSQLCheckersBuilder implements SupportedSQLCheckersBuilder<ShardingRule> { | ||
|
||
@Override | ||
public Collection<SupportedSQLChecker<?, ShardingRule>> getSupportedSQLCheckers() { | ||
return Arrays.asList( | ||
new ShardingAlterIndexSupportedChecker(), | ||
new ShardingAlterTableSupportedChecker(), | ||
new ShardingAlterViewSupportedChecker(), | ||
new ShardingCreateFunctionSupportedChecker(), | ||
new ShardingCreateIndexSupportedChecker(), | ||
new ShardingCreateProcedureSupportedChecker(), | ||
new ShardingCreateTableSupportedChecker(), | ||
new ShardingCreateViewSupportedChecker(), | ||
new ShardingDropIndexSupportedChecker(), | ||
new ShardingDropTableSupportedChecker(), | ||
new ShardingRenameTableSupportedChecker(), | ||
new ShardingCopySupportedChecker(), | ||
new ShardingDeleteSupportedChecker(), | ||
new ShardingInsertSupportedChecker(), | ||
new ShardingLoadDataSupportedChecker(), | ||
new ShardingLoadXmlSupportedChecker(), | ||
new ShardingUpdateSupportedChecker()); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return ShardingOrder.ORDER; | ||
} | ||
|
||
@Override | ||
public Class<ShardingRule> getTypeClass() { | ||
return ShardingRule.class; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...org/apache/shardingsphere/sharding/checker/sql/common/ShardingSupportedCommonChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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.sharding.checker.sql.common; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; | ||
import org.apache.shardingsphere.infra.binder.context.type.TableAvailable; | ||
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; | ||
import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException; | ||
import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException; | ||
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; | ||
import org.apache.shardingsphere.sharding.exception.syntax.DMLWithMultipleShardingTablesException; | ||
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException; | ||
import org.apache.shardingsphere.sharding.rule.ShardingRule; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Sharding supported common checker. | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class ShardingSupportedCommonChecker { | ||
|
||
/** | ||
* Check sharding table. | ||
* | ||
* @param shardingRule sharding rule | ||
* @param operation operation | ||
* @param tables tables | ||
* @throws UnsupportedShardingOperationException unsupported sharding operation exception | ||
*/ | ||
public static void checkShardingTable(final ShardingRule shardingRule, final String operation, final Collection<SimpleTableSegment> tables) { | ||
for (SimpleTableSegment each : tables) { | ||
String tableName = each.getTableName().getIdentifier().getValue(); | ||
ShardingSpherePreconditions.checkState(!shardingRule.isShardingTable(tableName), () -> new UnsupportedShardingOperationException(operation, tableName)); | ||
} | ||
} | ||
|
||
/** | ||
* Check table exist. | ||
* | ||
* @param schema ShardingSphere schema | ||
* @param tables tables | ||
* @throws NoSuchTableException no such table exception | ||
*/ | ||
public static void checkTableExist(final ShardingSphereSchema schema, final Collection<SimpleTableSegment> tables) { | ||
for (SimpleTableSegment each : tables) { | ||
String tableName = each.getTableName().getIdentifier().getValue(); | ||
ShardingSpherePreconditions.checkState(schema.containsTable(tableName), () -> new NoSuchTableException(tableName)); | ||
} | ||
} | ||
|
||
/** | ||
* Check table not exist. | ||
* | ||
* @param schema ShardingSphere schema | ||
* @param tables tables | ||
* @throws TableExistsException table exists exception | ||
*/ | ||
public static void checkTableNotExist(final ShardingSphereSchema schema, final Collection<SimpleTableSegment> tables) { | ||
for (SimpleTableSegment each : tables) { | ||
String tableName = each.getTableName().getIdentifier().getValue(); | ||
ShardingSpherePreconditions.checkState(!schema.containsTable(tableName), () -> new TableExistsException(tableName)); | ||
} | ||
} | ||
|
||
/** | ||
* Check multiple table. | ||
* | ||
* @param shardingRule sharding rule | ||
* @param sqlStatementContext sqlStatementContext | ||
*/ | ||
public static void checkMultipleTable(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext) { | ||
Collection<String> tableNames = ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames(); | ||
boolean isAllShardingTables = shardingRule.isAllShardingTables(tableNames) && (1 == tableNames.size() || shardingRule.isAllBindingTables(tableNames)); | ||
boolean isAllSingleTables = !shardingRule.containsShardingTable(tableNames); | ||
ShardingSpherePreconditions.checkState(isAllShardingTables || isAllSingleTables, () -> new DMLWithMultipleShardingTablesException(tableNames)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...rg/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterTableSupportedChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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.sharding.checker.sql.ddl; | ||
|
||
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; | ||
import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterTableStatementContext; | ||
import org.apache.shardingsphere.infra.checker.SupportedSQLChecker; | ||
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; | ||
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; | ||
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; | ||
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException; | ||
import org.apache.shardingsphere.sharding.rule.ShardingRule; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Alter table supported checker for sharding. | ||
*/ | ||
public final class ShardingAlterTableSupportedChecker implements SupportedSQLChecker<AlterTableStatementContext, ShardingRule> { | ||
|
||
@Override | ||
public boolean isCheck(final SQLStatementContext sqlStatementContext) { | ||
return sqlStatementContext instanceof AlterTableStatementContext; | ||
} | ||
|
||
@Override | ||
public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final AlterTableStatementContext sqlStatementContext) { | ||
Collection<String> tableNames = sqlStatementContext.getTablesContext().getSimpleTables().stream() | ||
.map(each -> each.getTableName().getIdentifier().getValue()).collect(Collectors.toList()); | ||
Optional<SimpleTableSegment> renameTable = sqlStatementContext.getSqlStatement().getRenameTable(); | ||
ShardingSpherePreconditions.checkState(!renameTable.isPresent() || !rule.containsShardingTable(tableNames), | ||
() -> new UnsupportedShardingOperationException("ALTER TABLE ... RENAME TO ...", renameTable.map(optional -> optional.getTableName().getIdentifier().getValue()).orElse(""))); | ||
} | ||
} |
Oops, something went wrong.