1 21 22 package org.apache.derby.iapi.sql.dictionary; 23 24 import org.apache.derby.iapi.services.io.Formatable; 25 import org.apache.derby.iapi.sql.depend.Dependent; 26 import org.apache.derby.iapi.sql.depend.Provider; 27 import org.apache.derby.iapi.error.StandardException; 28 import org.apache.derby.catalog.UUID; 29 import java.sql.Timestamp ; 30 31 import org.apache.derby.iapi.reference.SQLState; 32 import org.apache.derby.iapi.services.sanity.SanityManager; 33 import org.apache.derby.iapi.sql.StatementType; 34 import org.apache.derby.catalog.DependableFinder; 35 import org.apache.derby.catalog.Dependable; 36 import org.apache.derby.iapi.services.io.StoredFormatIds; 37 import org.apache.derby.iapi.sql.depend.DependencyManager; 38 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 39 import org.apache.derby.iapi.services.context.ContextService; 40 41 import org.apache.derby.impl.sql.execute.DropTriggerConstantAction; 42 43 import java.io.ObjectOutput ; 44 import java.io.ObjectInput ; 45 import java.io.IOException ; 46 47 88 public class TriggerDescriptor extends TupleDescriptor 89 implements UniqueSQLObjectDescriptor, Provider, Dependent, Formatable 90 { 91 public static final int SYSTRIGGERS_STATE_FIELD = 8; 93 94 public static final int TRIGGER_EVENT_UPDATE = 1; 95 public static final int TRIGGER_EVENT_DELETE = 2; 96 public static final int TRIGGER_EVENT_INSERT = 4; 97 98 99 private UUID id; 100 private String name; 101 private String oldReferencingName; 102 private String newReferencingName; 103 private String triggerDefinition; 104 private SchemaDescriptor sd; 105 private int eventMask; 106 private boolean isBefore; 107 private boolean isRow; 108 private boolean referencingOld; 109 private boolean referencingNew; 110 private TableDescriptor td; 111 private UUID actionSPSId; 112 private SPSDescriptor actionSPS; 113 private UUID whenSPSId; 114 private SPSDescriptor whenSPS; 115 private boolean isEnabled; 116 private int[] referencedCols; 117 private Timestamp creationTimestamp; 118 private UUID triggerSchemaId; 119 private UUID triggerTableId; 120 121 122 125 public TriggerDescriptor() {} 126 127 149 public TriggerDescriptor 150 ( 151 DataDictionary dataDictionary, 152 SchemaDescriptor sd, 153 UUID id, 154 String name, 155 int eventMask, 156 boolean isBefore, 157 boolean isRow, 158 boolean isEnabled, 159 TableDescriptor td, 160 UUID whenSPSId, 161 UUID actionSPSId, 162 Timestamp creationTimestamp, 163 int[] referencedCols, 164 String triggerDefinition, 165 boolean referencingOld, 166 boolean referencingNew, 167 String oldReferencingName, 168 String newReferencingName 169 ) 170 { 171 super(dataDictionary); 172 this.id = id; 173 this.sd = sd; 174 this.name = name; 175 this.eventMask = eventMask; 176 this.isBefore = isBefore; 177 this.isRow = isRow; 178 this.td = td; 179 this.actionSPSId = actionSPSId; 180 this.whenSPSId = whenSPSId; 181 this.isEnabled = isEnabled; 182 this.referencedCols = referencedCols; 183 this.creationTimestamp = creationTimestamp; 184 this.triggerDefinition = triggerDefinition; 185 this.referencingOld = referencingOld; 186 this.referencingNew = referencingNew; 187 this.oldReferencingName = oldReferencingName; 188 this.newReferencingName = newReferencingName; 189 triggerSchemaId = sd.getUUID(); 190 triggerTableId = td.getUUID(); 191 } 192 193 194 199 public UUID getUUID() 200 { 201 return id; 202 } 203 204 209 public String getName() 210 { 211 return name; 212 } 213 214 public UUID getTableId() { 215 return triggerTableId; 216 } 217 218 225 public SchemaDescriptor getSchemaDescriptor() 226 throws StandardException 227 { 228 if (sd == null) 229 { 230 sd = getDataDictionary().getSchemaDescriptor(triggerSchemaId, null); 231 } 232 return sd; 233 } 234 235 243 public boolean listensForEvent(int event) 244 { 245 return (event & eventMask) == event; 246 } 247 248 249 256 public int getTriggerEventMask() 257 { 258 return eventMask; 259 } 260 261 266 public Timestamp getCreationTimestamp() 267 { 268 return creationTimestamp; 269 } 270 271 276 public boolean isBeforeTrigger() 277 { 278 return isBefore; 279 } 280 281 286 public boolean isRowTrigger() 287 { 288 return isRow; 289 } 290 291 292 297 public UUID getActionId() 298 { 299 return actionSPSId; 300 } 301 302 309 public SPSDescriptor getActionSPS(LanguageConnectionContext lcc) 310 throws StandardException 311 { 312 if (actionSPS == null) 313 { 314 lcc.beginNestedTransaction(true); 320 actionSPS = getDataDictionary().getSPSDescriptor(actionSPSId); 321 lcc.commitNestedTransaction(); 322 } 323 return actionSPS; 324 } 325 326 331 public UUID getWhenClauseId() 332 { 333 return whenSPSId; 334 } 335 336 343 public SPSDescriptor getWhenClauseSPS() 344 throws StandardException 345 { 346 if (whenSPS == null) 347 { 348 whenSPS = getDataDictionary().getSPSDescriptor(whenSPSId); 349 } 350 return whenSPS; 351 } 352 353 361 public TableDescriptor getTableDescriptor() 362 throws StandardException 363 { 364 if (td == null) 365 { 366 td = getDataDictionary().getTableDescriptor(triggerTableId); 367 } 368 return td; 369 } 370 371 378 387 393 public int[] getReferencedCols() 394 { 395 return referencedCols; 396 } 397 398 403 public boolean isEnabled() 404 { 405 return isEnabled; 406 } 407 408 412 public void setEnabled() 413 { 414 isEnabled = true; 415 } 416 417 421 public void setDisabled() 422 { 423 isEnabled = false; 424 } 425 426 438 public boolean needsToFire(int stmtType, int[] modifiedCols) 439 throws StandardException 440 { 441 442 if (SanityManager.DEBUG) 443 { 444 if (!((stmtType == StatementType.INSERT) || 445 (stmtType == StatementType.BULK_INSERT_REPLACE) || 446 (stmtType == StatementType.UPDATE) || 447 (stmtType == StatementType.DELETE))) 448 { 449 SanityManager.THROWASSERT("invalid statement type "+stmtType); 450 } 451 } 452 453 456 if (!isEnabled) 457 { 458 return false; 459 } 460 461 if (stmtType == StatementType.INSERT) 462 { 463 return (eventMask & TRIGGER_EVENT_INSERT) == eventMask; 464 } 465 if (stmtType == StatementType.DELETE) 466 { 467 return (eventMask & TRIGGER_EVENT_DELETE) == eventMask; 468 } 469 470 if (stmtType == StatementType.BULK_INSERT_REPLACE) 473 { 474 throw StandardException.newException(SQLState.LANG_NO_BULK_INSERT_REPLACE_WITH_TRIGGER, 475 getTableDescriptor().getQualifiedName(), name); 476 } 477 478 return ((eventMask & TRIGGER_EVENT_UPDATE) == eventMask) && 480 ConstraintDescriptor.doColumnsIntersect(modifiedCols, referencedCols); 481 } 482 483 488 public String getTriggerDefinition() 489 { 490 return triggerDefinition; 491 } 492 493 500 public boolean getReferencingOld() 501 { 502 return referencingOld; 503 } 504 505 512 public boolean getReferencingNew() 513 { 514 return referencingNew; 515 } 516 517 524 public String getOldReferencingName() 525 { 526 return oldReferencingName; 527 } 528 529 536 public String getNewReferencingName() 537 { 538 return newReferencingName; 539 } 540 541 public String toString() 542 { 543 if (SanityManager.DEBUG) 544 { 545 return "TRIGGER: "+name; 546 } 547 else 548 { 549 return ""; 550 } 551 } 552 553 559 564 public DependableFinder getDependableFinder() 565 { 566 return getDependableFinder(StoredFormatIds.TRIGGER_DESCRIPTOR_FINDER_V01_ID); 567 } 568 569 574 public String getObjectName() 575 { 576 return name; 577 } 578 579 584 public UUID getObjectID() 585 { 586 return id; 587 } 588 589 594 public String getClassType() 595 { 596 return Dependable.TRIGGER; 597 } 598 599 613 public synchronized boolean isValid() 614 { 615 return true; 616 } 617 618 628 public void prepareToInvalidate 629 ( 630 Provider p, 631 int action, 632 LanguageConnectionContext lcc 633 ) throws StandardException 634 { 635 switch (action) 636 { 637 653 case DependencyManager.DROP_TABLE: 654 case DependencyManager.DROP_SYNONYM: 655 case DependencyManager.DROP_SPS: 656 case DependencyManager.RENAME: 657 case DependencyManager.REVOKE_PRIVILEGE_RESTRICT: 658 DependencyManager dm = getDataDictionary().getDependencyManager(); 659 throw StandardException.newException(SQLState.LANG_PROVIDER_HAS_DEPENDENT_OBJECT, 660 dm.getActionString(action), 661 p.getObjectName(), "TRIGGER", name); 662 663 671 default: 672 break; 673 } 674 } 675 676 686 public void makeInvalid(int action, LanguageConnectionContext lcc) throws StandardException 687 { 688 693 switch (action) 694 { 695 case DependencyManager.USER_RECOMPILE_REQUEST: 697 DependencyManager dm = getDataDictionary().getDependencyManager(); 698 dm.invalidateFor(this, DependencyManager.PREPARED_STATEMENT_RELEASE, lcc); 699 break; 700 701 case DependencyManager.REVOKE_PRIVILEGE: 705 DropTriggerConstantAction.dropTriggerDescriptor( 706 lcc, getDataDictionary().getDependencyManager(), 707 getDataDictionary(), lcc.getTransactionExecute(), this, 708 null); 709 break; 710 711 default: 712 break; 713 } 714 715 } 716 717 723 public void makeValid(LanguageConnectionContext lcc) 724 { 725 } 726 727 728 734 742 public void readExternal(ObjectInput in) 743 throws IOException , ClassNotFoundException 744 { 745 id = (UUID)in.readObject(); 746 name = (String )in.readObject(); 747 triggerSchemaId = (UUID)in.readObject(); 748 triggerTableId = (UUID)in.readObject(); 749 eventMask = in.readInt(); 750 isBefore = in.readBoolean(); 751 isRow = in.readBoolean(); 752 isEnabled = in.readBoolean(); 753 whenSPSId = (UUID)in.readObject(); 754 actionSPSId = (UUID)in.readObject(); 755 int length = in.readInt(); 756 if (length != 0) 757 { 758 referencedCols = new int[length]; 759 for (int i = 0; i < length; i++) 760 { 761 referencedCols[i] = in.readInt(); 762 } 763 } 764 triggerDefinition = (String )in.readObject(); 765 referencingOld = in.readBoolean(); 766 referencingNew = in.readBoolean(); 767 oldReferencingName = (String )in.readObject(); 768 newReferencingName = (String )in.readObject(); 769 770 } 771 772 protected DataDictionary getDataDictionary() throws StandardException 773 { 774 779 DataDictionary dd = super.getDataDictionary(); 780 if (dd == null) 781 { 782 LanguageConnectionContext lcc = (LanguageConnectionContext) 783 ContextService.getContext(LanguageConnectionContext.CONTEXT_ID); 784 dd = lcc.getDataDictionary(); 785 setDataDictionary(dd); 786 } 787 return dd; 788 } 789 790 797 public void writeExternal( ObjectOutput out ) 798 throws IOException 799 { 800 if (SanityManager.DEBUG) 801 { 802 SanityManager.ASSERT(triggerSchemaId != null, 803 "triggerSchemaId expected to be non-null"); 804 SanityManager.ASSERT(triggerTableId != null, 805 "triggerTableId expected to be non-null"); 806 } 807 out.writeObject(id); 808 out.writeObject(name); 809 out.writeObject(triggerSchemaId); 810 out.writeObject(triggerTableId); 811 out.writeInt(eventMask); 812 out.writeBoolean(isBefore); 813 out.writeBoolean(isRow); 814 out.writeBoolean(isEnabled); 815 out.writeObject(whenSPSId); 816 out.writeObject(actionSPSId); 817 if (referencedCols == null) 818 { 819 out.writeInt(0); 820 } 821 else 822 { 823 out.writeInt(referencedCols.length); 824 for (int i = 0; i < referencedCols.length; i++) 825 { 826 out.writeInt(referencedCols[i]); 827 } 828 } 829 out.writeObject(triggerDefinition); 830 out.writeBoolean(referencingOld); 831 out.writeBoolean(referencingNew); 832 out.writeObject(oldReferencingName); 833 out.writeObject(newReferencingName); 834 } 835 836 841 public int getTypeFormatId() { return StoredFormatIds.TRIGGER_DESCRIPTOR_V01_ID; } 842 843 844 public String getDescriptorType() 845 { 846 return "Trigger"; 847 } 848 849 850 public String getDescriptorName() { return name; } 851 852 } 853 854 | Popular Tags |