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.checker.DatabaseRuleConfigurationEmptyChecker;
27  import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
28  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
29  import org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute;
30  import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
31  import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
32  import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine;
33  import org.apache.shardingsphere.mode.manager.ContextManager;
34  import org.apache.shardingsphere.mode.node.rule.tuple.annotation.RuleNodeTupleEntity;
35  import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
36  
37  import java.util.Objects;
38  
39  /**
40   * Drop database rule operator.
41   */
42  @RequiredArgsConstructor
43  public final class DropDatabaseRuleOperator implements DatabaseRuleOperator {
44      
45      private final ContextManager contextManager;
46      
47      @SuppressWarnings("rawtypes")
48      private final DatabaseRuleDropExecutor executor;
49      
50      @Override
51      @SuppressWarnings("unchecked")
52      public void operate(final DatabaseRuleDefinitionStatement sqlStatement, final ShardingSphereDatabase database, final RuleConfiguration currentRuleConfig) {
53          if (!executor.hasAnyOneToBeDropped(sqlStatement)) {
54              return;
55          }
56          if (sqlStatement instanceof StaticDataSourceContainedRuleAwareStatement) {
57              for (StaticDataSourceRuleAttribute each : database.getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class)) {
58                  ((StaticDataSourceContainedRuleAwareStatement) sqlStatement).getNames().forEach(each::cleanStorageNodeDataSource);
59              }
60              // TODO refactor to new metadata refresh way
61          }
62          MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getModeFacade().getMetaDataManagerService();
63          RuleConfiguration toBeDroppedRuleItemConfig = executor.buildToBeDroppedRuleConfiguration(sqlStatement);
64          metaDataManagerPersistService.removeRuleConfigurationItem(database, toBeDroppedRuleItemConfig);
65          RuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
66          if (null != toBeAlteredRuleConfig
67                  && TypedSPILoader.getService(DatabaseRuleConfigurationEmptyChecker.class, toBeAlteredRuleConfig.getClass()).isEmpty((DatabaseRuleConfiguration) toBeAlteredRuleConfig)) {
68              YamlRuleConfiguration yamlRuleConfig = new YamlRuleConfigurationSwapperEngine().swapToYamlRuleConfiguration(currentRuleConfig);
69              metaDataManagerPersistService.removeRuleConfiguration(database, currentRuleConfig, Objects.requireNonNull(yamlRuleConfig.getClass().getAnnotation(RuleNodeTupleEntity.class)).value());
70          } else {
71              metaDataManagerPersistService.alterRuleConfiguration(database, toBeAlteredRuleConfig);
72          }
73      }
74  }