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.serversystem.*; 9 import com.daffodilwoods.daffodildb.server.sessionsystem.*; 10 import com.daffodilwoods.database.general.*; 11 import com.daffodilwoods.database.resource.*; 12 13 20 public class ReferencingConstraintsVerifier { 21 22 _ReferencingConstraintCharacteristics referencingConstraintCharacteristics; 23 QualifiedIdentifier tableName; 24 boolean deferrable; 25 HashMap constraintPool; 26 boolean checkedReferencing = false; 27 28 36 public ReferencingConstraintsVerifier(_ReferencingConstraintCharacteristics 37 referencingCharacteristics, 38 QualifiedIdentifier tName, boolean deff) throws 39 DException { 40 referencingConstraintCharacteristics = referencingCharacteristics; 41 tableName = tName; 42 deferrable = deff; 43 constraintPool = new HashMap(); 44 } 45 46 54 55 public void verifyReferencingConstraintsForUpdate(int[] columns, 56 _ServerSession globalSession, 57 _StatementExecutionContext statementExecutionContext) throws DException { 58 _ReferentialConstraint[] referencingDescriptor = null; 59 60 Iterator iterator = constraintPool.keySet().iterator(); 61 if (constraintPool.size() == 0 && checkedReferencing) { 62 return; 63 } 64 else if(constraintPool.size() == 0 && !checkedReferencing){ 65 synchronized (this) { 66 if (!checkedReferencing) { 67 referencingDescriptor = referencingConstraintCharacteristics. 68 getReferencingConstraints(); 69 } 70 if (referencingDescriptor == null) { 71 return; 72 } 73 if (!checkedReferencing){ 74 for (int i = 0; i < referencingDescriptor.length; i++) { 75 String name = referencingDescriptor[i].getQualifiedIdentifier(). 76 getIdentifier(); 77 ReferencingConstraints referencingConstraints = new 78 ReferencingConstraints(referencingDescriptor[i], globalSession); 79 referencingConstraints.setBitSet(referencingDescriptor[i]. 80 getReferencingBitSet()); 81 constraintPool.put(name, referencingConstraints); 82 checkedReferencing = true; 83 } 84 } 85 } 86 } 87 88 89 ConstraintInformation constraintInformation = getConstraintInformation( 90 statementExecutionContext.getUserSession()); 91 if (!deferrable) { 92 ArrayList immToDeff = constraintInformation. 93 getImmediateToDefferedConstraints(tableName); 94 if (statementExecutionContext.getOperationTypeSameOrOpposite()) { 95 if (immToDeff == null) { 96 fireAllConstraints(statementExecutionContext.getRecordVersion(), 97 columns, globalSession); 98 } 99 else { 100 ArrayList constraintsToFire = getRequiredConstraintsToFire(immToDeff); 101 for (int i = 0; i < constraintsToFire.size(); i++) { 102 fireThisConstraint( (ReferencingConstraints) constraintsToFire.get( 103 i), statementExecutionContext.getRecordVersion(), columns, 104 globalSession); 105 } 106 } 107 } 108 else { 109 if (immToDeff != null) { 110 String Cname = null; 111 for (int j = 0; j < immToDeff.size(); j++) { 112 Cname = (String ) immToDeff.get(j); 113 if (constraintPool.containsKey(Cname)) { 114 fireThisConstraint( (ReferencingConstraints) constraintPool.get( 115 Cname), statementExecutionContext.getRecordVersion(), columns, 116 globalSession); 117 } 118 } 119 } 120 } 121 } 122 else { ArrayList deffToImm = constraintInformation. 124 getDefferedToImmediateConstraints(tableName); 125 126 if (statementExecutionContext.getOperationTypeSameOrOpposite()) { 127 if (deffToImm == null) { 128 fireAllConstraints(statementExecutionContext.getRecordVersion(), 129 columns, globalSession); 130 } 131 else { 132 ArrayList constraintsToFire = getRequiredConstraintsToFire(deffToImm); 133 for (int i = 0; i < constraintsToFire.size(); i++) { 134 fireThisConstraint( (ReferencingConstraints) constraintsToFire.get( 135 i), statementExecutionContext.getRecordVersion(), columns, 136 globalSession); 137 } 138 } 139 } 140 else { if (deffToImm != null) { 142 String Cname = null; 143 for (int j = 0; j < deffToImm.size(); j++) { 144 Cname = (String ) deffToImm.get(j); 145 if (constraintPool.containsKey(Cname)) { 146 fireThisConstraint( (ReferencingConstraints) constraintPool.get( 147 Cname), statementExecutionContext.getRecordVersion(), columns, 148 globalSession); 149 } 150 } 151 } 152 } 153 } 154 } 155 156 163 164 public void verifyReferencingConstraints(int[] columns, 165 _ServerSession globalSession, 166 _StatementExecutionContext 167 statementExecutionContext) throws 168 DException { 169 170 if (constraintPool.size() == 0 && checkedReferencing) { 171 return; 172 } 173 else if(constraintPool.size() == 0 && !checkedReferencing) { 174 _ReferentialConstraint[] referencingDescriptor = null; 175 synchronized (this) { 176 if (!checkedReferencing) { 177 referencingDescriptor = referencingConstraintCharacteristics. 178 getReferencingConstraints(); 179 180 } 181 if (referencingDescriptor == null) { 182 return; 183 } 184 if (!checkedReferencing){ 185 for (int i = 0; i < referencingDescriptor.length; i++) { 186 String name = referencingDescriptor[i].getQualifiedIdentifier(). 187 getIdentifier(); 188 ReferencingConstraints referencingConstraints = new 189 ReferencingConstraints(referencingDescriptor[i], globalSession); 190 referencingConstraints.setBitSet(referencingDescriptor[i]. 191 getReferencingBitSet()); 192 constraintPool.put(name, referencingConstraints); 193 checkedReferencing = true; 194 } 195 } 196 } 197 } 198 199 200 ConstraintInformation constraintInformation = getConstraintInformation( 201 statementExecutionContext.getUserSession()); 202 if (!deferrable) { ArrayList immToDeff = constraintInformation. 204 getImmediateToDefferedConstraints(tableName); 205 if (statementExecutionContext.getOperationTypeSameOrOpposite()) { 206 if (immToDeff == null) { 207 fireAllConstraints(statementExecutionContext.getRecordVersion(), 208 globalSession); 209 } 210 else { 211 ArrayList constraintsToFire = getRequiredConstraintsToFire(immToDeff); 212 for (int i = 0; i < constraintsToFire.size(); i++) { 213 fireThisConstraint( (ReferencingConstraints) constraintsToFire.get( 214 i), statementExecutionContext.getRecordVersion(), globalSession); 215 } 216 } 217 } 218 else { 219 if (immToDeff != null) { 220 String Cname = null; 221 for (int j = 0; j < immToDeff.size(); j++) { 222 Cname = (String ) immToDeff.get(j); 223 if (constraintPool.containsKey(Cname)) { 224 fireThisConstraint( (ReferencingConstraints) constraintPool.get( 225 Cname), statementExecutionContext.getRecordVersion(), 226 globalSession); 227 } 228 } 229 } 230 } 231 return; 232 } 233 else { ArrayList deffToImm = constraintInformation. 235 getDefferedToImmediateConstraints(tableName); 236 237 if (statementExecutionContext.getOperationTypeSameOrOpposite()) { 238 if (deffToImm == null) { 239 fireAllConstraints(statementExecutionContext.getRecordVersion(), 240 globalSession); 241 } 242 else { 243 ArrayList constraintsToFire = getRequiredConstraintsToFire(deffToImm); 244 for (int i = 0; i < constraintsToFire.size(); i++) { 245 fireThisConstraint( (ReferencingConstraints) constraintsToFire.get( 246 i), statementExecutionContext.getRecordVersion(), globalSession); 247 } 248 } 249 } 250 else { if (deffToImm != null) { 252 String Cname = null; 253 for (int j = 0; j < deffToImm.size(); j++) { 254 Cname = (String ) deffToImm.get(j); 255 if (constraintPool.containsKey(Cname)) { 256 fireThisConstraint( (ReferencingConstraints) constraintPool.get( 257 Cname), statementExecutionContext.getRecordVersion(), 258 globalSession); 259 } 260 } 261 } 262 } 263 } 264 } 265 266 272 private void fireAllConstraints(RecordVersion recordVersion, 273 _ServerSession globalSession) throws 274 DException { 275 Iterator iterator = constraintPool.keySet().iterator(); 276 ReferencingConstraints tempCStore = null; 277 do { 278 tempCStore = (ReferencingConstraints) constraintPool.get(iterator.next()); 279 fireThisConstraint(tempCStore, recordVersion, globalSession); 280 } 281 while (iterator.hasNext()); 282 283 } 284 285 292 private void fireThisConstraint(ReferencingConstraints referencingConstraints, 293 RecordVersion recordVersion, 294 _ServerSession globalSession) throws 295 DException { 296 referencingConstraints.checkReferencingConstraints(recordVersion, 297 globalSession); 298 } 299 300 307 private void fireAllConstraints(RecordVersion recordVersion, int[] columns, 308 _ServerSession globalSession) throws 309 DException { 310 Iterator iterator = constraintPool.keySet().iterator(); 311 ReferencingConstraints tempCStore = null; 312 do { 313 tempCStore = (ReferencingConstraints) constraintPool.get(iterator.next()); 314 fireThisConstraint(tempCStore, recordVersion, columns, globalSession); 315 } 316 while (iterator.hasNext()); 317 } 318 319 327 public void fireThisConstraint(ReferencingConstraints referencingConstraints, 328 RecordVersion recordVersion, int[] columns, 329 _ServerSession globalSession) throws 330 DException { 331 if (referencingConstraints.fireDescriptor(columns)) { 332 referencingConstraints.checkReferencingConstraints(recordVersion, 333 globalSession); 334 } 335 } 336 337 public String toString() { 338 String tab = null; 339 try { 340 tab = tableName.getName(); 341 } 342 catch (Exception e) { 343 e.printStackTrace(); 344 } 345 return " -------------------- Referencing Constraint Verifeir :::::::: " + 346 tab; 347 } 348 349 356 private ConstraintInformation getConstraintInformation(_UserSession 357 userSession) throws DException { 358 return ( (Session) userSession.getSession()).getConstraintInformation(); 359 } 360 361 366 private ArrayList getRequiredConstraintsToFire(ArrayList 367 sessionImmediateConstraints) { 368 ArrayList toRet = new ArrayList(); 369 Iterator iterator = constraintPool.keySet().iterator(); 370 String Cname = null; 371 do { 372 Cname = (String ) iterator.next(); 373 if (!sessionImmediateConstraints.contains(Cname)) { 374 toRet.add(constraintPool.get(Cname)); 375 } 376 } 377 while (iterator.hasNext()); 378 return toRet; 379 } 380 381 } 382 | Popular Tags |