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.sqlfederation.optimizer.metadata.schema;
19  
20  import com.cedarsoftware.util.CaseInsensitiveMap;
21  import lombok.Getter;
22  import org.apache.calcite.adapter.java.JavaTypeFactory;
23  import org.apache.calcite.rel.type.RelDataType;
24  import org.apache.calcite.rel.type.RelDataTypeImpl;
25  import org.apache.calcite.schema.Table;
26  import org.apache.calcite.schema.impl.AbstractSchema;
27  import org.apache.calcite.schema.impl.ViewTable;
28  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
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.sqlfederation.optimizer.metadata.util.SQLFederationDataTypeUtils;
33  import org.apache.shardingsphere.sqlfederation.optimizer.statistic.SQLFederationStatistic;
34  
35  import java.util.Collections;
36  import java.util.Map;
37  
38  /**
39   * SQL federation schema.
40   */
41  @Getter
42  public final class SQLFederationSchema extends AbstractSchema {
43      
44      private final String name;
45      
46      private final Map<String, Table> tableMap;
47      
48      public SQLFederationSchema(final String schemaName, final ShardingSphereSchema schema, final DatabaseType protocolType, final JavaTypeFactory javaTypeFactory) {
49          name = schemaName;
50          tableMap = createTableMap(schema, protocolType, javaTypeFactory);
51      }
52      
53      private Map<String, Table> createTableMap(final ShardingSphereSchema schema, final DatabaseType protocolType, final JavaTypeFactory javaTypeFactory) {
54          Map<String, Table> result = new CaseInsensitiveMap<>(schema.getTables().size(), 1F);
55          for (ShardingSphereTable each : schema.getTables().values()) {
56              if (schema.containsView(each.getName())) {
57                  result.put(each.getName(), getViewTable(schema, each, protocolType, javaTypeFactory));
58              } else {
59                  // TODO implement table statistic logic after using custom operators
60                  result.put(each.getName(), new SQLFederationTable(each, new SQLFederationStatistic(), protocolType));
61              }
62          }
63          return result;
64      }
65      
66      private ViewTable getViewTable(final ShardingSphereSchema schema, final ShardingSphereTable table, final DatabaseType protocolType, final JavaTypeFactory javaTypeFactory) {
67          RelDataType relDataType = SQLFederationDataTypeUtils.createRelDataType(table, protocolType, javaTypeFactory);
68          ShardingSphereView view = schema.getView(table.getName());
69          return new ViewTable(javaTypeFactory.getJavaClass(relDataType), RelDataTypeImpl.proto(relDataType), view.getViewDefinition(), Collections.emptyList(), Collections.emptyList());
70      }
71  }