Class InternalTriggerExecutionContext
- java.lang.Object
-
- org.apache.derby.impl.sql.execute.InternalTriggerExecutionContext
-
- All Implemented Interfaces:
TriggerExecutionContext,ExecutionStmtValidator
class InternalTriggerExecutionContext extends java.lang.Object implements TriggerExecutionContext, ExecutionStmtValidator
There is one of these beasts per INSERT/DELETE/UPDATE statement. It fulfills the contract for the externally visible trigger execution context and it validates that a statement that is about to be executed doesn't violate the restrictions placed upon what can be executed from a trigger.Note that it is crucial that cleanup() is called once the DML has completed, cleanup() makes sure that users can't do something invalid on a tec reference that they were holding from when the trigger fired.
-
-
Field Summary
Fields Modifier and Type Field Description protected CursorResultSetafterResultSetprotected ExecRowafterRowused exclusively for InsertResultSets which have autoincrement columns.private java.util.Vector<AutoincrementCounter>aiCountersaiCounters is a vector of AutoincrementCounters used to keep state which might be used by the trigger.private java.util.Hashtable<java.lang.String,java.lang.Long>aiHTaiHT is a hash table of auto increment (key, value) pairs.protected CursorResultSetbeforeResultSetprotected ConnectionContextccprotected booleancleanupCalledprotected intdmlTypeprotected TriggerEventeventprotected LanguageConnectionContextlccprivate java.util.Vector<java.sql.ResultSet>resultSetVectorprotected java.lang.StringstatementTextprotected UUIDtargetTableIdprotected java.lang.StringtargetTableNameprotected TriggerDescriptortriggerd-
Fields inherited from interface org.apache.derby.iapi.db.TriggerExecutionContext
DELETE_EVENT, INSERT_EVENT, UPDATE_EVENT
-
-
Constructor Summary
Constructors Constructor Description InternalTriggerExecutionContext(LanguageConnectionContext lcc, ConnectionContext cc, java.lang.String statementText, int dmlType, UUID targetTableId, java.lang.String targetTableName, java.util.Vector<AutoincrementCounter> aiCounters)Build a trigger execution context.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcleanup()Cleanup the trigger execution context.(package private) voidclearCurrentTriggerEvent()(package private) voidclearTrigger()voidcopyHashtableToAIHT(java.util.Map<java.lang.String,java.lang.Long> from)Copy a map of auto increment values into the trigger execution context hash table of auto increment values.private voidensureProperContext()Make sure that the user isn't trying to get a result set after we have cleaned up.java.lang.LonggetAutoincrementValue(java.lang.String identity)Get the last auto-increment value for the specified column.java.lang.StringgetEventStatementText()Get the text of the statement that caused the trigger to fire.intgetEventType()Get the type for the event that caused the trigger to fire.java.sql.ResultSetgetNewRow()Like getAfterResultSet(), but returns a result set positioned on the first row of the before result set.java.sql.ResultSetgetNewRowSet()Returns a result set row the new images of the changed rows.java.sql.ResultSetgetOldRow()Like getBeforeResultSet(), but returns a result set positioned on the first row of the before result set.java.sql.ResultSetgetOldRowSet()Returns a result set row the old images of the changed rows.UUIDgetTargetTableId()Get the target table UUID upon which the trigger event is declared.java.lang.StringgetTargetTableName()Get the target table name upon which the trigger event is declared.voidresetAICounters(boolean begin)Reset Autoincrement counters to the beginning or the end.(package private) voidsetAfterResultSet(CursorResultSet rs)(package private) voidsetBeforeResultSet(CursorResultSet rs)(package private) voidsetCurrentTriggerEvent(TriggerEvent event)(package private) voidsetTrigger(TriggerDescriptor triggerd)java.lang.StringtoString()voidupdateAICounters()Update auto increment counters from the last row inserted.voidvalidateStatement(ConstantAction constantAction)Make sure that whatever statement is about to be executed is ok from the context of this trigger.
-
-
-
Field Detail
-
dmlType
protected int dmlType
-
statementText
protected java.lang.String statementText
-
cc
protected ConnectionContext cc
-
targetTableId
protected UUID targetTableId
-
targetTableName
protected java.lang.String targetTableName
-
lcc
protected LanguageConnectionContext lcc
-
beforeResultSet
protected CursorResultSet beforeResultSet
-
afterResultSet
protected CursorResultSet afterResultSet
-
afterRow
protected ExecRow afterRow
used exclusively for InsertResultSets which have autoincrement columns.
-
cleanupCalled
protected boolean cleanupCalled
-
event
protected TriggerEvent event
-
triggerd
protected TriggerDescriptor triggerd
-
resultSetVector
private java.util.Vector<java.sql.ResultSet> resultSetVector
-
aiCounters
private java.util.Vector<AutoincrementCounter> aiCounters
aiCounters is a vector of AutoincrementCounters used to keep state which might be used by the trigger. This is only used by Insert triggers-- Delete and Update triggers do not use this variable.- See Also:
AutoincrementCounter
-
aiHT
private java.util.Hashtable<java.lang.String,java.lang.Long> aiHT
aiHT is a hash table of auto increment (key, value) pairs. This is used for auto increment values generated by the trigger.
-
-
Constructor Detail
-
InternalTriggerExecutionContext
InternalTriggerExecutionContext(LanguageConnectionContext lcc, ConnectionContext cc, java.lang.String statementText, int dmlType, UUID targetTableId, java.lang.String targetTableName, java.util.Vector<AutoincrementCounter> aiCounters) throws StandardException
Build a trigger execution context.About the only thing of real interest to outside observers is that it pushes itself as the trigger execution context in the lcc. Be sure to call cleanup() when you are done, or you will be flogged like the reprobate that you are.
- Parameters:
lcc- the lccstatementText- the text of the statement that caused the trigger to fire. may be null if we are replicatingtargetTableId- the UUID of the table upon which the trigger firedtargetTableName- the name of the table upon which the trigger firedaiCounters- A vector of AutoincrementCounters to keep state of the ai columns in this insert trigger.a- Throws:
StandardException- on error
-
-
Method Detail
-
setBeforeResultSet
void setBeforeResultSet(CursorResultSet rs)
-
setAfterResultSet
void setAfterResultSet(CursorResultSet rs) throws StandardException
- Throws:
StandardException
-
setCurrentTriggerEvent
void setCurrentTriggerEvent(TriggerEvent event)
-
clearCurrentTriggerEvent
void clearCurrentTriggerEvent()
-
setTrigger
void setTrigger(TriggerDescriptor triggerd)
-
clearTrigger
void clearTrigger() throws StandardException- Throws:
StandardException
-
cleanup
protected void cleanup() throws StandardExceptionCleanup the trigger execution context. MUST be called when the caller is done with the trigger execution context.We go to somewhat exaggerated lengths to free up all our resources here because a user may hold on to a TEC after it is valid, so we clean everything up to be on the safe side.
- Throws:
StandardException- on unexpected error
-
ensureProperContext
private void ensureProperContext() throws java.sql.SQLExceptionMake sure that the user isn't trying to get a result set after we have cleaned up.- Throws:
java.sql.SQLException
-
validateStatement
public void validateStatement(ConstantAction constantAction) throws StandardException
Make sure that whatever statement is about to be executed is ok from the context of this trigger.Note that we are sub classed in replication for checks for replication specific language.
- Specified by:
validateStatementin interfaceExecutionStmtValidator- Parameters:
constantAction- the constant action of the action that we are to validate- Throws:
StandardException- on error- See Also:
ConstantAction
-
getTargetTableName
public java.lang.String getTargetTableName()
Get the target table name upon which the trigger event is declared.- Specified by:
getTargetTableNamein interfaceTriggerExecutionContext- Returns:
- the target table
-
getTargetTableId
public UUID getTargetTableId()
Get the target table UUID upon which the trigger event is declared.- Specified by:
getTargetTableIdin interfaceTriggerExecutionContext- Returns:
- the uuid of the target table
-
getEventType
public int getEventType()
Get the type for the event that caused the trigger to fire.- Specified by:
getEventTypein interfaceTriggerExecutionContext- Returns:
- the event type (e.g. UPDATE_EVENT)
-
getEventStatementText
public java.lang.String getEventStatementText()
Get the text of the statement that caused the trigger to fire.- Specified by:
getEventStatementTextin interfaceTriggerExecutionContext- Returns:
- the statement text
-
getOldRowSet
public java.sql.ResultSet getOldRowSet() throws java.sql.SQLExceptionReturns a result set row the old images of the changed rows. For a row trigger, the result set will have a single row. For a statement trigger, this result set has every row that has changed or will change. If a statement trigger does not affect a row, then the result set will be empty (i.e. ResultSet.next() will return false).- Specified by:
getOldRowSetin interfaceTriggerExecutionContext- Returns:
- the ResultSet containing before images of the rows changed by the triggering event.
- Throws:
java.sql.SQLException- if called after the triggering event has completed
-
getNewRowSet
public java.sql.ResultSet getNewRowSet() throws java.sql.SQLExceptionReturns a result set row the new images of the changed rows. For a row trigger, the result set will have a single row. For a statement trigger, this result set has every row that has changed or will change. If a statement trigger does not affect a row, then the result set will be empty (i.e. ResultSet.next() will return false).- Specified by:
getNewRowSetin interfaceTriggerExecutionContext- Returns:
- the ResultSet containing after images of the rows changed by the triggering event.
- Throws:
java.sql.SQLException- if called after the triggering event has completed
-
getOldRow
public java.sql.ResultSet getOldRow() throws java.sql.SQLExceptionLike getBeforeResultSet(), but returns a result set positioned on the first row of the before result set. Used as a convenience to get a column for a row trigger. Equivalent to getBeforeResultSet() followed by next().- Specified by:
getOldRowin interfaceTriggerExecutionContext- Returns:
- the ResultSet positioned on the old row image.
- Throws:
java.sql.SQLException- if called after the triggering event has completed
-
getNewRow
public java.sql.ResultSet getNewRow() throws java.sql.SQLExceptionLike getAfterResultSet(), but returns a result set positioned on the first row of the before result set. Used as a convenience to get a column for a row trigger. Equivalent to getAfterResultSet() followed by next().- Specified by:
getNewRowin interfaceTriggerExecutionContext- Returns:
- the ResultSet positioned on the new row image.
- Throws:
java.sql.SQLException- if called after the triggering event has completed
-
getAutoincrementValue
public java.lang.Long getAutoincrementValue(java.lang.String identity)
Description copied from interface:TriggerExecutionContextGet the last auto-increment value for the specified column.- Specified by:
getAutoincrementValuein interfaceTriggerExecutionContext- Parameters:
identity- the fully qualified name of the identity column- Returns:
- the last auto-increment value, or
nullif not found
-
copyHashtableToAIHT
public void copyHashtableToAIHT(java.util.Map<java.lang.String,java.lang.Long> from)
Copy a map of auto increment values into the trigger execution context hash table of auto increment values.
-
resetAICounters
public void resetAICounters(boolean begin)
Reset Autoincrement counters to the beginning or the end.- Parameters:
begin- if True, reset the AutoincremnetCounter to the beginning-- used to reset the counters for the next trigger. If false, reset it to the end-- this sets up the counter appropriately for a AFTER STATEMENT trigger.
-
updateAICounters
public void updateAICounters() throws StandardExceptionUpdate auto increment counters from the last row inserted.- Throws:
StandardException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-