1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
112
113
114
115 public void putColumn(final ShardingSphereColumn column) {
116 columns.put(column.getName().toLowerCase(), column);
117 }
118
119
120
121
122
123
124
125 public ShardingSphereColumn getColumn(final String columnName) {
126 return columns.get(columnName.toLowerCase());
127 }
128
129
130
131
132
133
134 public Collection<ShardingSphereColumn> getColumnValues() {
135 return columns.values();
136 }
137
138
139
140
141
142
143
144 public boolean containsColumn(final String columnName) {
145 return null != columnName && columns.containsKey(columnName.toLowerCase());
146 }
147
148
149
150
151
152
153 public void putIndex(final ShardingSphereIndex index) {
154 indexes.put(index.getName().toLowerCase(), index);
155 }
156
157
158
159
160
161
162 public void removeIndex(final String indexName) {
163 indexes.remove(indexName.toLowerCase());
164 }
165
166
167
168
169
170
171
172 public ShardingSphereIndex getIndex(final String indexName) {
173 return indexes.get(indexName.toLowerCase());
174 }
175
176
177
178
179
180
181 public Collection<ShardingSphereIndex> getIndexValues() {
182 return indexes.values();
183 }
184
185
186
187
188
189
190
191 public boolean containsIndex(final String indexName) {
192 return null != indexName && indexes.containsKey(indexName.toLowerCase());
193 }
194
195
196
197
198
199
200 public Collection<ShardingSphereConstraint> getConstraintValues() {
201 return constraints.values();
202 }
203 }