KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > sql > dictionary > ViewDescriptor


1 /*
2
3    Derby - Class org.apache.derby.iapi.sql.dictionary.ViewDescriptor
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.iapi.sql.dictionary;
23
24 import org.apache.derby.catalog.UUID;
25
26 import org.apache.derby.iapi.sql.depend.Dependent;
27 import org.apache.derby.iapi.sql.depend.Provider;
28 import org.apache.derby.iapi.sql.dictionary.GenericDescriptorList;
29 import org.apache.derby.iapi.error.StandardException;
30 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
31 import org.apache.derby.iapi.store.access.TransactionController;
32 import org.apache.derby.iapi.sql.depend.DependencyManager;
33
34 import org.apache.derby.iapi.services.context.ContextService;
35
36 import org.apache.derby.iapi.reference.SQLState;
37 import org.apache.derby.iapi.services.sanity.SanityManager;
38 import org.apache.derby.iapi.sql.StatementType;
39 import org.apache.derby.catalog.DependableFinder;
40 import org.apache.derby.catalog.Dependable;
41 import org.apache.derby.iapi.services.io.StoredFormatIds;
42 import org.apache.derby.impl.sql.execute.DropTriggerConstantAction;
43
44 /**
45  * This is the implementation of ViewDescriptor. Users of View descriptors
46  * should only use the following methods:
47  * <ol>
48  * <li> getUUID
49  * <li> setUUID
50  * <li> getViewText
51  * <li> setViewName
52  * <li> getCheckOptionType
53  * <li> getCompSchemaId
54  * </ol>
55  *
56  * @version 0.1
57  * @author Jeff Lichtman
58  */

