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.statement.ddl.AlterTableStatement;
34  
35  import java.sql.SQLException;
36  import java.util.Collection;
37  import java.util.Collections;
38  import java.util.LinkedList;
39  import java.util.Map;
40  import java.util.Optional;
41  
42  /**
43   * Schema refresher for alter table statement.
44   */
45  public final class AlterTableStatementSchemaRefresher implements MetaDataRefresher<AlterTableStatement> {
46      
47      @Override
48      public void refresh(final ModeContextManager modeContextManager, final ShardingSphereDatabase database, final Collection<String> logicDataSourceNames,
49                          final String schemaName, final DatabaseType databaseType, final AlterTableStatement sqlStatement, final ConfigurationProperties props) throws SQLException {
50          String tableName = TableRefreshUtils.getTableName(databaseType, sqlStatement.getTable().getTableName().getIdentifier());
51          AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), schemaName, logicDataSourceNames);
52          if (sqlStatement.getRenameTable().isPresent()) {
53              String renameTable = sqlStatement.getRenameTable().get().getTableName().getIdentifier().getValue();
54              alterSchemaMetaDataPOJO.getAlteredTables().add(getTable(database, logicDataSourceNames, schemaName, renameTable, props));
55              alterSchemaMetaDataPOJO.getDroppedTables().add(tableName);
56          } else {
57              alterSchemaMetaDataPOJO.getAlteredTables().add(getTable(database, logicDataSourceNames, schemaName, tableName, props));
58          }
59          modeContextManager.alterSchemaMetaData(alterSchemaMetaDataPOJO);
60      }
61      
62      private ShardingSphereTable getTable(final ShardingSphereDatabase database, final Collection<String> logicDataSourceNames, final String schemaName,
63                                           final String tableName, final ConfigurationProperties props) throws SQLException {
64          RuleMetaData ruleMetaData = new RuleMetaData(new LinkedList<>(database.getRuleMetaData().getRules()));
65          if (TableRefreshUtils.isSingleTable(tableName, database)) {
66              ruleMetaData.getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, tableName));
67          }
68          GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
69                  database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName);
70          Map<String, ShardingSphereSchema> schemaMap = GenericSchemaBuilder.build(Collections.singletonList(tableName), material);
71          return Optional.ofNullable(schemaMap.get(schemaName)).map(optional -> optional.getTable(tableName))
72                  .orElseGet(() -> new ShardingSphereTable(tableName, Collections.emptyList(), Collections.emptyList(), Collections.emptyList()));
73      }
74      
75      @Override
76      public Class<AlterTableStatement> getType() {
77          return AlterTableStatement.class;
78      }
79  }