KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.DropTriggerConstantAction
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.services.sanity.SanityManager;
25
26 import org.apache.derby.iapi.error.StandardException;
27 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
28
29 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
30 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
31 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
32 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
33 import org.apache.derby.iapi.sql.dictionary.SPSDescriptor;
34 import org.apache.derby.iapi.sql.dictionary.TriggerDescriptor;
35 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
36
37 import org.apache.derby.iapi.sql.depend.DependencyManager;
38
39 import org.apache.derby.iapi.reference.SQLState;
40
41 import org.apache.derby.iapi.sql.execute.ConstantAction;
42
43 import org.apache.derby.iapi.sql.Activation;
44
45 import org.apache.derby.iapi.store.access.TransactionController;
46
47 import org.apache.derby.catalog.UUID;
48
49 /**
50  * This class describes actions that are ALWAYS performed for a
51  * DROP TRIGGER Statement at Execution time.
52  *
53  * @author Jamie
54  */

55 public class DropTriggerConstantAction extends DDLSingleTableConstantAction
56 {
57
58     private final String JavaDoc triggerName;
59     private final SchemaDescriptor sd;
60
61     // CONSTRUCTORS
62

63     /**
64      * Make the ConstantAction for a DROP TRIGGER statement.
65      *
66      * @param sd Schema that stored prepared statement lives in.
67      * @param triggerName Name of the Trigger
68      * @param tableId The table upon which the trigger is defined
69      *
70      */

71     DropTriggerConstantAction
72     (
73         SchemaDescriptor sd,
74         String JavaDoc triggerName,
75         UUID tableId
76     )
77     {
78         super(tableId);
79         this.sd = sd;
80         this.triggerName = triggerName;
81
82         if (SanityManager.DEBUG)
83         {
84             SanityManager.ASSERT(sd != null, "SchemaDescriptor is null");
85         }
86     }
87
88     /**
89      * This is the guts of the Execution-time logic for DROP STATEMENT.
90      *
91      * @see ConstantAction#executeConstantAction
92      *
93      * @exception StandardException Thrown on failure
94      */

95     public void executeConstantAction( Activation activation )
96                         throws StandardException
97     {
98         TriggerDescriptor triggerd;
99
100         LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
101         DataDictionary dd = lcc.getDataDictionary();
102         DependencyManager dm = dd.getDependencyManager();
103
104
105         /*
106         ** Inform the data dictionary that we are about to write to it.
107         ** There are several calls to data dictionary "get" methods here
108         ** that might be done in "read" mode in the data dictionary, but
109         ** it seemed safer to do this whole operation in "write" mode.
110         **
111         ** We tell the data dictionary we're done writing at the end of
112         ** the transaction.
113         */

114         dd.startWriting(lcc);
115
116         TableDescriptor td = dd.getTableDescriptor(tableId);
117         if (td == null)
118         {
119             throw StandardException.newException(
120                                 SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION,
121                                 tableId.toString());
122         }
123         TransactionController tc = lcc.getTransactionExecute();
124         lockTableForDDL(tc, td.getHeapConglomerateId(), true);
125         // get td again in case table shape is changed before lock is acquired
126
td = dd.getTableDescriptor(tableId);
127         if (td == null)
128         {
129             throw StandardException.newException(
130                                 SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION,
131                                 tableId.toString());
132         }
133
134         /*
135         ** Get the trigger descriptor. We're responsible for raising
136         ** the error if it isn't found
137         */

138         triggerd = dd.getTriggerDescriptor(triggerName, sd);
139
140         if (triggerd == null)
141         {
142             throw StandardException.newException(SQLState.LANG_OBJECT_NOT_FOUND_DURING_EXECUTION, "TRIGGER",
143                     (sd.getSchemaName() + "." + triggerName));
144         }
145
146         /*
147         ** Prepare all dependents to invalidate. (This is there chance
148         ** to say that they can't be invalidated. For example, an open
149         ** cursor referencing a table/trigger that the user is attempting to
150         ** drop.) If no one objects, then invalidate any dependent objects.
151         */

152         dropTriggerDescriptor(lcc, dm, dd, tc, triggerd, activation);
153     }
154
155     public static void dropTriggerDescriptor
156     (
157         LanguageConnectionContext lcc,
158         DependencyManager dm,
159         DataDictionary dd,
160         TransactionController tc,
161         TriggerDescriptor triggerd,
162         Activation activation
163     ) throws StandardException
164     {
165         if (SanityManager.DEBUG)
166         {
167             SanityManager.ASSERT(triggerd!=null, "trigger descriptor is null");
168         }
169
170         dm.invalidateFor(triggerd, DependencyManager.DROP_TRIGGER, lcc);
171
172         // Drop the trigger
173
dd.dropTriggerDescriptor(triggerd, tc);
174
175         // Clear the dependencies for the trigger
176
dm.clearDependencies(lcc, triggerd);
177
178         // Drop the spses
179
SPSDescriptor spsd = dd.getSPSDescriptor(triggerd.getActionId());
180
181         // there shouldn't be any dependencies, but in case
182
// there are, lets clear them
183
dm.invalidateFor(spsd, DependencyManager.DROP_TRIGGER, lcc);
184         dm.clearDependencies(lcc, spsd);
185         dd.dropSPSDescriptor(spsd, tc);
186         
187         if (triggerd.getWhenClauseId() != null)
188         {
189             spsd = dd.getSPSDescriptor(triggerd.getWhenClauseId());
190             dm.invalidateFor(spsd, DependencyManager.DROP_TRIGGER, lcc);
191             dm.clearDependencies(lcc, spsd);
192             dd.dropSPSDescriptor(spsd, tc);
193         }
194     }
195
196     public String JavaDoc toString()
197     {
198         // Do not put this under SanityManager.DEBUG - it is needed for
199
// error reporting.
200
return "DROP TRIGGER "+triggerName;
201     }
202 }
203
Popular Tags