KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > ConstraintDefinitionNode


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.ConstraintDefinitionNode
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.impl.sql.compile;
23
24 import org.apache.derby.iapi.sql.StatementType;
25
26 import org.apache.derby.iapi.types.TypeId;
27 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
28 import org.apache.derby.iapi.error.StandardException;
29 import org.apache.derby.iapi.services.sanity.SanityManager;
30 import org.apache.derby.iapi.sql.depend.ProviderList;
31
32 import org.apache.derby.impl.sql.compile.ActivationClassBuilder;
33
34 import org.apache.derby.iapi.services.monitor.Monitor;
35
36 import org.apache.derby.iapi.util.JBitSet;
37 import org.apache.derby.iapi.util.ReuseFactory;
38
39 import org.apache.derby.catalog.UUID;
40 import org.apache.derby.iapi.services.uuid.UUIDFactory;
41 import org.apache.derby.iapi.reference.SQLState;
42 import java.util.Properties JavaDoc;
43
44 /**
45  * A ConstraintDefintionNode is a class for all nodes that can represent
46  * constraint definitions.
47  *
48  * @author Jerry Brenner
49  */

50
51 public class ConstraintDefinitionNode extends TableElementNode
52 {
53     
54     private TableName constraintName;
55     protected int constraintType;
56     protected Properties JavaDoc properties;
57     ProviderList apl;
58
59     UUIDFactory uuidFactory;
60
61     String JavaDoc backingIndexName;
62     UUID backingIndexUUID;
63     int[] checkColumnReferences;
64     ResultColumnList columnList;
65     String JavaDoc constraintText;
66     ValueNode checkCondition;
67     private int behavior;
68     private int verifyType = DataDictionary.DROP_CONSTRAINT; // By default do not check the constraint type
69

70     public void init(
71                     Object JavaDoc constraintName,
72                     Object JavaDoc constraintType,
73                     Object JavaDoc rcl,
74                     Object JavaDoc properties,
75                     Object JavaDoc checkCondition,
76                     Object JavaDoc constraintText,
77                     Object JavaDoc behavior)
78     {
79         this.constraintName = (TableName) constraintName;
80
81         /* We need to pass null as name to TableElementNode's constructor
82          * since constraintName may be null.
83          */

84         super.init(null);
85         if (this.constraintName != null)
86         {
87             this.name = this.constraintName.getTableName();
88         }
89         this.constraintType = ((Integer JavaDoc) constraintType).intValue();
90         this.properties = (Properties JavaDoc) properties;
91         this.columnList = (ResultColumnList) rcl;
92         this.checkCondition = (ValueNode) checkCondition;
93         this.constraintText = (String JavaDoc) constraintText;
94         this.behavior = ((Integer JavaDoc) behavior).intValue();
95     }
96
97     public void init(
98                     Object JavaDoc constraintName,
99                     Object JavaDoc constraintType,
100                     Object JavaDoc rcl,
101                     Object JavaDoc properties,
102                     Object JavaDoc checkCondition,
103                     Object JavaDoc constraintText)
104     {
105         init(
106                     constraintName,
107                     constraintType,
108                     rcl,
109                     properties,
110                     checkCondition,
111                     constraintText,
112                     ReuseFactory.getInteger(StatementType.DROP_DEFAULT)
113                     );
114     }
115
116     public void init(
117                     Object JavaDoc constraintName,
118                     Object JavaDoc constraintType,
119                     Object JavaDoc rcl,
120                     Object JavaDoc properties,
121                     Object JavaDoc checkCondition,
122                     Object JavaDoc constraintText,
123                     Object JavaDoc behavior,
124                     Object JavaDoc verifyType)
125     {
126         init( constraintName, constraintType, rcl, properties, checkCondition, constraintText, behavior);
127         this.verifyType = ((Integer JavaDoc) verifyType).intValue();
128     }
129     
130     /**
131      * Convert this object to a String. See comments in QueryTreeNode.java
132      * for how this should be done for tree printing.
133      *
134      * @return This object as a String
135      */

136
137     public String JavaDoc toString()
138     {
139         if (SanityManager.DEBUG)
140         {
141             return "constraintName: " +
142                 ( ( constraintName != null) ?
143                         constraintName.toString() : "null" ) + "\n" +
144                 "constraintType: " + constraintType + "\n" +
145                 "properties: " +
146                 ((properties != null) ? properties.toString() : "null") + "\n" +
147                 super.toString();
148         }
149         else
150         {
151             return "";
152         }
153     }
154
155     /**
156      * Bind this constraint definition.
157      *
158        @param ddlNode the create or alter table node
159      * @param dd the dd
160      *
161      * @exception StandardException on error
162      */

163     protected void bind(DDLStatementNode ddlNode, DataDictionary dd) throws StandardException
164     {
165         // we need to allow drops on constraints with different schemas
166
// to support removing constraints created pre 5.2.
167
if (constraintType == DataDictionary.DROP_CONSTRAINT)
168             return;
169
170         // ensure the schema of the constraint matches the schema of the table
171
if (constraintName != null) {
172
173             String JavaDoc constraintSchema = constraintName.getSchemaName();
174
175
176             if (constraintSchema != null) {
177
178
179
180                 TableName tableName = ddlNode.getObjectName();
181                 String JavaDoc tableSchema = tableName.getSchemaName();
182                 if (tableSchema == null) {
183                     tableSchema = getSchemaDescriptor((String JavaDoc) null).getSchemaName();
184                     tableName.setSchemaName(tableSchema);
185                 }
186                 if (!constraintSchema.equals(tableSchema)) {
187                     throw StandardException.newException(SQLState.LANG_CONSTRAINT_SCHEMA_MISMATCH,
188                                                 constraintName, tableName);
189
190                 }
191             }
192         }
193         else {
194             name = getBackingIndexName(dd);
195         }
196     }
197
198     /**
199       * Get the name of the constraint. If the user didn't provide one, we make one up. This allows Replication
200       * to agree with the core compiler on the names of constraints.
201       *
202       * @return constraint name
203       */

204     String JavaDoc getConstraintMoniker()
205     {
206         return name;
207     }
208
209     /**
210         To support dropping exisiting constraints that may have mismatched schema names
211         we need to support ALTER TABLE S1.T DROP CONSTRAINT S2.C.
212         If a constraint name was specified this returns it, otherwise it returns null.
213     */

214     String JavaDoc getDropSchemaName() {
215         if (constraintName != null)
216             return constraintName.getSchemaName();
217         return null;
218     }
219
220     /**
221       * Allocates a UUID if one doesn't already exist for the index backing this constraint. This allows Replication
222       * logic to agree with the core compiler on what the UUIDs of indices are.
223       *
224       * @return a UUID for the constraint. allocates one if this is the first time this method is called.
225       */

226     UUID getBackingIndexUUID()
227     {
228         if ( backingIndexUUID == null )
229         {
230             backingIndexUUID = getUUIDFactory().createUUID();
231         }
232
233         return backingIndexUUID;
234     }
235
236     /**
237       * Gets a unique name for the backing index for this constraint of the form SQLyymmddhhmmssxxn
238       * yy - year, mm - month, dd - day of month, hh - hour, mm - minute, ss - second,
239       * xx - the first 2 digits of millisec because we don't have enough space to keep the exact millisec value,
240       * n - number between 0-9
241       *
242       * @return name of backing index
243       */

244     String JavaDoc getBackingIndexName(DataDictionary dd)
245     {
246         if ( backingIndexName == null )
247             backingIndexName = dd.getSystemSQLName();
248
249         return backingIndexName;
250     }
251
252     /**
253      * Set the auxiliary provider list.
254      *
255      * @param apl The new auxiliary provider list.
256      */

257     void setAuxiliaryProviderList(ProviderList apl)
258     {
259         this.apl = apl;
260     }
261
262     /**
263      * Return the auxiliary provider list.
264      *
265      * @return The auxiliary provider list.
266      */

267     public ProviderList getAuxiliaryProviderList()
268     {
269         return apl;
270     }
271
272     /**
273      * Is this a primary key constraint.
274      *
275      * @return boolean Whether or not this is a primary key constraint
276      */

277     boolean hasPrimaryKeyConstraint()
278     {
279         return constraintType == DataDictionary.PRIMARYKEY_CONSTRAINT;
280     }
281
282     /**
283      * Is this a unique key constraint.
284      *
285      * @return boolean Whether or not this is a unique key constraint
286      */

287     boolean hasUniqueKeyConstraint()
288     {
289         return constraintType == DataDictionary.UNIQUE_CONSTRAINT;
290     }
291
292     /**
293      * Is this a foreign key constraint.
294      *
295      * @return boolean Whether or not this is a unique key constraint
296      */

297     boolean hasForeignKeyConstraint()
298     {
299         return constraintType == DataDictionary.FOREIGNKEY_CONSTRAINT;
300     }
301
302     /**
303      * Does this element have a check constraint.
304      *
305      * @return boolean Whether or not this element has a check constraint
306      */

307     boolean hasCheckConstraint()
308     {
309         return constraintType == DataDictionary.CHECK_CONSTRAINT;
310     }
311
312     /**
313      * Does this element have a constraint on it.
314      *
315      * @return boolean Whether or not this element has a constraint on it
316      */

317     boolean hasConstraint()
318     {
319         return true;
320     }
321
322     /**
323      * Is this a foreign key constraint.
324      *
325      * @return boolean Whether or not this is a unique key constraint
326      */

327     boolean requiresBackingIndex()
328     {
329         switch (constraintType)
330         {
331             case DataDictionary.FOREIGNKEY_CONSTRAINT:
332             case DataDictionary.PRIMARYKEY_CONSTRAINT:
333             case DataDictionary.UNIQUE_CONSTRAINT:
334                 return true;
335             default:
336                 return false;
337         }
338     }
339
340     /**
341      * Is this a foreign key constraint.
342      *
343      * @return boolean Whether or not this is a unique key constraint
344      */

345     boolean requiresUniqueIndex()
346     {
347         switch (constraintType)
348         {
349             case DataDictionary.PRIMARYKEY_CONSTRAINT:
350             case DataDictionary.UNIQUE_CONSTRAINT:
351                 return true;
352             default:
353                 return false;
354         }
355     }
356
357     /**
358      * Get the constraint type
359      *
360      * @return constraintType The constraint type.
361      */

362     int getConstraintType()
363     {
364         return constraintType;
365     }
366
367     /**
368      * Set the optional properties for the backing index to this constraint.
369      *
370      * @param properties The optional Properties for this constraint.
371      */

372     public void setProperties(Properties JavaDoc properties)
373     {
374         this.properties = properties;
375     }
376
377     /**
378      * Get the optional properties for the backing index to this constraint.
379      *
380      *
381      * @return The optional properties for the backing index to this constraint
382      */

383     public Properties JavaDoc getProperties()
384     {
385         return properties;
386     }
387
388
389     /**
390      * Is this constraint referenced.
391      *
392      * @return true/false
393      */

394     public boolean isReferenced()
395     {
396         return false;
397     }
398
399     /**
400      * Get the count of enabled fks
401      * that reference this constraint
402      *
403      * @return the number
404      */

405     public int getReferenceCount()
406     {
407         return 0;
408     }
409     /**
410      * Is this constraint enabled.
411      *
412      * @return true/false
413      */

414     public boolean isEnabled()
415     {
416         return true;
417     }
418
419     /**
420      * Get the column list from this node.
421      *
422      * @return ResultColumnList The column list from this table constraint.
423      */

424     public ResultColumnList getColumnList()
425     {
426         return columnList;
427     }
428
429     /**
430      * Set the column list for this node. This is useful for check constraints
431      * where the list of referenced columns is built at bind time.
432      *
433      * @param columnList The new columnList.
434      */

435     public void setColumnList(ResultColumnList columnList)
436     {
437         this.columnList = columnList;
438     }
439
440     /**
441      * Get the check condition from this table constraint.
442      *
443      * @return The check condition from this node.
444      */

445     public ValueNode getCheckCondition()
446     {
447         return checkCondition;
448     }
449
450     /**
451      * Set the check condition for this table constraint.
452      *
453      * @param checkCondition The check condition
454      */

455     public void setCheckCondition(ValueNode checkCondition)
456     {
457         this.checkCondition = checkCondition;
458     }
459
460     /**
461      * Get the text of the constraint. (Only meaningful for check constraints.)
462      *
463      * @return The constraint text.
464      */

465     public String JavaDoc getConstraintText()
466     {
467         return constraintText;
468     }
469
470     /**
471      * Get the array of 1-based column references for a check constraint.
472      *
473      * @return The array of 1-based column references for a check constraint.
474      */

475     public int[] getCheckColumnReferences()
476     {
477         return checkColumnReferences;
478     }
479
480     /**
481      * Set the array of 1-based column references for a check constraint.
482      *
483      * @param checkColumnReferences The array of 1-based column references
484      * for the check constraint.
485      */

486     public void setCheckColumnReferences(int[] checkColumnReferences)
487     {
488         this.checkColumnReferences = checkColumnReferences;
489     }
490
491     /**
492      * Return the behavior of this constriant (DropStatementNode.xxx)
493      *
494      * @return the behavior
495      */

496     int getDropBehavior()
497     {
498         return behavior;
499     }
500
501     /**
502      * @return the expected type of the constraint, DataDictionary.DROP_CONSTRAINT if the constraint is
503      * to be dropped without checking its type.
504      */

505     int getVerifyType()
506     {
507         return verifyType;
508     }
509
510     ///////////////////////////////////////////////////////////////////////////
511
//
512
// MINIONS
513
//
514
///////////////////////////////////////////////////////////////////////////
515
/**
516       * Get the UUID factory
517       *
518       * @return the UUID factory
519       *
520       */

521     private UUIDFactory getUUIDFactory()
522     {
523         if ( uuidFactory == null )
524         {
525             uuidFactory = Monitor.getMonitor().getUUIDFactory();
526         }
527         return uuidFactory;
528     }
529 }
530
Popular Tags