KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > catalog > DDdependableFinder


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.catalog.DDdependableFinder
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.catalog;
23
24 import org.apache.derby.iapi.services.context.ContextManager;
25 import org.apache.derby.iapi.services.context.ContextService;
26
27 import org.apache.derby.iapi.services.monitor.Monitor;
28
29 import org.apache.derby.iapi.services.sanity.SanityManager;
30
31 import org.apache.derby.iapi.services.io.StoredFormatIds;
32 import org.apache.derby.iapi.services.io.Formatable;
33
34 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
35 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor;
36 import org.apache.derby.iapi.sql.dictionary.DefaultDescriptor;
37 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
38 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
39 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
40
41 import org.apache.derby.iapi.error.StandardException;
42 import org.apache.derby.iapi.error.PublicAPI;
43
44 import org.apache.derby.iapi.services.i18n.MessageService;
45
46 import org.apache.derby.iapi.services.uuid.UUIDFactory;
47
48 import org.apache.derby.catalog.UUID;
49 import org.apache.derby.catalog.Dependable;
50 import org.apache.derby.catalog.DependableFinder;
51
52 import org.apache.derby.iapi.reference.SQLState;
53
54 import java.io.ObjectOutput JavaDoc;
55 import java.io.ObjectInput JavaDoc;
56 import java.io.IOException JavaDoc;
57
58 /**
59  * Class for all DependableFinders in the core DataDictionary
60  *
61  *
62  * @author Rick
63  */

