use of org.apache.derby.iapi.sql.dictionary.TriggerDescriptor in project derby by apache.
the class SYSTRIGGERSRowFactory method buildDescriptor.
// /////////////////////////////////////////////////////////////////////////
//
// ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory
//
// /////////////////////////////////////////////////////////////////////////
/**
* Make an Tuple Descriptor out of a SYSTRIGGERS row
*
* @param row a SYSTRIGGERS row
* @param parentTupleDescriptor unused
* @param dd dataDictionary
*
* @return a descriptor equivalent to a SYSTRIGGERS row
*
* @exception StandardException thrown on failure
*/
public TupleDescriptor buildDescriptor(ExecRow row, TupleDescriptor parentTupleDescriptor, DataDictionary dd) throws StandardException {
DataValueDescriptor col;
String name;
char theChar;
String uuidStr;
String triggerDefinition;
String oldReferencingName;
String newReferencingName;
UUID uuid;
// schema
UUID suuid;
// referenced table
UUID tuuid;
// action sps uuid string
UUID actionSPSID = null;
// when clause sps uuid string
UUID whenSPSID = null;
Timestamp createTime;
int eventMask = 0;
boolean isBefore;
boolean isRow;
boolean isEnabled;
boolean referencingOld;
boolean referencingNew;
ReferencedColumns rcd;
TriggerDescriptor descriptor;
DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
if (SanityManager.DEBUG) {
// The expected number of columns depends on the version of the
// data dictionary. The WHENCLAUSETEXT column was added in version
// 10.11 (DERBY-534).
int expectedCols = dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_11, null) ? SYSTRIGGERS_COLUMN_COUNT : (SYSTRIGGERS_COLUMN_COUNT - 1);
SanityManager.ASSERT(row.nColumns() == expectedCols, "Wrong number of columns for a SYSTRIGGERS row");
}
// 1st column is TRIGGERID (UUID - char(36))
col = row.getColumn(1);
uuidStr = col.getString();
uuid = getUUIDFactory().recreateUUID(uuidStr);
// 2nd column is TRIGGERNAME (varchar(128))
col = row.getColumn(2);
name = col.getString();
// 3rd column is SCHEMAID (UUID - char(36))
col = row.getColumn(3);
uuidStr = col.getString();
suuid = getUUIDFactory().recreateUUID(uuidStr);
// 4th column is CREATIONTIMESTAMP (TIMESTAMP)
col = row.getColumn(4);
createTime = col.getTimestamp(getCalendarForCreationTimestamp());
// 5th column is EVENT (char(1))
col = row.getColumn(5);
theChar = col.getString().charAt(0);
switch(theChar) {
case 'U':
eventMask = TriggerDescriptor.TRIGGER_EVENT_UPDATE;
break;
case 'I':
eventMask = TriggerDescriptor.TRIGGER_EVENT_INSERT;
break;
case 'D':
eventMask = TriggerDescriptor.TRIGGER_EVENT_DELETE;
break;
default:
if (SanityManager.DEBUG) {
SanityManager.THROWASSERT("bad event mask: " + theChar);
}
}
// 6th column is FIRINGTIME (char(1))
isBefore = getCharBoolean(row.getColumn(6), 'B', 'A');
// 7th column is TYPE (char(1))
isRow = getCharBoolean(row.getColumn(7), 'R', 'S');
// 8th column is STATE (char(1))
isEnabled = getCharBoolean(row.getColumn(8), 'E', 'D');
// 9th column is TABLEID (UUID - char(36))
col = row.getColumn(9);
uuidStr = col.getString();
tuuid = getUUIDFactory().recreateUUID(uuidStr);
// 10th column is WHENSTMTID (UUID - char(36))
col = row.getColumn(10);
uuidStr = col.getString();
if (uuidStr != null)
whenSPSID = getUUIDFactory().recreateUUID(uuidStr);
// 11th column is ACTIONSTMTID (UUID - char(36))
col = row.getColumn(11);
uuidStr = col.getString();
if (uuidStr != null)
actionSPSID = getUUIDFactory().recreateUUID(uuidStr);
// 12th column is REFERENCEDCOLUMNS user type org.apache.derby.catalog.ReferencedColumns
col = row.getColumn(12);
rcd = (ReferencedColumns) col.getObject();
// 13th column is TRIGGERDEFINITION (longvarchar)
col = row.getColumn(13);
triggerDefinition = col.getString();
// 14th column is REFERENCINGOLD (boolean)
col = row.getColumn(14);
referencingOld = col.getBoolean();
// 15th column is REFERENCINGNEW (boolean)
col = row.getColumn(15);
referencingNew = col.getBoolean();
// 16th column is REFERENCINGNAME (varchar(128))
col = row.getColumn(16);
oldReferencingName = col.getString();
// 17th column is REFERENCINGNAME (varchar(128))
col = row.getColumn(17);
newReferencingName = col.getString();
// 18th column is WHENCLAUSETEXT (longvarchar)
String whenClauseText = null;
if (row.nColumns() >= 18) {
// This column is present only if the data dictionary version is
// 10.11 or higher.
col = row.getColumn(18);
whenClauseText = col.getString();
}
descriptor = ddg.newTriggerDescriptor(dd.getSchemaDescriptor(suuid, null), uuid, name, eventMask, isBefore, isRow, isEnabled, dd.getTableDescriptor(tuuid), whenSPSID, actionSPSID, createTime, (rcd == null) ? (int[]) null : rcd.getReferencedColumnPositions(), (rcd == null) ? (int[]) null : rcd.getTriggerActionReferencedColumnPositions(), triggerDefinition, referencingOld, referencingNew, oldReferencingName, newReferencingName, whenClauseText);
return descriptor;
}
use of org.apache.derby.iapi.sql.dictionary.TriggerDescriptor in project derby by apache.
the class SYSTRIGGERSRowFactory method makeRow.
/**
* Helper method that contains common logic for {@code makeRow()} and
* {@code makeEmptyRowForCurrentVersion()}. Creates a row for the
* SYSTRIGGERS conglomerate.
*
* @param td the {@code TriggerDescriptor} to create a row from (can be
* {@code null} if the returned row should be empty)
* @param columnCount the number of columns in the returned row (used for
* trimming off columns in soft upgrade mode to match the format in
* the old dictionary version)
* @return a row for the SYSTRIGGERS conglomerate
* @throws StandardException if an error happens when creating the row
*/
private ExecRow makeRow(TupleDescriptor td, int columnCount) throws StandardException {
String name = null;
UUID uuid = null;
// schema
UUID suuid = null;
// referenced table
UUID tuuid = null;
// action sps uuid string
UUID actionSPSID = null;
// when clause sps uuid string
UUID whenSPSID = null;
Timestamp createTime = null;
String event = null;
String time = null;
String type = null;
String enabled = null;
String triggerDefinition = null;
String oldReferencingName = null;
String newReferencingName = null;
ReferencedColumns rcd = null;
boolean referencingOld = false;
boolean referencingNew = false;
String whenClauseText = null;
if (td != null) {
TriggerDescriptor triggerDescriptor = (TriggerDescriptor) td;
name = triggerDescriptor.getName();
uuid = triggerDescriptor.getUUID();
suuid = triggerDescriptor.getSchemaDescriptor().getUUID();
createTime = triggerDescriptor.getCreationTimestamp();
// for now we are assuming that a trigger can only listen to a single event
event = triggerDescriptor.listensForEvent(TriggerDescriptor.TRIGGER_EVENT_UPDATE) ? "U" : triggerDescriptor.listensForEvent(TriggerDescriptor.TRIGGER_EVENT_DELETE) ? "D" : "I";
time = triggerDescriptor.isBeforeTrigger() ? "B" : "A";
type = triggerDescriptor.isRowTrigger() ? "R" : "S";
enabled = triggerDescriptor.isEnabled() ? "E" : "D";
tuuid = triggerDescriptor.getTableDescriptor().getUUID();
int[] refCols = triggerDescriptor.getReferencedCols();
int[] refColsInTriggerAction = triggerDescriptor.getReferencedColsInTriggerAction();
rcd = (refCols != null || refColsInTriggerAction != null) ? new ReferencedColumnsDescriptorImpl(refCols, refColsInTriggerAction) : null;
actionSPSID = triggerDescriptor.getActionId();
whenSPSID = triggerDescriptor.getWhenClauseId();
triggerDefinition = triggerDescriptor.getTriggerDefinition();
referencingOld = triggerDescriptor.getReferencingOld();
referencingNew = triggerDescriptor.getReferencingNew();
oldReferencingName = triggerDescriptor.getOldReferencingName();
newReferencingName = triggerDescriptor.getNewReferencingName();
whenClauseText = triggerDescriptor.getWhenClauseText();
}
/* Build the row to insert */
ExecRow row = getExecutionFactory().getValueRow(columnCount);
/* 1st column is TRIGGERID */
row.setColumn(1, new SQLChar((uuid == null) ? null : uuid.toString()));
/* 2nd column is TRIGGERNAME */
row.setColumn(2, new SQLVarchar(name));
/* 3rd column is SCHEMAID */
row.setColumn(3, new SQLChar((suuid == null) ? null : suuid.toString()));
/* 4th column is CREATIONTIMESTAMP */
SQLTimestamp creationTimestamp = (createTime == null) ? new SQLTimestamp(null) : new SQLTimestamp(createTime, getCalendarForCreationTimestamp());
row.setColumn(4, creationTimestamp);
/* 5th column is EVENT */
row.setColumn(5, new SQLChar(event));
/* 6th column is FIRINGTIME */
row.setColumn(6, new SQLChar(time));
/* 7th column is TYPE */
row.setColumn(7, new SQLChar(type));
/* 8th column is STATE */
row.setColumn(8, new SQLChar(enabled));
/* 9th column is TABLEID */
row.setColumn(9, new SQLChar((tuuid == null) ? null : tuuid.toString()));
/* 10th column is WHENSTMTID */
row.setColumn(10, new SQLChar((whenSPSID == null) ? null : whenSPSID.toString()));
/* 11th column is ACTIONSTMTID */
row.setColumn(11, new SQLChar((actionSPSID == null) ? null : actionSPSID.toString()));
/* 12th column is REFERENCEDCOLUMNS
* (user type org.apache.derby.catalog.ReferencedColumns)
*/
row.setColumn(12, new UserType(rcd));
/* 13th column is TRIGGERDEFINITION */
row.setColumn(13, dvf.getLongvarcharDataValue(triggerDefinition));
/* 14th column is REFERENCINGOLD */
row.setColumn(14, new SQLBoolean(referencingOld));
/* 15th column is REFERENCINGNEW */
row.setColumn(15, new SQLBoolean(referencingNew));
/* 16th column is OLDREFERENCINGNAME */
row.setColumn(16, new SQLVarchar(oldReferencingName));
/* 17th column is NEWREFERENCINGNAME */
row.setColumn(17, new SQLVarchar(newReferencingName));
/* 18th column is WHENCLAUSETEXT */
if (row.nColumns() >= 18) {
// This column is present only if the data dictionary version is
// 10.11 or higher.
row.setColumn(18, dvf.getLongvarcharDataValue(whenClauseText));
}
return row;
}
use of org.apache.derby.iapi.sql.dictionary.TriggerDescriptor in project derby by apache.
the class DeleteNode method getDeleteReadMap.
/**
* Builds a bitmap of all columns which should be read from the
* Store in order to satisfy an DELETE statement.
*
* 1) finds all indices on this table
* 2) adds the index columns to a bitmap of affected columns
* 3) adds the index descriptors to a list of conglomerate
* descriptors.
* 4) finds all DELETE triggers on the table
* 5) if there are any DELETE triggers, then do one of the following
* a)If all of the triggers have MISSING referencing clause, then that
* means that the trigger actions do not have access to before and
* after values. In that case, there is no need to blanketly decide
* to include all the columns in the read map just because there are
* triggers defined on the table.
* b)Since one/more triggers have REFERENCING clause on them, get all
* the columns because we don't know what the user will ultimately
* reference.
* 6) adds the triggers to an evolving list of triggers
*
* @param conglomerates OUT: list of affected indices
* @param relevantTriggers IN/OUT. Passed in as an empty list. Filled in as we go.
* @param needsDeferredProcessing IN/OUT. true if the statement already needs
* deferred processing. set while evaluating this
* routine if a trigger requires
* deferred processing
*
* @return a FormatableBitSet of columns to be read out of the base table
*
* @exception StandardException Thrown on error
*/
private static FormatableBitSet getDeleteReadMap(TableDescriptor baseTable, List<ConglomerateDescriptor> conglomerates, TriggerDescriptorList relevantTriggers, boolean[] needsDeferredProcessing) throws StandardException {
int columnCount = baseTable.getMaxColumnID();
FormatableBitSet columnMap = new FormatableBitSet(columnCount + 1);
/*
** Get a list of the indexes that need to be
** updated. ColumnMap contains all indexed
** columns where 1 or more columns in the index
** are going to be modified.
**
** Notice that we don't need to add constraint
** columns. This is because we add all key constraints
** (e.g. foreign keys) as a side effect of adding their
** indexes above. And we don't need to deal with
** check constraints on a delete.
**
** Adding indexes also takes care of the replication
** requirement of having the primary key.
*/
DMLModStatementNode.getXAffectedIndexes(baseTable, null, columnMap, conglomerates);
/*
** If we have any DELETE triggers, then do one of the following
** 1)If all of the triggers have MISSING referencing clause, then that
** means that the trigger actions do not have access to before and
** after values. In that case, there is no need to blanketly decide to
** include all the columns in the read map just because there are
** triggers defined on the table.
** 2)Since one/more triggers have REFERENCING clause on them, get all
** the columns because we don't know what the user will ultimately reference.
*/
baseTable.getAllRelevantTriggers(StatementType.DELETE, (int[]) null, relevantTriggers);
if (relevantTriggers.size() > 0) {
needsDeferredProcessing[0] = true;
boolean needToIncludeAllColumns = false;
for (TriggerDescriptor trd : relevantTriggers) {
// If yes, then read all the columns from the trigger table.
if (!trd.getReferencingNew() && !trd.getReferencingOld())
continue;
else {
needToIncludeAllColumns = true;
break;
}
}
if (needToIncludeAllColumns) {
for (int i = 1; i <= columnCount; i++) {
columnMap.set(i);
}
}
}
return columnMap;
}
use of org.apache.derby.iapi.sql.dictionary.TriggerDescriptor in project derby by apache.
the class DropTriggerNode method bindStatement.
/**
* Bind this DropTriggerNode. This means looking up the trigger,
* verifying it exists and getting its table uuid.
*
* @exception StandardException Thrown on error
*/
@Override
public void bindStatement() throws StandardException {
CompilerContext cc = getCompilerContext();
DataDictionary dd = getDataDictionary();
SchemaDescriptor sd = getSchemaDescriptor();
TriggerDescriptor triggerDescriptor = null;
if (sd.getUUID() != null)
triggerDescriptor = dd.getTriggerDescriptor(getRelativeName(), sd);
if (triggerDescriptor == null) {
throw StandardException.newException(SQLState.LANG_OBJECT_NOT_FOUND, "TRIGGER", getFullName());
}
/* Get the table descriptor */
td = triggerDescriptor.getTableDescriptor();
cc.createDependency(td);
cc.createDependency(triggerDescriptor);
}
use of org.apache.derby.iapi.sql.dictionary.TriggerDescriptor in project derby by apache.
the class CreateTriggerConstantAction method executeConstantAction.
/**
* This is the guts of the Execution-time logic for CREATE TRIGGER.
*
* @see ConstantAction#executeConstantAction
*
* @exception StandardException Thrown on failure
*/
public void executeConstantAction(Activation activation) throws StandardException {
SPSDescriptor whenspsd = null;
SPSDescriptor actionspsd;
LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
DataDictionary dd = lcc.getDataDictionary();
DependencyManager dm = dd.getDependencyManager();
TransactionController tc = lcc.getTransactionExecute();
/*
** Indicate that we are about to modify the data dictionary.
**
** We tell the data dictionary we're done writing at the end of
** the transaction.
*/
dd.startWriting(lcc);
SchemaDescriptor triggerSd = getSchemaDescriptorForCreate(dd, activation, triggerSchemaName);
if (spsCompSchemaId == null) {
SchemaDescriptor def = lcc.getDefaultSchema();
if (def.getUUID() == null) {
// Descriptor for default schema is stale,
// look it up in the dictionary
def = dd.getSchemaDescriptor(def.getDescriptorName(), tc, false);
}
/*
** It is possible for spsCompSchemaId to be null. For instance,
** the current schema may not have been physically created yet but
** it exists "virtually". In this case, its UUID will have the
** value of null meaning that it is not persistent. e.g.:
**
** CONNECT 'db;create=true' user 'ernie';
** CREATE TABLE bert.t1 (i INT);
** CREATE TRIGGER bert.tr1 AFTER INSERT ON bert.t1
** FOR EACH STATEMENT MODE DB2SQL
** SELECT * FROM SYS.SYSTABLES;
**
** Note that in the above case, the trigger action statement have a
** null compilation schema. A compilation schema with null value
** indicates that the trigger action statement text does not have
** any dependencies with the CURRENT SCHEMA. This means:
**
** o It is safe to compile this statement in any schema since
** there is no dependency with the CURRENT SCHEMA. i.e.: All
** relevent identifiers are qualified with a specific schema.
**
** o The statement cache mechanism can utilize this piece of
** information to enable better statement plan sharing across
** connections in different schemas; thus, avoiding unnecessary
** statement compilation.
*/
if (def != null)
spsCompSchemaId = def.getUUID();
}
String tabName;
if (triggerTable != null) {
triggerTableId = triggerTable.getUUID();
tabName = triggerTable.getName();
} else
tabName = "with UUID " + triggerTableId;
/* We need to get table descriptor again. We simply can't trust the
* one we got at compile time, the lock on system table was released
* when compile was done, and the table might well have been dropped.
*/
triggerTable = dd.getTableDescriptor(triggerTableId);
if (triggerTable == null) {
throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, tabName);
}
/* Lock the table for DDL. Otherwise during our execution, the table
* might be changed, even dropped. Beetle 4269
*/
lockTableForDDL(tc, triggerTable.getHeapConglomerateId(), true);
/* get triggerTable again for correctness, in case it's changed before
* the lock is aquired
*/
triggerTable = dd.getTableDescriptor(triggerTableId);
if (triggerTable == null) {
throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, tabName);
}
/*
** Send an invalidate on the table from which
** the triggering event emanates. This it
** to make sure that DML statements on this table
** will be recompiled. Do this before we create
** our trigger spses lest we invalidate them just
** after creating them.
*/
dm.invalidateFor(triggerTable, DependencyManager.CREATE_TRIGGER, lcc);
/*
** Lets get our trigger id up front, we'll use it when
** we create our spses.
*/
UUID tmpTriggerId = dd.getUUIDFactory().createUUID();
actionSPSId = (actionSPSId == null) ? dd.getUUIDFactory().createUUID() : actionSPSId;
if (whenSPSId == null && whenText != null) {
whenSPSId = dd.getUUIDFactory().createUUID();
}
DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
/*
** Create the trigger descriptor first so the trigger action
** compilation can pick up the relevant trigger especially in
** the case of self triggering.
*/
TriggerDescriptor triggerd = ddg.newTriggerDescriptor(triggerSd, tmpTriggerId, triggerName, eventMask, isBefore, isRow, isEnabled, triggerTable, whenSPSId, actionSPSId, makeCreationTimestamp(dd), referencedCols, referencedColsInTriggerAction, originalActionText, referencingOld, referencingNew, oldReferencingName, newReferencingName, originalWhenText);
dd.addDescriptor(triggerd, triggerSd, DataDictionary.SYSTRIGGERS_CATALOG_NUM, false, tc);
/*
** If we have a WHEN action we create it now.
*/
if (whenText != null) {
// The WHEN clause is just a search condition and not a full
// SQL statement. Turn in into a VALUES statement.
String whenValuesStmt = "VALUES " + whenText;
whenspsd = createSPS(lcc, ddg, dd, tc, tmpTriggerId, triggerSd, whenSPSId, spsCompSchemaId, whenValuesStmt, true, triggerTable);
}
/*
** Create the trigger action
*/
actionspsd = createSPS(lcc, ddg, dd, tc, tmpTriggerId, triggerSd, actionSPSId, spsCompSchemaId, actionText, false, triggerTable);
/*
** Make underlying spses dependent on the trigger.
*/
if (whenspsd != null) {
dm.addDependency(triggerd, whenspsd, lcc.getContextManager());
}
dm.addDependency(triggerd, actionspsd, lcc.getContextManager());
dm.addDependency(triggerd, triggerTable, lcc.getContextManager());
// from the triggered statement or the WHEN clause.
for (ProviderInfo info : providerInfo) {
Provider provider = (Provider) info.getDependableFinder().getDependable(dd, info.getObjectId());
dm.addDependency(triggerd, provider, lcc.getContextManager());
}
// store trigger's dependency on various privileges in the dependeny system
storeViewTriggerDependenciesOnPrivileges(activation, triggerd);
}
Aggregations