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 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 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 [] {tableName.catalog, tableName.schema, 55 tableName.name}); 56 } 57 58 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 Cname = null; 124 for (int j = 0; j < immToDeff.size(); j++) { 125 Cname = (String ) 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 Cname = null; 160 for (int j = 0; j < deffToImm.size(); j++) { 161 Cname = (String ) 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 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 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) { 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 Cname = null; 243 for (int j = 0; j < immToDeff.size(); j++) { 244 Cname = (String ) 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 Cname = null; 279 for (int j = 0; j < deffToImm.size(); j++) { 280 Cname = (String ) 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 298 private ArrayList getRequiredConstraintsToFire(ArrayList 299 sessionImmediateConstraints) { 300 ArrayList toRet = new ArrayList(); 301 Iterator iterator = constraintPool.keySet().iterator(); 302 String Cname = null; 303 do { 304 Cname = (String ) 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 321 private ArrayList getImmediateConstraintsToFire(ArrayList immToDeff) throws 322 DException { 323 ArrayList toRet = new ArrayList(); 324 Iterator iterator = constraintPool.keySet().iterator(); 325 String Cname = null; 326 do { 327 Cname = (String ) 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 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 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 377 378 379 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 name = checkConstraint[i].getQualifiedIdentifier().getName(); 400 constraintStore.setConstraintName(name); 401 constraintStore.setBitSet(checkConstraint[i].getBitSet()); 402 constraintPool.put(name, constraintStore); 403 } 404 } 405 406 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 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 [][]) allRefs.toArray(new Object [0][0])); 442 Object [] values = new Object [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 ; CheckConstraintException cce = new CheckConstraintException("DSE1251", 465 new Object [] {"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 |