Class TriggerDescriptor

All Implemented Interfaces:
Externalizable, Serializable, Dependable, Formatable, TypedFormat, Dependent, Provider

public class TriggerDescriptor extends UniqueSQLObjectDescriptor implements Provider, Dependent, Formatable
A trigger.

We are dependent on TableDescriptors, SPSDescriptors (for our WHEN clause and our action). Note that we don't strictly need to be dependent on out SPSes because we could just disallow anyone from dropping an sps of type 'T', but to keep dependencies uniform, we'll do be dependent.

We are a provider for DML (PreparedStatements or SPSes) The public methods for this class are:

  1. getUUID
  2. getName
  3. getSchemaDescriptor
  4. public boolean listensForEvent(int event);
  5. public int getTriggerEventMask();
  6. public Timestamp getCreationTimestamp();
  7. public boolean isBeforeTrigger();
  8. public boolean isRowTrigger();
  9. public UUID getActionId();
  10. public SPSDescriptor getActionSPS();
  11. public UUID getWhenClauseId();
  12. public SPSDescriptor getWhenClauseSPS()
  13. public String getWhenClauseText();
  14. public TableDescriptor getTableDescriptor()
  15. public ReferencedColumns getReferencedColumnsDescriptor()
  16. public int[] getReferencedCols();
  17. public int[] getReferencedColsInTriggerAction();
  18. public boolean enforced();
  19. public void setEnabled();
  20. public void setDisabled();
  21. public boolean needsToFire(int stmtType, int[] modifiedCols)
  22. public String getTriggerDefinition();
  23. public boolean getReferencingOld();
  24. public boolean getReferencingNew();
  25. public String getOldReferencingName();
  26. public String getNewReferencingName();
