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.metadata.database.schema.builder;
19  
20  import lombok.Getter;
21  import lombok.RequiredArgsConstructor;
22  import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
23  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
24  import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
25  import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
26  
27  import javax.sql.DataSource;
28  import java.util.Collection;
29  import java.util.LinkedHashMap;
30  import java.util.Map;
31  import java.util.Map.Entry;
32  import java.util.stream.Collectors;
33  
34  /**
35   * ShardingSphere schema builder material.
36   */
37  @RequiredArgsConstructor
38  @Getter
39  public final class GenericSchemaBuilderMaterial {
40      
41      private final DatabaseType protocolType;
42      
43      private final Map<String, DatabaseType> storageTypes;
44      
45      private final Map<String, DataSource> dataSourceMap;
46      
47      private final Collection<ShardingSphereRule> rules;
48      
49      private final ConfigurationProperties props;
50      
51      private final String defaultSchemaName;
52      
53      public GenericSchemaBuilderMaterial(final DatabaseType protocolType, final Map<String, StorageUnit> storageUnits,
54                                          final Collection<ShardingSphereRule> rules, final ConfigurationProperties props, final String defaultSchemaName) {
55          this(protocolType, storageUnits.entrySet().stream()
56                  .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageType(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
57                  storageUnits.entrySet().stream()
58                          .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
59                  rules, props, defaultSchemaName);
60      }
61      
62      /**
63       * Judge whether same protocol and storage database types.
64       * 
65       * @return is same or not
66       */
67      public boolean isSameProtocolAndStorageTypes() {
68          return storageTypes.values().stream().allMatch(protocolType::equals);
69      }
70  }