KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.catalog.SYSALIASESRowFactory
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.types.TypeId;
25 import org.apache.derby.iapi.reference.JDBC30Translation;
26 import org.apache.derby.iapi.sql.dictionary.SystemColumn;
27 import org.apache.derby.catalog.TypeDescriptor;
28
29 import org.apache.derby.iapi.types.DataValueDescriptor;
30
31 import org.apache.derby.iapi.types.DataValueFactory;
32 import org.apache.derby.iapi.types.RowLocation;
33
34 import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;
35 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
36 import org.apache.derby.iapi.sql.dictionary.AliasDescriptor;
37 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
38 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
39 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
40 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;
41
42 import org.apache.derby.iapi.sql.execute.ExecutionContext;
43 import org.apache.derby.iapi.sql.execute.ExecIndexRow;
44 import org.apache.derby.iapi.sql.execute.ExecRow;
45 import org.apache.derby.iapi.sql.execute.ExecutionFactory;
46
47 import org.apache.derby.iapi.error.StandardException;
48
49 import org.apache.derby.iapi.services.sanity.SanityManager;
50
51 import org.apache.derby.iapi.services.uuid.UUIDFactory;
52
53 import org.apache.derby.catalog.AliasInfo;
54 import org.apache.derby.catalog.UUID;
55
56
57 /**
58  * Factory for creating a SYSALIASES row.
59  *
60  * Here are the directions for adding a new system supplied alias.
61  * Misc:
62  * All system supplied aliases are class aliases at this point.
63  * Additional arrays will need to be added if we supply system
64  * aliases of other types.
65  * The preloadAliasIDs array is an array of hard coded UUIDs
66  * for the system supplied aliases.
67  * The preloadAliases array is the array of aliases
68  * for the system supplied aliases. This array is in alphabetical
69  * order by package and class in Xena. Each alias is the uppercase
70  * class name of the alias.
71  * The preloadJavaClassNames array is the array of full package.class
72  * names for the system supplied aliases. This array is in alphabetical
73  * order by package and class in Xena.
74  * SYSALIASES_NUM_BOOT_ROWS is the number of boot rows in sys.sysaliases
75  * in a new database.
76  *
77  *
78  * @author jerry
79  */

80
81 class SYSALIASESRowFactory extends CatalogRowFactory
82 {
83
84     private static final int SYSALIASES_COLUMN_COUNT = 9;
85     private static final int SYSALIASES_ALIASID = 1;
86     private static final int SYSALIASES_ALIAS = 2;
87     private static final int SYSALIASES_SCHEMAID = 3;
88     private static final int SYSALIASES_JAVACLASSNAME = 4;
89     private static final int SYSALIASES_ALIASTYPE = 5;
90     private static final int SYSALIASES_NAMESPACE = 6;
91     private static final int SYSALIASES_SYSTEMALIAS = 7;
92     public static final int SYSALIASES_ALIASINFO = 8;
93     private static final int SYSALIASES_SPECIFIC_NAME = 9;
94
95  
96     protected static final int SYSALIASES_INDEX1_ID = 0;
97
98     protected static final int SYSALIASES_INDEX2_ID = 1;
99
100     protected static final int SYSALIASES_INDEX3_ID = 2;
101
102     // null means all unique.
103
private static final boolean[] uniqueness = null;
104
105     private static int[][] indexColumnPositions =
106     {
107         {SYSALIASES_SCHEMAID, SYSALIASES_ALIAS, SYSALIASES_NAMESPACE},
108         {SYSALIASES_ALIASID},
109         {SYSALIASES_SCHEMAID, SYSALIASES_SPECIFIC_NAME},
110     };
111
112     private static final String JavaDoc[] uuids =
113     {
114          "c013800d-00d7-ddbd-08ce-000a0a411400" // catalog UUID
115
,"c013800d-00d7-ddbd-75d4-000a0a411400" // heap UUID
116
,"c013800d-00d7-ddbe-b99d-000a0a411400" // SYSALIASES_INDEX1
117
,"c013800d-00d7-ddbe-c4e1-000a0a411400" // SYSALIASES_INDEX2
118
,"c013800d-00d7-ddbe-34ae-000a0a411400" // SYSALIASES_INDEX3
119
};
120
121     /////////////////////////////////////////////////////////////////////////////
122
//
123
// CONSTRUCTORS
124
//
125
/////////////////////////////////////////////////////////////////////////////
126

127     public SYSALIASESRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf,
128                                  boolean convertIdToLower)
129     {
130         super(uuidf,ef,dvf,convertIdToLower);
131         initInfo(SYSALIASES_COLUMN_COUNT, "SYSALIASES", indexColumnPositions, uniqueness, uuids);
132     }
133
134     /////////////////////////////////////////////////////////////////////////////
135
//
136
// METHODS
137
//
138
/////////////////////////////////////////////////////////////////////////////
139

140   /**
141      * Make a SYSALIASES row
142      *
143      *
144      * @return Row suitable for inserting into SYSALIASES.
145      *
146      * @exception StandardException thrown on failure
147      */

