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.infra.connection.refresher.type.table;
19  
20  import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
21  import org.apache.shardingsphere.infra.connection.refresher.MetaDataRefresher;
22  import org.apache.shardingsphere.infra.connection.refresher.util.TableRefreshUtils;
23  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
24  import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
25  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
26  import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
27  import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilder;
28  import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial;
29  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
30  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
31  import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO;
32  import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute;
33  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.table.RenameTableDefinitionSegment;
34  import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.RenameTableStatement;
35  
36  import java.sql.SQLException;
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.LinkedList;
40  import java.util.Map;
41  import java.util.Optional;
42  
43  /**
44   * Schema refresher for rename table statement.
45   */
46  public final class RenameTableStatementSchemaRefresher implements MetaDataRefresher<RenameTableStatement> {
47      
48      @Override
49      public void refresh(final ModeContextManager modeContextManager, final ShardingSphereDatabase database, final Collection<String> logicDataSourceNames,
50                          final String schemaName, final DatabaseType databaseType, final RenameTableStatement sqlStatement, final ConfigurationProperties props) throws SQLException {
51          for (RenameTableDefinitionSegment each : sqlStatement.getRenameTables()) {
52              AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), schemaName, logicDataSourceNames);
53              alterSchemaMetaDataPOJO.getAlteredTables().add(getTable(database, logicDataSourceNames, schemaName,
54                      TableRefreshUtils.getTableName(databaseType, each.getRenameTable().getTableName().getIdentifier()), props));
55              alterSchemaMetaDataPOJO.getDroppedTables().add(each.getTable().getTableName().getIdentifier().getValue());
56              modeContextManager.alterSchemaMetaData(alterSchemaMetaDataPOJO);
57          }
58      }
59      
60      private ShardingSphereTable getTable(final ShardingSphereDatabase database, final Collection<String> logicDataSourceNames, final String schemaName, final String tableName,
61                                           final ConfigurationProperties props) throws SQLException {
62          RuleMetaData ruleMetaData = new RuleMetaData(new LinkedList<>(database.getRuleMetaData().getRules()));
63          if (TableRefreshUtils.isSingleTable(tableName, database) && !logicDataSourceNames.isEmpty()) {
64              ruleMetaData.getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, tableName));
65          }
66          GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
67                  database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName);
68          Map<String, ShardingSphereSchema> schemaMap = GenericSchemaBuilder.build(Collections.singletonList(tableName), material);
69          return Optional.ofNullable(schemaMap.get(schemaName)).map(optional -> optional.getTable(tableName))
70                  .orElseGet(() -> new ShardingSphereTable(tableName, Collections.emptyList(), Collections.emptyList(), Collections.emptyList()));
71      }
72      
73      @Override
74      public Class<RenameTableStatement> getType() {
75          return RenameTableStatement.class;
76      }
77  }