001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.catalog.SYSALIASESRowFactory
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.types.TypeId;
025: import org.apache.derby.iapi.reference.JDBC30Translation;
026: import org.apache.derby.iapi.sql.dictionary.SystemColumn;
027: import org.apache.derby.catalog.TypeDescriptor;
028:
029: import org.apache.derby.iapi.types.DataValueDescriptor;
030:
031: import org.apache.derby.iapi.types.DataValueFactory;
032: import org.apache.derby.iapi.types.RowLocation;
033:
034: import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;
035: import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
036: import org.apache.derby.iapi.sql.dictionary.AliasDescriptor;
037: import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
038: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
039: import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
040: import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;
041:
042: import org.apache.derby.iapi.sql.execute.ExecutionContext;
043: import org.apache.derby.iapi.sql.execute.ExecIndexRow;
044: import org.apache.derby.iapi.sql.execute.ExecRow;
045: import org.apache.derby.iapi.sql.execute.ExecutionFactory;
046:
047: import org.apache.derby.iapi.error.StandardException;
048:
049: import org.apache.derby.iapi.services.sanity.SanityManager;
050:
051: import org.apache.derby.iapi.services.uuid.UUIDFactory;
052:
053: import org.apache.derby.catalog.AliasInfo;
054: import org.apache.derby.catalog.UUID;
055:
056: /**
057: * Factory for creating a SYSALIASES row.
058: *
059: * Here are the directions for adding a new system supplied alias.
060: * Misc:
061: * All system supplied aliases are class aliases at this point.
062: * Additional arrays will need to be added if we supply system
063: * aliases of other types.
064: * The preloadAliasIDs array is an array of hard coded UUIDs
065: * for the system supplied aliases.
066: * The preloadAliases array is the array of aliases
067: * for the system supplied aliases. This array is in alphabetical
068: * order by package and class in Xena. Each alias is the uppercase
069: * class name of the alias.
070: * The preloadJavaClassNames array is the array of full package.class
071: * names for the system supplied aliases. This array is in alphabetical
072: * order by package and class in Xena.
073: * SYSALIASES_NUM_BOOT_ROWS is the number of boot rows in sys.sysaliases
074: * in a new database.
075: *
076: *
077: * @author jerry
078: */
079:
080: class SYSALIASESRowFactory extends CatalogRowFactory {
081:
082: private static final int SYSALIASES_COLUMN_COUNT = 9;
083: private static final int SYSALIASES_ALIASID = 1;
084: private static final int SYSALIASES_ALIAS = 2;
085: private static final int SYSALIASES_SCHEMAID = 3;
086: private static final int SYSALIASES_JAVACLASSNAME = 4;
087: private static final int SYSALIASES_ALIASTYPE = 5;
088: private static final int SYSALIASES_NAMESPACE = 6;
089: private static final int SYSALIASES_SYSTEMALIAS = 7;
090: public static final int SYSALIASES_ALIASINFO = 8;
091: private static final int SYSALIASES_SPECIFIC_NAME = 9;
092:
093: protected static final int SYSALIASES_INDEX1_ID = 0;
094:
095: protected static final int SYSALIASES_INDEX2_ID = 1;
096:
097: protected static final int SYSALIASES_INDEX3_ID = 2;
098:
099: // null means all unique.
100: private static final boolean[] uniqueness = null;
101:
102: private static int[][] indexColumnPositions = {
103: { SYSALIASES_SCHEMAID, SYSALIASES_ALIAS,
104: SYSALIASES_NAMESPACE }, { SYSALIASES_ALIASID },
105: { SYSALIASES_SCHEMAID, SYSALIASES_SPECIFIC_NAME }, };
106:
107: private static final String[] uuids = {
108: "c013800d-00d7-ddbd-08ce-000a0a411400" // catalog UUID
109: , "c013800d-00d7-ddbd-75d4-000a0a411400" // heap UUID
110: , "c013800d-00d7-ddbe-b99d-000a0a411400" // SYSALIASES_INDEX1
111: , "c013800d-00d7-ddbe-c4e1-000a0a411400" // SYSALIASES_INDEX2
112: , "c013800d-00d7-ddbe-34ae-000a0a411400" // SYSALIASES_INDEX3
113: };
114:
115: /////////////////////////////////////////////////////////////////////////////
116: //
117: // CONSTRUCTORS
118: //
119: /////////////////////////////////////////////////////////////////////////////
120:
121: public SYSALIASESRowFactory(UUIDFactory uuidf, ExecutionFactory ef,
122: DataValueFactory dvf, boolean convertIdToLower) {
123: super (uuidf, ef, dvf, convertIdToLower);
124: initInfo(SYSALIASES_COLUMN_COUNT, "SYSALIASES",
125: indexColumnPositions, uniqueness, uuids);
126: }
127:
128: /////////////////////////////////////////////////////////////////////////////
129: //
130: // METHODS
131: //
132: /////////////////////////////////////////////////////////////////////////////
133:
134: /**
135: * Make a SYSALIASES row
136: *
137: *
138: * @return Row suitable for inserting into SYSALIASES.
139: *
140: * @exception StandardException thrown on failure
141: */
142: public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent)
143: throws StandardException {
144: DataValueDescriptor col;
145: String schemaID = null;
146: String javaClassName = null;
147: String sAliasType = null;
148: String aliasID = null;
149: String aliasName = null;
150: String specificName = null;
151: char cAliasType = AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR;
152: char cNameSpace = AliasInfo.ALIAS_NAME_SPACE_PROCEDURE_AS_CHAR;
153: boolean systemAlias = false;
154: AliasInfo aliasInfo = null;
155:
156: if (td != null) {
157:
158: AliasDescriptor ad = (AliasDescriptor) td;
159: aliasID = ad.getUUID().toString();
160: aliasName = ad.getDescriptorName();
161: schemaID = ad.getSchemaUUID().toString();
162: javaClassName = ad.getJavaClassName();
163: cAliasType = ad.getAliasType();
164: cNameSpace = ad.getNameSpace();
165: systemAlias = ad.getSystemAlias();
166: aliasInfo = ad.getAliasInfo();
167: specificName = ad.getSpecificName();
168:
169: char[] charArray = new char[1];
170: charArray[0] = cAliasType;
171: sAliasType = new String(charArray);
172:
173: if (SanityManager.DEBUG) {
174: switch (cAliasType) {
175: case AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR:
176: case AliasInfo.ALIAS_TYPE_FUNCTION_AS_CHAR:
177: case AliasInfo.ALIAS_TYPE_SYNONYM_AS_CHAR:
178: break;
179:
180: default:
181: SanityManager.THROWASSERT("Unexpected value ("
182: + cAliasType + ") for aliasType");
183: }
184: }
185: }
186:
187: /* Insert info into sysaliases */
188:
189: /* RESOLVE - It would be nice to require less knowledge about sysaliases
190: * and have this be more table driven.
191: */
192:
193: /* Build the row to insert */
194: ExecRow row = getExecutionFactory().getValueRow(
195: SYSALIASES_COLUMN_COUNT);
196:
197: /* 1st column is ALIASID (UUID - char(36)) */
198: row
199: .setColumn(SYSALIASES_ALIASID, dvf
200: .getCharDataValue(aliasID));
201:
202: /* 2nd column is ALIAS (varchar(128))) */
203: row.setColumn(SYSALIASES_ALIAS, dvf
204: .getVarcharDataValue(aliasName));
205: // System.out.println(" added row-- " + aliasName);
206:
207: /* 3rd column is SCHEMAID (UUID - char(36)) */
208: row.setColumn(SYSALIASES_SCHEMAID, dvf
209: .getCharDataValue(schemaID));
210:
211: /* 4th column is JAVACLASSNAME (longvarchar) */
212: row.setColumn(SYSALIASES_JAVACLASSNAME, dvf
213: .getLongvarcharDataValue(javaClassName));
214:
215: /* 5th column is ALIASTYPE (char(1)) */
216: row.setColumn(SYSALIASES_ALIASTYPE, dvf
217: .getCharDataValue(sAliasType));
218:
219: /* 6th column is NAMESPACE (char(1)) */
220: String sNameSpace = new String(new char[] { cNameSpace });
221:
222: row.setColumn(SYSALIASES_NAMESPACE, dvf
223: .getCharDataValue(sNameSpace));
224:
225: /* 7th column is SYSTEMALIAS (boolean) */
226: row.setColumn(SYSALIASES_SYSTEMALIAS, dvf
227: .getDataValue(systemAlias));
228:
229: /* 8th column is ALIASINFO (org.apache.derby.catalog.AliasInfo) */
230: row
231: .setColumn(SYSALIASES_ALIASINFO, dvf
232: .getDataValue(aliasInfo));
233:
234: /* 9th column is specific name */
235: row.setColumn(SYSALIASES_SPECIFIC_NAME, dvf
236: .getVarcharDataValue(specificName));
237:
238: return row;
239: }
240:
241: ///////////////////////////////////////////////////////////////////////////
242: //
243: // ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory
244: //
245: ///////////////////////////////////////////////////////////////////////////
246:
247: /**
248: * Make a AliasDescriptor out of a SYSALIASES row
249: *
250: * @param row a SYSALIASES row
251: * @param parentTupleDescriptor Null for this kind of descriptor.
252: * @param dd dataDictionary
253: *
254: * @exception StandardException thrown on failure
255: */
256: public TupleDescriptor buildDescriptor(ExecRow row,
257: TupleDescriptor parentTupleDescriptor, DataDictionary dd)
258: throws StandardException {
259: if (SanityManager.DEBUG) {
260: SanityManager.ASSERT(
261: row.nColumns() == SYSALIASES_COLUMN_COUNT,
262: "Wrong number of columns for a SYSALIASES row");
263: }
264:
265: char cAliasType;
266: char cNameSpace;
267: DataValueDescriptor col;
268: String aliasID;
269: UUID aliasUUID;
270: String aliasName;
271: String javaClassName;
272: String sAliasType;
273: String sNameSpace;
274: String typeStr;
275: boolean systemAlias = false;
276: AliasInfo aliasInfo = null;
277:
278: /* 1st column is ALIASID (UUID - char(36)) */
279: col = row.getColumn(SYSALIASES_ALIASID);
280: aliasID = col.getString();
281: aliasUUID = getUUIDFactory().recreateUUID(aliasID);
282:
283: /* 2nd column is ALIAS (varchar(128)) */
284: col = row.getColumn(SYSALIASES_ALIAS);
285: aliasName = col.getString();
286:
287: /* 3rd column is SCHEMAID (UUID - char(36)) */
288: col = row.getColumn(SYSALIASES_SCHEMAID);
289: UUID schemaUUID = col.isNull() ? null : getUUIDFactory()
290: .recreateUUID(col.getString());
291:
292: /* 4th column is JAVACLASSNAME (longvarchar) */
293: col = row.getColumn(SYSALIASES_JAVACLASSNAME);
294: javaClassName = col.getString();
295:
296: /* 5th column is ALIASTYPE (char(1)) */
297: col = row.getColumn(SYSALIASES_ALIASTYPE);
298: sAliasType = col.getString();
299: if (SanityManager.DEBUG) {
300: SanityManager.ASSERT(sAliasType.length() == 1,
301: "Fifth column (aliastype) type incorrect");
302: switch (sAliasType.charAt(0)) {
303: case AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR:
304: case AliasInfo.ALIAS_TYPE_FUNCTION_AS_CHAR:
305: case AliasInfo.ALIAS_TYPE_SYNONYM_AS_CHAR:
306: break;
307:
308: default:
309: SanityManager.THROWASSERT("Invalid type value '"
310: + sAliasType + "' for alias");
311: }
312: }
313:
314: cAliasType = sAliasType.charAt(0);
315:
316: /* 6th column is NAMESPACE (char(1)) */
317: col = row.getColumn(SYSALIASES_NAMESPACE);
318: sNameSpace = col.getString();
319: if (SanityManager.DEBUG) {
320: SanityManager.ASSERT(sNameSpace.length() == 1,
321: "Sixth column (namespace) type incorrect");
322: switch (sNameSpace.charAt(0)) {
323: case AliasInfo.ALIAS_NAME_SPACE_PROCEDURE_AS_CHAR:
324: case AliasInfo.ALIAS_NAME_SPACE_FUNCTION_AS_CHAR:
325: case AliasInfo.ALIAS_TYPE_SYNONYM_AS_CHAR:
326: break;
327:
328: default:
329: SanityManager.THROWASSERT("Invalid type value '"
330: + sNameSpace + "' for alias");
331: }
332: }
333:
334: cNameSpace = sNameSpace.charAt(0);
335:
336: /* 7th column is SYSTEMALIAS (boolean) */
337: col = row.getColumn(SYSALIASES_SYSTEMALIAS);
338: systemAlias = col.getBoolean();
339:
340: /* 8th column is ALIASINFO (org.apache.derby.catalog.AliasInfo) */
341: col = row.getColumn(SYSALIASES_ALIASINFO);
342: aliasInfo = (AliasInfo) col.getObject();
343:
344: /* 9th column is specific name */
345: col = row.getColumn(SYSALIASES_SPECIFIC_NAME);
346: String specificName = col.getString();
347:
348: /* now build and return the descriptor */
349: return new AliasDescriptor(dd, aliasUUID, aliasName,
350: schemaUUID, javaClassName, cAliasType, cNameSpace,
351: systemAlias, aliasInfo, specificName);
352: }
353:
354: /**
355: * Builds a list of columns suitable for creating this Catalog.
356: * DERBY-1734 fixed an issue where older code created the
357: * BOOLEAN column SYSTEMALIAS with maximum length 0 instead of 1.
358: * DERBY-1742 was opened to track if upgrade changes are needed.
359: *
360: *
361: * @return array of SystemColumn suitable for making this catalog.
362: */
363: public SystemColumn[] buildColumnList() {
364: return new SystemColumn[] {
365:
366: SystemColumnImpl.getUUIDColumn("ALIASID", false),
367: SystemColumnImpl.getIdentifierColumn("ALIAS", false),
368: SystemColumnImpl.getUUIDColumn("SCHEMAID", true),
369: SystemColumnImpl.getColumn("JAVACLASSNAME",
370: java.sql.Types.LONGVARCHAR, false,
371: Integer.MAX_VALUE),
372: SystemColumnImpl.getIndicatorColumn("ALIASTYPE"),
373: SystemColumnImpl.getIndicatorColumn("NAMESPACE"),
374: SystemColumnImpl.getColumn("SYSTEMALIAS",
375: JDBC30Translation.SQL_TYPES_BOOLEAN, false),
376: SystemColumnImpl.getJavaColumn("ALIASINFO",
377: "org.apache.derby.catalog.AliasInfo", true),
378: SystemColumnImpl.getIdentifierColumn("SPECIFICNAME",
379: false) };
380: }
381: }
|