KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.catalog.SYSTABLESRowFactory
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.DataValueDescriptor;
25
26 import org.apache.derby.iapi.sql.dictionary.SystemColumn;
27
28 import org.apache.derby.iapi.types.DataValueFactory;
29 import org.apache.derby.iapi.types.RowLocation;
30
31 import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;
32 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;
33 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
34 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
35
36 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
37 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
38
39 import org.apache.derby.iapi.services.sanity.SanityManager;
40
41 import org.apache.derby.iapi.sql.execute.ExecIndexRow;
42 import org.apache.derby.iapi.sql.execute.ExecutionFactory;
43 import org.apache.derby.iapi.sql.execute.ExecRow;
44
45 import org.apache.derby.iapi.error.StandardException;
46
47 import org.apache.derby.catalog.UUID;
48 import org.apache.derby.iapi.services.uuid.UUIDFactory;
49
50 /**
51  * Factory for creating a SYSTABLES row.
52  *
53  *
54  * @version 0.1
55  * @author Rick Hillegas (extracted from DataDictionaryImpl).
56  */

57
58 class SYSTABLESRowFactory extends CatalogRowFactory
59 {
60     private static final String JavaDoc TABLENAME_STRING = "SYSTABLES";
61
62     protected static final int SYSTABLES_COLUMN_COUNT = 5;
63     /* Column #s for systables (1 based) */
64     protected static final int SYSTABLES_TABLEID = 1;
65     protected static final int SYSTABLES_TABLENAME = 2;
66     protected static final int SYSTABLES_TABLETYPE = 3;
67     protected static final int SYSTABLES_SCHEMAID = 4;
68     protected static final int SYSTABLES_LOCKGRANULARITY = 5;
69
70     protected static final int SYSTABLES_INDEX1_ID = 0;
71     protected static final int SYSTABLES_INDEX1_TABLENAME = 1;
72     protected static final int SYSTABLES_INDEX1_SCHEMAID = 2;
73
74     protected static final int SYSTABLES_INDEX2_ID = 1;
75     protected static final int SYSTABLES_INDEX2_TABLEID = 1;
76     
77     // all indexes are unique.
78

79     private static final String JavaDoc[] uuids =
80     {
81          "80000018-00d0-fd77-3ed8-000a0a0b1900" // catalog UUID
82
,"80000028-00d0-fd77-3ed8-000a0a0b1900" // heap UUID
83
,"8000001a-00d0-fd77-3ed8-000a0a0b1900" // SYSTABLES_INDEX1
84
,"8000001c-00d0-fd77-3ed8-000a0a0b1900" // SYSTABLES_INDEX2
85
};
86
87     private static final int[][] indexColumnPositions =
88     {
89         { SYSTABLES_TABLENAME, SYSTABLES_SCHEMAID},
90         { SYSTABLES_TABLEID }
91     };
92
93     /////////////////////////////////////////////////////////////////////////////
94
//
95
// CONSTRUCTORS
96
//
97
/////////////////////////////////////////////////////////////////////////////
98

99     SYSTABLESRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf,
100                                 boolean convertIdToLower)
101     {
102         super(uuidf,ef,dvf,convertIdToLower);
103         initInfo(SYSTABLES_COLUMN_COUNT, TABLENAME_STRING, indexColumnPositions, (boolean[]) null, uuids);
104     }
105
106     /////////////////////////////////////////////////////////////////////////////
107
//
108
// METHODS
109
//
110
/////////////////////////////////////////////////////////////////////////////
111

112     /**
113      * Make a SYSTABLES row
114      *
115      * @return Row suitable for inserting into SYSTABLES.
116      *
117      * @exception StandardException thrown on failure
118      */

