1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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 }