1 package com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.constraintsystem; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 6 import com.daffodilwoods.daffodildb.server.serversystem.*; 7 import com.daffodilwoods.daffodildb.server.sessionsystem.*; 8 import com.daffodilwoods.database.general.*; 9 import com.daffodilwoods.database.resource.*; 10 11 12 19 public class ReferencedConstraintsVerifier { 20 21 private _ReferencedConstraintCharacteristics referencedConstraintCharacteristics; 22 private _DataDictionary dataDictionary; 23 private QualifiedIdentifier tableName; 24 private boolean deferrable ; 25 private boolean checkedReferencedForUpdate,checkedReferencedForDelete; 26 private HashMap rcPoolUpdate; 27 private HashMap rcPoolDelete ; 28 _ReferentialConstraint[] constraints = null; 29 _ReferentialConstraint[] constraints_del = null; 30 39 public ReferencedConstraintsVerifier( _ReferencedConstraintCharacteristics refConstraintCharacteristics, _DataDictionary dd , QualifiedIdentifier tName , boolean deff ) throws DException { 40 referencedConstraintCharacteristics = refConstraintCharacteristics; 41 dataDictionary = dd; 42 deferrable = deff; 43 tableName = tName; 44 rcPoolDelete = new HashMap(); 45 rcPoolUpdate = new HashMap(); 46 } 47 48 57 58 59 60 public void verifyReferencedConstraintsForUpdate( int[] columns , _ServerSession globalSession , _StatementExecutionContext statementExecutionContext ) throws DException { 61 if (rcPoolUpdate.size() == 0 && checkedReferencedForUpdate) { 62 return; 63 } 64 else if(rcPoolUpdate.size() == 0 && !checkedReferencedForUpdate) { 65 synchronized (this) { 66 if(!checkedReferencedForUpdate){ 67 constraints = referencedConstraintCharacteristics.getReferencedConstraintsForDelete(); 68 } 69 if (constraints == null) { 70 return; 71 } 72 73 if(!checkedReferencedForUpdate){ 74 for (int j = 0; j < constraints.length; j++) { 75 String name = constraints[j].getConstraintName(); 76 ReferencedConstraints referencedConstraint = new 77 ReferencedConstraints(constraints[j], dataDictionary, 78 globalSession, false); 79 referencedConstraint.setBitSet(constraints[j].getReferencedBitSet()); 80 rcPoolUpdate.put(name, referencedConstraint); 81 checkedReferencedForUpdate = true; 82 } 83 } 84 } 85 } 86 87 ConstraintInformation constraintInformation = getConstraintInformation(statementExecutionContext.getUserSession()) ; 88 if(!deferrable){ 89 ArrayList immToDeff = constraintInformation.getImmediateToDefferedConstraints( tableName ); 90 91 if( statementExecutionContext.getOperationTypeSameOrOpposite() ){ 92 if( immToDeff == null ) 93 fireAllConstraints(statementExecutionContext,columns ); 94 else { 95 ArrayList constraintsToFire = getRequiredConstraintsToFireForUpdate( immToDeff ); 96 for( int i=0; i<constraintsToFire.size(); i++ ) 97 fireThisConstraint( (ReferencedConstraints)constraintsToFire.get(i) , statementExecutionContext , columns ); 98 } 99 } 100 else { 101 if( immToDeff != null ){ 102 String Cname = null; 103 for( int j=0; j<immToDeff.size(); j++ ){ 104 Cname = (String ) immToDeff.get(j); 105 if( rcPoolUpdate.containsKey(Cname) ) 106 fireThisConstraint( (ReferencedConstraints)rcPoolUpdate.get(Cname) , statementExecutionContext ,columns ); 107 } 108 } 109 } 110 } 111 else { ArrayList deffToImm = constraintInformation.getDefferedToImmediateConstraints( tableName ); 113 114 if( statementExecutionContext.getOperationTypeSameOrOpposite() ){ 115 if( deffToImm == null ) 116 fireAllConstraints(statementExecutionContext ,columns ); 117 else { 118 ArrayList constraintsToFire = getRequiredConstraintsToFireForUpdate( deffToImm ); 119 for( int i=0; i<constraintsToFire.size(); i++ ) 120 fireThisConstraint((ReferencedConstraints)constraintsToFire.get(i),statementExecutionContext ,columns ); 121 } 122 } 123 else { 125 if( deffToImm != null ){ 126 String Cname = null; 127 for( int j=0; j<deffToImm.size(); j++ ){ 128 Cname = (String ) deffToImm.get(j); 129 if( rcPoolUpdate.containsKey(Cname) ) 130 fireThisConstraint( (ReferencedConstraints)rcPoolUpdate.get(Cname) , statementExecutionContext , columns ); 131 } 132 } 133 } 134 } 135 } 136 137 144 145 public void verifyReferencedConstraintsForDelete( _ServerSession globalSession , _StatementExecutionContext statementExecutionContext ) throws DException { 146 if (rcPoolDelete.size() == 0 && checkedReferencedForDelete) { 147 return; 148 } 149 else if(rcPoolDelete.size() == 0 && !checkedReferencedForDelete) { 150 151 synchronized (this) { 152 if(!checkedReferencedForDelete){ 153 constraints = referencedConstraintCharacteristics.getReferencedConstraintsForDelete(); 154 } 155 if (constraints == null) { 156 return; 157 } 158 if(!checkedReferencedForDelete){ 159 for (int i = 0; i < constraints.length; i++) { 160 String name = constraints[i].getConstraintName(); 161 ReferencedConstraints referencedConstraint = new 162 ReferencedConstraints(constraints[i], dataDictionary, 163 globalSession, true); 164 165 rcPoolDelete.put(name, referencedConstraint); 166 checkedReferencedForDelete = true; 167 } 168 } 169 } 170 } 171 172 ConstraintInformation constraintInformation = getConstraintInformation(statementExecutionContext.getUserSession()) ; 173 if(!deferrable){ 174 ArrayList immToDeff = constraintInformation.getImmediateToDefferedConstraints( tableName ); 175 if( statementExecutionContext.getOperationTypeSameOrOpposite() ){ 176 if( immToDeff == null ) 177 fireAllConstraints(statementExecutionContext); 178 else { 179 ArrayList constraintsToFire = getRequiredConstraintsToFireForDelete( immToDeff ); 180 for( int i=0; i<constraintsToFire.size(); i++ ) 181 fireThisConstraint( (ReferencedConstraints)constraintsToFire.get(i) , statementExecutionContext ); 182 } 183 } 184 else { 185 if( immToDeff != null ){ 186 String Cname = null; 187 for( int j=0; j<immToDeff.size(); j++ ){ 188 Cname = (String ) immToDeff.get(j); 189 if( rcPoolDelete.containsKey(Cname) ) 190 fireThisConstraint( (ReferencedConstraints)rcPoolDelete.get(Cname) , statementExecutionContext ); 191 } 192 } 193 } 194 return; 195 } 196 else { 197 ArrayList deffToImm = constraintInformation.getDefferedToImmediateConstraints( tableName ); 198 if( statementExecutionContext.getOperationTypeSameOrOpposite() ){ 199 if( deffToImm == null ) 200 fireAllConstraints(statementExecutionContext); 201 else { 202 ArrayList constraintsToFire = getRequiredConstraintsToFireForDelete( deffToImm ); 203 for( int i=0; i<constraintsToFire.size(); i++ ) 204 fireThisConstraint((ReferencedConstraints)constraintsToFire.get(i),statementExecutionContext ); 205 } 206 } 207 else { 209 if( deffToImm != null ){ 210 String Cname = null; 211 for( int j=0; j<deffToImm.size(); j++ ){ 212 Cname = (String ) deffToImm.get(j); 213 if( rcPoolDelete.containsKey(Cname) ) 214 fireThisConstraint( (ReferencedConstraints)rcPoolDelete.get(Cname) , statementExecutionContext ); 215 } 216 } 217 } 218 } 219 } 220 221 222 private ArrayList getDefferedDefferedConstraints( ArrayList sessionImmediateConstraints ) { 223 ArrayList toRet = new ArrayList(); 224 Iterator iterator = rcPoolUpdate.keySet().iterator() ; 225 String Cname = null ; 226 do{ 227 Cname = (String )iterator.next(); 228 if( !sessionImmediateConstraints.contains(Cname) ) 229 toRet.add(rcPoolUpdate.get(Cname)); 230 }while( iterator.hasNext() ); 231 return toRet; 232 } 233 234 241 242 private void fireAllConstraints(_StatementExecutionContext sec,int[] columns) throws DException { 243 Iterator iterator = rcPoolUpdate.keySet().iterator(); 244 ReferencedConstraints tempCStore = null; 245 do { 246 tempCStore = (ReferencedConstraints) rcPoolUpdate.get(iterator.next()); 247 fireThisConstraint(tempCStore, sec, columns); 248 } 249 while (iterator.hasNext()); 250 } 251 252 261 262 private void fireThisConstraint(ReferencedConstraints referencedConstraints , _StatementExecutionContext sec,int[] columns) throws DException { 263 if( referencedConstraints.fireDescriptor( columns ) ) 264 referencedConstraints.checkReferencedConstraints(sec); 265 } 266 267 268 275 276 private void fireAllConstraints(_StatementExecutionContext sec) throws DException { 277 Iterator iterator = rcPoolDelete.keySet().iterator() ; 278 ReferencedConstraints tempCStore = null; 279 do{ 280 String nn = (String ) iterator.next(); 281 tempCStore = (ReferencedConstraints)rcPoolDelete.get(nn); 282 fireThisConstraint(tempCStore,sec); 283 }while( iterator.hasNext() ); 284 } 285 286 294 295 private void fireThisConstraint(ReferencedConstraints referencedConstraints , _StatementExecutionContext sec ) throws DException { 296 referencedConstraints.checkReferencedConstraints(sec); 297 } 298 299 306 307 308 309 private ConstraintInformation getConstraintInformation( _UserSession userSession ) throws DException { 310 return ((Session)userSession.getSession()).getConstraintInformation(); 311 } 312 313 319 private ArrayList getRequiredConstraintsToFireForDelete( ArrayList sessionImmediateConstraints ) { 320 ArrayList toRet = new ArrayList(); 321 Iterator iterator = rcPoolDelete.keySet().iterator() ; 322 String Cname = null ; 323 do{ 324 Cname = (String )iterator.next(); 325 if( !sessionImmediateConstraints.contains(Cname) ) 326 toRet.add(rcPoolDelete.get(Cname)); 327 }while( iterator.hasNext() ); 328 return toRet; 329 } 330 331 337 private ArrayList getRequiredConstraintsToFireForUpdate( ArrayList sessionImmediateConstraints ) { 338 ArrayList toRet = new ArrayList(); 339 Iterator iterator = rcPoolUpdate.keySet().iterator() ; 340 String Cname = null ; 341 do{ 342 Cname = (String )iterator.next(); 343 if( !sessionImmediateConstraints.contains(Cname) ) 344 toRet.add(rcPoolUpdate.get(Cname)); 345 }while( iterator.hasNext() ); 346 return toRet; 347 } 348 } 349 | Popular Tags |