148     public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent)
149                     throws StandardException
150     {
151         DataValueDescriptor col;
152         String JavaDoc schemaID = null;
153         String JavaDoc javaClassName = null;
154         String JavaDoc sAliasType = null;
155         String JavaDoc aliasID = null;
156         String JavaDoc aliasName = null;
157         String JavaDoc specificName = null;
158         char cAliasType = AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR;
159         char cNameSpace = AliasInfo.ALIAS_NAME_SPACE_PROCEDURE_AS_CHAR;
160         boolean systemAlias = false;
161         AliasInfo aliasInfo = null;
162
163         if (td != null) {
164
165             AliasDescriptor ad = (AliasDescriptor)td;
166             aliasID = ad.getUUID().toString();
167             aliasName = ad.getDescriptorName();
168             schemaID = ad.getSchemaUUID().toString();
169             javaClassName = ad.getJavaClassName();
170             cAliasType = ad.getAliasType();
171             cNameSpace = ad.getNameSpace();
172             systemAlias = ad.getSystemAlias();
173             aliasInfo = ad.getAliasInfo();
174             specificName = ad.getSpecificName();
175
176             char[] charArray = new char[1];
177             charArray[0] = cAliasType;
178             sAliasType = new String JavaDoc(charArray);
179
180             if (SanityManager.DEBUG)
181             {
182                 switch (cAliasType)
183                 {
184                     case AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR:
185                     case AliasInfo.ALIAS_TYPE_FUNCTION_AS_CHAR:
186                     case AliasInfo.ALIAS_TYPE_SYNONYM_AS_CHAR:
187                         break;
188
189                     default:
190                         SanityManager.THROWASSERT(
191                             "Unexpected value (" + cAliasType +
192                             ") for aliasType");
193                 }
194             }
195         }
196
197
198         /* Insert info into sysaliases */
199
200         /* RESOLVE - It would be nice to require less knowledge about sysaliases
201          * and have this be more table driven.
202          */

203
204         /* Build the row to insert */
205         ExecRow row = getExecutionFactory().getValueRow(SYSALIASES_COLUMN_COUNT);
206
207         /* 1st column is ALIASID (UUID - char(36)) */
208         row.setColumn(SYSALIASES_ALIASID, dvf.getCharDataValue(aliasID));
209
210         /* 2nd column is ALIAS (varchar(128))) */
211         row.setColumn(SYSALIASES_ALIAS, dvf.getVarcharDataValue(aliasName));
212         // System.out.println(" added row-- " + aliasName);
213

214         /* 3rd column is SCHEMAID (UUID - char(36)) */
215         row.setColumn(SYSALIASES_SCHEMAID, dvf.getCharDataValue(schemaID));
216
217         /* 4th column is JAVACLASSNAME (longvarchar) */
218         row.setColumn(SYSALIASES_JAVACLASSNAME, dvf.getLongvarcharDataValue(javaClassName));
219
220         /* 5th column is ALIASTYPE (char(1)) */
221         row.setColumn(SYSALIASES_ALIASTYPE, dvf.getCharDataValue(sAliasType));
222
223         /* 6th column is NAMESPACE (char(1)) */
224         String JavaDoc sNameSpace = new String JavaDoc(new char[] { cNameSpace });
225
226         row.setColumn
227             (SYSALIASES_NAMESPACE, dvf.getCharDataValue(sNameSpace));
228
229
230         /* 7th column is SYSTEMALIAS (boolean) */
231         row.setColumn
232             (SYSALIASES_SYSTEMALIAS, dvf.getDataValue(systemAlias));
233
234         /* 8th column is ALIASINFO (org.apache.derby.catalog.AliasInfo) */
235         row.setColumn(SYSALIASES_ALIASINFO,
236             dvf.getDataValue(aliasInfo));
237
238         /* 9th column is specific name */
239         row.setColumn
240             (SYSALIASES_SPECIFIC_NAME, dvf.getVarcharDataValue(specificName));
241
242
243         return row;
244     }
245
246     ///////////////////////////////////////////////////////////////////////////
247
//
248
// ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory
249
//
250
///////////////////////////////////////////////////////////////////////////
251

252     /**
253      * Make a AliasDescriptor out of a SYSALIASES row
254      *
255      * @param row a SYSALIASES row
256      * @param parentTupleDescriptor Null for this kind of descriptor.
257      * @param dd dataDictionary
258      *
259      * @exception StandardException thrown on failure
260      */