See Also:
  • Field Details

    • SYSTRIGGERS_STATE_FIELD

      public static final int SYSTRIGGERS_STATE_FIELD
      See Also:
    • TRIGGER_EVENT_UPDATE

      public static final int TRIGGER_EVENT_UPDATE
      See Also:
    • TRIGGER_EVENT_DELETE

      public static final int TRIGGER_EVENT_DELETE
      See Also:
    • TRIGGER_EVENT_INSERT

      public static final int TRIGGER_EVENT_INSERT
      See Also:
    • id

      private UUID id
    • name

      private String name
    • oldReferencingName

      private String oldReferencingName
    • newReferencingName

      private String newReferencingName
    • triggerDefinition

      private String triggerDefinition
    • sd

      private SchemaDescriptor sd
    • eventMask

      private int eventMask
    • isBefore

      private boolean isBefore
    • isRow

      private boolean isRow
    • referencingOld

      private boolean referencingOld
    • referencingNew

      private boolean referencingNew
    • td

      private TableDescriptor td
    • actionSPSId

      private UUID actionSPSId
    • actionSPS

      private SPSDescriptor actionSPS
    • whenSPSId

      private UUID whenSPSId
    • whenSPS

      private SPSDescriptor whenSPS
    • isEnabled

      private boolean isEnabled
    • referencedCols

      private int[] referencedCols
    • referencedColsInTriggerAction

      private int[] referencedColsInTriggerAction
    • creationTimestamp

      private Timestamp creationTimestamp
    • triggerSchemaId

      private UUID triggerSchemaId
    • triggerTableId

      private UUID triggerTableId
    • whenClauseText

      private String whenClauseText
  • Constructor Details

    • TriggerDescriptor

      public TriggerDescriptor()
      Niladic constructor, for formatable
    • TriggerDescriptor

      TriggerDescriptor(DataDictionary dataDictionary, SchemaDescriptor sd, UUID id, String name, int eventMask, boolean isBefore, boolean isRow, boolean isEnabled, TableDescriptor td, UUID whenSPSId, UUID actionSPSId, Timestamp creationTimestamp, int[] referencedCols, int[] referencedColsInTriggerAction, String triggerDefinition, boolean referencingOld, boolean referencingNew, String oldReferencingName, String newReferencingName, String whenClauseText)
      Constructor. Used when creating a trigger from SYS.SYSTRIGGERS
      Parameters:
      dataDictionary - the data dictionary
      sd - the schema descriptor for this trigger
      id - the trigger id
      name - the trigger name
      eventMask - TriggerDescriptor.TRIGGER_EVENT_XXXX
      isBefore - is this a before (as opposed to after) trigger
      isRow - is this a row trigger or statement trigger
      isEnabled - is this trigger enabled or disabled
      td - the table upon which this trigger is defined
      whenSPSId - the sps id for the when clause (may be null)
      actionSPSId - the spsid for the trigger action (may be null)
      creationTimestamp - when was this trigger created?
      referencedCols - what columns does this trigger reference (may be null)
      referencedColsInTriggerAction - what columns does the trigger action reference through old/new transition variables (may be null)
      triggerDefinition - The original user text of the trigger action
      referencingOld - whether or not OLD appears in REFERENCING clause
      referencingNew - whether or not NEW appears in REFERENCING clause
      oldReferencingName - old referencing table name, if any, that appears in REFERCING clause
      newReferencingName - new referencing table name, if any, that appears in REFERCING clause
      whenClauseText - the SQL text of the WHEN clause, or null if there is no WHEN clause
  • Method Details

    • getUUID

      public UUID getUUID()
      Get the trigger UUID
      Specified by:
      getUUID in class UniqueTupleDescriptor
      Returns:
      the id
    • getName

      public String getName()
      Get the trigger name
      Specified by:
      getName in class UniqueSQLObjectDescriptor
      Returns:
      the name
    • getTableId

      public UUID getTableId()
    • getSchemaDescriptor

      public SchemaDescriptor getSchemaDescriptor() throws StandardException
      Get the triggers schema descriptor
      Specified by:
      getSchemaDescriptor in class UniqueSQLObjectDescriptor
      Returns:
      the schema descriptor
      Throws:
      StandardException - on error
    • listensForEvent

      public boolean listensForEvent(int event)
      Indicate whether this trigger listens for this type of event.
      Parameters:
      event - TRIGGER_EVENT_XXXX
      Returns:
      true if it listens to the specified event.
    • getTriggerEventMask

      public int getTriggerEventMask()
      Get the trigger event mask. Currently, a trigger may only listen for a single event, though it may OR multiple events in the future.
      Returns:
      the trigger event mask
    • getCreationTimestamp

      public Timestamp getCreationTimestamp()
      Get the time that this trigger was created.
      Returns:
      the time the trigger was created
    • isBeforeTrigger

      public boolean isBeforeTrigger()
      Is this a before trigger
      Returns:
      true if it is a before trigger
    • isRowTrigger

      public boolean isRowTrigger()
      Is this a row trigger
      Returns:
      true if it is a before trigger
    • getActionId

      public UUID getActionId()
      Get the trigger action sps UUID
      Returns:
      the uuid of the sps action
    • getActionSPS

      public SPSDescriptor getActionSPS(LanguageConnectionContext lcc) throws StandardException
      Get the trigger action sps from SYSSTATEMENTS. If we find that the sps is invalid and the trigger is defined at row level and it has OLD/NEW transient variables through REFERENCES clause, then the sps from SYSSTATEMENTS may not be valid anymore. In such a case, we regenerate the trigger action sql and use that for the sps and update SYSSTATEMENTS using this new sps. This update of SYSSTATEMENTS was introduced with DERBY-4874
      Parameters:
      lcc - The LanguageConnectionContext to use.
      Returns:
      the trigger action sps
      Throws:
      StandardException - on error
    • getSPS

      private SPSDescriptor getSPS(LanguageConnectionContext lcc, boolean isWhenClause) throws StandardException
      Get the SPS for the triggered SQL statement or the WHEN clause.
      Parameters:
      lcc - the LanguageConnectionContext to use
      isWhenClause - true if the SPS for the WHEN clause is requested, false if it is the triggered SQL statement
      Returns:
      the requested SPS
      Throws:
      StandardException - if an error occurs
    • getWhenClauseId

      public UUID getWhenClauseId()
      Get the trigger when clause sps UUID
      Returns:
      the uuid of the sps action
    • getWhenClauseText

      public String getWhenClauseText()
      Get the SQL text of the WHEN clause.
      Returns:
      SQL text for the WHEN clause, or null if there is no WHEN clause
    • getWhenClauseSPS

      public SPSDescriptor getWhenClauseSPS(LanguageConnectionContext lcc) throws StandardException
      Get the trigger when clause sps
      Parameters:
      lcc - the LanguageConnectionContext to use
      Returns:
      the sps of the when clause
      Throws:
      StandardException - on error
    • getTableDescriptor

      public TableDescriptor getTableDescriptor() throws StandardException
      Get the trigger table descriptor
      Returns:
      the table descripor upon which this trigger is declared
      Throws:
      StandardException - on error
    • getReferencedCols

      public int[] getReferencedCols()
      Get the referenced column array for this trigger, used in "alter table drop column", we get the handle and change it
      Returns:
      the referenced column array
    • setReferencedCols

      public void setReferencedCols(int[] newCols)
      Update the array of referenced columns
    • getReferencedColsInTriggerAction

      public int[] getReferencedColsInTriggerAction()
      Get the referenced column array for the trigger action columns.
      Returns:
      the referenced column array
    • setReferencedColsInTriggerAction

      public void setReferencedColsInTriggerAction(int[] referencedColsInTriggerAction)
      Set the referenced column array for trigger actions
    • isEnabled

      public boolean isEnabled()
      Is this trigger enforced
      Returns:
      true if it is enforced
    • setEnabled

      public void setEnabled()
      Mark this trigger as enforced
    • setDisabled

      public void setDisabled()
      Mark this trigger as disabled
    • needsToFire

      public boolean needsToFire(int stmtType, int[] modifiedCols) throws StandardException
      Does this trigger need to fire on this type of DML?
      Parameters:
      stmtType - the type of DML (StatementType.INSERT|StatementType.UPDATE|StatementType.DELETE)
      modifiedCols - the columns modified, or null for all
      Returns:
      true/false
      Throws:
      StandardException - on error
    • getTriggerDefinition

      public String getTriggerDefinition()
      Get the original trigger definition.
      Returns:
      The trigger definition.
    • getReferencingOld

      public boolean getReferencingOld()
      Get whether or not OLD was replaced in the REFERENCING clause.
      Returns:
      Whether or not OLD was replaced in the REFERENCING clause.
    • getReferencingNew

      public boolean getReferencingNew()
      Get whether or not NEW was replaced in the REFERENCING clause.
      Returns:
      Whether or not NEW was replaced in the REFERENCING clause.
    • getOldReferencingName

      public String getOldReferencingName()
      Get the old Referencing name, if any, from the REFERENCING clause.
      Returns:
      The old Referencing name, if any, from the REFERENCING clause.
    • getNewReferencingName

      public String getNewReferencingName()
      Get the new Referencing name, if any, from the REFERENCING clause.
      Returns:
      The new Referencing name, if any, from the REFERENCING clause.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getDependableFinder

      public DependableFinder getDependableFinder()
      Description copied from interface: Dependable
      Get an object which can be written to disk and which, when read from disk, will find or reconstruct this in-memory Dependable.
      Specified by:
      getDependableFinder in interface Dependable
      Returns:
      the stored form of this provider
      See Also:
    • getObjectName

      public String getObjectName()
      Return the name of this Provider. (Useful for errors.)
      Specified by:
      getObjectName in interface Dependable
      Returns:
      String The name of this provider.
    • getObjectID

      public UUID getObjectID()
      Get the provider's UUID
      Specified by:
      getObjectID in interface Dependable
      Returns:
      The provider's UUID
    • getClassType

      public String getClassType()
      Get the provider's type.
      Specified by:
      getClassType in interface Dependable
      Returns:
      char The provider's type.
    • isValid

      public boolean isValid()
      Check that all of the dependent's dependencies are valid.
      Specified by:
      isValid in interface Dependent
      Returns:
      true if the dependent is currently valid
    • prepareToInvalidate

      public void prepareToInvalidate(Provider p, int action, LanguageConnectionContext lcc) throws StandardException
      Prepare to mark the dependent as invalid (due to at least one of its dependencies being invalid).
      Specified by:
      prepareToInvalidate in interface Dependent
      Parameters:
      p - the provider
      action - The action causing the invalidation
      lcc - the language connection context
      Throws:
      StandardException - thrown if unable to make it invalid
    • makeInvalid

      public void makeInvalid(int action, LanguageConnectionContext lcc) throws StandardException
      Mark the dependent as invalid (due to at least one of its dependencies being invalid). Always an error for a trigger -- should never have gotten here.
      Specified by:
      makeInvalid in interface Dependent
      Parameters:
      action - The action causing the invalidation
      lcc - the language connection context
      Throws:
      StandardException - thrown if called in sanity mode
    • drop

      public void drop(LanguageConnectionContext lcc) throws StandardException
      Throws:
      StandardException
    • readExternal

      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
      Read this object from a stream of stored objects.
      Specified by:
      readExternal in interface Externalizable
      Parameters:
      in - read this.
      Throws:
      IOException - thrown on error
      ClassNotFoundException - thrown on error
    • getDataDictionary

      protected DataDictionary getDataDictionary()
      Overrides:
      getDataDictionary in class TupleDescriptor
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      Write this object to a stream of stored objects.
      Specified by:
      writeExternal in interface Externalizable
      Parameters:
      out - write bytes here.
      Throws:
      IOException - thrown on error
    • getTypeFormatId

      public int getTypeFormatId()
      Get the formatID which corresponds to this class.
      Specified by:
      getTypeFormatId in interface TypedFormat
      Returns:
      the formatID of this class
    • getDescriptorType

      public String getDescriptorType()
      Description copied from class: TupleDescriptor
      Each descriptor must identify itself with its type; i.e index, check constraint whatever.
      Overrides:
      getDescriptorType in class TupleDescriptor
      See Also:
    • getDescriptorName

      public String getDescriptorName()
      Overrides:
      getDescriptorName in class TupleDescriptor
      See Also:
    • getContext

      private static Context getContext(String contextID)
      Privileged lookup of a Context. Must be private so that user code can't call this entry point.