59
60 public final class ViewDescriptor extends TupleDescriptor
61     implements UniqueTupleDescriptor, Dependent, Provider
62 {
63     private final int checkOption;
64     private String JavaDoc viewName;
65     private final String JavaDoc viewText;
66     private UUID uuid;
67     private final UUID compSchemaId;
68
69     public static final int NO_CHECK_OPTION = 0;
70
71     /**
72      * Constructor for a ViewDescriptor.
73      *
74      * @param dataDictionary The data dictionary that this descriptor lives in
75      * @param viewID The UUID for the view
76      * @param viewName The name of the view
77      * @param viewText The text of the query expression from the view definition.
78      * @param checkOption int check option type
79      * @param compSchemaId the schemaid to compile in
80      */

81
82     public ViewDescriptor(DataDictionary dataDictionary, UUID viewID, String JavaDoc viewName, String JavaDoc viewText,
83                               int checkOption, UUID compSchemaId)
84     {
85         super( dataDictionary );
86
87         uuid = viewID;
88         this.viewText = viewText;
89         this.viewName = viewName;
90
91         /* RESOLVE - No check options for now */
92         if (SanityManager.DEBUG)
93         {
94             if (checkOption != ViewDescriptor.NO_CHECK_OPTION)
95             {
96                 SanityManager.THROWASSERT("checkOption (" + checkOption +
97                 ") expected to be " + ViewDescriptor.NO_CHECK_OPTION);
98             }
99         }
100         this.checkOption = checkOption;
101         this.compSchemaId = compSchemaId;
102     }
103
104     //
105
// ViewDescriptor interface
106
//
107

108     /**
109      * Gets the UUID of the view.
110      *
111      * @return The UUID of the view.
112      */

113     public UUID getUUID()
114     {
115         return uuid;
116     }
117
118     /**
119      * Sets the UUID of the view.
120      *
121      * @param uuid The UUID of the view.
122      */

123     public void setUUID(UUID uuid)
124     {
125         this.uuid = uuid;
126     }
127
128     /**
129      * Gets the text of the view definition.
130      *
131      * @return A String containing the text of the CREATE VIEW
132      * statement that created the view
133      */

134     public String JavaDoc getViewText()
135     {
136         return viewText;
137     }
138
139     /**
140      * Sets the name of the view.
141      *
142      * @param name The name of the view.
143      */

144     public void setViewName(String JavaDoc name)
145     {
146         viewName = name;
147     }
148
149     /**
150      * Gets an identifier telling what type of check option
151      * is on this view.
152      *
153      * @return An identifier telling what type of check option
154      * is on the view.
155      */

156     public int getCheckOptionType()
157     {
158         return checkOption;
159     }
160
161     /**
162      * Get the compilation type schema id when this view
163      * was first bound.
164      *
165      * @return the schema UUID
166      */

167     public UUID getCompSchemaId()
168     {
169         return compSchemaId;
170     }
171
172
173     //
174
// Provider interface
175
//
176

177     /**
178         @return the stored form of this provider
179
180             @see Dependable#getDependableFinder
181      */

182     public DependableFinder getDependableFinder()
183     {
184         return getDependableFinder(StoredFormatIds.VIEW_DESCRIPTOR_FINDER_V01_ID);
185     }
186
187     /**
188      * Return the name of this Provider. (Useful for errors.)
189      *
190      * @return String The name of this provider.
191      */

192     public String JavaDoc getObjectName()
193     {
194         return viewName;
195     }
196
197     /**
198      * Get the provider's UUID
199      *
200      * @return String The provider's UUID
201      */

202     public UUID getObjectID()
203     {
204         return uuid;
205     }
206
207     /**
208      * Get the provider's type.
209      *
210      * @return String The provider's type.
211      */

212     public String JavaDoc getClassType()
213     {
214         return Dependable.VIEW;
215     }
216
217     //
218
// Dependent Inteface
219
//
220
/**
221         Check that all of the dependent's dependencies are valid.
222
223         @return true if the dependent is currently valid
224      */

225     public boolean isValid()
226     {
227         return true;
228     }
229
230     /**
231         Prepare to mark the dependent as invalid (due to at least one of
232         its dependencies being invalid).
233
234         @param action The action causing the invalidation
235         @param p the provider
236
237         @exception StandardException thrown if unable to make it invalid
238      */

239     public void prepareToInvalidate(Provider p, int action,
240                     LanguageConnectionContext lcc)
241         throws StandardException
242     {
243         switch ( action )
244         {
245             /*
246              * We don't care about creating or dropping indexes or
247              * alter table on an underlying table.
248              */

249             case DependencyManager.CREATE_INDEX:
250             case DependencyManager.DROP_INDEX:
251             case DependencyManager.CREATE_CONSTRAINT:
252             case DependencyManager.ALTER_TABLE:
253             case DependencyManager.CREATE_TRIGGER:
254             case DependencyManager.DROP_TRIGGER:
255
256             case DependencyManager.BULK_INSERT:
257             case DependencyManager.COMPRESS_TABLE:
258             case DependencyManager.RENAME_INDEX:
259             case DependencyManager.UPDATE_STATISTICS:
260             case DependencyManager.DROP_STATISTICS:
261             case DependencyManager.TRUNCATE_TABLE:
262             /*
263             ** Set constriants is a bit odd in that it
264             ** will send a SET_CONSTRAINTS on the table
265             ** when it enables a constraint, rather than
266             ** on the constraint. So since we depend on
267             ** the table, we have to deal with this action.
268             */

269             case DependencyManager.SET_CONSTRAINTS_ENABLE:
270             case DependencyManager.SET_TRIGGERS_ENABLE:
271             //When REVOKE_PRIVILEGE gets sent (this happens for privilege
272
//types SELECT, UPDATE, DELETE, INSERT, REFERENCES, TRIGGER), we
273
//don't do anything here. Later in makeInvalid method, we make
274
//the ViewDescriptor drop itself.
275
case DependencyManager.REVOKE_PRIVILEGE:
276                 break;
277
278             //Notice that REVOKE_PRIVILEGE_RESTRICT is not caught earlier.
279
//It gets handled in this default: action where an exception
280
//will be thrown. This is because, if such an invalidation
281
//action type is ever received by a dependent, the dependent
282
//show throw an exception.
283
//In Derby, at this point, REVOKE_PRIVILEGE_RESTRICT gets sent
284
//when execute privilege on a routine is getting revoked.
285
default:
286
287                 DependencyManager dm;
288
289                 dm = getDataDictionary().getDependencyManager();
290                 throw StandardException.newException(SQLState.LANG_PROVIDER_HAS_DEPENDENT_VIEW,
291                     dm.getActionString(action),
292                     p.getObjectName(), viewName);
293
294         } // end switch
295
}
296
297     /**
298         Mark the dependent as invalid (due to at least one of
299         its dependencies being invalid).
300
301         @param action The action causing the invalidation
302
303         @exception StandardException thrown if unable to make it invalid
304      */

305     public void makeInvalid(int action, LanguageConnectionContext lcc)
306         throws StandardException
307     {
308         switch ( action )
309         {
310             /* We don't care about creating or dropping indexes or
311              * alter table on an underlying table.
312              */

313             case DependencyManager.CREATE_INDEX:
314             case DependencyManager.DROP_INDEX:
315             case DependencyManager.ALTER_TABLE:
316             case DependencyManager.CREATE_CONSTRAINT:
317             case DependencyManager.BULK_INSERT:
318             case DependencyManager.COMPRESS_TABLE:
319             case DependencyManager.SET_CONSTRAINTS_ENABLE:
320             case DependencyManager.SET_TRIGGERS_ENABLE:
321             case DependencyManager.CREATE_TRIGGER:
322             case DependencyManager.DROP_TRIGGER:
323             case DependencyManager.RENAME_INDEX:
324             case DependencyManager.UPDATE_STATISTICS:
325             case DependencyManager.DROP_STATISTICS:
326             case DependencyManager.TRUNCATE_TABLE:
327                 break;
328
329             //When REVOKE_PRIVILEGE gets sent (this happens for privilege
330
//types SELECT, UPDATE, DELETE, INSERT, REFERENCES, TRIGGER), we
331
//make the ViewDescriptor drop itself.
332
case DependencyManager.REVOKE_PRIVILEGE:
333                 dropViewWork(getDataDictionary(),
334                         getDataDictionary().getDependencyManager(), lcc,
335                         lcc.getTransactionExecute(),
336                         getDataDictionary().getTableDescriptor(uuid).getSchemaDescriptor(),
337                         getDataDictionary().getTableDescriptor(uuid), false);
338                 return;
339
340             default:
341
342                 /* We should never get here, since we can't have dangling references */
343                 if (SanityManager.DEBUG)
344                 {
345                     SanityManager.THROWASSERT("did not expect to get called");
346                 }
347                 break;
348
349         } // end switch
350

351     }
352
353     /**
354         Attempt to revalidate the dependent. For prepared statements,
355         this could go through its dependencies and check that they
356         are up to date; if not, it would recompile the statement.
357         Any failure during this attempt should throw
358         StandardException.unableToRevalidate().
359
360         @exception StandardException thrown if unable to make it valid
361      */

362     public void makeValid(LanguageConnectionContext lcc)
363         throws StandardException
364     {
365     }
366
367     //
368
// class interface
369
//
370

371     /**
372      * Prints the contents of the ViewDescriptor
373      *
374      * @return The contents as a String
375      */

376     public String JavaDoc toString()
377     {
378         if (SanityManager.DEBUG)
379         {
380             return "uuid: " + uuid + " viewName: " + viewName + "\n" +
381                 "viewText: " + viewText + "\n" +
382                 "checkOption: " + checkOption + "\n" +
383                 "compSchemaId: " + compSchemaId + "\n";
384         }
385         else
386         {
387             return "";
388         }
389     }
390
391     public void dropViewWork(DataDictionary dd, DependencyManager dm,
392                               LanguageConnectionContext lcc, TransactionController tc,
393                               SchemaDescriptor sd, TableDescriptor td, boolean cascade)
394         throws StandardException
395     {
396         /* Drop the columns */
397         dd.dropAllColumnDescriptors(td.getUUID(), tc);
398
399         /* Prepare all dependents to invalidate. (This is there chance
400          * to say that they can't be invalidated. For example, an open
401          * cursor referencing a table/view that the user is attempting to
402          * drop.) If no one objects, then invalidate any dependent objects.
403          */

404         dm.invalidateFor(td, DependencyManager.DROP_VIEW, lcc);
405
406         /* Clear the dependencies for the view */
407         dm.clearDependencies(lcc, this);
408
409         /* Drop the view */
410         dd.dropViewDescriptor(this, tc);
411
412         /* Drop all table and column permission descriptors */
413         dd.dropAllTableAndColPermDescriptors(td.getUUID(), tc);
414
415         /* Drop the table */
416         dd.dropTableDescriptor(td, sd, tc);
417     }
418
419
420 }
421
Popular Tags