Class TriggerDescriptor

  • All Implemented Interfaces:
    java.io.Externalizable, java.io.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:
    Serialized Form
    • Field Detail

      • SYSTRIGGERS_STATE_FIELD

        public static final int SYSTRIGGERS_STATE_FIELD
        See Also:
        Constant Field Values
      • id

        private UUID id
      • name

        private java.lang.String name
      • oldReferencingName

        private java.lang.String oldReferencingName
      • newReferencingName

        private java.lang.String newReferencingName
      • triggerDefinition

        private java.lang.String triggerDefinition
      • eventMask

        private int eventMask
      • isBefore

        private boolean isBefore
      • isRow

        private boolean isRow
      • referencingOld

        private boolean referencingOld
      • referencingNew

        private boolean referencingNew
      • actionSPSId

        private UUID actionSPSId
      • whenSPSId

        private UUID whenSPSId
      • isEnabled

        private boolean isEnabled
      • referencedCols

        private int[] referencedCols
      • referencedColsInTriggerAction

        private int[] referencedColsInTriggerAction
      • creationTimestamp

        private java.sql.Timestamp creationTimestamp
      • triggerSchemaId

        private UUID triggerSchemaId
      • triggerTableId

        private UUID triggerTableId
      • whenClauseText

        private java.lang.String whenClauseText
    • Constructor Detail

      • TriggerDescriptor

        public TriggerDescriptor()
        Niladic constructor, for formatable
      • TriggerDescriptor

        TriggerDescriptor​(DataDictionary dataDictionary,
                          SchemaDescriptor sd,
                          UUID id,
                          java.lang.String name,
                          int eventMask,
                          boolean isBefore,
                          boolean isRow,
                          boolean isEnabled,
                          TableDescriptor td,
                          UUID whenSPSId,
                          UUID actionSPSId,
                          java.sql.Timestamp creationTimestamp,
                          int[] referencedCols,
                          int[] referencedColsInTriggerAction,
                          java.lang.String triggerDefinition,
                          boolean referencingOld,
                          boolean referencingNew,
                          java.lang.String oldReferencingName,
                          java.lang.String newReferencingName,
                          java.lang.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 Detail

      • getTableId

        public UUID getTableId()
      • 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 java.sql.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 java.lang.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
      • 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 java.lang.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 java.lang.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 java.lang.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 java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getObjectName

        public java.lang.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 java.lang.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
      • 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:
        lcc - the language connection context
        action - The action causing the invalidation
        Throws:
        StandardException - thrown if called in sanity mode
      • readExternal

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

        public void writeExternal​(java.io.ObjectOutput out)
                           throws java.io.IOException
        Write this object to a stream of stored objects.
        Specified by:
        writeExternal in interface java.io.Externalizable
        Parameters:
        out - write bytes here.
        Throws:
        java.io.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
      • getContext

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