KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor
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.catalog.UUID;
26
27 import org.apache.derby.iapi.reference.SQLState;
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29 import org.apache.derby.iapi.sql.StatementType;
30 import org.apache.derby.iapi.services.io.StoredFormatIds;
31 import org.apache.derby.iapi.error.StandardException;
32 import org.apache.derby.iapi.sql.depend.DependencyManager;
33 import org.apache.derby.iapi.sql.depend.Dependent;
34 import org.apache.derby.iapi.sql.depend.Dependency;
35 import org.apache.derby.iapi.sql.depend.Provider;
36 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
37 import org.apache.derby.iapi.services.sanity.SanityManager;
38
39 /**
40  * A foreign key.
41  *
42  * @author Jamie
43  */

44 public class ForeignKeyConstraintDescriptor extends KeyConstraintDescriptor
45 {
46     /**
47        interface to this descriptor
48        <ol>
49        <li>public ReferencedKeyConstraintDescriptor getReferencedConstraint()
50        throws StandardException;
51        <li>public UUID getReferencedConstraintId()
52        throws StandardException;
53        <li>public boolean isSelfReferencingFK()
54        throws StandardException;
55        <ol>
56     */

57
58     // Implementation
59
ReferencedKeyConstraintDescriptor referencedConstraintDescriptor;
60     UUID referencedConstraintId;
61     int raDeleteRule;
62     int raUpdateRule;
63     /**
64      * Constructor for a ForeignKeyConstraintDescriptor
65      *
66      * @param dataDictionary The data dictionary that this descriptor lives in
67      * @param table The descriptor of the table the constraint is on
68      * @param constraintName The name of the constraint.
69      * @param deferrable If the constraint can be deferred.
70      * @param initiallyDeferred If the constraint starts life deferred.
71      * @param fkColumns columns in the foreign key
72      * @param constraintId UUID of constraint
73      * @param indexId The UUID for the backing index
74      * @param schemaDesc The SchemaDescriptor for the constraint
75      * @param referencedConstraintDescriptor is referenced constraint descriptor
76      * @param isEnabled is the constraint enabled?
77      */

78     protected ForeignKeyConstraintDescriptor(
79             DataDictionary dataDictionary,
80             TableDescriptor table,
81             String JavaDoc constraintName,
82             boolean deferrable,
83             boolean initiallyDeferred,
84             int[] fkColumns,
85             UUID constraintId,
86             UUID indexId,
87             SchemaDescriptor schemaDesc,
88             ReferencedKeyConstraintDescriptor referencedConstraintDescriptor,
89             boolean isEnabled,
90             int raDeleteRule,
91             int raUpdateRule
92             )
93     {
94         super(dataDictionary, table, constraintName, deferrable,
95               initiallyDeferred, fkColumns,
96               constraintId, indexId, schemaDesc, isEnabled);
97
98         this.referencedConstraintDescriptor = referencedConstraintDescriptor;
99         this.raDeleteRule = raDeleteRule;
100         this.raUpdateRule = raUpdateRule;
101     }
102
103     /**
104      * Constructor for a ForeignKeyConstraintDescriptor
105      *
106      * @param dataDictionary The data dictionary that this descriptor lives in
107      * @param table The descriptor of the table the constraint is on
108      * @param constraintName The name of the constraint.
109      * @param deferrable If the constraint can be deferred.
110      * @param initiallyDeferred If the constraint starts life deferred.
111      * @param fkColumns columns in the foreign key
112      * @param constraintId UUID of constraint
113      * @param indexId The UUID for the backing index
114      * @param schemaDesc The SchemaDescriptor for the constraint
115      * @param referencedConstraintId is referenced constraint id
116      * @param isEnabled is the constraint enabled?
117      */

118     ForeignKeyConstraintDescriptor(
119             DataDictionary dataDictionary,
120             TableDescriptor table,
121             String JavaDoc constraintName,
122             boolean deferrable,
123             boolean initiallyDeferred,
124             int[] fkColumns,
125             UUID constraintId,
126             UUID indexId,
127             SchemaDescriptor schemaDesc,
128             UUID referencedConstraintId,
129             boolean isEnabled,
130             int raDeleteRule,
131             int raUpdateRule
132             )
133     {
134         super(dataDictionary, table, constraintName, deferrable,
135               initiallyDeferred, fkColumns,
136               constraintId, indexId, schemaDesc, isEnabled);
137         this.referencedConstraintId = referencedConstraintId;
138         this.raDeleteRule = raDeleteRule;
139         this.raUpdateRule = raUpdateRule;
140
141     }
142
143     /**
144      * Get the constraint that this FK references. Will
145      * return either a primary key or a unique key constriant.
146      *
147      * @return the constraint
148      *
149      * @exception StandardException on error
150      */

151     public ReferencedKeyConstraintDescriptor getReferencedConstraint()
152         throws StandardException
153     {
154         if (referencedConstraintDescriptor != null)
155         {
156             return referencedConstraintDescriptor;
157         }
158
159         if (referencedConstraintId == null)
160         {
161             getReferencedConstraintId();
162         }
163
164         TableDescriptor refTd = getDataDictionary().getConstraintTableDescriptor(referencedConstraintId);
165
166         if (SanityManager.DEBUG)
167         {
168             if (refTd == null)
169             {
170                 SanityManager.THROWASSERT("not able to find "+referencedConstraintId+
171                             " in SYS.SYSCONSTRAINTS");
172             }
173         }
174
175         ConstraintDescriptorList cdl = getDataDictionary().getConstraintDescriptors(refTd);
176         referencedConstraintDescriptor = (ReferencedKeyConstraintDescriptor)
177                                     cdl.getConstraintDescriptorById(referencedConstraintId);
178
179         if (SanityManager.DEBUG)
180         {
181             if (referencedConstraintDescriptor == null)
182             {
183                 SanityManager.THROWASSERT("not able to find "
184                     +referencedConstraintDescriptor+ " off of table descriptor "
185                     +refTd.getName());
186             }
187         }
188
189         return referencedConstraintDescriptor;
190     }
191
192     
193     /**
194      * Get the constraint id for the constraint that this FK references.
195      * Will return either a primary key or a unique key constriant.
196      *
197      * @return the constraint id
198      *
199      * @exception StandardException on error
200      */

201     public UUID getReferencedConstraintId() throws StandardException
202     {
203         if (referencedConstraintDescriptor != null)
204         {
205             return referencedConstraintDescriptor.getUUID();
206         }
207
208         SubKeyConstraintDescriptor subKey;
209         subKey = getDataDictionary().getSubKeyConstraint(constraintId,
210                                         DataDictionary.FOREIGNKEY_CONSTRAINT);
211         if (SanityManager.DEBUG)
212         {
213             if (subKey == null)
214             {
215                 SanityManager.THROWASSERT("not able to find "+constraintName+
216                             " in SYS.SYSFOREIGNKEYS");
217             }
218         }
219         referencedConstraintId = subKey.getKeyConstraintId();
220         return referencedConstraintId;
221     }
222
223     /**
224      * Gets an identifier telling what type of descriptor it is
225      * (UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK).
226      *
227      * @return An identifier telling what type of descriptor it is
228      * (UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK).
229      */

230     public int getConstraintType()
231     {
232         return DataDictionary.FOREIGNKEY_CONSTRAINT;
233     }
234
235     /**
236      * Does this constraint need to fire on this type of
237      * DML? True if insert or update and columns intersect
238      *
239      * @param stmtType the type of DML
240      * (StatementType.INSERT|StatementType.UPDATE|StatementType.DELETE)
241      * @param modifiedCols the columns modified, or null for all
242      *
243      * @return true/false
244      */

245     public boolean needsToFire(int stmtType, int[] modifiedCols)
246     {
247         /*
248         ** If we are disabled, we never fire
249         */

250         if (!isEnabled)
251         {
252             return false;
253         }
254
255         if (stmtType == StatementType.DELETE)
256         {
257             return false;
258         }
259         if (stmtType == StatementType.INSERT)
260         {
261             return true;
262         }
263
264         // if update, only relevant if columns intersect
265
return doColumnsIntersect(modifiedCols, getReferencedColumns());
266     }
267
268     /**
269      * Am I a self-referencing FK? True if my referenced
270      * constraint is on the same table as me.
271      *
272      * @return true/false
273      *
274      * @exception StandardException on error
275      */

276     public boolean isSelfReferencingFK()
277         throws StandardException
278     {
279         ReferencedKeyConstraintDescriptor refcd = getReferencedConstraint();
280         return (refcd.getTableId().equals(getTableId()));
281     }
282
283     /**
284      * Gets a referential action rule on a DELETE
285      * @return referential rule defined by the user during foreign key creattion
286      * for a delete (like CASCDE , RESTRICT ..etc)
287      */

288     public int getRaDeleteRule()
289     {
290         return raDeleteRule;
291     }
292     
293     
294     /**
295      * Gets a referential action rule on a UPDATE
296      * @return referential rule defined by the user during foreign key creattion
297      * for an UPDATE (like CASCDE , RESTRICT ..etc)
298      */

299     public int getRaUpdateRule()
300     {
301         return raUpdateRule;
302     }
303     
304 }
305
306
307
308
309
310
311
312
Popular Tags