KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.RISetChecker
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.execute.ExecRow;
28 import org.apache.derby.iapi.sql.execute.ExecIndexRow;
29 import org.apache.derby.iapi.store.access.TransactionController;
30
31 /**
32  * Checks a set or referential integrity constraints. Used
33  * to shield the caller from ReferencedKeyRIChecker and
34  * ForeignKeyRICheckers.
35  */

36 public class RISetChecker
37 {
38     private GenericRIChecker[] checkers;
39
40     /**
41      * @param tc the xact controller
42      * @param fkInfo the foreign key information
43      *
44      * @exception StandardException Thrown on failure
45      */

46     public RISetChecker(TransactionController tc, FKInfo fkInfo[])
47         throws StandardException
48     {
49         if (fkInfo == null)
50         {
51             return;
52         }
53
54         checkers = new GenericRIChecker[fkInfo.length];
55
56         for (int i = 0; i < fkInfo.length; i++)
57         {
58             checkers[i] = (fkInfo[i].type == FKInfo.FOREIGN_KEY) ?
59                 (GenericRIChecker)new ForeignKeyRIChecker(tc, fkInfo[i]) :
60                 (GenericRIChecker)new ReferencedKeyRIChecker(tc, fkInfo[i]);
61         }
62     }
63
64     /**
65      * Do any work needed to reopen our ri checkers
66      * for another round of checks. Must do a close()
67      * first.
68      *
69      * @exception StandardException on error
70      */

71     void reopen() throws StandardException
72     {
73         // currently a noop
74
}
75
76     /**
77      * Check that there are no referenced primary keys in
78      * the passed in row. So for each foreign key that
79      * references a primary key constraint, make sure
80      * that there is no row that matches the values in
81      * the passed in row.
82      *
83      * @param row the row to check
84      *
85      * @exception StandardException on unexpected error, or
86      * on a primary/unique key violation
87      */

88     public void doPKCheck(ExecRow row, boolean restrictCheckOnly) throws StandardException
89     {
90         if (checkers == null)
91             return;
92
93         for (int i = 0; i < checkers.length; i++)
94         {
95             if (checkers[i] instanceof ReferencedKeyRIChecker)
96             {
97                 checkers[i].doCheck(row,restrictCheckOnly);
98             }
99         }
100     }
101
102     /**
103      * Check that everything in the row is ok, i.e.
104      * that there are no foreign keys in the passed
105      * in row that have invalid values.
106      *
107      * @param row the row to check
108      *
109      * @exception StandardException on unexpected error, or
110      * on a primary/unique key violation
111      */

112     public void doFKCheck(ExecRow row) throws StandardException
113     {
114         if (checkers == null)
115             return;
116
117         for (int i = 0; i < checkers.length; i++)
118         {
119             if (checkers[i] instanceof ForeignKeyRIChecker)
120             {
121                 checkers[i].doCheck(row);
122             }
123         }
124     }
125
126     /**
127      * Execute the specific RI check on the passed in row.
128      *
129      * @param index index into fkInfo
130      * @param row the row to check
131      *
132      * @exception StandardException on unexpected error, or
133      * on a primary/unique key violation
134      */

135     public void doRICheck(int index, ExecRow row, boolean restrictCheckOnly) throws StandardException
136     {
137         if (SanityManager.DEBUG)
138         {
139             if (checkers == null)
140             {
141                 SanityManager.THROWASSERT("no checkers, how can i execute checker "+index);
142             }
143
144             if (index >= checkers.length)
145             {
146                 SanityManager.THROWASSERT("there are only "+
147                     checkers.length+" checkers, "+index+" is invalid");
148             }
149         }
150
151         checkers[index].doCheck(row, restrictCheckOnly);
152     }
153
154     /**
155      * clean up
156      *
157      * @exception StandardException on error
158      */

159     public void close() throws StandardException
160     {
161         if (checkers == null)
162             return;
163
164         for (int i = 0; i < checkers.length; i++)
165         {
166             checkers[i].close();
167         }
168     }
169 }
170
171
Popular Tags