1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList |
4 | |
5 | Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.sql.dictionary; |
22 | |
23 | import org.apache.derby.iapi.error.StandardException; |
24 | import org.apache.derby.iapi.services.sanity.SanityManager; |
25 | |
26 | import org.apache.derby.catalog.UUID; |
27 | |
28 | import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor; |
29 | import org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor; |
30 | import org.apache.derby.iapi.sql.dictionary.DataDictionary; |
31 | import org.apache.derby.iapi.sql.dictionary.KeyConstraintDescriptor; |
32 | import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; |
33 | |
34 | import org.apache.derby.iapi.error.StandardException; |
35 | import org.apache.derby.iapi.services.sanity.SanityManager; |
36 | |
37 | import org.apache.derby.catalog.UUID; |
38 | |
39 | import java.util.ArrayList; |
40 | |
41 | public class ConstraintDescriptorList extends ArrayList |
42 | { |
43 | |
44 | private boolean scanned; |
45 | |
46 | /** |
47 | * Mark whether or not the underlying system table has |
48 | * been scanned. (If a table does not have any |
49 | * constraints then the size of its CDL will always |
50 | * be 0. We used these get/set methods to determine |
51 | * when we need to scan the table. |
52 | * |
53 | * @param scanned Whether or not the underlying system table has been scanned. |
54 | */ |
55 | public void setScanned(boolean scanned) |
56 | { |
57 | this.scanned = scanned; |
58 | } |
59 | |
60 | /** |
61 | * Return whether or not the underlying system table has been scanned. |
62 | * |
63 | * @return Where or not the underlying system table has been scanned. |
64 | */ |
65 | public boolean getScanned() |
66 | { |
67 | return scanned; |
68 | } |
69 | |
70 | /** |
71 | * Get the ConstraintDescriptor with the matching UUID String for the backing index. |
72 | * |
73 | * @param indexUUID The UUID for the backing index. |
74 | * |
75 | * @return The matching ConstraintDescriptor. |
76 | */ |
77 | public ConstraintDescriptor getConstraintDescriptor(UUID indexUUID) |
78 | { |
79 | ConstraintDescriptor retCD = null; |
80 | int size = size(); |
81 | |
82 | for (int index = 0; index < size; index++) |
83 | { |
84 | ConstraintDescriptor cd = elementAt(index); |
85 | |
86 | if (! (cd instanceof KeyConstraintDescriptor)) |
87 | { |
88 | continue; |
89 | } |
90 | |
91 | KeyConstraintDescriptor keyCD = (KeyConstraintDescriptor) cd; |
92 | |
93 | if (keyCD.getIndexId().equals(indexUUID)) |
94 | { |
95 | retCD = cd; |
96 | break; |
97 | } |
98 | } |
99 | return retCD; |
100 | } |
101 | |
102 | /** |
103 | * Get the ConstraintDescriptor with the matching constraint id. |
104 | * |
105 | * @param uuid The constraint id. |
106 | * |
107 | * @return The matching ConstraintDescriptor. |
108 | */ |
109 | public ConstraintDescriptor getConstraintDescriptorById(UUID uuid) |
110 | { |
111 | ConstraintDescriptor returnCD = null; |
112 | int size = size(); |
113 | |
114 | for (int index = 0; index < size; index++) |
115 | { |
116 | ConstraintDescriptor cd = elementAt(index); |
117 | |
118 | if (cd.getUUID().equals(uuid)) |
119 | { |
120 | returnCD = cd; |
121 | break; |
122 | } |
123 | } |
124 | return returnCD; |
125 | } |
126 | |
127 | /** |
128 | * Drop the constraint with the given UUID. |
129 | * |
130 | * @param uuid The constraint id. |
131 | * |
132 | * @return The matching ConstraintDescriptor. |
133 | */ |
134 | public ConstraintDescriptor dropConstraintDescriptorById(UUID uuid) |
135 | { |
136 | ConstraintDescriptor cd = null; |
137 | int size = size(); |
138 | |
139 | for (int index = 0; index < size; index++) |
140 | { |
141 | cd = elementAt(index); |
142 | |
143 | if (cd.getUUID().equals(uuid)) |
144 | { |
145 | remove( cd ); |
146 | break; |
147 | } |
148 | } |
149 | |
150 | return cd; |
151 | } |
152 | |
153 | |
154 | |
155 | /** |
156 | * Get the ConstraintDescriptor with the matching constraint name. |
157 | * |
158 | * @param sd The constraint schema descriptor. |
159 | * @param name The constraint name. |
160 | * |
161 | * @return The matching ConstraintDescriptor. |
162 | */ |
163 | public ConstraintDescriptor getConstraintDescriptorByName(SchemaDescriptor sd, |
164 | String name) |
165 | { |
166 | ConstraintDescriptor retCD = null; |
167 | int size = size(); |
168 | |
169 | for (int index = 0; index < size; index++) |
170 | { |
171 | ConstraintDescriptor cd = elementAt(index); |
172 | |
173 | if (cd.getConstraintName().equals(name)) |
174 | { |
175 | if ((sd == null) || |
176 | (sd.equals(cd.getSchemaDescriptor()))) |
177 | { |
178 | retCD = cd; |
179 | break; |
180 | } |
181 | } |
182 | } |
183 | return retCD; |
184 | } |
185 | |
186 | |
187 | /** |
188 | * Get the ConstraintDescriptor with the matching constraint name. |
189 | * |
190 | * @return The matching ConstraintDescriptor. |
191 | */ |
192 | public ReferencedKeyConstraintDescriptor getPrimaryKey() |
193 | { |
194 | int size = size(); |
195 | |
196 | for (int index = 0; index < size; index++) |
197 | { |
198 | ConstraintDescriptor cd = elementAt(index); |
199 | |
200 | if (cd.getConstraintType() == DataDictionary.PRIMARYKEY_CONSTRAINT) |
201 | { |
202 | return (ReferencedKeyConstraintDescriptor)cd; |
203 | } |
204 | } |
205 | return (ReferencedKeyConstraintDescriptor)null; |
206 | } |
207 | |
208 | /** |
209 | * Return a list of constraints where enabled is |
210 | * as passed in. |
211 | * |
212 | * @param enabled true or false |
213 | * |
214 | * @return a constraint descriptor list built from this. Always |
215 | * a new list even if all the elements in this were of the correct |
216 | * type (i.e. not optimized for the case where every element is |
217 | * desired). |
218 | */ |
219 | public ConstraintDescriptorList getConstraintDescriptorList(boolean enabled) |
220 | { |
221 | ConstraintDescriptorList cdl = new ConstraintDescriptorList(); |
222 | int size = size(); |
223 | |
224 | for (int index = 0; index < size; index++) |
225 | { |
226 | ConstraintDescriptor cd = elementAt(index); |
227 | |
228 | if (cd.isEnabled() == enabled) |
229 | { |
230 | cdl.add(cd); |
231 | } |
232 | } |
233 | return cdl; |
234 | } |
235 | |
236 | /** |
237 | * Return the nth (0-based) element in the list. |
238 | * |
239 | * @param n Which element to return. |
240 | * |
241 | * @return The nth element in the list. |
242 | */ |
243 | public ConstraintDescriptor elementAt(int n) |
244 | { |
245 | return (ConstraintDescriptor) get(n); |
246 | } |
247 | |
248 | /** |
249 | * Return a ConstraintDescriptorList containing the ConstraintDescriptors |
250 | * of the specified type that are in this list. |
251 | * |
252 | * @param type The constraint type. |
253 | * |
254 | * @return A ConstraintDescriptorList containing the ConstraintDescriptors |
255 | * of the specified type that are in this list. |
256 | */ |
257 | public ConstraintDescriptorList getSubList(int type) |
258 | { |
259 | ConstraintDescriptor cd = null; |
260 | ConstraintDescriptorList cdl = new ConstraintDescriptorList(); |
261 | int size = size(); |
262 | |
263 | for (int index = 0; index < size; index++) |
264 | { |
265 | cd = elementAt(index); |
266 | |
267 | if (cd.getConstraintType() == type) |
268 | { |
269 | cdl.add(cd); |
270 | } |
271 | } |
272 | return cdl; |
273 | } |
274 | } |