--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.execute.TriggerInfo\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.sql.execute;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import org.apache.derby.iapi.sql.dictionary.DataDictionary;\r
+import org.apache.derby.iapi.sql.dictionary.GenericDescriptorList;\r
+import org.apache.derby.iapi.sql.dictionary.TableDescriptor;\r
+import org.apache.derby.iapi.sql.dictionary.TriggerDescriptor;\r
+\r
+import org.apache.derby.iapi.services.monitor.Monitor;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import org.apache.derby.iapi.services.io.StoredFormatIds;\r
+import org.apache.derby.iapi.services.io.FormatIdUtil;\r
+import org.apache.derby.iapi.services.io.ArrayUtil;\r
+import org.apache.derby.iapi.services.io.Formatable;\r
+import org.apache.derby.catalog.UUID;\r
+\r
+import java.io.ObjectOutput;\r
+import java.io.ObjectInput;\r
+import java.io.IOException;\r
+import java.util.Enumeration;\r
+\r
+import java.util.Vector;\r
+\r
+/**\r
+ * This is a simple class used to store the run time information\r
+ * about a foreign key. Used by DML to figure out what to\r
+ * check.\r
+ *\r
+ */\r
+public final class TriggerInfo implements Formatable \r
+{\r
+ /********************************************************\r
+ **\r
+ ** This class implements Formatable. That means that it\r
+ ** can write itself to and from a formatted stream. If\r
+ ** you add more fields to this class, make sure that you\r
+ ** also write/read them with the writeExternal()/readExternal()\r
+ ** methods.\r
+ **\r
+ ** If, inbetween releases, you add more fields to this class,\r
+ ** then you should bump the version number emitted by the getTypeFormatId()\r
+ ** method. OR, since this is something that is used\r
+ ** in stored prepared statements, it is ok to change it\r
+ ** if you make sure that stored prepared statements are\r
+ ** invalidated across releases.\r
+ **\r
+ ********************************************************/\r
+\r
+ TriggerDescriptor[] triggerArray; \r
+ String[] columnNames;\r
+ int[] columnIds;\r
+\r
+ /**\r
+ * Niladic constructor for Formattable\r
+ */\r
+ public TriggerInfo() {}\r
+\r
+ /**\r
+ * Constructor for TriggerInfo\r
+ *\r
+ * @param td the table upon which the trigger is declared\r
+ * @param changedCols the columns that are changed in the dml that is\r
+ * causing the trigger to fire\r
+ * @param triggers the list of trigger descriptors\r
+ * \r
+ */\r
+ public TriggerInfo\r
+ (\r
+ TableDescriptor td,\r
+ int[] changedCols,\r
+ GenericDescriptorList triggers\r
+ )\r
+ {\r
+ this.columnIds = changedCols;\r
+\r
+ if (columnIds != null)\r
+ {\r
+ /*\r
+ ** Find the names of all the columns that are\r
+ ** being changd.\r
+ */\r
+ columnNames = new String[columnIds.length];\r
+ for (int i = 0; i < columnIds.length; i++)\r
+ {\r
+ columnNames[i] = td.getColumnDescriptor(columnIds[i]).getColumnName();\r
+ }\r
+ }\r
+\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ SanityManager.ASSERT(triggers != null, "null trigger descriptor list");\r
+ SanityManager.ASSERT(triggers.size() > 0, "trigger descriptor list has no elements");\r
+ }\r
+\r
+ /*\r
+ ** Copy the trigger descriptors into an array of the right type\r
+ */\r
+ Enumeration descs = triggers.elements();\r
+ \r
+ int size = triggers.size();\r
+ triggerArray = new TriggerDescriptor[size];\r
+\r
+ for (int i = 0; i < size; i++)\r
+ {\r
+ triggerArray[i] = (TriggerDescriptor) descs.nextElement();\r
+ }\r
+ }\r
+\r
+ /*\r
+ * private constructor for TriggerInfo\r
+ */\r
+ private TriggerInfo\r
+ (\r
+ TriggerDescriptor[] triggers,\r
+ int[] changedColsIds,\r
+ String[] changedColsNames\r
+ ) \r
+ {\r
+ this.columnIds = changedColsIds;\r
+ this.columnNames = changedColsNames;\r
+ this.triggerArray = triggers;\r
+ }\r
+\r
+ /**\r
+ * Do we have a trigger or triggers that meet\r
+ * the criteria\r
+ *\r
+ * @param isBefore true for a before trigger, false\r
+ * for after trigger, null for either\r
+ * @param isRow true for a row trigger, false\r
+ * for statement trigger, null for either\r
+ *\r
+ * @return true if we have a trigger that meets the\r
+ * criteria\r
+ */\r
+ boolean hasTrigger(boolean isBefore, boolean isRow)\r
+ {\r
+ if (triggerArray == null)\r
+ {\r
+ return false;\r
+ }\r
+\r
+ return hasTrigger(new Boolean(isBefore), new Boolean(isRow));\r
+ }\r
+\r
+ /**\r
+ * Do we have a trigger or triggers that meet\r
+ * the criteria\r
+ *\r
+ * @param isBefore true for a before trigger, false\r
+ * for after trigger, null for either\r
+ * @param isRow true for a row trigger, false\r
+ * for statement trigger, null for either\r
+ *\r
+ * @return true if we have a trigger that meets the\r
+ * criteria\r
+ */\r
+ private boolean hasTrigger(Boolean isBefore, Boolean isRow)\r
+ {\r
+ if (triggerArray == null)\r
+ {\r
+ return false;\r
+ }\r
+ for (int i = 0; i < triggerArray.length; i++)\r
+ {\r
+ if (((isBefore == null) || \r
+ (triggerArray[i].isBeforeTrigger() == isBefore.booleanValue())) &&\r
+ ((isRow == null) || \r
+ (triggerArray[i].isRowTrigger() == isRow.booleanValue())))\r
+ {\r
+ return true;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ TriggerDescriptor[] getTriggerArray()\r
+ {\r
+ return triggerArray;\r
+ }\r
+ //////////////////////////////////////////////\r
+ //\r
+ // FORMATABLE\r
+ //\r
+ //////////////////////////////////////////////\r
+ /**\r
+ * Write this object out\r
+ *\r
+ * @param out write bytes here\r
+ *\r
+ * @exception IOException thrown on error\r
+ */\r
+ public void writeExternal(ObjectOutput out) throws IOException\r
+ {\r
+ ArrayUtil.writeArray(out, triggerArray);\r
+ ArrayUtil.writeIntArray(out, columnIds);\r
+ ArrayUtil.writeArray(out, columnNames);\r
+ }\r
+\r
+ /**\r
+ * Read this object from a stream of stored objects.\r
+ *\r
+ * @param in read this.\r
+ *\r
+ * @exception IOException thrown on error\r
+ * @exception ClassNotFoundException thrown on error\r
+ */\r
+ public void readExternal(ObjectInput in)\r
+ throws IOException, ClassNotFoundException\r
+ {\r
+ triggerArray = new TriggerDescriptor[ArrayUtil.readArrayLength(in)];\r
+ ArrayUtil.readArrayItems(in, triggerArray);\r
+\r
+ columnIds = ArrayUtil.readIntArray(in);\r
+\r
+ int len = ArrayUtil.readArrayLength(in);\r
+ if (len > 0)\r
+ {\r
+ columnNames = new String[len];\r
+ ArrayUtil.readArrayItems(in, columnNames);\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Get the formatID which corresponds to this class.\r
+ *\r
+ * @return the formatID of this class\r
+ */\r
+ public int getTypeFormatId() { return StoredFormatIds.TRIGGER_INFO_V01_ID; }\r
+\r
+ //////////////////////////////////////////////////////////////\r
+ //\r
+ // Misc\r
+ //\r
+ //////////////////////////////////////////////////////////////\r
+ public String toString()\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ StringBuffer str = new StringBuffer();\r
+ str.append("\nColumn names modified:\t\t(");\r
+ for (int i = 0; i < columnNames.length; i++)\r
+ {\r
+ if (i > 0)\r
+ str.append(",");\r
+ \r
+ str.append(columnNames[i]);\r
+ }\r
+ str.append(")");\r
+\r
+ str.append("\nColumn ids modified:\t\t(");\r
+ for (int i = 0; i < columnIds.length; i++)\r
+ {\r
+ if (i > 0)\r
+ str.append(",");\r
+ \r
+ str.append(columnIds[i]);\r
+ }\r
+ str.append(")");\r
+\r
+ str.append("\nTriggers:");\r
+ for (int i = 0; i < triggerArray.length; i++)\r
+ {\r
+ str.append("\n"+triggerArray[i]);\r
+ }\r
+ return str.toString();\r
+ }\r
+ else\r
+ {\r
+ return "";\r
+ }\r
+ }\r
+}\r