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.model;
19  
20  import lombok.EqualsAndHashCode;
21  import lombok.Getter;
22  import lombok.ToString;
23  import org.apache.shardingsphere.infra.database.core.metadata.database.enums.TableType;
24  
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.LinkedHashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * ShardingSphere table.
34   */
35  @Getter
36  @EqualsAndHashCode
37  @ToString
38  public final class ShardingSphereTable {
39      
40      private final String name;
41      
42      private final Map<String, ShardingSphereColumn> columns;
43      
44      private final Map<String, ShardingSphereIndex> indexes;
45      
46      private final Map<String, ShardingSphereConstraint> constraints;
47      
48      private final List<String> columnNames = new ArrayList<>();
49      
50      private final List<String> visibleColumns = new ArrayList<>();
51      
52      private final List<String> primaryKeyColumns = new ArrayList<>();
53      
54      private final TableType type;
55      
56      public ShardingSphereTable() {
57          this("", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), TableType.TABLE);
58      }
59      
60      public ShardingSphereTable(final String name, final Collection<ShardingSphereColumn> columns,
61                                 final Collection<ShardingSphereIndex> indexes, final Collection<ShardingSphereConstraint> constraints) {
62          this.name = name;
63          this.columns = createColumns(columns);
64          this.indexes = createIndexes(indexes);
65          this.constraints = createConstraints(constraints);
66          this.type = TableType.TABLE;
67      }
68      
69      public ShardingSphereTable(final String name, final Collection<ShardingSphereColumn> columns,
70                                 final Collection<ShardingSphereIndex> indexes, final Collection<ShardingSphereConstraint> constraints, final TableType type) {
71          this.name = name;
72          this.columns = createColumns(columns);
73          this.indexes = createIndexes(indexes);
74          this.constraints = createConstraints(constraints);
75          this.type = type;
76      }
77      
78      private Map<String, ShardingSphereColumn> createColumns(final Collection<ShardingSphereColumn> columns) {
79          Map<String, ShardingSphereColumn> result = new LinkedHashMap<>(columns.size(), 1F);
80          for (ShardingSphereColumn each : columns) {
81              String lowerColumnName = each.getName().toLowerCase();
82              result.put(lowerColumnName, each);
83              columnNames.add(each.getName());
84              if (each.isPrimaryKey()) {
85                  primaryKeyColumns.add(lowerColumnName);
86              }
87              if (each.isVisible()) {
88                  visibleColumns.add(each.getName());
89              }
90          }
91          return result;
92      }
93      
94      private Map<String, ShardingSphereIndex> createIndexes(final Collection<ShardingSphereIndex> indexes) {
95          Map<String, ShardingSphereIndex> result = new LinkedHashMap<>(indexes.size(), 1F);
96          for (ShardingSphereIndex each : indexes) {
97              result.put(each.getName().toLowerCase(), each);
98          }
99          return result;
100     }
101     
102     private Map<String, ShardingSphereConstraint> createConstraints(final Collection<ShardingSphereConstraint> constraints) {
103         Map<String, ShardingSphereConstraint> result = new LinkedHashMap<>(constraints.size(), 1F);
104         for (ShardingSphereConstraint each : constraints) {
105             result.put(each.getName().toLowerCase(), each);
106         }
107         return result;
108     }
109     
110     /**
111      * Put column meta data.
112      *
113      * @param column column meta data
114      */
115     public void putColumn(final ShardingSphereColumn column) {
116         columns.put(column.getName().toLowerCase(), column);
117     }
118     
119     /**
120      * Get column meta data via column name.
121      *
122      * @param columnName column name
123      * @return column meta data
124      */
125     public ShardingSphereColumn getColumn(final String columnName) {
126         return columns.get(columnName.toLowerCase());
127     }
128     
129     /**
130      * Get column meta data collection.
131      *
132      * @return column meta data collection
133      */
134     public Collection<ShardingSphereColumn> getColumnValues() {
135         return columns.values();
136     }
137     
138     /**
139      * Judge whether contains column or not.
140      *
141      * @param columnName column name
142      * @return whether contains column or not
143      */
144     public boolean containsColumn(final String columnName) {
145         return null != columnName && columns.containsKey(columnName.toLowerCase());
146     }
147     
148     /**
149      * Put index meta data.
150      *
151      * @param index index meta data
152      */
153     public void putIndex(final ShardingSphereIndex index) {
154         indexes.put(index.getName().toLowerCase(), index);
155     }
156     
157     /**
158      * Remove index meta data via index name.
159      *
160      * @param indexName index name
161      */
162     public void removeIndex(final String indexName) {
163         indexes.remove(indexName.toLowerCase());
164     }
165     
166     /**
167      * Get index meta data via index name.
168      *
169      * @param indexName index name
170      * @return index meta data
171      */
172     public ShardingSphereIndex getIndex(final String indexName) {
173         return indexes.get(indexName.toLowerCase());
174     }
175     
176     /**
177      * Get index meta data collection.
178      *
179      * @return index meta data collection
180      */
181     public Collection<ShardingSphereIndex> getIndexValues() {
182         return indexes.values();
183     }
184     
185     /**
186      * Judge whether contains index or not.
187      *
188      * @param indexName index name
189      * @return whether contains index or not
190      */
191     public boolean containsIndex(final String indexName) {
192         return null != indexName && indexes.containsKey(indexName.toLowerCase());
193     }
194     
195     /**
196      * Get constraint meta data collection.
197      *
198      * @return constraint meta data collection
199      */
200     public Collection<ShardingSphereConstraint> getConstraintValues() {
201         return constraints.values();
202     }
203 }