KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.catalog.SYSVIEWSRowFactory
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.types.TypeId;
27 import org.apache.derby.iapi.sql.dictionary.SystemColumn;
28 import org.apache.derby.catalog.TypeDescriptor;
29
30 import org.apache.derby.iapi.types.DataValueFactory;
31 import org.apache.derby.iapi.types.RowLocation;
32
33 import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;
34 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
35 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
36 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
37 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
38 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;
39 import org.apache.derby.iapi.sql.dictionary.ViewDescriptor;
40
41 import org.apache.derby.iapi.sql.execute.ExecutionContext;
42 import org.apache.derby.iapi.sql.execute.ExecIndexRow;
43 import org.apache.derby.iapi.sql.execute.ExecRow;
44 import org.apache.derby.iapi.sql.execute.ExecutionFactory;
45
46 import org.apache.derby.iapi.error.StandardException;
47
48 import org.apache.derby.iapi.services.sanity.SanityManager;
49
50 import org.apache.derby.iapi.services.uuid.UUIDFactory;
51 import org.apache.derby.catalog.UUID;
52 import org.apache.derby.catalog.IndexDescriptor;
53
54 import java.util.Properties JavaDoc;
55
56 /**
57  * Factory for creating a SYSVIEWS row.
58  *
59  * @author jerry
60  */

61
62 public class SYSVIEWSRowFactory extends CatalogRowFactory
63 {
64     private static final String JavaDoc TABLENAME_STRING = "SYSVIEWS";
65
66     protected static final int SYSVIEWS_COLUMN_COUNT = 4;
67     protected static final int SYSVIEWS_TABLEID = 1;
68     protected static final int SYSVIEWS_VIEWDEFINITION = 2;
69     protected static final int SYSVIEWS_CHECKOPTION = 3;
70     protected static final int SYSVIEWS_COMPILATION_SCHEMAID = 4;
71
72     // Column widths
73
protected static final int SYSVIEWS_TABLEID_WIDTH = 36;
74
75     protected static final int SYSVIEWS_INDEX1_ID = 0;
76
77     private static final int[][] indexColumnPositions =
78     {
79         {SYSVIEWS_TABLEID}
80     };
81
82
83     // if you add a non-unique index allocate this array.
84
private static final boolean[] uniqueness = null;
85
86     private static final String JavaDoc[] uuids =
87     {
88          "8000004d-00d0-fd77-3ed8-000a0a0b1900" // catalog UUID
89
,"80000050-00d0-fd77-3ed8-000a0a0b1900" // heap UUID
90
,"8000004f-00d0-fd77-3ed8-000a0a0b1900" // SYSVIEWS_INDEX1
91
};
92
93     /////////////////////////////////////////////////////////////////////////////
94
//
95
// CONSTRUCTORS
96
//
97
/////////////////////////////////////////////////////////////////////////////
98

99     public SYSVIEWSRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf,
100                                boolean convertIdToLower)
101     {
102         super(uuidf,ef,dvf,convertIdToLower);
103         initInfo(SYSVIEWS_COLUMN_COUNT, TABLENAME_STRING,
104                  indexColumnPositions, uniqueness, uuids );
105     }
106
107     /////////////////////////////////////////////////////////////////////////////
108
//
109
// METHODS
110
//
111
/////////////////////////////////////////////////////////////////////////////
112

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

120     public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent)
121         throws StandardException
122     {
123         DataValueDescriptor col;
124         ExecRow row;
125         String JavaDoc tableID = null;
126         String JavaDoc compSchemaId = null;
127         String JavaDoc viewText = null;
128         String JavaDoc checkSType = null;
129         int checkIType;
130
131         if (td != null)
132         {
133             UUID tableUUID;
134             ViewDescriptor vd = (ViewDescriptor)td;
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             tableUUID = vd.getUUID();
141             if ( tableUUID == null )
142             {
143                 tableUUID = getUUIDFactory().createUUID();
144                 vd.setUUID(tableUUID);
145             }
146             tableID = tableUUID.toString();
147             viewText = vd.getViewText();
148
149             /* RESOLVE - check constraints not supported yet */
150             checkIType = vd.getCheckOptionType();
151
152             if (SanityManager.DEBUG)
153             {
154                 if (checkIType != ViewDescriptor.NO_CHECK_OPTION)
155                 {
156                     SanityManager.THROWASSERT("checkIType expected to be " +
157                         ViewDescriptor.NO_CHECK_OPTION +
158                         ", not " + checkIType);
159                 }
160             }
161             checkSType = "N";
162
163             UUID tmpId = vd.getCompSchemaId();
164             compSchemaId = (tmpId == null) ? null : tmpId.toString();
165         }
166
167         /* Insert info into sysviews */
168
169         /* RESOLVE - It would be nice to require less knowledge about sysviews
170          * and have this be more table driven.
171          */

172
173         /* Build the row to insert */
174         row = getExecutionFactory().getValueRow(SYSVIEWS_COLUMN_COUNT);
175
176         /* 1st column is TABLEID (UUID - char(36)) */
177         row.setColumn(SYSVIEWS_TABLEID, dvf.getCharDataValue(tableID));
178
179         /* 2nd column is VIEWDEFINITION */
180         row.setColumn(SYSVIEWS_VIEWDEFINITION,
181                 dvf.getLongvarcharDataValue(viewText));
182
183         /* 3rd column is CHECKOPTION (char(1)) */
184         row.setColumn(SYSVIEWS_CHECKOPTION, dvf.getCharDataValue(checkSType));
185
186         /* 4th column is COMPILATIONSCHEMAID (UUID - char(36)) */
187         row.setColumn(SYSVIEWS_COMPILATION_SCHEMAID, dvf.getCharDataValue(compSchemaId));
188
189         return row;
190     }
191     
192     ///////////////////////////////////////////////////////////////////////////
193
//
194
// ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory
195
//
196
///////////////////////////////////////////////////////////////////////////
197