119
120     public ExecRow makeRow(TupleDescriptor td,
121                            TupleDescriptor parent)
122                     throws StandardException
123     {
124         UUID oid;
125         String JavaDoc tabSType = null;
126         int tabIType;
127         ExecRow row;
128         String JavaDoc lockGranularity = null;
129         String JavaDoc tableID = null;
130         String JavaDoc schemaID = null;
131         String JavaDoc tableName = null;
132
133
134         if (td != null)
135         {
136             /*
137             ** We only allocate a new UUID if the descriptor doesn't already have one.
138             ** For descriptors replicated from a Source system, we already have an UUID.
139             */

140             TableDescriptor descriptor = (TableDescriptor)td;
141             SchemaDescriptor schema = (SchemaDescriptor)parent;
142
143             oid = descriptor.getUUID();
144             if ( oid == null )
145             {
146                 oid = getUUIDFactory().createUUID();
147                 descriptor.setUUID(oid);
148             }
149             tableID = oid.toString();
150             
151             if (SanityManager.DEBUG)
152             {
153                 SanityManager.ASSERT(schema != null,
154                             "Schema should not be null unless empty row is true");
155                 if (schema.getUUID() == null)
156                 {
157                     SanityManager.THROWASSERT("schema " + schema + " has a null OID");
158                 }
159             }
160         
161             schemaID = schema.getUUID().toString();
162
163             tableName = descriptor.getName();
164
165             /* RESOLVE - Table Type should really be a char in the descriptor
166              * T, S, V, S instead of 0, 1, 2, 3
167              */

168             tabIType = descriptor.getTableType();
169             switch (tabIType)
170             {
171                 case TableDescriptor.BASE_TABLE_TYPE:
172                     tabSType = "T";
173                     break;
174                 case TableDescriptor.SYSTEM_TABLE_TYPE:
175                     tabSType = "S";
176                     break;
177                 case TableDescriptor.VIEW_TYPE:
178                     tabSType = "V";
179                     break;
180
181                 case TableDescriptor.SYNONYM_TYPE:
182                     tabSType = "A";
183                     break;
184
185                 default:
186                     if (SanityManager.DEBUG)
187                         SanityManager.THROWASSERT("invalid table type");
188             }
189             char[] lockGChar = new char[1];
190             lockGChar[0] = descriptor.getLockGranularity();
191             lockGranularity = new String JavaDoc(lockGChar);
192         }
193
194         /* Insert info into systables */
195
196         /* RESOLVE - It would be nice to require less knowledge about systables
197          * and have this be more table driven.
198          */

199
200         /* Build the row to insert */
201         row = getExecutionFactory().getValueRow(SYSTABLES_COLUMN_COUNT);
202
203         /* 1st column is TABLEID (UUID - char(36)) */
204         row.setColumn(SYSTABLES_TABLEID, dvf.getCharDataValue(tableID));
205
206         /* 2nd column is NAME (varchar(30)) */
207         row.setColumn(SYSTABLES_TABLENAME, dvf.getVarcharDataValue(tableName));
208
209         /* 3rd column is TABLETYPE (char(1)) */
210         row.setColumn(SYSTABLES_TABLETYPE, dvf.getCharDataValue(tabSType));
211
212         /* 4th column is SCHEMAID (UUID - char(36)) */
213         row.setColumn(SYSTABLES_SCHEMAID, dvf.getCharDataValue(schemaID));
214
215         /* 5th column is LOCKGRANULARITY (char(1)) */
216         row.setColumn(SYSTABLES_LOCKGRANULARITY, dvf.getCharDataValue(lockGranularity));
217
218         return row;
219     }
220
221     /**
222      * Builds an empty index row.
223      *
224      * @param indexNumber Index to build empty row for.
225      * @param rowLocation Row location for last column of index row
226      *
227      * @return corresponding empty index row
228      * @exception StandardException thrown on failure
229      */

230     ExecIndexRow buildEmptyIndexRow( int indexNumber,
231                                             RowLocation rowLocation)
232             throws StandardException
233     {
234         int ncols = getIndexColumnCount(indexNumber);
235         ExecIndexRow row = getExecutionFactory().getIndexableRow(ncols + 1);
236
237         row.setColumn(ncols + 1, rowLocation);
238
239         switch( indexNumber )
240         {
241             case SYSTABLES_INDEX1_ID:
242                 /* 1st column is TABLENAME (varchar(128)) */
243                 row.setColumn(1, getDataValueFactory().getVarcharDataValue((String JavaDoc) null));
244
245                 /* 2nd column is SCHEMAID (UUID - char(36)) */
246                 row.setColumn(2, getDataValueFactory().getCharDataValue((String JavaDoc) null));
247
248                 break;
249
250             case SYSTABLES_INDEX2_ID:
251                 /* 1st column is TABLEID (UUID - char(36)) */
252                 row.setColumn(1, getDataValueFactory().getCharDataValue((String JavaDoc) null));
253                 break;
254         } // end switch
255

256         return row;
257     }
258
259     ///////////////////////////////////////////////////////////////////////////
260
//
261
// ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory
262
//
263
///////////////////////////////////////////////////////////////////////////
264

265     /**
266      * Make a TableDescriptor out of a SYSTABLES row
267      *
268      * @param row a SYSTABLES row
269      * @param parentTupleDescriptor Null for this kind of descriptor.
270      * @param dd dataDictionary
271      *
272      * @return a table descriptor equivalent to a SYSTABLES row
273      *
274      * @exception StandardException thrown on failure
275      */

