KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.RowTriggerExecutor
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.sql.execute.CursorResultSet;
25 import org.apache.derby.iapi.error.StandardException;
26 import org.apache.derby.iapi.sql.dictionary.SPSDescriptor;
27 import org.apache.derby.iapi.sql.dictionary.TriggerDescriptor;
28 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
29 import org.apache.derby.iapi.sql.Activation;
30
31 /**
32  * A row trigger executor is an object that executes
33  * a row trigger. It is instantiated at execution time.
34  * There is one per row trigger.
35  */

36 public class RowTriggerExecutor extends GenericTriggerExecutor
37 {
38     /**
39      * Constructor
40      *
41      * @param tec the execution context
42      * @param triggerd the trigger descriptor
43      * @param activation the activation
44      * @param lcc the lcc
45      */

46     RowTriggerExecutor
47     (
48         InternalTriggerExecutionContext tec,
49         TriggerDescriptor triggerd,
50         Activation activation,
51         LanguageConnectionContext lcc
52     )
53     {
54         super(tec, triggerd, activation, lcc);
55     }
56
57     /**
58      * Fire the trigger based on the event.
59      *
60      * @param event the trigger event
61      * @param brs the before result set
62      * @param ars the after result set
63      *
64      * @exception StandardExcetion on error or general trigger
65      * exception
66      */

67     void fireTrigger
68     (
69         TriggerEvent event,
70         CursorResultSet brs,
71         CursorResultSet ars
72     ) throws StandardException
73     {
74         tec.setTrigger(triggerd);
75         
76         try
77         {
78             while (true)
79             {
80                 if (brs != null)
81                 {
82                     if (brs.getNextRow() == null)
83                         break;
84                 }
85     
86                 if (ars != null)
87                 {
88                     if (ars.getNextRow() == null)
89                         break;
90                 }
91     
92                 tec.setBeforeResultSet(brs == null ?
93                         null :
94                         TemporaryRowHolderResultSet.
95                                        getNewRSOnCurrentRow(activation, brs));
96                     
97                 tec.setAfterResultSet(ars == null ?
98                                       null :
99                                       TemporaryRowHolderResultSet.
100                                       getNewRSOnCurrentRow(activation, ars));
101
102                 /*
103                     This is the key to handling autoincrement values that might
104                     be seen by insert triggers. For an AFTER ROW trigger, update
105                     the autoincrement counters before executing the SPS for the
106                     trigger.
107                 */

108                 if (event.isAfter())
109                     tec.updateAICounters();
110
111                 executeSPS(getAction());
112                 
113                 /*
114                   For BEFORE ROW triggers, update the ai values after the SPS
115                   has been executed. This way the SPS will see ai values from
116                   the previous row.
117                 */

118                 if (event.isBefore())
119                     tec.updateAICounters();
120             }
121         }
122         finally
123         {
124             clearSPS();
125             tec.clearTrigger();
126         }
127     }
128 }
129
Popular Tags