View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Sharding DDL statement validator.
34   */
35  public abstract class ShardingDDLStatementValidator implements ShardingStatementValidator {
36      
37      /**
38       * Validate sharding table.
39       *
40       * @param shardingRule sharding rule
41       * @param operation operation
42       * @param tables tables
43       * @throws UnsupportedShardingOperationException unsupported sharding operation exception
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       * Validate table exist.
56       *
57       * @param schema ShardingSphere schema
58       * @param tables tables
59       * @throws NoSuchTableException no such table exception
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       * Validate table not exist.
72       *
73       * @param schema ShardingSphere schema
74       * @param tables tables
75       * @throws TableExistsException table exists exception
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       * Judge whether route unit and data node are different size.
88       * 
89       * @param shardingRule sharding rule
90       * @param routeContext route context
91       * @param tableName table name
92       * @return route unit and data node are different size or not
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       * Judge whether schema contains index or not.
100      *
101      * @param schema ShardingSphere schema
102      * @param index index
103      * @return whether schema contains index or not
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 }