001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.catalog.DDdependableFinder
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.catalog;
023:
024: import org.apache.derby.iapi.services.context.ContextManager;
025: import org.apache.derby.iapi.services.context.ContextService;
026:
027: import org.apache.derby.iapi.services.monitor.Monitor;
028:
029: import org.apache.derby.iapi.services.sanity.SanityManager;
030:
031: import org.apache.derby.iapi.services.io.StoredFormatIds;
032: import org.apache.derby.iapi.services.io.Formatable;
033:
034: import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
035: import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor;
036: import org.apache.derby.iapi.sql.dictionary.DefaultDescriptor;
037: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
038: import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
039: import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
040:
041: import org.apache.derby.iapi.error.StandardException;
042: import org.apache.derby.iapi.error.PublicAPI;
043:
044: import org.apache.derby.iapi.services.i18n.MessageService;
045:
046: import org.apache.derby.iapi.services.uuid.UUIDFactory;
047:
048: import org.apache.derby.catalog.UUID;
049: import org.apache.derby.catalog.Dependable;
050: import org.apache.derby.catalog.DependableFinder;
051:
052: import org.apache.derby.iapi.reference.SQLState;
053:
054: import java.io.ObjectOutput;
055: import java.io.ObjectInput;
056: import java.io.IOException;
057:
058: /**
059: * Class for all DependableFinders in the core DataDictionary
060: *
061: *
062: * @author Rick
063: */
064:
065: public class DDdependableFinder implements DependableFinder, Formatable {
066: ////////////////////////////////////////////////////////////////////////
067: //
068: // STATE
069: //
070: ////////////////////////////////////////////////////////////////////////
071:
072: private transient DataDictionary dataDictionary;
073: private transient UUIDFactory uuidFactory;
074:
075: private final int formatId;
076:
077: ////////////////////////////////////////////////////////////////////////
078: //
079: // CONSTRUCTORS
080: //
081: ////////////////////////////////////////////////////////////////////////
082:
083: /**
084: * Public constructor for Formatable hoo-hah.
085: */
086: public DDdependableFinder(int formatId) {
087: this .formatId = formatId;
088: }
089:
090: //////////////////////////////////////////////////////////////////
091: //
092: // OBJECT SUPPORT
093: //
094: //////////////////////////////////////////////////////////////////
095:
096: public String toString() {
097: return getSQLObjectType();
098: }
099:
100: //////////////////////////////////////////////////////////////////
101: //
102: // VACUOUS FORMATABLE INTERFACE. ALL THAT A VACUOUSDEPENDABLEFINDER
103: // NEEDS TO DO IS STAMP ITS FORMAT ID ONTO THE OUTPUT STREAM.
104: //
105: //////////////////////////////////////////////////////////////////
106:
107: /**
108: * Read this object from a stream of stored objects. Nothing to
109: * do. Our persistent representation is just a 2-byte format id.
110: *
111: * @param in read this.
112: */
113: public void readExternal(ObjectInput in) throws IOException,
114: ClassNotFoundException {
115: }
116:
117: /**
118: * Write this object to a stream of stored objects. Again, nothing
119: * to do. We just stamp the output stream with our Format id.
120: *
121: * @param out write bytes here.
122: */
123: public void writeExternal(ObjectOutput out) throws IOException {
124: }
125:
126: /**
127: * Get the formatID which corresponds to this class.
128: *
129: * @return the formatID of this class
130: */
131: public final int getTypeFormatId() {
132: return formatId;
133: }
134:
135: ////////////////////////////////////////////////////////////////////////
136: //
137: // DDdependable METHODS
138: //
139: ////////////////////////////////////////////////////////////////////////
140:
141: /**
142: * Gets the in-memory object associated with the passed-in object ID.
143: *
144: * @param dependableObjectID the UUID of the Dependable as a String.
145: * Used to locate that Dependable
146: *
147: * @return the associated Dependable
148: *
149: * @exception java.sql.SQLException thrown on error
150: */
151: public final Dependable getDependable(String dependableObjectID)
152: throws java.sql.SQLException {
153: /*
154: ** Call the specific implementation of getDependable
155: ** to do the work
156: */
157: return getDependable(recreateUUID(dependableObjectID));
158: }
159:
160: /**
161: * Gets the AliasDescriptor associated with the passed-in object ID.
162: *
163: * @param dependableObjectID the object ID of an Alias. Used to locate its AliasDescriptor
164: *
165: * @return the associated AliasDescriptor
166: * @exception java.sql.SQLException thrown on error
167: */
168: public final Dependable getDependable(UUID dependableObjectID)
169: throws java.sql.SQLException {
170: try {
171: return getDependable(getDataDictionary(),
172: dependableObjectID);
173: } catch (StandardException se) {
174: throw PublicAPI.wrapStandardException(se);
175: }
176: }
177:
178: /**
179: * @see DependableFinder#getSQLObjectName
180: * @exception java.sql.SQLException thrown on error
181: */
182: public final String getSQLObjectName(String idString)
183: throws java.sql.SQLException {
184:
185: try {
186:
187: // This should really be getDependable(idString).getObjectName()
188: // and then the sub-classes would not have to provide a getSQLObjectName
189: // method. Currently getDependable(idString).getObjectName() would
190: // not always return the same result - fix in main.
191:
192: return getSQLObjectName(getDataDictionary(),
193: recreateUUID(idString));
194: } catch (StandardException se) {
195: throw PublicAPI.wrapStandardException(se);
196: }
197: }
198:
199: /**
200: * @see DependableFinder#getSQLObjectType
201: */
202: public String getSQLObjectType() {
203: switch (formatId) {
204: case StoredFormatIds.ALIAS_DESCRIPTOR_FINDER_V01_ID:
205: return Dependable.ALIAS;
206:
207: case StoredFormatIds.CONGLOMERATE_DESCRIPTOR_FINDER_V01_ID:
208: return Dependable.CONGLOMERATE;
209:
210: case StoredFormatIds.CONSTRAINT_DESCRIPTOR_FINDER_V01_ID:
211: return Dependable.CONSTRAINT;
212:
213: case StoredFormatIds.DEFAULT_DESCRIPTOR_FINDER_V01_ID:
214: return Dependable.DEFAULT;
215:
216: case StoredFormatIds.FILE_INFO_FINDER_V01_ID:
217: return Dependable.FILE;
218:
219: case StoredFormatIds.SCHEMA_DESCRIPTOR_FINDER_V01_ID:
220: return Dependable.SCHEMA;
221:
222: case StoredFormatIds.SPS_DESCRIPTOR_FINDER_V01_ID:
223: return Dependable.STORED_PREPARED_STATEMENT;
224:
225: case StoredFormatIds.TABLE_DESCRIPTOR_FINDER_V01_ID:
226: return Dependable.TABLE;
227:
228: case StoredFormatIds.COLUMN_DESCRIPTOR_FINDER_V01_ID:
229: return Dependable.COLUMNS_IN_TABLE;
230:
231: case StoredFormatIds.TRIGGER_DESCRIPTOR_FINDER_V01_ID:
232: return Dependable.TRIGGER;
233:
234: case StoredFormatIds.VIEW_DESCRIPTOR_FINDER_V01_ID:
235: return Dependable.VIEW;
236:
237: case StoredFormatIds.TABLE_PERMISSION_FINDER_V01_ID:
238: return Dependable.TABLE_PERMISSION;
239:
240: case StoredFormatIds.COLUMNS_PERMISSION_FINDER_V01_ID:
241: return Dependable.COLUMNS_PERMISSION;
242:
243: case StoredFormatIds.ROUTINE_PERMISSION_FINDER_V01_ID:
244: return Dependable.ROUTINE_PERMISSION;
245:
246: default:
247: if (SanityManager.DEBUG) {
248: SanityManager
249: .THROWASSERT("getSQLObjectType() called with unexpeced formatId = "
250: + formatId);
251: }
252: return null;
253: }
254: }
255:
256: /**
257: * Gets the datadictionary for this connection.
258: *
259: * @return the data dictionary for this connection
260: *
261: * @exception StandardException Thrown on failure
262: */
263: private DataDictionary getDataDictionary() throws StandardException {
264: if (dataDictionary == null) {
265: ContextManager cm = ContextService.getFactory()
266: .getCurrentContextManager();
267: DataDictionaryContext ddc = (DataDictionaryContext) (cm
268: .getContext(DataDictionaryContext.CONTEXT_ID));
269: dataDictionary = ddc.getDataDictionary();
270: }
271: return dataDictionary;
272: }
273:
274: /**
275: * Get the UUID for the given string
276: *
277: * @param idString the string
278: *
279: * @return the UUID
280: */
281: private UUID recreateUUID(String idString) {
282: if (uuidFactory == null) {
283: uuidFactory = Monitor.getMonitor().getUUIDFactory();
284: }
285: return uuidFactory.recreateUUID(idString);
286: }
287:
288: /**
289: Get the dependable for the given UUID
290: @exception StandardException thrown on error
291: */
292: protected Dependable getDependable(DataDictionary dd,
293: UUID dependableObjectID) throws StandardException {
294: switch (formatId) {
295: case StoredFormatIds.ALIAS_DESCRIPTOR_FINDER_V01_ID:
296: return dd.getAliasDescriptor(dependableObjectID);
297:
298: case StoredFormatIds.CONGLOMERATE_DESCRIPTOR_FINDER_V01_ID:
299: return dd.getConglomerateDescriptor(dependableObjectID);
300:
301: case StoredFormatIds.CONSTRAINT_DESCRIPTOR_FINDER_V01_ID:
302: return dd.getConstraintDescriptor(dependableObjectID);
303:
304: case StoredFormatIds.DEFAULT_DESCRIPTOR_FINDER_V01_ID:
305: ColumnDescriptor cd = dd
306: .getColumnDescriptorByDefaultId(dependableObjectID);
307: DefaultDescriptor ddi = new DefaultDescriptor(dd, cd
308: .getDefaultUUID(), cd.getReferencingUUID(), cd
309: .getPosition());
310: return ddi;
311:
312: case StoredFormatIds.FILE_INFO_FINDER_V01_ID:
313: return dd.getFileInfoDescriptor(dependableObjectID);
314:
315: case StoredFormatIds.SCHEMA_DESCRIPTOR_FINDER_V01_ID:
316: return dd.getSchemaDescriptor(dependableObjectID, null);
317:
318: case StoredFormatIds.SPS_DESCRIPTOR_FINDER_V01_ID:
319: return dd.getSPSDescriptor(dependableObjectID);
320:
321: case StoredFormatIds.TABLE_DESCRIPTOR_FINDER_V01_ID:
322: return dd.getTableDescriptor(dependableObjectID);
323:
324: case StoredFormatIds.TRIGGER_DESCRIPTOR_FINDER_V01_ID:
325: return dd.getTriggerDescriptor(dependableObjectID);
326:
327: case StoredFormatIds.VIEW_DESCRIPTOR_FINDER_V01_ID:
328: return dd.getViewDescriptor(dependableObjectID);
329:
330: case StoredFormatIds.COLUMNS_PERMISSION_FINDER_V01_ID:
331: return dd.getColumnPermissions(dependableObjectID);
332:
333: case StoredFormatIds.TABLE_PERMISSION_FINDER_V01_ID:
334: return dd.getTablePermissions(dependableObjectID);
335:
336: case StoredFormatIds.ROUTINE_PERMISSION_FINDER_V01_ID:
337: return dd.getRoutinePermissions(dependableObjectID);
338:
339: default:
340: if (SanityManager.DEBUG) {
341: SanityManager
342: .THROWASSERT("getDependable() called with unexpeced formatId = "
343: + formatId);
344: }
345: return null;
346: }
347: }
348:
349: /**
350: Get the SQL object name for the given UUID
351: @exception StandardException thrown on error
352: */
353: protected String getSQLObjectName(DataDictionary dd,
354: UUID dependableObjectID) throws StandardException {
355: switch (formatId) {
356: case StoredFormatIds.ALIAS_DESCRIPTOR_FINDER_V01_ID:
357: return dd.getAliasDescriptor(dependableObjectID)
358: .getDescriptorName();
359:
360: case StoredFormatIds.CONGLOMERATE_DESCRIPTOR_FINDER_V01_ID:
361: return dd.getConglomerateDescriptor(dependableObjectID)
362: .getConglomerateName();
363:
364: case StoredFormatIds.CONSTRAINT_DESCRIPTOR_FINDER_V01_ID:
365: return dd.getConstraintDescriptor(dependableObjectID)
366: .getConstraintName();
367:
368: case StoredFormatIds.DEFAULT_DESCRIPTOR_FINDER_V01_ID:
369: ColumnDescriptor columnDescriptor = dd
370: .getColumnDescriptorByDefaultId(dependableObjectID);
371: TableDescriptor tableDescriptor = dd
372: .getTableDescriptor(columnDescriptor
373: .getReferencingUUID());
374:
375: return MessageService.getTextMessage(
376: SQLState.LANG_COLUMN_DEFAULT, tableDescriptor
377: .getQualifiedName()
378: + "." + columnDescriptor.getColumnName());
379:
380: case StoredFormatIds.FILE_INFO_FINDER_V01_ID:
381: return dd.getFileInfoDescriptor(dependableObjectID)
382: .getName();
383:
384: case StoredFormatIds.SCHEMA_DESCRIPTOR_FINDER_V01_ID:
385: return dd.getSchemaDescriptor(dependableObjectID, null)
386: .getSchemaName();
387:
388: case StoredFormatIds.SPS_DESCRIPTOR_FINDER_V01_ID:
389: return dd.getSPSDescriptor(dependableObjectID).getName();
390:
391: case StoredFormatIds.TABLE_DESCRIPTOR_FINDER_V01_ID:
392: case StoredFormatIds.COLUMN_DESCRIPTOR_FINDER_V01_ID:
393: return getDependable(dd, dependableObjectID)
394: .getObjectName();
395:
396: case StoredFormatIds.TRIGGER_DESCRIPTOR_FINDER_V01_ID:
397: return dd.getTriggerDescriptor(dependableObjectID)
398: .getName();
399:
400: case StoredFormatIds.VIEW_DESCRIPTOR_FINDER_V01_ID:
401: return dd.getTableDescriptor(dependableObjectID).getName();
402:
403: case StoredFormatIds.COLUMNS_PERMISSION_FINDER_V01_ID:
404: return dd.getColumnPermissions(dependableObjectID)
405: .getObjectName();
406:
407: case StoredFormatIds.TABLE_PERMISSION_FINDER_V01_ID:
408: return dd.getTablePermissions(dependableObjectID)
409: .getObjectName();
410:
411: case StoredFormatIds.ROUTINE_PERMISSION_FINDER_V01_ID:
412: return dd.getRoutinePermissions(dependableObjectID)
413: .getObjectName();
414:
415: default:
416: if (SanityManager.DEBUG) {
417: SanityManager
418: .THROWASSERT("getSQLObjectName() called with unexpeced formatId = "
419: + formatId);
420: }
421: return null;
422: }
423: }
424: }
|