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.infra.exception.kernel.metadata.rule.MissingRequiredRuleException;
22  import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired;
23  import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.database.DatabaseRuleDropExecutor;
24  import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
25  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
26  import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
27  import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
28  import org.apache.shardingsphere.sharding.distsql.handler.enums.ShardingStrategyLevelType;
29  import org.apache.shardingsphere.sharding.distsql.statement.DropDefaultShardingStrategyStatement;
30  import org.apache.shardingsphere.sharding.rule.ShardingRule;
31  
32  import java.util.Optional;
33  
34  /**
35   * Drop default sharding strategy executor.
36   */
37  @DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
38  @Setter
39  public final class DropDefaultShardingStrategyExecutor implements DatabaseRuleDropExecutor<DropDefaultShardingStrategyStatement, ShardingRule, ShardingRuleConfiguration> {
40      
41      private ShardingSphereDatabase database;
42      
43      private ShardingRule rule;
44      
45      @Override
46      public void checkBeforeUpdate(final DropDefaultShardingStrategyStatement sqlStatement) {
47          if (!sqlStatement.isIfExists()) {
48              checkExist(sqlStatement);
49          }
50      }
51      
52      private void checkExist(final DropDefaultShardingStrategyStatement sqlStatement) {
53          Optional<ShardingStrategyConfiguration> strategyConfig = getStrategyConfiguration(sqlStatement.getDefaultType());
54          ShardingSpherePreconditions.checkState(strategyConfig.isPresent(), () -> new MissingRequiredRuleException(
55                  String.format("Default sharding %s strategy", sqlStatement.getDefaultType().toLowerCase()), database.getName()));
56      }
57      
58      private Optional<ShardingStrategyConfiguration> getStrategyConfiguration(final String type) {
59          ShardingStrategyConfiguration result = type.equalsIgnoreCase(ShardingStrategyLevelType.TABLE.name())
60                  ? rule.getConfiguration().getDefaultTableShardingStrategy()
61                  : rule.getConfiguration().getDefaultDatabaseShardingStrategy();
62          return Optional.ofNullable(result);
63      }
64      
65      @Override
66      public boolean hasAnyOneToBeDropped(final DropDefaultShardingStrategyStatement sqlStatement) {
67          return sqlStatement.getDefaultType().equalsIgnoreCase(ShardingStrategyLevelType.TABLE.name())
68                  ? null != rule.getConfiguration().getDefaultTableShardingStrategy()
69                  : null != rule.getConfiguration().getDefaultDatabaseShardingStrategy();
70      }
71      
72      @Override
73      public ShardingRuleConfiguration buildToBeDroppedRuleConfiguration(final DropDefaultShardingStrategyStatement sqlStatement) {
74          ShardingRuleConfiguration result = new ShardingRuleConfiguration();
75          if (sqlStatement.getDefaultType().equalsIgnoreCase(ShardingStrategyLevelType.TABLE.name())) {
76              result.setDefaultTableShardingStrategy(rule.getConfiguration().getDefaultTableShardingStrategy());
77              rule.getConfiguration().setDefaultTableShardingStrategy(null);
78          } else {
79              result.setDefaultDatabaseShardingStrategy(rule.getConfiguration().getDefaultDatabaseShardingStrategy());
80              rule.getConfiguration().setDefaultDatabaseShardingStrategy(null);
81          }
82          UnusedAlgorithmFinder.findUnusedShardingAlgorithm(rule.getConfiguration()).forEach(each -> result.getShardingAlgorithms().put(each, rule.getConfiguration().getShardingAlgorithms().get(each)));
83          return result;
84      }
85      
86      @Override
87      public Class<ShardingRule> getRuleClass() {
88          return ShardingRule.class;
89      }
90      
91      @Override
92      public Class<DropDefaultShardingStrategyStatement> getType() {
93          return DropDefaultShardingStrategyStatement.class;
94      }
95  }