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.view;
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.model.ShardingSphereView;
32  import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO;
33  import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute;
34  import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateViewStatement;
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 create view statement.
45   */
46  public final class CreateViewStatementSchemaRefresher implements MetaDataRefresher<CreateViewStatement> {
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 CreateViewStatement sqlStatement, final ConfigurationProperties props) throws SQLException {
51          String viewName = TableRefreshUtils.getTableName(databaseType, sqlStatement.getView().getTableName().getIdentifier());
52          RuleMetaData ruleMetaData = new RuleMetaData(new LinkedList<>(database.getRuleMetaData().getRules()));
53          if (TableRefreshUtils.isSingleTable(viewName, database)) {
54              ruleMetaData.getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, viewName));
55          }
56          GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
57                  database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName);
58          Map<String, ShardingSphereSchema> schemaMap = GenericSchemaBuilder.build(Collections.singletonList(viewName), material);
59          Optional<ShardingSphereTable> actualTableMetaData = Optional.ofNullable(schemaMap.get(schemaName)).map(optional -> optional.getTable(viewName));
60          if (actualTableMetaData.isPresent()) {
61              AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), schemaName, logicDataSourceNames);
62              alterSchemaMetaDataPOJO.getAlteredTables().add(actualTableMetaData.get());
63              alterSchemaMetaDataPOJO.getAlteredViews().add(new ShardingSphereView(viewName, sqlStatement.getViewDefinition()));
64              modeContextManager.alterSchemaMetaData(alterSchemaMetaDataPOJO);
65          }
66      }
67      
68      @Override
69      public Class<CreateViewStatement> getType() {
70          return CreateViewStatement.class;
71      }
72  }