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.distsql.handler.update;
19  
20  import lombok.Setter;
21  import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired;
22  import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.database.DatabaseRuleAlterExecutor;
23  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
24  import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
25  import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
26  import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
27  import org.apache.shardingsphere.sharding.distsql.handler.checker.ShardingTableRuleStatementChecker;
28  import org.apache.shardingsphere.sharding.distsql.handler.converter.ShardingTableRuleStatementConverter;
29  import org.apache.shardingsphere.sharding.distsql.statement.AlterShardingTableRuleStatement;
30  import org.apache.shardingsphere.sharding.rule.ShardingRule;
31  
32  import java.util.Collection;
33  import java.util.stream.Collectors;
34  
35  /**
36   * Alter sharding table rule executor.
37   */
38  @DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
39  @Setter
40  public final class AlterShardingTableRuleExecutor implements DatabaseRuleAlterExecutor<AlterShardingTableRuleStatement, ShardingRule, ShardingRuleConfiguration> {
41      
42      private ShardingSphereDatabase database;
43      
44      private ShardingRule rule;
45      
46      @Override
47      public void checkBeforeUpdate(final AlterShardingTableRuleStatement sqlStatement) {
48          ShardingTableRuleStatementChecker.checkAlteration(database, sqlStatement.getRules(), rule.getConfiguration());
49      }
50      
51      @Override
52      public ShardingRuleConfiguration buildToBeAlteredRuleConfiguration(final AlterShardingTableRuleStatement sqlStatement) {
53          return ShardingTableRuleStatementConverter.convert(sqlStatement.getRules());
54      }
55      
56      @Override
57      public ShardingRuleConfiguration buildToBeDroppedRuleConfiguration(final ShardingRuleConfiguration toBeAlteredRuleConfig) {
58          ShardingRuleConfiguration result = new ShardingRuleConfiguration();
59          Collection<String> toBeAlteredShardingTableNames = toBeAlteredRuleConfig.getTables().stream().map(ShardingTableRuleConfiguration::getLogicTable).collect(Collectors.toSet());
60          for (ShardingAutoTableRuleConfiguration each : rule.getConfiguration().getAutoTables()) {
61              if (toBeAlteredShardingTableNames.contains(each.getLogicTable())) {
62                  result.getAutoTables().add(each);
63              }
64          }
65          Collection<String> toBeAlteredAutoTableNames = toBeAlteredRuleConfig.getAutoTables().stream().map(ShardingAutoTableRuleConfiguration::getLogicTable).collect(Collectors.toSet());
66          for (ShardingTableRuleConfiguration each : rule.getConfiguration().getTables()) {
67              if (toBeAlteredAutoTableNames.contains(each.getLogicTable())) {
68                  result.getTables().add(each);
69              }
70          }
71          UnusedAlgorithmFinder.findUnusedShardingAlgorithm(rule.getConfiguration()).forEach(each -> result.getShardingAlgorithms().put(each, rule.getConfiguration().getShardingAlgorithms().get(each)));
72          UnusedAlgorithmFinder.findUnusedKeyGenerator(rule.getConfiguration()).forEach(each -> result.getKeyGenerators().put(each, rule.getConfiguration().getKeyGenerators().get(each)));
73          UnusedAlgorithmFinder.findUnusedAuditor(rule.getConfiguration()).forEach(each -> result.getAuditors().put(each, rule.getConfiguration().getAuditors().get(each)));
74          return result;
75      }
76      
77      @Override
78      public Class<ShardingRule> getRuleClass() {
79          return ShardingRule.class;
80      }
81      
82      @Override
83      public Class<AlterShardingTableRuleStatement> getType() {
84          return AlterShardingTableRuleStatement.class;
85      }
86  }