KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > TriggerInfo


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.TriggerInfo
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.execute;
23
24 import org.apache.derby.iapi.error.StandardException;
25
26 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
27 import org.apache.derby.iapi.sql.dictionary.GenericDescriptorList;
28 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
29 import org.apache.derby.iapi.sql.dictionary.TriggerDescriptor;
30
31 import org.apache.derby.iapi.services.monitor.Monitor;
32
33 import org.apache.derby.iapi.services.sanity.SanityManager;
34 import org.apache.derby.iapi.services.io.StoredFormatIds;
35 import org.apache.derby.iapi.services.io.FormatIdUtil;
36 import org.apache.derby.iapi.services.io.ArrayUtil;
37 import org.apache.derby.iapi.services.io.Formatable;
38 import org.apache.derby.catalog.UUID;
39
40 import java.io.ObjectOutput JavaDoc;
41 import java.io.ObjectInput JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.util.Enumeration JavaDoc;
44
45 import java.util.Vector JavaDoc;
46
47 /**
48  * This is a simple class used to store the run time information
49  * about a foreign key. Used by DML to figure out what to
50  * check.
51  *
52  * @author jamie
53  */

54 public final class TriggerInfo implements Formatable
55 {
56     /********************************************************
57     **
58     ** This class implements Formatable. That means that it
59     ** can write itself to and from a formatted stream. If
60     ** you add more fields to this class, make sure that you
61     ** also write/read them with the writeExternal()/readExternal()
62     ** methods.
63     **
64     ** If, inbetween releases, you add more fields to this class,
65     ** then you should bump the version number emitted by the getTypeFormatId()
66     ** method. OR, since this is something that is used
67     ** in stored prepared statements, it is ok to change it
68     ** if you make sure that stored prepared statements are
69     ** invalidated across releases.
70     **
71     ********************************************************/

72
73     TriggerDescriptor[] triggerArray;
74     String JavaDoc[] columnNames;
75     int[] columnIds;
76
77     /**
78      * Niladic constructor for Formattable
79      */

80     public TriggerInfo() {}
81
82     /**
83      * Constructor for TriggerInfo
84      *
85      * @param td the table upon which the trigger is declared
86      * @param changedCols the columns that are changed in the dml that is
87      * causing the trigger to fire
88      * @param triggers the list of trigger descriptors
89      *
90      */

91     public TriggerInfo
92     (
93         TableDescriptor td,
94         int[] changedCols,
95         GenericDescriptorList triggers
96     )
97     {
98         this.columnIds = changedCols;
99
100         if (columnIds != null)
101         {
102             /*
103             ** Find the names of all the columns that are
104             ** being changd.
105             */

106             columnNames = new String JavaDoc[columnIds.length];
107             for (int i = 0; i < columnIds.length; i++)
108             {
109                 columnNames[i] = td.getColumnDescriptor(columnIds[i]).getColumnName();
110             }
111         }
112
113         if (SanityManager.DEBUG)
114         {
115             SanityManager.ASSERT(triggers != null, "null trigger descriptor list");
116             SanityManager.ASSERT(triggers.size() > 0, "trigger descriptor list has no elements");
117         }
118
119         /*
120         ** Copy the trigger descriptors into an array of the right type
121         */

122         Enumeration JavaDoc descs = triggers.elements();
123         
124         int size = triggers.size();
125         triggerArray = new TriggerDescriptor[size];
126
127         for (int i = 0; i < size; i++)
128         {
129             triggerArray[i] = (TriggerDescriptor) descs.nextElement();
130         }
131     }
132
133     /*
134      * private constructor for TriggerInfo
135      */

136     private TriggerInfo
137     (
138         TriggerDescriptor[] triggers,
139         int[] changedColsIds,
140         String JavaDoc[] changedColsNames
141     )
142     {
143         this.columnIds = changedColsIds;
144         this.columnNames = changedColsNames;
145         this.triggerArray = triggers;
146     }
147
148     /**
149      * Do we have a trigger or triggers that meet
150      * the criteria
151      *
152      * @param isBefore true for a before trigger, false
153      * for after trigger, null for either
154      * @param isRow true for a row trigger, false
155      * for statement trigger, null for either
156      *
157      * @return true if we have a trigger that meets the
158      * criteria
159      */

160     boolean hasTrigger(boolean isBefore, boolean isRow)
161     {
162         if (triggerArray == null)
163         {
164             return false;
165         }
166
167         return hasTrigger(new Boolean JavaDoc(isBefore), new Boolean JavaDoc(isRow));
168     }
169
170     /**
171      * Do we have a trigger or triggers that meet
172      * the criteria
173      *
174      * @param isBefore true for a before trigger, false
175      * for after trigger, null for either
176      * @param isRow true for a row trigger, false
177      * for statement trigger, null for either
178      *
179      * @return true if we have a trigger that meets the
180      * criteria
181      */

182     private boolean hasTrigger(Boolean JavaDoc isBefore, Boolean JavaDoc isRow)
183     {
184         if (triggerArray == null)
185         {
186             return false;
187         }
188         for (int i = 0; i < triggerArray.length; i++)
189         {
190             if (((isBefore == null) ||
191                     (triggerArray[i].isBeforeTrigger() == isBefore.booleanValue())) &&
192                 ((isRow == null) ||
193                     (triggerArray[i].isRowTrigger() == isRow.booleanValue())))
194             {
195                 return true;
196             }
197         }
198         return false;
199     }
200
201     TriggerDescriptor[] getTriggerArray()
202     {
203         return triggerArray;
204     }
205     //////////////////////////////////////////////
206
//
207
// FORMATABLE
208
//
209
//////////////////////////////////////////////
210
/**
211      * Write this object out
212      *
213      * @param out write bytes here
214      *
215      * @exception IOException thrown on error
216      */

217     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
218     {
219         ArrayUtil.writeArray(out, triggerArray);
220         ArrayUtil.writeIntArray(out, columnIds);
221         ArrayUtil.writeArray(out, columnNames);
222     }
223
224     /**
225      * Read this object from a stream of stored objects.
226      *
227      * @param in read this.
228      *
229      * @exception IOException thrown on error
230      * @exception ClassNotFoundException thrown on error
231      */

232     public void readExternal(ObjectInput JavaDoc in)
233         throws IOException JavaDoc, ClassNotFoundException JavaDoc
234     {
235         triggerArray = new TriggerDescriptor[ArrayUtil.readArrayLength(in)];
236         ArrayUtil.readArrayItems(in, triggerArray);
237
238         columnIds = ArrayUtil.readIntArray(in);
239
240         int len = ArrayUtil.readArrayLength(in);
241         if (len > 0)
242         {
243             columnNames = new String JavaDoc[len];
244             ArrayUtil.readArrayItems(in, columnNames);
245         }
246     }
247     
248     /**
249      * Get the formatID which corresponds to this class.
250      *
251      * @return the formatID of this class
252      */

253     public int getTypeFormatId() { return StoredFormatIds.TRIGGER_INFO_V01_ID; }
254
255     //////////////////////////////////////////////////////////////
256
//
257
// Misc
258
//
259
//////////////////////////////////////////////////////////////
260
public String JavaDoc toString()
261     {
262         if (SanityManager.DEBUG)
263         {
264             StringBuffer JavaDoc str = new StringBuffer JavaDoc();
265             str.append("\nColumn names modified:\t\t(");
266             for (int i = 0; i < columnNames.length; i++)
267             {
268                 if (i > 0)
269                     str.append(",");
270             
271                 str.append(columnNames[i]);
272             }
273             str.append(")");
274
275             str.append("\nColumn ids modified:\t\t(");
276             for (int i = 0; i < columnIds.length; i++)
277             {
278                 if (i > 0)
279                     str.append(",");
280             
281                 str.append(columnIds[i]);
282             }
283             str.append(")");
284
285             str.append("\nTriggers:");
286             for (int i = 0; i < triggerArray.length; i++)
287             {
288                 str.append("\n"+triggerArray[i]);
289             }
290             return str.toString();
291         }
292         else
293         {
294             return "";
295         }
296     }
297 }
298
Popular Tags