KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > ReferencedKeyRIChecker


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.ReferencedKeyRIChecker
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.execute;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25 import org.apache.derby.iapi.error.StandardException;
26
27 import org.apache.derby.iapi.sql.StatementUtil;
28 import org.apache.derby.iapi.sql.execute.ExecRow;
29 import org.apache.derby.iapi.sql.execute.ExecIndexRow;
30 import org.apache.derby.iapi.reference.SQLState;
31 import org.apache.derby.iapi.store.access.ScanController;
32 import org.apache.derby.iapi.store.access.TransactionController;
33 import org.apache.derby.iapi.sql.StatementType;
34
35 /**
36  * A Referential Integrity checker for a change
37  * to a referenced key (primary or unique). Makes
38  * sure that all the referenced key row is not
39  * referenced by any of its foreign keys. see
40  * ForeignKeyRIChecker for the code that validates
41  * changes to foreign keys.
42  */

43 public class ReferencedKeyRIChecker extends GenericRIChecker
44 {
45     /**
46      * @param tc the xact controller
47      * @param fkinfo the foreign key information
48      *
49      * @exception StandardException Thrown on failure
50      */

51     ReferencedKeyRIChecker(TransactionController tc, FKInfo fkinfo)
52         throws StandardException
53     {
54         super(tc, fkinfo);
55
56         if (SanityManager.DEBUG)
57         {
58             if (fkInfo.type != FKInfo.REFERENCED_KEY)
59             {
60                 SanityManager.THROWASSERT("invalid type "+fkInfo.type+
61                     " for a ReferencedKeyRIChecker");
62             }
63         }
64     }
65
66     /**
67      * Check that the row either has a null column(s), or
68      * has no corresponding foreign keys.
69      * <p>
70      * If a foreign key is found, an exception is thrown.
71      * If not, the scan is closed.
72      *
73      * @param row the row to check
74      *
75      * @exception StandardException on unexpected error, or
76      * on a primary/unique key violation
77      */

78     void doCheck(ExecRow row, boolean restrictCheckOnly) throws StandardException
79     {
80         /*
81         ** If any of the columns are null, then the
82         ** check always succeeds.
83         */

84         if (isAnyFieldNull(row))
85         {
86             return;
87         }
88
89         /*
90         ** Otherwise, should be no rows found.
91         ** Check each conglomerate.
92         */

93         ScanController scan;
94
95         for (int i = 0; i < fkInfo.fkConglomNumbers.length; i++)
96         {
97             
98             if(restrictCheckOnly)
99             {
100                 if(fkInfo.raRules[i] != StatementType.RA_RESTRICT)
101                     continue;
102             }
103
104             scan = getScanController(fkInfo.fkConglomNumbers[i], fkScocis[i], fkDcocis[i], row);
105             if (scan.next())
106             {
107                 close();
108                 StandardException se = StandardException.newException(SQLState.LANG_FK_VIOLATION, fkInfo.fkConstraintNames[i],
109                                         fkInfo.tableName,
110                                         StatementUtil.typeName(fkInfo.stmtType),
111                                         RowUtil.toString(row, fkInfo.colArray));
112
113                 throw se;
114             }
115             /*
116             ** Move off of the current row to release any locks.
117             */

118             scan.next();
119         }
120     }
121 }
122
123
124
125
126
127
Popular Tags