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.Getter;
21  
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Map.Entry;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  /**
30   * ShardingSphere schema.
31   */
32  @Getter
33  public final class ShardingSphereSchema {
34      
35      private final Map<String, ShardingSphereTable> tables;
36      
37      private final Map<String, ShardingSphereView> views;
38      
39      @SuppressWarnings("CollectionWithoutInitialCapacity")
40      public ShardingSphereSchema() {
41          tables = new ConcurrentHashMap<>();
42          views = new ConcurrentHashMap<>();
43      }
44      
45      public ShardingSphereSchema(final Map<String, ShardingSphereTable> tables, final Map<String, ShardingSphereView> views) {
46          this.tables = new ConcurrentHashMap<>(tables.size(), 1F);
47          this.views = new ConcurrentHashMap<>(views.size(), 1F);
48          tables.forEach((key, value) -> this.tables.put(key.toLowerCase(), value));
49          views.forEach((key, value) -> this.views.put(key.toLowerCase(), value));
50      }
51      
52      /**
53       * Get all table names.
54       *
55       * @return all table names
56       */
57      public Collection<String> getAllTableNames() {
58          return tables.keySet();
59      }
60      
61      /**
62       * Get all view names.
63       *
64       * @return all view names
65       */
66      public Collection<String> getAllViewNames() {
67          return views.keySet();
68      }
69      
70      /**
71       * Get table meta data via table name.
72       *
73       * @param tableName tableName table name
74       * @return table meta data
75       */
76      public ShardingSphereTable getTable(final String tableName) {
77          return tables.get(tableName.toLowerCase());
78      }
79      
80      /**
81       * Get view meta data via view name.
82       *
83       * @param viewName viewName view name
84       * @return view meta data
85       */
86      public ShardingSphereView getView(final String viewName) {
87          return views.get(viewName.toLowerCase());
88      }
89      
90      /**
91       * Add table.
92       *
93       * @param tableName table name
94       * @param table table
95       */
96      public void putTable(final String tableName, final ShardingSphereTable table) {
97          tables.put(tableName.toLowerCase(), table);
98      }
99      
100     /**
101      * Add view.
102      *
103      * @param viewName view name
104      * @param view view
105      */
106     public void putView(final String viewName, final ShardingSphereView view) {
107         views.put(viewName.toLowerCase(), view);
108     }
109     
110     /**
111      * Add tables.
112      *
113      * @param tables tables
114      */
115     public void putAll(final Map<String, ShardingSphereTable> tables) {
116         for (Entry<String, ShardingSphereTable> entry : tables.entrySet()) {
117             putTable(entry.getKey(), entry.getValue());
118         }
119     }
120     
121     /**
122      * Remove table meta data.
123      *
124      * @param tableName table name
125      */
126     public void removeTable(final String tableName) {
127         tables.remove(tableName.toLowerCase());
128     }
129     
130     /**
131      * Remove view meta data.
132      *
133      * @param viewName view name
134      */
135     public void removeView(final String viewName) {
136         views.remove(viewName.toLowerCase());
137     }
138     
139     /**
140      * Judge contains table from table meta data or not.
141      *
142      * @param tableName table name
143      * @return contains table from table meta data or not
144      */
145     public boolean containsTable(final String tableName) {
146         return tables.containsKey(tableName.toLowerCase());
147     }
148     
149     /**
150      * Judge whether contains column name.
151      *
152      * @param tableName table name
153      * @param columnName column name
154      * @return contains column name or not
155      */
156     public boolean containsColumn(final String tableName, final String columnName) {
157         return containsTable(tableName) && getTable(tableName).containsColumn(columnName);
158     }
159     
160     /**
161      * Judge whether contains index name.
162      *
163      * @param tableName table name
164      * @param indexName index name
165      * @return whether contains index name or not
166      */
167     public boolean containsIndex(final String tableName, final String indexName) {
168         return containsTable(tableName) && getTable(tableName).containsIndex(indexName);
169     }
170     
171     /**
172      * Judge contains view from table meta data or not.
173      *
174      * @param viewName view name
175      * @return contains view from table meta data or not
176      */
177     public boolean containsView(final String viewName) {
178         return views.containsKey(viewName.toLowerCase());
179     }
180     
181     /**
182      * Get all column names via table.
183      *
184      * @param tableName table name
185      * @return column names
186      */
187     public List<String> getAllColumnNames(final String tableName) {
188         return containsTable(tableName) ? getTable(tableName).getColumnNames() : Collections.emptyList();
189     }
190     
191     /**
192      * Get visible column names via table.
193      *
194      * @param tableName table name
195      * @return visible column names
196      */
197     public List<String> getVisibleColumnNames(final String tableName) {
198         return containsTable(tableName) ? getTable(tableName).getVisibleColumns() : Collections.emptyList();
199     }
200 }