1 21 22 package org.apache.derby.iapi.sql.dictionary; 23 24 import org.apache.derby.iapi.error.StandardException; 25 26 import org.apache.derby.iapi.sql.depend.Provider; 27 import org.apache.derby.iapi.sql.depend.Dependent; 28 29 import org.apache.derby.catalog.UUID; 30 31 import org.apache.derby.iapi.reference.SQLState; 32 import org.apache.derby.iapi.services.sanity.SanityManager; 33 import org.apache.derby.catalog.DependableFinder; 34 import org.apache.derby.catalog.Dependable; 35 import org.apache.derby.iapi.services.io.StoredFormatIds; 36 import org.apache.derby.iapi.sql.depend.DependencyManager; 37 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 38 39 import org.apache.derby.impl.sql.execute.DropConstraintConstantAction; 40 41 49 50 public abstract class ConstraintDescriptor 51 extends TupleDescriptor 52 implements UniqueTupleDescriptor, Provider, Dependent 53 { 54 public static final int ENABLED = 1; 57 public static final int DISABLED = 2; 58 public static final int ALL = 3; 59 60 public static final int SYSCONSTRAINTS_STATE_FIELD = 6; 62 63 TableDescriptor table; 64 final String constraintName; 65 private final boolean deferrable; 66 private final boolean initiallyDeferred; 67 boolean isEnabled; 68 private final int[] referencedColumns; 69 final UUID constraintId; 70 private final SchemaDescriptor schemaDesc; 71 private ColumnDescriptorList colDL; 72 73 85 86 ConstraintDescriptor( 87 DataDictionary dataDictionary, 88 TableDescriptor table, 89 String constraintName, 90 boolean deferrable, 91 boolean initiallyDeferred, 92 int[] referencedColumns, 93 UUID constraintId, 94 SchemaDescriptor schemaDesc, 95 boolean isEnabled 96 ) 97 { 98 super( dataDictionary ); 99 100 this.table = table; 101 this.constraintName = constraintName; 102 this.deferrable = deferrable; 103 this.initiallyDeferred = initiallyDeferred; 104 this.referencedColumns = referencedColumns; 105 this.constraintId = constraintId; 106 this.schemaDesc = schemaDesc; 107 this.isEnabled = isEnabled; 108 } 109 110 111 116 public UUID getTableId() 117 { 118 return table.getUUID(); 119 } 120 121 126 public UUID getUUID() 127 { 128 return constraintId; 129 } 130 131 136 public String getConstraintName() 137 { 138 return constraintName; 139 } 140 141 148 public abstract int getConstraintType(); 149 150 public abstract UUID getConglomerateId(); 151 152 157 public String getConstraintText() 158 { 159 return null; 160 } 161 162 169 public boolean deferrable() 170 { 171 return deferrable; 172 } 173 174 182 public boolean initiallyDeferred() 183 { 184 return initiallyDeferred; 185 } 186 187 203 public int[] getReferencedColumns() 204 { 205 return referencedColumns; 206 } 207 208 213 public abstract boolean hasBackingIndex(); 214 215 221 public SchemaDescriptor getSchemaDescriptor() 222 { 223 return schemaDesc; 224 } 225 226 235 public int[] getKeyColumns() 236 { 237 return getReferencedColumns(); 238 } 239 240 245 public boolean isEnabled() 246 { 247 return isEnabled; 248 } 249 250 254 public void setEnabled() 255 { 256 isEnabled = true; 257 } 258 259 263 public void setDisabled() 264 { 265 isEnabled = false; 266 } 267 268 274 public boolean isReferenced() 275 { 276 return false; 277 } 278 279 286 public int getReferenceCount() 287 { 288 return 0; 289 } 290 291 301 public abstract boolean needsToFire(int stmtType, int[] modifiedCols); 302 303 309 public TableDescriptor getTableDescriptor() 310 { 311 return table; 312 } 313 314 322 public ColumnDescriptorList getColumnDescriptors() 323 throws StandardException 324 { 325 if (colDL == null) 326 { 327 DataDictionary dd = getDataDictionary(); 328 colDL = new ColumnDescriptorList(); 329 330 int[] refCols = getReferencedColumns(); 331 for (int i = 0; i < refCols.length; i++) 332 { 333 colDL.add(table.getColumnDescriptor(refCols[i])); 334 } 335 } 336 return colDL; 337 } 338 339 351 public boolean areColumnsComparable(ColumnDescriptorList otherColumns) 352 throws StandardException 353 { 354 ColumnDescriptor myColumn; 355 ColumnDescriptor otherColumn; 356 357 ColumnDescriptorList myColDl = getColumnDescriptors(); 358 359 362 if (otherColumns.size() != myColDl.size()) 363 { 364 return false; 365 } 366 367 int mySize = myColDl.size(); 368 int otherSize = otherColumns.size(); 369 int index; 370 for (index = 0; index < mySize && index < otherSize; index++) 371 { 372 myColumn = (ColumnDescriptor) myColDl.elementAt(index); 373 otherColumn = (ColumnDescriptor) otherColumns.elementAt(index); 374 375 380 if (!(myColumn.getType()).isExactTypeAndLengthMatch( 381 (otherColumn.getType()))) 382 { 383 break; 384 } 385 } 386 387 return (index == mySize && index == otherSize); 388 } 389 390 396 public boolean columnIntersects(int columnArray[]) 397 { 398 return doColumnsIntersect(getReferencedColumns(), columnArray); 400 } 401 402 413 static boolean doColumnsIntersect(int[] otherColumns, int[] referencedColumns) 414 { 415 421 if ((otherColumns == null) || (referencedColumns == null)) 422 { 423 return true; 424 } 425 426 for (int outer = 0; outer < referencedColumns.length; outer++) 427 { 428 for (int inner = 0; inner < otherColumns.length; inner++) 429 { 430 if (referencedColumns[outer] == otherColumns[inner]) 431 { 432 return true; 433 } 434 } 435 } 436 return false; 437 } 438 439 444 445 public String toString() 446 { 447 if (SanityManager.DEBUG) 448 { 449 String tableDesc = 450 "table: " + 451 table.getQualifiedName() + "(" + 452 table.getUUID()+","+ 453 table.getTableType()+")"; 454 455 return tableDesc + "\n"+ 456 "constraintName: " + constraintName + "\n" + 457 "constraintId: " + constraintId + "\n" + 458 "deferrable: " + deferrable + "\n" + 459 "initiallyDeferred: " + initiallyDeferred + "\n" + 460 "referencedColumns: " + referencedColumns + "\n" + 461 "schemaDesc: " + schemaDesc + "\n" 462 ; 463 } 464 else 465 { 466 return ""; 467 } 468 } 469 470 476 481 public DependableFinder getDependableFinder() 482 { 483 return getDependableFinder(StoredFormatIds.CONSTRAINT_DESCRIPTOR_FINDER_V01_ID); 484 } 485 486 491 public String getObjectName() 492 { 493 return constraintName; 494 } 495 496 501 public UUID getObjectID() 502 { 503 return constraintId; 504 } 505 506 511 public String getClassType() 512 { 513 return Dependable.CONSTRAINT; 514 } 515 516 526 public synchronized boolean isValid() 527 { 528 return true; 529 } 530 531 540 public void prepareToInvalidate(Provider p, int action, 541 LanguageConnectionContext lcc) 542 throws StandardException 543 { 544 DependencyManager dm = getDataDictionary().getDependencyManager(); 545 546 switch (action) 547 { 548 553 case DependencyManager.SET_CONSTRAINTS_ENABLE: 554 case DependencyManager.SET_CONSTRAINTS_DISABLE: 555 case DependencyManager.SET_TRIGGERS_ENABLE: 556 case DependencyManager.SET_TRIGGERS_DISABLE: 557 case DependencyManager.REVOKE_PRIVILEGE: 562 break; 563 564 568 default: 581 throw StandardException.newException(SQLState.LANG_PROVIDER_HAS_DEPENDENT_OBJECT, 582 dm.getActionString(action), 583 p.getObjectName(), "CONSTRAINT", constraintName); 584 } 585 } 586 587 596 public void makeInvalid(int action, LanguageConnectionContext lcc) 597 throws StandardException 598 { 599 603 604 if (action == DependencyManager.REVOKE_PRIVILEGE) 606 { 607 DropConstraintConstantAction.dropConstraintAndIndex( 612 getDataDictionary().getDependencyManager(), 613 table, 614 getDataDictionary(), 615 this, 616 lcc.getTransactionExecute(), 617 lcc, true); 618 return; 619 } 620 621 624 if ((action != DependencyManager.SET_CONSTRAINTS_DISABLE) && 625 (action != DependencyManager.SET_CONSTRAINTS_ENABLE) && 626 (action != DependencyManager.SET_TRIGGERS_ENABLE) && 627 (action != DependencyManager.SET_TRIGGERS_DISABLE) 628 ) 629 { 630 634 if (SanityManager.DEBUG) 635 { 636 DependencyManager dm; 637 638 dm = getDataDictionary().getDependencyManager(); 639 640 SanityManager.THROWASSERT("makeInvalid("+ 641 dm.getActionString(action)+ 642 ") not expected to get called"); 643 } 644 } 645 } 646 647 651 public void makeValid(LanguageConnectionContext lcc) 652 { 653 } 654 655 656 public String getDescriptorName() { return constraintName; } 657 658 public String getDescriptorType() { return "Constraint"; } 659 } 660 | Popular Tags |