KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > sql > dictionary > ConstraintDescriptorList


1 /*
2
3    Derby - Class org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.sql.dictionary;
23
24 import org.apache.derby.iapi.error.StandardException;
25 import org.apache.derby.iapi.services.sanity.SanityManager;
26
27 import org.apache.derby.catalog.UUID;
28
29 import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor;
30 import org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor;
31 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
32 import org.apache.derby.iapi.sql.dictionary.KeyConstraintDescriptor;
33 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
34
35 import org.apache.derby.iapi.error.StandardException;
36 import org.apache.derby.iapi.services.sanity.SanityManager;
37
38 import org.apache.derby.catalog.UUID;
39
40 import java.util.ArrayList JavaDoc;
41
42 public class ConstraintDescriptorList extends ArrayList JavaDoc
43 {
44
45     private boolean scanned;
46
47     /**
48      * Mark whether or not the underlying system table has
49      * been scanned. (If a table does not have any
50      * constraints then the size of its CDL will always
51      * be 0. We used these get/set methods to determine
52      * when we need to scan the table.
53      *
54      * @param scanned Whether or not the underlying system table has been scanned.
55      */

56     public void setScanned(boolean scanned)
57     {
58         this.scanned = scanned;
59     }
60
61     /**
62      * Return whether or not the underlying system table has been scanned.
63      *
64      * @return Where or not the underlying system table has been scanned.
65      */

66     public boolean getScanned()
67     {
68         return scanned;
69     }
70
71     /**
72      * Get the ConstraintDescriptor with the matching UUID String for the backing index.
73      *
74      * @param indexUUID The UUID for the backing index.
75      *
76      * @return The matching ConstraintDescriptor.
77      */

78     public ConstraintDescriptor getConstraintDescriptor(UUID indexUUID)
79     {
80         ConstraintDescriptor retCD = null;
81         int size = size();
82
83         for (int index = 0; index < size; index++)
84         {
85             ConstraintDescriptor cd = elementAt(index);
86
87             if (! (cd instanceof KeyConstraintDescriptor))
88             {
89                 continue;
90             }
91
92             KeyConstraintDescriptor keyCD = (KeyConstraintDescriptor) cd;
93
94             if (keyCD.getIndexId().equals(indexUUID))
95             {
96                 retCD = cd;
97                 break;
98             }
99         }
100         return retCD;
101     }
102
103     /**
104      * Get the ConstraintDescriptor with the matching constraint id.
105      *
106      * @param uuid The constraint id.
107      *
108      * @return The matching ConstraintDescriptor.
109      */

110     public ConstraintDescriptor getConstraintDescriptorById(UUID uuid)
111     {
112         ConstraintDescriptor returnCD = null;
113         int size = size();
114
115         for (int index = 0; index < size; index++)
116         {
117             ConstraintDescriptor cd = elementAt(index);
118
119             if (cd.getUUID().equals(uuid))
120             {
121                 returnCD = cd;
122                 break;
123             }
124         }
125         return returnCD;
126     }
127
128     /**
129       * Drop the constraint with the given UUID.
130       *
131       * @param uuid The constraint id.
132       *
133       * @return The matching ConstraintDescriptor.
134       */

135     public ConstraintDescriptor dropConstraintDescriptorById(UUID uuid)
136     {
137         ConstraintDescriptor cd = null;
138         int size = size();
139
140         for (int index = 0; index < size; index++)
141         {
142             cd = elementAt(index);
143
144             if (cd.getUUID().equals(uuid))
145             {
146                 remove( cd );
147                 break;
148             }
149         }
150
151         return cd;
152     }
153
154
155
156     /**
157      * Get the ConstraintDescriptor with the matching constraint name.
158      *
159      * @param sd The constraint schema descriptor.
160      * @param name The constraint name.
161      *
162      * @return The matching ConstraintDescriptor.
163      */

164     public ConstraintDescriptor getConstraintDescriptorByName(SchemaDescriptor sd,
165                                                                 String JavaDoc name)
166     {
167         ConstraintDescriptor retCD = null;
168         int size = size();
169
170         for (int index = 0; index < size; index++)
171         {
172             ConstraintDescriptor cd = elementAt(index);
173
174             if (cd.getConstraintName().equals(name))
175             {
176                 if ((sd == null) ||
177                     (sd.equals(cd.getSchemaDescriptor())))
178                 {
179                     retCD = cd;
180                     break;
181                 }
182             }
183         }
184         return retCD;
185     }
186
187
188     /**
189      * Get the ConstraintDescriptor with the matching constraint name.
190      *
191      * @return The matching ConstraintDescriptor.
192      */

193     public ReferencedKeyConstraintDescriptor getPrimaryKey()
194     {
195         int size = size();
196
197         for (int index = 0; index < size; index++)
198         {
199             ConstraintDescriptor cd = elementAt(index);
200
201             if (cd.getConstraintType() == DataDictionary.PRIMARYKEY_CONSTRAINT)
202             {
203                 return (ReferencedKeyConstraintDescriptor)cd;
204             }
205         }
206         return (ReferencedKeyConstraintDescriptor)null;
207     }
208
209     /**
210      * Return a list of constraints where enabled is
211      * as passed in.
212      *
213      * @param enabled true or false
214      *
215      * @return a constraint descriptor list built from this. Always
216      * a new list even if all the elements in this were of the correct
217      * type (i.e. not optimized for the case where every element is
218      * desired).
219      */

220     public ConstraintDescriptorList getConstraintDescriptorList(boolean enabled)
221     {
222         ConstraintDescriptorList cdl = new ConstraintDescriptorList();
223         int size = size();
224
225         for (int index = 0; index < size; index++)
226         {
227             ConstraintDescriptor cd = elementAt(index);
228
229             if (cd.isEnabled() == enabled)
230             {
231                 cdl.add(cd);
232             }
233         }
234         return cdl;
235     }
236
237     /**
238      * Return the nth (0-based) element in the list.
239      *
240      * @param n Which element to return.
241      *
242      * @return The nth element in the list.
243      */

244     public ConstraintDescriptor elementAt(int n)
245     {
246         return (ConstraintDescriptor) get(n);
247     }
248
249     /**
250      * Return a ConstraintDescriptorList containing the ConstraintDescriptors
251      * of the specified type that are in this list.
252      *
253      * @param type The constraint type.
254      *
255      * @return A ConstraintDescriptorList containing the ConstraintDescriptors
256      * of the specified type that are in this list.
257      */

258     public ConstraintDescriptorList getSubList(int type)
259     {
260         ConstraintDescriptor cd = null;
261         ConstraintDescriptorList cdl = new ConstraintDescriptorList();
262         int size = size();
263
264         for (int index = 0; index < size; index++)
265         {
266             cd = elementAt(index);
267
268             if (cd.getConstraintType() == type)
269             {
270                 cdl.add(cd);
271             }
272         }
273         return cdl;
274     }
275 }
276
Popular Tags