64
65 public class DDdependableFinder implements DependableFinder, Formatable
66 {
67     ////////////////////////////////////////////////////////////////////////
68
//
69
// STATE
70
//
71
////////////////////////////////////////////////////////////////////////
72

73     private transient DataDictionary dataDictionary;
74     private transient UUIDFactory uuidFactory;
75
76     private final int formatId;
77
78     ////////////////////////////////////////////////////////////////////////
79
//
80
// CONSTRUCTORS
81
//
82
////////////////////////////////////////////////////////////////////////
83

84     /**
85       * Public constructor for Formatable hoo-hah.
86       */

87     public DDdependableFinder(int formatId)
88     {
89         this.formatId = formatId;
90     }
91
92     //////////////////////////////////////////////////////////////////
93
//
94
// OBJECT SUPPORT
95
//
96
//////////////////////////////////////////////////////////////////
97

98     public String JavaDoc toString()
99     {
100         return getSQLObjectType();
101     }
102
103     //////////////////////////////////////////////////////////////////
104
//
105
// VACUOUS FORMATABLE INTERFACE. ALL THAT A VACUOUSDEPENDABLEFINDER
106
// NEEDS TO DO IS STAMP ITS FORMAT ID ONTO THE OUTPUT STREAM.
107
//
108
//////////////////////////////////////////////////////////////////
109

110     /**
111      * Read this object from a stream of stored objects. Nothing to
112      * do. Our persistent representation is just a 2-byte format id.
113      *
114      * @param in read this.
115      */

116     public void readExternal( ObjectInput JavaDoc in )
117             throws IOException JavaDoc, ClassNotFoundException JavaDoc
118     {
119     }
120
121     /**
122      * Write this object to a stream of stored objects. Again, nothing
123      * to do. We just stamp the output stream with our Format id.
124      *
125      * @param out write bytes here.
126      */

127     public void writeExternal( ObjectOutput JavaDoc out )
128             throws IOException JavaDoc
129     {
130     }
131
132     /**
133      * Get the formatID which corresponds to this class.
134      *
135      * @return the formatID of this class
136      */

137     public final int getTypeFormatId()
138     {
139         return formatId;
140     }
141
142     ////////////////////////////////////////////////////////////////////////
143
//
144
// DDdependable METHODS
145
//
146
////////////////////////////////////////////////////////////////////////
147

148
149     /**
150      * Gets the in-memory object associated with the passed-in object ID.
151      *
152      * @param dependableObjectID the UUID of the Dependable as a String.
153      * Used to locate that Dependable
154      *
155      * @return the associated Dependable
156      *
157      * @exception java.sql.SQLException thrown on error
158      */

159     public final Dependable getDependable(String JavaDoc dependableObjectID) throws java.sql.SQLException JavaDoc
160     {
161         /*
162         ** Call the specific implementation of getDependable
163         ** to do the work
164         */

165         return getDependable(recreateUUID(dependableObjectID));
166     }
167
168     /**
169       * Gets the AliasDescriptor associated with the passed-in object ID.
170       *
171       * @param dependableObjectID the object ID of an Alias. Used to locate its AliasDescriptor
172       *
173       * @return the associated AliasDescriptor
174       * @exception java.sql.SQLException thrown on error
175       */

176     public final Dependable getDependable(UUID dependableObjectID)
177          throws java.sql.SQLException JavaDoc
178     {
179         try
180         {
181             return getDependable(getDataDictionary(),dependableObjectID);
182         }
183         catch (StandardException se)
184         {
185             throw PublicAPI.wrapStandardException(se);
186         }
187     }
188
189     /**
190       * @see DependableFinder#getSQLObjectName
191       * @exception java.sql.SQLException thrown on error
192       */

193     public final String JavaDoc getSQLObjectName(String JavaDoc idString) throws java.sql.SQLException JavaDoc
194     {
195
196         try {
197
198             // This should really be getDependable(idString).getObjectName()
199
// and then the sub-classes would not have to provide a getSQLObjectName
200
// method. Currently getDependable(idString).getObjectName() would
201
// not always return the same result - fix in main.
202

203             return getSQLObjectName(getDataDictionary(), recreateUUID(idString));
204         }
205         catch (StandardException se)
206         {
207             throw PublicAPI.wrapStandardException( se );
208         }
209     }
210
211
212     /**
213       * @see DependableFinder#getSQLObjectType
214       */

215     public String JavaDoc getSQLObjectType()
216     {
217         switch (formatId)
218         {
219             case StoredFormatIds.ALIAS_DESCRIPTOR_FINDER_V01_ID:
220                 return Dependable.ALIAS;
221
222             case StoredFormatIds.CONGLOMERATE_DESCRIPTOR_FINDER_V01_ID:
223                 return Dependable.CONGLOMERATE;
224
225             case StoredFormatIds.CONSTRAINT_DESCRIPTOR_FINDER_V01_ID:
226                 return Dependable.CONSTRAINT;
227
228             case StoredFormatIds.DEFAULT_DESCRIPTOR_FINDER_V01_ID:
229                 return Dependable.DEFAULT;
230
231             case StoredFormatIds.FILE_INFO_FINDER_V01_ID:
232                 return Dependable.FILE;
233
234             case StoredFormatIds.SCHEMA_DESCRIPTOR_FINDER_V01_ID:
235                 return Dependable.SCHEMA;
236
237             case StoredFormatIds.SPS_DESCRIPTOR_FINDER_V01_ID:
238                 return Dependable.STORED_PREPARED_STATEMENT;
239
240             case StoredFormatIds.TABLE_DESCRIPTOR_FINDER_V01_ID:
241                 return Dependable.TABLE;
242
243             case StoredFormatIds.COLUMN_DESCRIPTOR_FINDER_V01_ID:
244                 return Dependable.COLUMNS_IN_TABLE;
245
246             case StoredFormatIds.TRIGGER_DESCRIPTOR_FINDER_V01_ID:
247                 return Dependable.TRIGGER;
248
249             case StoredFormatIds.VIEW_DESCRIPTOR_FINDER_V01_ID:
250                 return Dependable.VIEW;
251
252             case StoredFormatIds.TABLE_PERMISSION_FINDER_V01_ID:
253                 return Dependable.TABLE_PERMISSION;
254             
255             case StoredFormatIds.COLUMNS_PERMISSION_FINDER_V01_ID:
256                 return Dependable.COLUMNS_PERMISSION;
257
258             case StoredFormatIds.ROUTINE_PERMISSION_FINDER_V01_ID:
259                 return Dependable.ROUTINE_PERMISSION;
260
261             default:
262                 if (SanityManager.DEBUG)
263                 {
264                     SanityManager.THROWASSERT(
265                         "getSQLObjectType() called with unexpeced formatId = " + formatId);
266                 }
267                 return null;
268         }
269     }
270
271     /**
272       * Gets the datadictionary for this connection.
273       *
274       * @return the data dictionary for this connection
275       *
276       * @exception StandardException Thrown on failure
277       */

278     private DataDictionary getDataDictionary()
279                         throws StandardException
280     {
281         if ( dataDictionary == null )
282         {
283             ContextManager cm = ContextService.getFactory().getCurrentContextManager();
284             DataDictionaryContext ddc = (DataDictionaryContext)
285                                           (cm.getContext(DataDictionaryContext.CONTEXT_ID));
286             dataDictionary = ddc.getDataDictionary();
287         }
288         return dataDictionary;
289     }
290
291     /**
292      * Get the UUID for the given string
293      *
294      * @param idString the string
295      *
296      * @return the UUID
297      */

298     private UUID recreateUUID(String JavaDoc idString)
299     {
300         if (uuidFactory == null)
301         {
302             uuidFactory = Monitor.getMonitor().getUUIDFactory();
303         }
304         return uuidFactory.recreateUUID(idString);
305     }
306
307     /**
308         Get the dependable for the given UUID
309         @exception StandardException thrown on error
310     */

311     protected Dependable getDependable(DataDictionary dd, UUID dependableObjectID)
312         throws StandardException
313     {
314         switch (formatId)
315         {
316             case StoredFormatIds.ALIAS_DESCRIPTOR_FINDER_V01_ID:
317                 return dd.getAliasDescriptor(dependableObjectID);
318
319             case StoredFormatIds.CONGLOMERATE_DESCRIPTOR_FINDER_V01_ID:
320                 return dd.getConglomerateDescriptor(dependableObjectID);
321
322             case StoredFormatIds.CONSTRAINT_DESCRIPTOR_FINDER_V01_ID:
323                 return dd.getConstraintDescriptor(dependableObjectID);
324
325             case StoredFormatIds.DEFAULT_DESCRIPTOR_FINDER_V01_ID:
326                 ColumnDescriptor cd = dd.getColumnDescriptorByDefaultId(dependableObjectID);
327                 DefaultDescriptor ddi = new DefaultDescriptor(
328                                                 dd,
329                                                 cd.getDefaultUUID(), cd.getReferencingUUID(),
330                                                 cd.getPosition());
331                 return ddi;
332
333             case StoredFormatIds.FILE_INFO_FINDER_V01_ID:
334                 return dd.getFileInfoDescriptor(dependableObjectID);
335
336             case StoredFormatIds.SCHEMA_DESCRIPTOR_FINDER_V01_ID:
337                 return dd.getSchemaDescriptor(dependableObjectID, null);
338
339             case StoredFormatIds.SPS_DESCRIPTOR_FINDER_V01_ID:
340                 return dd.getSPSDescriptor(dependableObjectID);
341
342             case StoredFormatIds.TABLE_DESCRIPTOR_FINDER_V01_ID:
343                 return dd.getTableDescriptor(dependableObjectID);
344
345             case StoredFormatIds.TRIGGER_DESCRIPTOR_FINDER_V01_ID:
346                 return dd.getTriggerDescriptor(dependableObjectID);
347
348             case StoredFormatIds.VIEW_DESCRIPTOR_FINDER_V01_ID:
349                 return dd.getViewDescriptor(dependableObjectID);
350
351             case StoredFormatIds.COLUMNS_PERMISSION_FINDER_V01_ID:
352                 return dd.getColumnPermissions(dependableObjectID);
353
354             case StoredFormatIds.TABLE_PERMISSION_FINDER_V01_ID:
355                 return dd.getTablePermissions(dependableObjectID);
356
357             case StoredFormatIds.ROUTINE_PERMISSION_FINDER_V01_ID:
358                 return dd.getRoutinePermissions(dependableObjectID);
359
360             default:
361                 if (SanityManager.DEBUG)
362                 {
363                     SanityManager.THROWASSERT(
364                         "getDependable() called with unexpeced formatId = " + formatId);
365                 }
366                 return null;
367         }
368     }
369
370     /**
371         Get the SQL object name for the given UUID
372         @exception StandardException thrown on error
373     */

374     protected String JavaDoc getSQLObjectName(DataDictionary dd, UUID dependableObjectID)
375         throws StandardException
376     {
377         switch (formatId)
378         {
379             case StoredFormatIds.ALIAS_DESCRIPTOR_FINDER_V01_ID:
380                 return dd.getAliasDescriptor(dependableObjectID).getDescriptorName();
381
382             case StoredFormatIds.CONGLOMERATE_DESCRIPTOR_FINDER_V01_ID:
383                 return dd.getConglomerateDescriptor(dependableObjectID).getConglomerateName();
384
385             case StoredFormatIds.CONSTRAINT_DESCRIPTOR_FINDER_V01_ID:
386                 return dd.getConstraintDescriptor(dependableObjectID).getConstraintName();
387
388             case StoredFormatIds.DEFAULT_DESCRIPTOR_FINDER_V01_ID:
389                 ColumnDescriptor columnDescriptor = dd.getColumnDescriptorByDefaultId( dependableObjectID );
390                 TableDescriptor tableDescriptor = dd.getTableDescriptor(
391                                         columnDescriptor.getReferencingUUID());
392
393                 return MessageService.getTextMessage(
394                             SQLState.LANG_COLUMN_DEFAULT,
395                             tableDescriptor.getQualifiedName() + "." +
396                             columnDescriptor.getColumnName());
397
398             case StoredFormatIds.FILE_INFO_FINDER_V01_ID:
399                 return dd.getFileInfoDescriptor(dependableObjectID).getName();
400
401             case StoredFormatIds.SCHEMA_DESCRIPTOR_FINDER_V01_ID:
402                 return dd.getSchemaDescriptor(dependableObjectID, null).getSchemaName();
403
404             case StoredFormatIds.SPS_DESCRIPTOR_FINDER_V01_ID:
405                 return dd.getSPSDescriptor(dependableObjectID).getName();
406
407             case StoredFormatIds.TABLE_DESCRIPTOR_FINDER_V01_ID:
408             case StoredFormatIds.COLUMN_DESCRIPTOR_FINDER_V01_ID:
409                 return getDependable(dd, dependableObjectID).getObjectName();
410
411             case StoredFormatIds.TRIGGER_DESCRIPTOR_FINDER_V01_ID:
412                 return dd.getTriggerDescriptor(dependableObjectID).getName();
413
414             case StoredFormatIds.VIEW_DESCRIPTOR_FINDER_V01_ID:
415                 return dd.getTableDescriptor(dependableObjectID).getName();
416
417             case StoredFormatIds.COLUMNS_PERMISSION_FINDER_V01_ID:
418                 return dd.getColumnPermissions(dependableObjectID).getObjectName();
419
420             case StoredFormatIds.TABLE_PERMISSION_FINDER_V01_ID:
421                 return dd.getTablePermissions(dependableObjectID).getObjectName();
422
423             case StoredFormatIds.ROUTINE_PERMISSION_FINDER_V01_ID:
424                 return dd.getRoutinePermissions(dependableObjectID).getObjectName();
425
426             default:
427                 if (SanityManager.DEBUG)
428                 {
429                     SanityManager.THROWASSERT(
430                         "getSQLObjectName() called with unexpeced formatId = " + formatId);
431                 }
432                 return null;
433         }
434     }
435 }
436
Popular Tags