KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.FKConstraintDefinitionNode
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.types.TypeId;
25 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
26 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
27 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
28 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor;
29 import org.apache.derby.iapi.sql.conn.Authorizer;
30 import org.apache.derby.iapi.error.StandardException;
31 import org.apache.derby.iapi.services.sanity.SanityManager;
32
33 import org.apache.derby.iapi.error.StandardException;
34 import org.apache.derby.iapi.reference.SQLState;
35
36 import org.apache.derby.impl.sql.compile.ActivationClassBuilder;
37 import org.apache.derby.impl.sql.execute.ConstraintInfo;
38
39 import org.apache.derby.iapi.util.JBitSet;
40 import org.apache.derby.iapi.util.ReuseFactory;
41 import org.apache.derby.iapi.sql.dictionary.DDUtils;
42
43 /**
44  * A FKConstraintDefintionNode represents table constraint definitions.
45  *
46  * @author jamie
47  */

48
49 public final class FKConstraintDefinitionNode extends ConstraintDefinitionNode
50 {
51     TableName refTableName;
52     ResultColumnList refRcl;
53     SchemaDescriptor refTableSd;
54     int refActionDeleteRule; // referential action on delete
55
int refActionUpdateRule; // referential action on update
56
public void init(
57                         Object JavaDoc constraintName,
58                         Object JavaDoc refTableName,
59                         Object JavaDoc fkRcl,
60                         Object JavaDoc refRcl,
61                         Object JavaDoc refActions)
62     {
63         super.init(
64                 constraintName,
65                 ReuseFactory.getInteger(DataDictionary.FOREIGNKEY_CONSTRAINT),
66                 fkRcl,
67                 null,
68                 null,
69                 null);
70         this.refRcl = (ResultColumnList) refRcl;
71         this.refTableName = (TableName) refTableName;
72
73         this.refActionDeleteRule = ((int[]) refActions)[0];
74         this.refActionUpdateRule = ((int[]) refActions)[1];
75     }
76
77     /**
78      * Bind this constraint definition. Figure out some
79      * information about the table we are binding against.
80      *
81      * @param dd DataDictionary
82      *
83      * @exception StandardException on error
84      */

85     protected void bind(DDLStatementNode ddlNode, DataDictionary dd) throws StandardException
86     {
87         super.bind(ddlNode, dd);
88
89         refTableSd = getSchemaDescriptor(refTableName.getSchemaName());
90
91         if (refTableSd.isSystemSchema())
92         {
93             throw StandardException.newException(SQLState.LANG_NO_FK_ON_SYSTEM_SCHEMA);
94         }
95
96         // check the referenced table, unless this is a self-referencing constraint
97
if (refTableName.equals(ddlNode.getObjectName()))
98             return;
99
100         // error when the referenced table does not exist
101
TableDescriptor td = getTableDescriptor(refTableName.getTableName(), refTableSd);
102         if (td == null)
103             throw StandardException.newException(SQLState.LANG_INVALID_FK_NO_REF_TAB,
104                                                 getConstraintMoniker(),
105                                                 refTableName.getTableName());
106
107         // Verify if REFERENCES_PRIV is granted to columns referenced
108
getCompilerContext().pushCurrentPrivType(getPrivType());
109
110         // Indicate that this statement has a dependency on the
111
// table which is referenced by this foreign key:
112
getCompilerContext().createDependency(td);
113
114         // If references clause doesn't have columnlist, get primary key info
115
if (refRcl.size()==0 && (td.getPrimaryKey() != null))
116         {
117             // Get the primary key columns
118
int[] refCols = td.getPrimaryKey().getReferencedColumns();
119             for (int i=0; i<refCols.length; i++)
120             {
121                 ColumnDescriptor cd = td.getColumnDescriptor(refCols[i]);
122                 // Set tableDescriptor for this column descriptor. Needed for adding required table
123
// access permission. Column descriptors may not have this set already.
124
cd.setTableDescriptor(td);
125                 if (isPrivilegeCollectionRequired())
126                     getCompilerContext().addRequiredColumnPriv(cd);
127             }
128
129         }
130         else
131         {
132             for (int i=0; i<refRcl.size(); i++)
133             {
134                 ResultColumn rc = (ResultColumn) refRcl.elementAt(i);
135                 ColumnDescriptor cd = td.getColumnDescriptor(rc.getName());
136                 if (cd != null)
137                 {
138                     // Set tableDescriptor for this column descriptor. Needed for adding required table
139
// access permission. Column descriptors may not have this set already.
140
cd.setTableDescriptor(td);
141                     if (isPrivilegeCollectionRequired())
142                         getCompilerContext().addRequiredColumnPriv(cd);
143                 }
144             }
145         }
146         getCompilerContext().popCurrentPrivType();
147     }
148
149     public ConstraintInfo getReferencedConstraintInfo()
150     {
151         if (SanityManager.DEBUG)
152         {
153             SanityManager.ASSERT(refTableSd != null,
154                     "You must call bind() before calling getConstraintInfo");
155         }
156         return new ConstraintInfo(refTableName.getTableName(), refTableSd,
157                                   refRcl.getColumnNames(), refActionDeleteRule,
158                                   refActionUpdateRule);
159     }
160
161     public TableName getRefTableName() { return refTableName; }
162
163     int getPrivType()
164     {
165         return Authorizer.REFERENCES_PRIV;
166     }
167 }
168
Popular Tags