1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.sharding.route.engine.validator.ddl;
19
20 import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
21 import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException;
22 import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
23 import org.apache.shardingsphere.infra.route.context.RouteContext;
24 import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
25 import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
26 import org.apache.shardingsphere.sharding.rule.ShardingRule;
27 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
28 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
29
30 import java.util.Collection;
31
32
33
34
35 public abstract class ShardingDDLStatementValidator implements ShardingStatementValidator {
36
37
38
39
40
41
42
43
44
45 protected void validateShardingTable(final ShardingRule shardingRule, final String operation, final Collection<SimpleTableSegment> tables) {
46 for (SimpleTableSegment each : tables) {
47 String tableName = each.getTableName().getIdentifier().getValue();
48 if (shardingRule.isShardingTable(tableName)) {
49 throw new UnsupportedShardingOperationException(operation, tableName);
50 }
51 }
52 }
53
54
55
56
57
58
59
60
61 protected void validateTableExist(final ShardingSphereSchema schema, final Collection<SimpleTableSegment> tables) {
62 for (SimpleTableSegment each : tables) {
63 String tableName = each.getTableName().getIdentifier().getValue();
64 if (!schema.containsTable(tableName)) {
65 throw new NoSuchTableException(tableName);
66 }
67 }
68 }
69
70
71
72
73
74
75
76
77 protected void validateTableNotExist(final ShardingSphereSchema schema, final Collection<SimpleTableSegment> tables) {
78 for (SimpleTableSegment each : tables) {
79 String tableName = each.getTableName().getIdentifier().getValue();
80 if (schema.containsTable(tableName)) {
81 throw new TableExistsException(tableName);
82 }
83 }
84 }
85
86
87
88
89
90
91
92
93
94 protected boolean isRouteUnitDataNodeDifferentSize(final ShardingRule shardingRule, final RouteContext routeContext, final String tableName) {
95 return shardingRule.isShardingTable(tableName) && shardingRule.getShardingTable(tableName).getActualDataNodes().size() != routeContext.getRouteUnits().size();
96 }
97
98
99
100
101
102
103
104
105 protected boolean isSchemaContainsIndex(final ShardingSphereSchema schema, final IndexSegment index) {
106 return schema.getAllTableNames().stream().anyMatch(each -> schema.getTable(each).containsIndex(index.getIndexName().getIdentifier().getValue()));
107 }
108 }