276
277     public TupleDescriptor buildDescriptor(
278         ExecRow row,
279         TupleDescriptor parentTupleDescriptor,
280         DataDictionary dd )
281                     throws StandardException
282     {
283         if (SanityManager.DEBUG)
284         SanityManager.ASSERT(row.nColumns() == SYSTABLES_COLUMN_COUNT, "Wrong number of columns for a SYSTABLES row");
285
286         DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
287
288         String JavaDoc tableUUIDString;
289         String JavaDoc schemaUUIDString;
290         int tableTypeEnum;
291         String JavaDoc lockGranularity;
292         String JavaDoc tableName, tableType;
293         DataValueDescriptor col;
294         UUID tableUUID;
295         UUID schemaUUID;
296         SchemaDescriptor schema;
297         TableDescriptor tabDesc;
298
299         /* 1st column is TABLEID (UUID - char(36)) */
300         col = row.getColumn(SYSTABLES_TABLEID);
301         tableUUIDString = col.getString();
302         tableUUID = getUUIDFactory().recreateUUID(tableUUIDString);
303
304
305         /* 2nd column is TABLENAME (varchar(128)) */
306         col = row.getColumn(SYSTABLES_TABLENAME);
307         tableName = col.getString();
308
309         /* 3rd column is TABLETYPE (char(1)) */
310         col = row.getColumn(SYSTABLES_TABLETYPE);
311         tableType = col.getString();
312         if (SanityManager.DEBUG)
313         {
314             SanityManager.ASSERT(tableType.length() == 1, "Fourth column type incorrect");
315         }
316         switch (tableType.charAt(0))
317         {
318             case 'T' :
319                 tableTypeEnum = TableDescriptor.BASE_TABLE_TYPE;
320                 break;
321             case 'S' :
322                 tableTypeEnum = TableDescriptor.SYSTEM_TABLE_TYPE;
323                 break;
324             case 'V' :
325                 tableTypeEnum = TableDescriptor.VIEW_TYPE;
326                 break;
327             case 'A' :
328                 tableTypeEnum = TableDescriptor.SYNONYM_TYPE;
329                 break;
330             default:
331                 if (SanityManager.DEBUG)
332                 SanityManager.THROWASSERT("Fourth column value invalid");
333                 tableTypeEnum = -1;
334         }
335
336         /* 4th column is SCHEMAID (UUID - char(36)) */
337         col = row.getColumn(SYSTABLES_SCHEMAID);
338         schemaUUIDString = col.getString();
339         schemaUUID = getUUIDFactory().recreateUUID(schemaUUIDString);
340         
341         schema = dd.getSchemaDescriptor(schemaUUID, null);
342
343         /* 5th column is LOCKGRANULARITY (char(1)) */
344         col = row.getColumn(SYSTABLES_LOCKGRANULARITY);
345         lockGranularity = col.getString();
346         if (SanityManager.DEBUG)
347         {
348             SanityManager.ASSERT(lockGranularity.length() == 1, "Fifth column type incorrect");
349         }
350
351         // RESOLVE - Deal with lock granularity
352
tabDesc = ddg.newTableDescriptor(tableName, schema, tableTypeEnum, lockGranularity.charAt(0));
353         tabDesc.setUUID(tableUUID);
354         return tabDesc;
355     }
356
357     /**
358      * Get the table name out of this SYSTABLES row
359      *
360      * @param row a SYSTABLES row
361      *
362      * @return string, the table name
363      *
364      * @exception StandardException thrown on failure
365      */

366     protected String JavaDoc getTableName(ExecRow row)
367                     throws StandardException
368     {
369         DataValueDescriptor col;
370
371         col = row.getColumn(SYSTABLES_TABLENAME);
372         return col.getString();
373     }
374
375
376     /**
377      * Builds a list of columns suitable for creating this Catalog.
378      *
379      *
380      * @return array of SystemColumn suitable for making this catalog.
381      */

382     public SystemColumn[] buildColumnList()
383     {
384         SystemColumn[] columnList = new SystemColumn[SYSTABLES_COLUMN_COUNT];
385
386         // describe columns
387

388         columnList[0] = new SystemColumnImpl(
389                                 convertIdCase( "TABLEID"), // column name
390
SYSTABLES_TABLEID, // column number
391
0, // precision
392
0, // scale
393
false, // nullability
394
"CHAR", // dataType
395
true, // built-in type
396
36 // maxLength
397
);
398
399         columnList[1] = new SystemColumnImpl( // SQLIDENTIFIER
400
convertIdCase( "TABLENAME"), // column name
401
SYSTABLES_TABLENAME, // column number
402
false // nullability
403
);
404
405         columnList[2] = new SystemColumnImpl(
406                                 convertIdCase( "TABLETYPE"), // column name
407
SYSTABLES_TABLETYPE,// column number
408
0, // precision
409
0, // scale
410
false, // nullability
411
"CHAR", // dataType
412
true, // built-in type
413
1 // maxLength
414
);
415
416         columnList[3] = new SystemColumnImpl(
417                                 convertIdCase( "SCHEMAID"), // column name
418
SYSTABLES_SCHEMAID, // schema number
419
0, // precision
420
0, // scale
421
false, // nullability
422
"CHAR", // dataType
423
true, // built-in type
424
36 // maxLength
425
);
426
427         columnList[4] = new SystemColumnImpl(
428                                 convertIdCase( "LOCKGRANULARITY"), // column name
429
SYSTABLES_LOCKGRANULARITY,// column number
430
0, // precision
431
0, // scale
432
false, // nullability
433
"CHAR", // dataType
434
true, // built-in type
435
1 // maxLength
436
);
437
438         return columnList;
439     }
440
441 }
442
Popular Tags