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.datasystem.utility.*; 7 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record; 8 import com.daffodilwoods.daffodildb.server.serversystem.*; 9 import com.daffodilwoods.daffodildb.server.sessionsystem.*; 10 import com.daffodilwoods.daffodildb.server.sql99.common.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 12 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 13 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 14 import com.daffodilwoods.daffodildb.utils.field.*; 15 import com.daffodilwoods.database.general.*; 16 import com.daffodilwoods.database.resource.*; 17 import com.daffodilwoods.database.utility.*; 18 import com.daffodilwoods.daffodildb.utils.comparator.*; 19 import java.text.*; 20 import com.daffodilwoods.daffodildb.utils.*; 21 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*; 22 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 23 24 25 32 33 public class PrimaryAndUniqueConstraintVerifier { 34 _PrimaryAndUniqueConstraintCharacteristics primarAndUniqueConstraintCharacteristics; 35 QualifiedIdentifier tableName; 36 boolean deferrable; 37 ConstraintStore primaryConstraintStore = null ; 38 TableDetails tableDetails ; 39 ColumnDetails[] columnDetails; 40 boolean checkedPrimary = false; 41 boolean checkedUnique = false; 42 HashMap uniqueIterator; 43 _ColumnCharacteristics cc; 44 45 46 54 55 public PrimaryAndUniqueConstraintVerifier(_PrimaryAndUniqueConstraintCharacteristics primarAndUniqueCharacteristics, QualifiedIdentifier tName , boolean deff ) throws DException { 56 primarAndUniqueConstraintCharacteristics = primarAndUniqueCharacteristics; 57 tableName = tName; 58 deferrable = deff ; 59 uniqueIterator = new HashMap(); 60 tableDetails = new TableDetails(); 61 tableDetails.setTableName(new String []{tableName.catalog,tableName.schema,tableName.name}); 62 } 63 64 72 73 public synchronized void verifyPrimaryConstraints( int[] columns , _StatementExecutionContext sec , _ServerSession globalSession) throws DException{ 74 if( primaryConstraintStore == null ){ _UniqueConstraint p = null ; if( !checkedPrimary ){ 77 p = primarAndUniqueConstraintCharacteristics.getPrimaryConstraints() ; 78 checkedPrimary = true; 79 } 80 if( p == null ){ 81 return ; 82 } 83 tableDetails.cc = globalSession.getColumnCharacteristics(tableDetails.getQualifiedIdentifier()); 84 cc = sec.getColumnCharacteristics(tableName) ; 85 generateIteratorForPrimary( p , globalSession ); 86 } if(!deferrable){ ConstraintInformation constraintInformation = getConstraintInformation(sec.getUserSession()) ; 89 ArrayList immToDeff = constraintInformation.getImmediateToDefferedConstraints( tableName ); 90 if( sec.getOperationTypeSameOrOpposite() ){ if( immToDeff == null ) 92 fireConstraintsForPrimary(sec.getRecordVersion() ); 93 } 94 else { if( immToDeff != null ){ 96 String Cname; 97 for( int j=0; j<immToDeff.size(); j++ ){ 98 Cname = (String ) immToDeff.get(j); 99 if( primaryConstraintStore.constraintName.equalsIgnoreCase(Cname) ) 100 fireConstraintsForPrimary( sec.getRecordVersion() ); 101 } 102 } 103 } 104 } 105 else { ConstraintInformation constraintInformation = getConstraintInformation(sec.getUserSession()) ; 107 ArrayList deffToImm = constraintInformation.getDefferedToImmediateConstraints( tableName ); 108 if(sec.getOperationTypeSameOrOpposite()){ if(deffToImm == null) 110 fireConstraintsForPrimary(sec.getRecordVersion() ); 111 else{ 112 if(!deffToImm.contains(primaryConstraintStore.constraintName)) 113 fireConstraintsForPrimary(sec.getRecordVersion() ); 114 } 115 } 116 else{ if(deffToImm!=null){ 118 String cname = null; 119 for (int i = 0; i < deffToImm.size(); i++) { 120 cname = (String )deffToImm.get(i); 121 if(primaryConstraintStore.constraintName.equalsIgnoreCase(cname)) 122 fireConstraintsForPrimary(sec.getRecordVersion() ); 123 } 124 } 125 } 126 } 127 } 128 129 137 138 139 public synchronized void verifyUniqueConstraints( int[] columns , _StatementExecutionContext sec , _ServerSession globalSession) throws DException { 140 if( uniqueIterator.size() == 0 ){ 141 _UniqueConstraint[] u = null ; 142 if( !checkedUnique ){ 143 u = primarAndUniqueConstraintCharacteristics.getUniqueConstraints() ; 144 checkedUnique = true ; 145 } 146 if( u == null ){ 147 return ; 148 } 149 tableDetails.cc = globalSession.getColumnCharacteristics(tableDetails.getQualifiedIdentifier()); 150 cc = sec.getColumnCharacteristics(tableName) ; 151 generateIterators( u , globalSession ); 152 } 153 ConstraintInformation constraintInformation = getConstraintInformation(sec.getUserSession()) ; 154 if(!deferrable){ 155 ArrayList immToDeff = constraintInformation.getImmediateToDefferedConstraints( tableName ); 156 157 if( sec.getOperationTypeSameOrOpposite() ){ 158 if( immToDeff == null ) 159 fireAllConstraints(sec.getRecordVersion()); 160 else { 161 ArrayList constraintsToFire = getRequiredConstraintsToFire( immToDeff ); 162 for( int i=0; i<constraintsToFire.size(); i++ ) 163 fireThisConstraint( (ConstraintStore)constraintsToFire.get(i) , sec.getRecordVersion()); 164 } 165 } 166 else { 167 if( immToDeff != null ){ 168 String Cname = null; 169 for( int j=0; j<immToDeff.size(); j++ ){ 170 Cname = (String ) immToDeff.get(j); 171 if( uniqueIterator.containsKey(Cname) ) 172 fireThisConstraint( (ConstraintStore)uniqueIterator.get(Cname) , sec.getRecordVersion() ); 173 } 174 } 175 } 176 return; 177 } 178 else { 179 ArrayList deffToImm = constraintInformation.getDefferedToImmediateConstraints( tableName ); 180 181 ArrayList constraintsToFire = null; 182 if( sec.getOperationTypeSameOrOpposite() ){ 183 if( deffToImm == null ) 184 fireAllConstraints(sec.getRecordVersion()); 185 else { 186 constraintsToFire = getRequiredConstraintsToFire( deffToImm ); 187 for( int i=0; i<constraintsToFire.size(); i++ ) 188 fireThisConstraint( (ConstraintStore)constraintsToFire.get(i) , sec.getRecordVersion() ); 189 } 190 } 191 else 192 { 193 if( deffToImm != null ){ 194 String Cname = null; 195 for( int j=0; j<deffToImm.size(); j++ ){ 196 Cname = (String ) deffToImm.get(j); 197 if( uniqueIterator.containsKey(Cname) ) 198 fireThisConstraint( (ConstraintStore)uniqueIterator.get(Cname) , sec.getRecordVersion() ); 199 } 200 } 201 } 202 } 203 } 204 205 206 209 210 public synchronized void verifyUniqueConstraintsForUpdate( int[] columns , _StatementExecutionContext sec , _ServerSession globalSession) throws DException { 211 if( uniqueIterator.size() == 0 ){ 212 _UniqueConstraint[] u = null; 213 if( !checkedUnique ){ 214 u = primarAndUniqueConstraintCharacteristics.getUniqueConstraints() ; 215 checkedUnique = true; 216 } 217 if( u == null){ 218 return ; 219 } 220 tableDetails.cc = globalSession.getColumnCharacteristics(tableDetails.getQualifiedIdentifier()); 221 _ColumnCharacteristics cc = sec.getColumnCharacteristics(tableName) ; 222 generateIterators( u , globalSession ); 223 } 224 225 ConstraintInformation constraintInformation = getConstraintInformation(sec.getUserSession()) ; 226 if(!deferrable){ 227 ArrayList immToDeff = constraintInformation.getImmediateToDefferedConstraints( tableName ); 228 229 if( sec.getOperationTypeSameOrOpposite() ){ 230 if( immToDeff == null ) 231 fireAllConstraints(sec.getRecordVersion(),columns); 232 else { 233 ArrayList constraintsToFire = getRequiredConstraintsToFire( immToDeff ); 234 for( int i=0; i<constraintsToFire.size(); i++ ) 235 fireThisConstraint( (ConstraintStore)constraintsToFire.get(i) , sec.getRecordVersion(),columns); 236 } 237 } 238 else { 239 if( immToDeff != null ){ 240 String Cname = null; 241 for( int j=0; j<immToDeff.size(); j++ ){ 242 Cname = (String ) immToDeff.get(j); 243 if( uniqueIterator.containsKey(Cname) ) 244 fireThisConstraint( (ConstraintStore)uniqueIterator.get(Cname) , sec.getRecordVersion(), columns ); 245 } 246 } 247 } 248 return; 249 } 250 else { 251 ArrayList deffToImm = constraintInformation.getDefferedToImmediateConstraints( tableName ); 252 253 ArrayList constraintsToFire = null; 254 if( sec.getOperationTypeSameOrOpposite() ){ 255 if( deffToImm == null ) 256 fireAllConstraints(sec.getRecordVersion(),columns); 257 else { 258 constraintsToFire = getRequiredConstraintsToFire( deffToImm ); 259 for( int i=0; i<constraintsToFire.size(); i++ ) 260 fireThisConstraint( (ConstraintStore)constraintsToFire.get(i) , sec.getRecordVersion(), columns ); 261 } 262 } 263 else 264 { 265 if( deffToImm != null ){ 266 String Cname = null; 267 for( int j=0; j<deffToImm.size(); j++ ){ 268 Cname = (String ) deffToImm.get(j); 269 if( uniqueIterator.containsKey(Cname) ) 270 fireThisConstraint( (ConstraintStore)uniqueIterator.get(Cname) , sec.getRecordVersion(),columns ); 271 } 272 } 273 } 274 } 275 } 276 277 284 285 286 287 private synchronized void verifyUniqueness( ConstraintStore constraintStore, RecordVersion recordVersion ) throws DException { 288 Object [] parameters = getParameters( constraintStore.columns , recordVersion.getCurrentRecord() ); 289 _Iterator iterator = constraintStore.getIterator() ; 290 Object lastKey = constraintStore.getLastKey() ; 291 try { 292 if (lastKey == null || ( (_IndexIteratorInfo) iterator).getComparator().compare(parameters, lastKey) > 0) { 293 constraintStore.setLastKey(parameters); 294 return; 295 } 296 } catch (ClassCastException ex1) { 297 throw ex1; 298 } 299 try { 300 if (iterator.seek(parameters)) { 301 if (iterator.next()) { 302 Object [] newValues = (Object []) iterator.getColumnValues( 303 constraintStore.columns); 304 if(((_IndexIteratorInfo)iterator).getComparator().compare(parameters ,newValues ) == 0) 305 throw new UniqueConstraintException("DSE1032", null); 306 } 307 } 308 } 309 catch (NullPointerException ex) { 310 if (SystemTables.isSystemTable(tableName.getIdentifier())) 311 return; 312 throw ex; 313 } 314 } 315 316 317 324 private Object [] getParameters( int[] columns , _Record record ) throws DException { 325 Object [] parameters = new Object [columns.length]; 326 for( int i = 0 ; i < columns.length ; i++ ) 327 parameters[i] = record.getObject(columns[i]); 328 return parameters; 329 } 330 331 332 339 340 private boolean checkForAllNull( _Record record , int[] columns ) throws DException { 341 342 for( int i=0; i<columns.length; i++ ) 343 if( ((FieldBase)record.getObject( columns[i] )).isNull() == false ){ 344 return false; 345 } 346 return true; 347 } 348 349 356 357 private void generateIterators( _UniqueConstraint[] uniqueConstraint , _ServerSession globalSession ) throws DException { 358 for( int i=0; i<uniqueConstraint.length; i++ ){ 359 booleanvalueexpression bve = uniqueConstraint[i].getCondition(); 360 SetColumnProperties.setTableNamesAndDatatypesOfAllColumns(globalSession, bve.getColumnDetails(),new TableDetails[]{tableDetails},null,new ArrayList()); 361 _Iterator itera = ((GlobalSession)globalSession).getIterator(tableName ,uniqueConstraint[i].getColumns() ) ; 362 SuperComparator com = ( (_IndexIteratorInfo)itera ).getComparator() ; 363 Object lastKey = null; 364 if (itera.last()) 365 lastKey = itera.getColumnValues(uniqueConstraint[i].getColumns()); 366 ConstraintStore constraintStore = new ConstraintStore( itera , uniqueConstraint[i].getColumns() , bve ); 367 constraintStore.setLastKey(lastKey) ; 368 constraintStore.setComparator(com) ; 369 String name = uniqueConstraint[i].getQualifiedIdentifier().getName(); 370 constraintStore.setConstraintName(name); 371 constraintStore.setBitSet(uniqueConstraint[i].getBitSet()); 372 uniqueIterator.put(name,constraintStore); 373 } 374 } 375 376 377 private void generateIteratorForPrimary(_UniqueConstraint uniqueConstraint , _ServerSession globalSession ) throws DException { 378 booleanvalueexpression bve = uniqueConstraint.getCondition(); 379 SetColumnProperties.setTableNamesAndDatatypesOfAllColumns(globalSession,bve.getColumnDetails(),new TableDetails[]{tableDetails},null,null); 380 _Iterator iterator = ((GlobalSession)globalSession).getIterator(tableName ,uniqueConstraint.getColumns() ) ; 381 SuperComparator com = ( (_IndexIteratorInfo) iterator).getComparator(); 382 Object lastKey = null; 383 if(iterator.last() ) 384 lastKey = iterator.getColumnValues(uniqueConstraint.getColumns() ); 385 primaryConstraintStore = new ConstraintStore( iterator , uniqueConstraint.getColumns() , bve ); 386 primaryConstraintStore.setLastKey(lastKey) ; 387 primaryConstraintStore.setComparator(com) ; 388 String name = uniqueConstraint.getQualifiedIdentifier().getName(); 389 primaryConstraintStore.setConstraintName(name); 390 primaryConstraintStore.setBitSet(uniqueConstraint.getBitSet()); 391 392 } 393 394 400 401 private void fireThisConstraint(ConstraintStore cs , RecordVersion recordVersion,int[] columns) throws DException { 402 booleanvalueexpression BVE = null; 403 if( cs.fireDescriptor( columns ) ){ 404 BVE = cs.getCondition(); 405 try{ 406 if( !checkForAllNull( recordVersion.getCurrentRecord() , cs.columns ) ) 407 verifyUniqueness( cs , recordVersion ); 408 }catch ( DException e ) { 409 if( e.getDseCode().equalsIgnoreCase("DSE1032") ){ 410 UniqueConstraintException ue = new UniqueConstraintException("DSE1032",new Object []{cs.constraintName,tableName.getIdentifier()}); 411 ue.setCorrespondingTable( tableName ); 412 ue.setRecord( recordVersion.getCurrentRecord() ); 413 throw ue ; 414 } 415 } 416 } 417 } 418 419 private void fireThisConstraint(ConstraintStore cs , RecordVersion recordVersion) throws DException { 420 booleanvalueexpression BVE = null; 421 BVE = cs.getCondition(); 422 try{ 423 if (!checkForAllNull(recordVersion.getCurrentRecord(), cs.columns)) 424 verifyUniqueness(cs, recordVersion); 425 426 }catch ( DException e ) { 427 if( e.getDseCode().equalsIgnoreCase("DSE1032") ){ 428 UniqueConstraintException ue = new UniqueConstraintException("DSE1032",new Object []{cs.constraintName,tableName.getIdentifier()}); 429 ue.setCorrespondingTable( tableName ); 430 ue.setRecord( recordVersion.getCurrentRecord() ); 431 throw ue ; 432 } 433 } 434 } 435 436 437 442 private void fireAllConstraints(RecordVersion recordVersion) throws DException { 443 Iterator iterator = uniqueIterator.keySet().iterator() ; 444 ConstraintStore tempCStore = null; 445 while( iterator.hasNext() ){ 446 String key = (String ) iterator.next(); 447 tempCStore = (ConstraintStore)uniqueIterator.get(key); 448 fireThisConstraint(tempCStore,recordVersion ); 449 } 450 } 451 452 458 private void fireAllConstraints(RecordVersion recordVersion,int[] columns) throws DException { 459 Iterator iterator = uniqueIterator.keySet().iterator() ; 460 ConstraintStore tempCStore = null; 461 do{ 462 tempCStore = (ConstraintStore)uniqueIterator.get(iterator.next()); 463 fireThisConstraint(tempCStore,recordVersion ,columns); 464 }while( iterator.hasNext() ); 465 } 466 467 468 473 private void fireConstraintsForPrimary(RecordVersion recordVersion) throws DException{ 474 int[] cols = primaryConstraintStore.columns ; 475 _Record currentRecord = recordVersion.getCurrentRecord(); 476 for( int j = 0 ; j < cols.length ; j++ ) 477 if( ((FieldBase)currentRecord.getObject( cols[j]) ).isNull() ) { 478 ; throw new DException("DSE1291",new Object []{cc.getColumnName(cols[j]),tableName.getIdentifier(),}); 480 } 481 try{ 482 verifyUniqueness(primaryConstraintStore,recordVersion); 483 }catch ( DException e ) { 484 if( e.getDseCode().equalsIgnoreCase("DSE1032") ){ 485 String [] columnsToReturn = new String [cols.length] ; 486 for( int k=0; k<cols.length; k++ ) 487 columnsToReturn[k] = cc.getColumnName(cols[k]); 488 PrimaryConstraintException pce = new PrimaryConstraintException("DSE1255", new Object []{primaryConstraintStore.constraintName, tableName.getIdentifier()}); 489 pce.setCorrespondingTable( tableName ); 490 pce.setRecord(recordVersion.getCurrentRecord()); 491 pce.setPrimaryColumns( columnsToReturn ); 492 throw pce ; 493 } 494 } 495 } 496 497 502 private ArrayList getRequiredConstraintsToFire( ArrayList sessionImmediateConstraints ) { 503 ArrayList toRet = new ArrayList(); 504 Iterator iterator = uniqueIterator.keySet().iterator() ; 505 String Cname = null ; 506 do{ 507 Cname = (String )iterator.next(); 508 if( !sessionImmediateConstraints.contains(Cname) ) 509 toRet.add(uniqueIterator.get(Cname)); 510 }while( iterator.hasNext() ); 511 return toRet; 512 } 513 514 private ConstraintInformation getConstraintInformation( _UserSession userSession ) throws DException { 515 return ((Session)userSession.getSession()).getConstraintInformation(); 516 } 517 525 526 public synchronized void verifyPrimaryConstraintsForUpdate( int[] columns , _StatementExecutionContext sec , _ServerSession globalSession) throws DException{ 527 if( primaryConstraintStore == null ){ _UniqueConstraint p = null ; if( !checkedPrimary ){ 530 p = primarAndUniqueConstraintCharacteristics.getPrimaryConstraints() ; 531 checkedPrimary = true; 532 } 533 if( p == null ){ 534 return ; 535 } 536 tableDetails.cc = globalSession.getColumnCharacteristics(tableDetails.getQualifiedIdentifier()); 537 cc = sec.getColumnCharacteristics(tableName) ; 538 generateIteratorForPrimary( p , globalSession ); 539 } if(!primaryConstraintStore.fireDescriptor(columns)){ 541 return; 542 } 543 if(!deferrable){ ConstraintInformation constraintInformation = getConstraintInformation(sec.getUserSession()) ; 545 ArrayList immToDeff = constraintInformation.getImmediateToDefferedConstraints( tableName ); 546 if( sec.getOperationTypeSameOrOpposite() ){ if( immToDeff == null ) 548 fireConstraintsForPrimary(sec.getRecordVersion() ); 549 } 550 else { if( immToDeff != null ){ 552 String Cname; 553 for( int j=0; j<immToDeff.size(); j++ ){ 554 Cname = (String ) immToDeff.get(j); 555 if( primaryConstraintStore.constraintName.equalsIgnoreCase(Cname) ) 556 fireConstraintsForPrimary( sec.getRecordVersion() ); 557 } 558 } 559 } 560 } 561 else { ConstraintInformation constraintInformation = getConstraintInformation(sec.getUserSession()) ; 563 ArrayList deffToImm = constraintInformation.getDefferedToImmediateConstraints( tableName ); 564 if(sec.getOperationTypeSameOrOpposite()){ if(deffToImm == null) 566 fireConstraintsForPrimary(sec.getRecordVersion() ); 567 else{ 568 if(!deffToImm.contains(primaryConstraintStore.constraintName)) 569 fireConstraintsForPrimary(sec.getRecordVersion() ); 570 } 571 } 572 else{ if(deffToImm!=null){ 574 String cname = null; 575 for (int i = 0; i < deffToImm.size(); i++) { 576 cname = (String )deffToImm.get(i); 577 if(primaryConstraintStore.constraintName.equalsIgnoreCase(cname)) 578 fireConstraintsForPrimary(sec.getRecordVersion() ); 579 } 580 } 581 } 582 } 583 } 584 585 586 587 588 } 589 | Popular Tags |