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.distsql.handler.engine.update.rdl.rule.engine.database.type;
19  
20  import lombok.RequiredArgsConstructor;
21  import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.engine.database.DatabaseRuleOperator;
22  import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.database.DatabaseRuleDropExecutor;
23  import org.apache.shardingsphere.distsql.statement.rdl.rule.aware.StaticDataSourceContainedRuleAwareStatement;
24  import org.apache.shardingsphere.distsql.statement.rdl.rule.database.DatabaseRuleDefinitionStatement;
25  import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
26  import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
27  import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
28  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
29  import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
30  import org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute;
31  import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
32  import org.apache.shardingsphere.mode.tuple.annotation.RepositoryTupleEntity;
33  import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine;
34  import org.apache.shardingsphere.mode.manager.ContextManager;
35  
36  import java.util.Collection;
37  import java.util.Collections;
38  import java.util.Objects;
39  
40  /**
41   * Drop database rule operator.
42   */
43  @RequiredArgsConstructor
44  public final class DropDatabaseRuleOperator implements DatabaseRuleOperator {
45      
46      private final ContextManager contextManager;
47      
48      @SuppressWarnings("rawtypes")
49      private final DatabaseRuleDropExecutor executor;
50      
51      @Override
52      @SuppressWarnings("unchecked")
53      public Collection<MetaDataVersion> operate(final DatabaseRuleDefinitionStatement sqlStatement, final ShardingSphereDatabase database, final RuleConfiguration currentRuleConfig) {
54          if (!executor.hasAnyOneToBeDropped(sqlStatement)) {
55              return Collections.emptyList();
56          }
57          ModeContextManager modeContextManager = contextManager.getInstanceContext().getModeContextManager();
58          RuleConfiguration toBeDroppedRuleConfig = executor.buildToBeDroppedRuleConfiguration(sqlStatement);
59          if (sqlStatement instanceof StaticDataSourceContainedRuleAwareStatement) {
60              for (StaticDataSourceRuleAttribute each : database.getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class)) {
61                  ((StaticDataSourceContainedRuleAwareStatement) sqlStatement).getNames().forEach(each::cleanStorageNodeDataSource);
62              }
63              // TODO refactor to new metadata refresh way
64          }
65          modeContextManager.removeRuleConfigurationItem(database.getName(), toBeDroppedRuleConfig);
66          RuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
67          if (null != toBeAlteredRuleConfig && ((DatabaseRuleConfiguration) toBeAlteredRuleConfig).isEmpty()) {
68              YamlRuleConfiguration yamlRuleConfig = new YamlRuleConfigurationSwapperEngine().swapToYamlRuleConfiguration(currentRuleConfig);
69              modeContextManager.removeRuleConfiguration(database.getName(), Objects.requireNonNull(yamlRuleConfig.getClass().getAnnotation(RepositoryTupleEntity.class)).value());
70              return Collections.emptyList();
71          }
72          return modeContextManager.alterRuleConfiguration(database.getName(), toBeAlteredRuleConfig);
73      }
74  }