261     public TupleDescriptor buildDescriptor(
262         ExecRow row,
263         TupleDescriptor parentTupleDescriptor,
264         DataDictionary dd )
265                     throws StandardException
266     {
267         if (SanityManager.DEBUG)
268         {
269             SanityManager.ASSERT(
270                 row.nColumns() == SYSALIASES_COLUMN_COUNT,
271                 "Wrong number of columns for a SYSALIASES row");
272         }
273
274         char cAliasType;
275         char cNameSpace;
276         DataValueDescriptor col;
277         String JavaDoc aliasID;
278         UUID aliasUUID;
279         String JavaDoc aliasName;
280         String JavaDoc javaClassName;
281         String JavaDoc sAliasType;
282         String JavaDoc sNameSpace;
283         String JavaDoc typeStr;
284         boolean systemAlias = false;
285         AliasInfo aliasInfo = null;
286
287         /* 1st column is ALIASID (UUID - char(36)) */
288         col = row.getColumn(SYSALIASES_ALIASID);
289         aliasID = col.getString();
290         aliasUUID = getUUIDFactory().recreateUUID(aliasID);
291
292         /* 2nd column is ALIAS (varchar(128)) */
293         col = row.getColumn(SYSALIASES_ALIAS);
294         aliasName = col.getString();
295
296         /* 3rd column is SCHEMAID (UUID - char(36)) */
297         col = row.getColumn(SYSALIASES_SCHEMAID);
298         UUID schemaUUID = col.isNull() ? null : getUUIDFactory().recreateUUID(col.getString());
299
300         /* 4th column is JAVACLASSNAME (longvarchar) */
301         col = row.getColumn(SYSALIASES_JAVACLASSNAME);
302         javaClassName = col.getString();
303
304         /* 5th column is ALIASTYPE (char(1)) */
305         col = row.getColumn(SYSALIASES_ALIASTYPE);
306         sAliasType = col.getString();
307         if (SanityManager.DEBUG)
308         {
309             SanityManager.ASSERT(sAliasType.length() == 1,
310                 "Fifth column (aliastype) type incorrect");
311             switch (sAliasType.charAt(0))
312             {
313                 case AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR:
314                 case AliasInfo.ALIAS_TYPE_FUNCTION_AS_CHAR:
315                 case AliasInfo.ALIAS_TYPE_SYNONYM_AS_CHAR:
316                     break;
317
318                 default:
319                     SanityManager.THROWASSERT("Invalid type value '"
320                             +sAliasType+ "' for alias");
321             }
322         }
323
324         cAliasType = sAliasType.charAt(0);
325
326         /* 6th column is NAMESPACE (char(1)) */
327         col = row.getColumn(SYSALIASES_NAMESPACE);
328         sNameSpace = col.getString();
329         if (SanityManager.DEBUG)
330         {
331             SanityManager.ASSERT(sNameSpace.length() == 1,
332                 "Sixth column (namespace) type incorrect");
333             switch (sNameSpace.charAt(0))
334             {
335                 case AliasInfo.ALIAS_NAME_SPACE_PROCEDURE_AS_CHAR:
336                 case AliasInfo.ALIAS_NAME_SPACE_FUNCTION_AS_CHAR:
337                 case AliasInfo.ALIAS_TYPE_SYNONYM_AS_CHAR:
338                     break;
339
340                 default:
341                     SanityManager.THROWASSERT("Invalid type value '"
342                             +sNameSpace+ "' for alias");
343             }
344         }
345
346         cNameSpace = sNameSpace.charAt(0);
347
348
349         /* 7th column is SYSTEMALIAS (boolean) */
350         col = row.getColumn(SYSALIASES_SYSTEMALIAS);
351         systemAlias = col.getBoolean();
352
353         /* 8th column is ALIASINFO (org.apache.derby.catalog.AliasInfo) */
354         col = row.getColumn(SYSALIASES_ALIASINFO);
355         aliasInfo = (AliasInfo) col.getObject();
356
357         /* 9th column is specific name */
358         col = row.getColumn(SYSALIASES_SPECIFIC_NAME);
359         String JavaDoc specificName = col.getString();
360
361
362         /* now build and return the descriptor */
363         return new AliasDescriptor(dd, aliasUUID, aliasName,
364                                         schemaUUID, javaClassName, cAliasType,
365                                         cNameSpace, systemAlias,
366                                         aliasInfo, specificName);
367     }
368
369     /**
370      * Builds a list of columns suitable for creating this Catalog.
371      * DERBY-1734 fixed an issue where older code created the
372      * BOOLEAN column SYSTEMALIAS with maximum length 0 instead of 1.
373      * DERBY-1742 was opened to track if upgrade changes are needed.
374      *
375      *
376      * @return array of SystemColumn suitable for making this catalog.
377      */

378     public SystemColumn[] buildColumnList()
379     {
380       return new SystemColumn[] {
381         
382         SystemColumnImpl.getUUIDColumn("ALIASID", false),
383         SystemColumnImpl.getIdentifierColumn("ALIAS", false),
384         SystemColumnImpl.getUUIDColumn("SCHEMAID", true),
385         SystemColumnImpl.getColumn("JAVACLASSNAME",
386                 java.sql.Types.LONGVARCHAR, false, Integer.MAX_VALUE),
387         SystemColumnImpl.getIndicatorColumn("ALIASTYPE"),
388         SystemColumnImpl.getIndicatorColumn("NAMESPACE"),
389         SystemColumnImpl.getColumn("SYSTEMALIAS",
390                 JDBC30Translation.SQL_TYPES_BOOLEAN, false),
391         SystemColumnImpl.getJavaColumn("ALIASINFO",
392                 "org.apache.derby.catalog.AliasInfo", true),
393         SystemColumnImpl.getIdentifierColumn("SPECIFICNAME", false)
394         };
395     }
396 }
397
Popular Tags