KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > serversystem > dmlvalidation > constraintsystem > CheckConstraintsVerifier


1 package com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.
2     constraintsystem;
3
4 import java.util.*;
5
6 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
7 import com.daffodilwoods.daffodildb.server.datasystem.utility.*;
8 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record;
9 import com.daffodilwoods.daffodildb.server.serversystem.*;
10 import com.daffodilwoods.daffodildb.server.sessionsystem.*;
11 import com.daffodilwoods.daffodildb.server.sql99.common.*;
12 import com.daffodilwoods.daffodildb.server.sql99.expression.
13     booleanvalueexpression.*;
14 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
15 import com.daffodilwoods.database.general.*;
16 import com.daffodilwoods.database.resource.*;
17 import com.daffodilwoods.database.utility.*;
18
19 /**
20  *
21  * <p>Title: CheckConstraintsVerifier</p>
22  * <p>Description: Objective of the class is to verify all the check constraints
23  * that have been applied on the table for which this instance of check
24  * constraint verifier has been made.
25  */

26 public class CheckConstraintsVerifier {
27   _CheckConstraintCharacteristics checkConstraintCharacteristics;
28   QualifiedIdentifier tableName;
29   boolean deferred;
30   HashMap constraintPool;
31   boolean checkedCheck = false;
32   TableDetails tableDetails;
33   ColumnDetails[] columnDetails;
34
35   /**
36    * Concstructor : constructs a new instance of check constraints verifier
37    * with the given arguments
38    * @param checkCharacteristics instance of _checkconstraintscharacteristics
39    * used to get the check constraints that have been applied on the table.
40    * @param tName name of the table
41    * @param deff boolean to determine whether the constraints are deferred or not.
42    * @throws DException
43    */

44
45   public CheckConstraintsVerifier(_CheckConstraintCharacteristics
46                                   checkCharacteristics,
47                                   QualifiedIdentifier tName, boolean deff) throws
48       DException {
49     checkConstraintCharacteristics = checkCharacteristics;
50     tableName = tName;
51     deferred = deff;
52     constraintPool = new HashMap(3);
53     tableDetails = new TableDetails();
54     tableDetails.setTableName(new String JavaDoc[] {tableName.catalog, tableName.schema,
55                               tableName.name});
56   }
57
58   /**
59    * Verifies the variable values for the Constraints applied on columns indexes.
60    * if the condition evaluates to 0 that means values satisfies the condition specified.
61    * Fires specified immediate or deferred Check constraint for update
62    * operation corresponding.
63    * If the constraint to be fired is immediate, fires immediate
64    * constraint or the deferred constraint which is switched to
65    * immediate constraint.
66    * If the constraints are
67    * @param columns columns for which constraints are to be verified.
68    * @param sec instance of statementexecution context
69    * @param globalSession instance of server session
70    * @throws DException
71    */

72
73   public void verifyCheckConstraintsForUpdate(int[] columns,
74                                               _StatementExecutionContext sec,
75                                               _ServerSession globalSession) throws
76       DException {
77     if (constraintPool.size() == 0 && checkedCheck) {
78       return;
79
80     }
81     else if (constraintPool.size() == 0 && !checkedCheck){
82       _CheckConstraint[] checkConstraints = null;
83       synchronized (this) {
84         if (!checkedCheck) {
85           checkConstraints = checkConstraintCharacteristics.getCheckConstraints();
86         }
87         if (checkConstraints == null) {
88           return;
89         }
90         if(!checkedCheck){
91           tableDetails.cc = globalSession.getColumnCharacteristics(tableDetails.
92               getQualifiedIdentifier());
93           generateIterators(checkConstraints, globalSession);
94           checkedCheck = true;
95         }
96       }
97
98     }
99
100     _ServerSession checkConstraintServerSession = new
101         CheckConstraintServerSession(sec.getServerSession(), globalSession);
102     if (!deferred) {
103       ConstraintInformation constraintInformation = getConstraintInformation(
104           sec.getUserSession());
105       ArrayList immToDeff = constraintInformation.
106           getImmediateToDefferedConstraints(tableName);
107       if (sec.getOperationTypeSameOrOpposite()) {
108         if (immToDeff == null) {
109           fireAllConstraints(sec.getRecordVersion(), columns,
110                              checkConstraintServerSession);
111         }
112         else {
113           ArrayList constraintsToFire = getRequiredConstraintsToFire(immToDeff);
114           for (int i = 0; i < constraintsToFire.size(); i++) {
115             fireThisConstraint( (ConstraintStore) constraintsToFire.get(i),
116                                sec.getRecordVersion(), columns,
117                                checkConstraintServerSession);
118           }
119         }
120       }
121       else {
122         if (immToDeff != null) {
123           String JavaDoc Cname = null;
124           for (int j = 0; j < immToDeff.size(); j++) {
125             Cname = (String JavaDoc) immToDeff.get(j);
126             if (constraintPool.containsKey(Cname)) {
127               fireThisConstraint( (ConstraintStore) constraintPool.get(Cname),
128                                  sec.getRecordVersion(), columns,
129                                  checkConstraintServerSession);
130             }
131           }
132         }
133       }
134       return;
135     }
136     else {
137       ConstraintInformation constraintInformation = getConstraintInformation(
138           sec.getUserSession());
139       ArrayList deffToImm = constraintInformation.
140           getDefferedToImmediateConstraints(tableName);
141
142       ArrayList constraintsToFire = null;
143       if (sec.getOperationTypeSameOrOpposite()) {
144         if (deffToImm == null) {
145           fireAllConstraints(sec.getRecordVersion(), columns,
146                              checkConstraintServerSession);
147         }
148         else {
149           constraintsToFire = getRequiredConstraintsToFire(deffToImm);
150           for (int i = 0; i < constraintsToFire.size(); i++) {
151             fireThisConstraint( (ConstraintStore) constraintsToFire.get(i),
152                                sec.getRecordVersion(), columns,
153                                checkConstraintServerSession);
154           }
155         }
156       }
157       else {
158         if (deffToImm != null) {
159           String JavaDoc Cname = null;
160           for (int j = 0; j < deffToImm.size(); j++) {
161             Cname = (String JavaDoc) deffToImm.get(j);
162             if (constraintPool.containsKey(Cname)) {
163               fireThisConstraint( (ConstraintStore) constraintPool.get(Cname),
164                                  sec.getRecordVersion(), columns,
165                                  checkConstraintServerSession);
166             }
167           }
168         }
169       }
170     }
171   }
172
173   /**
174    * This method is used to check constraints when insert operation is performed
175    * on the table.
176    * @param statement execution context
177    * @param glonal session
178    * throws DException
179    */

180
181   public void verifyCheckConstraints(_StatementExecutionContext sec,
182                                      _ServerSession globalSession) throws
183       DException {
184     if (constraintPool.size() == 0 && checkedCheck) {
185       return;
186
187     }
188     else if (constraintPool.size() == 0 && !checkedCheck) {
189       _CheckConstraint[] checkConstraints = null;
190       synchronized (this) {
191         if (!checkedCheck) {
192           checkConstraints = checkConstraintCharacteristics.getCheckConstraints();
193
194
195         }
196         if (checkConstraints == null) {
197           return;
198         }
199         if(!checkedCheck){
200         tableDetails.cc = globalSession.getColumnCharacteristics(tableDetails.
201             getQualifiedIdentifier());
202         try {
203           generateIterators(checkConstraints, globalSession);
204         }
205         catch (DException e) {
206           constraintPool.clear();
207           checkedCheck = false;
208           throw e;
209         }
210         catch (NullPointerException JavaDoc e) {
211           throw e;
212
213         }
214         checkedCheck = true;
215         }
216       }
217     }
218
219     _ServerSession checkConstraintServerSession = new
220         CheckConstraintServerSession(sec.getServerSession(), globalSession);
221     if (!deferred) { // Immediate constraints to be fired
222
ConstraintInformation constraintInformation = getConstraintInformation(
223           sec.getUserSession());
224       ArrayList immToDeff = constraintInformation.
225           getImmediateToDefferedConstraints(tableName);
226       if (sec.getOperationTypeSameOrOpposite()) {
227         if (immToDeff == null) {
228           fireAllConstraints(sec.getRecordVersion(),
229                              checkConstraintServerSession);
230         }
231         else {
232           ArrayList constraintsToFire = getImmediateConstraintsToFire(immToDeff);
233           for (int i = 0; i < constraintsToFire.size(); i++) {
234             fireThisConstraint( (ConstraintStore) constraintsToFire.get(i),
235                                sec.getRecordVersion(),
236                                checkConstraintServerSession);
237           }
238         }
239       }
240       else {
241         if (immToDeff != null) {
242           String JavaDoc Cname = null;
243           for (int j = 0; j < immToDeff.size(); j++) {
244             Cname = (String JavaDoc) immToDeff.get(j);
245             if (constraintPool.containsKey(Cname)) {
246               fireThisConstraint( (ConstraintStore) constraintPool.get(Cname),
247                                  sec.getRecordVersion(),
248                                  checkConstraintServerSession);
249             }
250           }
251         }
252       }
253       return;
254     }
255     else {
256       ConstraintInformation constraintInformation = getConstraintInformation(
257           sec.getUserSession());
258       ArrayList deffToImm = constraintInformation.
259           getDefferedToImmediateConstraints(tableName);
260
261       ArrayList constraintsToFire = null;
262       if (sec.getOperationTypeSameOrOpposite()) {
263         if (deffToImm == null) {
264           fireAllConstraints(sec.getRecordVersion(),
265                              checkConstraintServerSession);
266         }
267         else {
268           constraintsToFire = getRequiredConstraintsToFire(deffToImm);
269           for (int i = 0; i < constraintsToFire.size(); i++) {
270             fireThisConstraint( (ConstraintStore) constraintsToFire.get(i),
271                                sec.getRecordVersion(),
272                                checkConstraintServerSession);
273           }
274         }
275       }
276       else {
277         if (deffToImm != null) {
278           String JavaDoc Cname = null;
279           for (int j = 0; j < deffToImm.size(); j++) {
280             Cname = (String JavaDoc) deffToImm.get(j);
281             if (constraintPool.containsKey(Cname)) {
282               fireThisConstraint( (ConstraintStore) constraintPool.get(Cname),
283                                  sec.getRecordVersion(),
284                                  checkConstraintServerSession);
285             }
286           }
287         }
288       }
289     }
290   }
291
292   /**
293    * This method determines deferred constraints except the constraints which are
294    * switched to immediate constraints since they are fired previously.
295    * @param sessionImmediateConstraints
296    * @return
297    */

298   private ArrayList getRequiredConstraintsToFire(ArrayList
299                                                  sessionImmediateConstraints) {
300     ArrayList toRet = new ArrayList();
301     Iterator iterator = constraintPool.keySet().iterator();
302     String JavaDoc Cname = null;
303     do {
304       Cname = (String JavaDoc) iterator.next();
305       if (!sessionImmediateConstraints.contains(Cname)) {
306         toRet.add(constraintPool.get(Cname));
307       }
308     }
309     while (iterator.hasNext());
310     return toRet;
311   }
312
313   /**
314    * This method determines the immedaite constraints to fire. This method
315    * takes off the constraints which were immediate initially and were changed
316    * into the deferred constraints.
317    * @param list of immediate constraints switched mode to deferred.
318    * returns list of immeditate constraints
319    * throws DException
320    */

321   private ArrayList getImmediateConstraintsToFire(ArrayList immToDeff) throws
322       DException {
323     ArrayList toRet = new ArrayList();
324     Iterator iterator = constraintPool.keySet().iterator();
325     String JavaDoc Cname = null;
326     do {
327       Cname = (String JavaDoc) iterator.next();
328       if (!immToDeff.contains(Cname)) {
329         toRet.add(constraintPool.get(Cname));
330       }
331     }
332     while (iterator.hasNext());
333     return toRet;
334   }
335
336   /**
337    * This method fires appropriate constraints.
338    * @param record version
339    * @param columns indexes
340    * throws DException
341    */

342   private void fireAllConstraints(RecordVersion recordVersion, int[] columns,
343                                   _ServerSession serverSession) throws
344       DException {
345     Iterator iterator = constraintPool.keySet().iterator();
346     ConstraintStore tempCStore = null;
347     do {
348       tempCStore = (ConstraintStore) constraintPool.get(iterator.next());
349       fireThisConstraint(tempCStore, recordVersion, columns, serverSession);
350     }
351     while (iterator.hasNext());
352   }
353
354   /**
355    * This method fires a constraint for which this method is called upon.
356    * @param constraint information object
357    * @param record version
358    * @param columns indexes
359    * throws DException
360    */

361   private void fireThisConstraint(ConstraintStore cs,
362                                   RecordVersion recordVersion, int[] columns,
363                                   _ServerSession serverSession) throws
364       DException {
365     if (cs.fireDescriptor(cs.columns)) {
366       fireThisConstraint(cs, recordVersion, serverSession);
367     }
368   }
369
370   /**
371    * This method fetches all the immedaite constraints in the current user
372    * session.
373    * @param user session
374    * returns list of immediate constraints
375    * throws DException
376    */

377
378
379   /**
380    * This method generates the iteartor on the condition used in check
381    * constraint definition. It creates a new object of constraint store to
382    * which it inserted in a hashmap corresponding to each constraint name.
383    * @param check constraint descriptors array containg all the check
384    * constraint on the table.
385    * @param global session
386    * throws DException
387    */

388
389   private void generateIterators(_CheckConstraint[] checkConstraint,
390                                  _ServerSession globalSession) throws
391       DException {
392     for (int i = 0; i < checkConstraint.length; i++) {
393       booleanvalueexpression bve = checkConstraint[i].getCondition();
394       SetColumnProperties.setTableNamesAndDatatypesOfAllColumns(globalSession,
395           bve.getColumnDetails(), new TableDetails[] {tableDetails}
396           , new ArrayList(), new ArrayList());
397       ConstraintStore constraintStore = new ConstraintStore(null,
398           checkConstraint[i].getColumns(), bve);
399       String JavaDoc name = checkConstraint[i].getQualifiedIdentifier().getName();
400       constraintStore.setConstraintName(name);
401       constraintStore.setBitSet(checkConstraint[i].getBitSet());
402       constraintPool.put(name, constraintStore);
403     }
404   }
405
406   /**
407    * This method fires appropriate constraints.
408    * @param recordversion
409    * throws DException
410    */

411
412   private void fireAllConstraints(RecordVersion recordVersion,
413                                   _ServerSession serverSession) throws
414       DException {
415     Iterator iterator = constraintPool.keySet().iterator();
416     ConstraintStore tempCStore = null;
417     do {
418       tempCStore = (ConstraintStore) constraintPool.get(iterator.next());
419       fireThisConstraint(tempCStore, recordVersion, serverSession);
420     }
421     while (iterator.hasNext());
422   }
423
424   /**
425    * This method fires appropriate constraint.
426    * @param cs
427    * @param recordVersion
428    * @throws DException
429    */

430   private void fireThisConstraint(ConstraintStore cs,
431                                   RecordVersion recordVersion,
432                                   _ServerSession serverSession) throws
433       DException {
434     cs.checkConstraintReferences(serverSession, tableDetails);
435     ArrayList allRefs = cs.checkConstraintAllRef;
436     _Reference[] ref = cs.checkSementicRef;
437     _Record currentRecord = recordVersion.getCurrentRecord();
438     boolean refFound = allRefs.size() > 0;
439     if (refFound) {
440       _VariableValues vv = new CheckVariableValues(currentRecord, serverSession,
441           (Object JavaDoc[][]) allRefs.toArray(new Object JavaDoc[0][0]));
442       Object JavaDoc[] values = new Object JavaDoc[ref.length];
443       for (int i = 0; i < values.length; i++) {
444         values[i] = currentRecord.getObject(ref[i].getColumn());
445       }
446       synchronized (this) {
447         vv.setConditionVariableValue(ref, values, 1);
448         fireCheckConstraints(cs, recordVersion, cs.getCondition(), vv);
449       }
450     }
451     else {
452       _VariableValues vv = new CheckVariableValues(currentRecord, serverSession, null);
453       fireCheckConstraints(cs, recordVersion, cs.getCondition(), vv);
454     }
455   }
456
457   private void fireCheckConstraints(ConstraintStore cs,
458                                     RecordVersion recordVersion,
459                                     booleanvalueexpression BVE,
460                                     _VariableValues vv) throws DException {
461     int eval = BVE.run(vv).hashCode();
462     if (eval == 1 || eval == -1) {
463          ;//// Removed By Program ** System.out.println(" ----------------------------------------------------------------------------------------- ");
464
CheckConstraintException cce = new CheckConstraintException("DSE1251",
465           new Object JavaDoc[] {"UPDATE", cs.constraintName, BVE.toString(),
466           tableName.getIdentifier()});
467       cce.setCheckConstraintExceptionValues(tableName, BVE.toString());
468       throw cce;
469     }
470   }
471
472   private ConstraintInformation getConstraintInformation(_UserSession
473       userSession) throws DException {
474     return ( (Session) userSession.getSession()).getConstraintInformation();
475   }
476 }
477
Popular Tags