198     /**
199      * Make a ViewDescriptor out of a SYSVIEWS row
200      *
201      * @param row a SYSVIEWS row
202      * @param parentTupleDescriptor Null for this kind of descriptor.
203      * @param dd dataDictionary
204      *
205      * @exception StandardException thrown on failure
206      */

207     public TupleDescriptor buildDescriptor(
208         ExecRow row,
209         TupleDescriptor parentTupleDescriptor,
210         DataDictionary dd )
211                     throws StandardException
212     {
213         ViewDescriptor vd = null;
214
215         if (SanityManager.DEBUG)
216         {
217             SanityManager.ASSERT(
218                 row.nColumns() == SYSVIEWS_COLUMN_COUNT,
219                 "Wrong number of columns for a SYSVIEWS row");
220         }
221
222         DataValueDescriptor col;
223         DataDescriptorGenerator ddg;
224         int checkIType;
225         String JavaDoc checkSType;
226         String JavaDoc tableID;
227         String JavaDoc compSchemaId;
228         String JavaDoc viewDefinition;
229         UUID tableUUID;
230         UUID compSchemaUUID = null;
231
232         ddg = dd.getDataDescriptorGenerator();
233
234         /* 1st column is TABLEID (UUID - char(36)) */
235         col = row.getColumn(SYSVIEWS_TABLEID);
236         tableID = col.getString();
237         tableUUID = getUUIDFactory().recreateUUID(tableID);
238
239         /* 2nd column is VIEWDEFINITION */
240         col = row.getColumn(SYSVIEWS_VIEWDEFINITION);
241         viewDefinition = col.getString();
242
243         /* 3rd column is CHECKOPTION (char(1)) */
244         col = row.getColumn(SYSVIEWS_CHECKOPTION);
245         checkSType = col.getString();
246
247         if (SanityManager.DEBUG)
248         {
249             if (!checkSType.equals("N"))
250             {
251                 SanityManager.THROWASSERT("checkSType expected to be 'N', not " + checkSType);
252             }
253         }
254
255         /* RESOLVE - no check options for now */
256         checkIType = ViewDescriptor.NO_CHECK_OPTION;
257
258         /* 4th column is COMPILATIONSCHEMAID (UUID - char(36)) */
259         col = row.getColumn(SYSVIEWS_COMPILATION_SCHEMAID);
260         compSchemaId = col.getString();
261         if (compSchemaId != null)
262         {
263             compSchemaUUID = getUUIDFactory().recreateUUID(compSchemaId);
264         }
265
266         /* now build and return the descriptor */
267         vd = ddg.newViewDescriptor(tableUUID, null, viewDefinition,
268                 checkIType, compSchemaUUID);
269         return vd;
270     }
271
272     /**
273      * Builds a list of columns suitable for creating this Catalog.
274      *
275      *
276      * @return array of SystemColumn suitable for making this catalog.
277      */

278     public SystemColumn[] buildColumnList()
279     {
280         SystemColumn[] columnList = new SystemColumn[SYSVIEWS_COLUMN_COUNT];
281
282         columnList[0] = new SystemColumnImpl(
283                             convertIdCase( "TABLEID"), // name
284
SYSVIEWS_TABLEID, // column number
285
0, // precision
286
0, // scale
287
false, // nullability
288
"CHAR", // dataType
289
true, // built-in type
290
36 // maxLength
291
);
292         // describe columns
293

294         columnList[1] =
295                     new SystemColumnImpl(
296                             convertIdCase( "VIEWDEFINITION"), // column name
297
SYSVIEWS_VIEWDEFINITION, // column number
298
0, // precision
299
0, // scale
300
false, // nullability
301
"LONG VARCHAR", // dataType
302
true, // built-in type
303
TypeId.LONGVARCHAR_MAXWIDTH // maxLength
304
);
305         columnList[2] =
306                     new SystemColumnImpl(
307                             convertIdCase( "CHECKOPTION"), // column name
308
SYSVIEWS_CHECKOPTION,// column number
309
0, // precision
310
0, // scale
311
false, // nullability
312
"CHAR", // dataType
313
true, // built-in type
314
1 // maxLength
315
);
316
317         columnList[3] = new SystemColumnImpl(
318                             convertIdCase( "COMPILATIONSCHEMAID"), // name
319
SYSVIEWS_COMPILATION_SCHEMAID, // column number
320
0, // precision
321
0, // scale
322
false, // nullability
323
"CHAR", // dataType
324
true, // built-in type
325
36 // maxLength
326
);
327         return columnList;
328     }
329 }
330
Popular Tags