Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/derby/impl/sql/catalog/SYSFILESRowFactory.java


1   /*
2   
3      Derby - Class org.apache.derby.impl.sql.catalog.SYSFILESRowFactory
4   
5      Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable.
6   
7      Licensed under the Apache License, Version 2.0 (the "License");
8      you may not use this file except in compliance with the License.
9      You may obtain a copy of the License at
10  
11        http://www.apache.org/licenses/LICENSE-2.0
12  
13     Unless required by applicable law or agreed to in writing, software
14     distributed under the License is distributed on an "AS IS" BASIS,
15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16     See the License for the specific language governing permissions and
17     limitations under the License.
18  
19   */
20  
21  package org.apache.derby.impl.sql.catalog;
22  
23  import org.apache.derby.iapi.services.monitor.Monitor;
24  import org.apache.derby.iapi.services.sanity.SanityManager;
25  import org.apache.derby.iapi.db.Database;
26  import org.apache.derby.iapi.error.StandardException;
27  
28  import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;
29  import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor;
30  import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
31  import org.apache.derby.iapi.sql.dictionary.DataDictionary;
32  import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
33  import org.apache.derby.iapi.sql.dictionary.FileInfoDescriptor;
34  import org.apache.derby.iapi.sql.dictionary.SystemColumn;
35  import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;
36  import org.apache.derby.iapi.types.TypeId;
37  import org.apache.derby.iapi.types.DataValueFactory;
38  import org.apache.derby.iapi.types.RowLocation;
39  import org.apache.derby.iapi.sql.execute.ExecIndexRow;
40  import org.apache.derby.iapi.sql.execute.ExecRow;
41  import org.apache.derby.iapi.sql.execute.ExecutionContext;
42  import org.apache.derby.iapi.sql.execute.ExecutionFactory;
43  import org.apache.derby.iapi.types.DataTypeDescriptor;
44  import org.apache.derby.iapi.types.DataValueDescriptor;
45  import org.apache.derby.iapi.types.TypeId;
46  import org.apache.derby.iapi.services.uuid.UUIDFactory;
47  import org.apache.derby.catalog.TypeDescriptor;
48  import org.apache.derby.catalog.UUID;
49  import java.util.Properties;
50  
51  /**
52   * Factory for creating a SYSFILES row.
53   *
54   *
55   * @version 0.1
56   * @author Rick Hillegas (extracted from DataDictionaryImpl).
57   */
58  
59  public class SYSFILESRowFactory extends CatalogRowFactory
60  {
61    protected static final String  TABLENAME_STRING = "SYSFILES";
62  
63    protected static final int    SYSFILES_COLUMN_COUNT = 4;
64  
65    /* Column #s (1 based) */
66    protected static final int    ID_COL_NUM = 1;
67    protected static final String   ID_COL_NAME = "FILEID";
68  
69    protected static final int    SCHEMA_ID_COL_NUM = 2;
70    protected static final String   SCHEMA_ID_COL_NAME = "SCHEMAID";
71  
72    protected static final int    NAME_COL_NUM = 3;
73    protected static final String   NAME_COL_NAME = "FILENAME";
74  
75    protected static final int    GENERATION_ID_COL_NUM = 4;
76    protected static final String   GENERATION_ID_COL_NAME = "GENERATIONID";
77  
78    protected static final int    SYSFILES_INDEX1_ID = 0;
79    protected static final int    SYSFILES_INDEX2_ID = 1;
80  
81    private static final int[][] indexColumnPositions =
82    {
83      {NAME_COL_NUM, SCHEMA_ID_COL_NUM},
84      {ID_COL_NUM}
85    };
86  
87    private static final String[][] indexColumnNames =
88    {
89      {NAME_COL_NAME, SCHEMA_ID_COL_NAME},
90      {ID_COL_NAME}
91    };
92  
93      private  static  final  boolean[]  uniqueness = null;
94  
95    private  static  final  String[]  uuids =
96    {
97      "80000000-00d3-e222-873f-000a0a0b1900",  // catalog UUID
98      "80000000-00d3-e222-9920-000a0a0b1900",  // heap UUID
99      "80000000-00d3-e222-a373-000a0a0b1900",  // SYSSQLFILES_INDEX1
100     "80000000-00d3-e222-be7b-000a0a0b1900"  // SYSSQLFILES_INDEX2
101   };
102 
103   /////////////////////////////////////////////////////////////////////////////
104   //
105   //  CONSTRUCTORS
106   //
107   /////////////////////////////////////////////////////////////////////////////
108 
109     public  SYSFILESRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf,
110                                  boolean convertIdToLower) 
111   {
112     super(uuidf,ef,dvf,convertIdToLower);
113     initInfo(SYSFILES_COLUMN_COUNT, TABLENAME_STRING, 
114          indexColumnPositions, indexColumnNames, uniqueness, uuids );
115   }
116 
117   /////////////////////////////////////////////////////////////////////////////
118   //
119   //  METHODS
120   //
121   /////////////////////////////////////////////////////////////////////////////
122 
123   /**
124    * Make a SYSFILES row
125    *
126    * @param emptyRow Make an empty row if this parameter is true
127    * @param descriptor A descriptor
128    *
129    * @return  Row suitable for inserting into SYSFILES
130    *
131    * @exception   StandardException thrown on failure
132    */
133 
134   public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent)
135           throws StandardException
136   {
137     String          id_S = null;
138     String          schemaId_S = null;
139     String                  SQLname = null;
140     long                    generationId = 0;
141     
142     ExecRow              row;
143 
144     if (td != null)  
145     {
146       FileInfoDescriptor descriptor = (FileInfoDescriptor)td;
147       id_S = descriptor.getUUID().toString();
148       schemaId_S = descriptor.getSchemaDescriptor().getUUID().toString();
149       SQLname = descriptor.getName();
150       generationId = descriptor.getGenerationId();
151     }
152   
153     /* Build the row to insert  */
154     row = getExecutionFactory().getValueRow(SYSFILES_COLUMN_COUNT);
155 
156     /* 1st column is ID (UUID - char(36)) */
157     row.setColumn(ID_COL_NUM, dvf.getCharDataValue(id_S));
158 
159     /* 2nd column is SCHEMAID (UUID - char(36)) */
160     row.setColumn(SCHEMA_ID_COL_NUM, dvf.getCharDataValue(schemaId_S));
161 
162     /* 3rd column is NAME (varchar(30)) */
163     row.setColumn(NAME_COL_NUM, dvf.getVarcharDataValue(SQLname));
164 
165     /* 4th column is GENERATIONID (long) */
166     row.setColumn(GENERATION_ID_COL_NUM, dvf.getDataValue(generationId));
167 
168     return row;
169   }
170 
171   /**
172    * Builds an empty index row.
173    *
174    *  @param  indexNumber  Index to build empty row for.
175    *  @param  rowLocation  Row location for last column of index row
176    *
177    * @return corresponding empty index row
178    * @exception   StandardException thrown on failure
179    */
180   public ExecIndexRow  buildEmptyIndexRow( int indexNumber,
181                       RowLocation rowLocation) 
182       throws StandardException
183   {
184     int ncols = getIndexColumnCount(indexNumber);
185     ExecIndexRow row = getExecutionFactory().getIndexableRow(ncols + 1);
186 
187     row.setColumn(ncols + 1,  rowLocation);
188 
189     switch( indexNumber )
190     {
191         case SYSFILES_INDEX1_ID:
192         /* 1st column is NAME (varchar(128)) */
193         row.setColumn(1, getDataValueFactory().getVarcharDataValue((String) null));
194 
195         /* 2nd column is SCHEMAID (UUID - char(36)) */
196         row.setColumn(2, getDataValueFactory().getCharDataValue((String) null));
197 
198         break;
199 
200         case SYSFILES_INDEX2_ID:
201         /* 1st column is ID (UUID - char(36)) */
202         row.setColumn(1,
203                 getDataValueFactory().getCharDataValue((String) null));
204 
205         break;
206     }  // end switch
207 
208     return  row;
209   }
210 
211   ///////////////////////////////////////////////////////////////////////////
212   //
213   //  ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory
214   //
215   ///////////////////////////////////////////////////////////////////////////
216 
217   /**
218    * Make a descriptor out of a SYSFILES row
219    *
220    * @param row a row
221    * @param parentTupleDescriptor  Null for this kind of descriptor.
222    * @param dd dataDictionary
223    *
224    * @return  a descriptor equivalent to a row
225    *
226    * @exception   StandardException thrown on failure
227    */
228   public TupleDescriptor buildDescriptor(
229     ExecRow          row,
230     TupleDescriptor      parentTupleDescriptor,
231     DataDictionary       dd )
232           throws StandardException
233   {
234     if (SanityManager.DEBUG)
235     {
236       if (row.nColumns() != SYSFILES_COLUMN_COUNT)
237       {
238         SanityManager.THROWASSERT("Wrong number of columns for a SYSFILES row: "+
239                row.nColumns());
240       }
241     }
242 
243     DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
244 
245     String  id_S;
246     UUID    id;
247     String  schemaId_S;
248     UUID    schemaId;
249     String  name;
250     long     generationId;
251     DataValueDescriptor  col;
252 
253     SchemaDescriptor  schemaDescriptor;
254     FileInfoDescriptor  result;
255 
256     /* 1st column is ID (UUID - char(36)) */
257     col = row.getColumn(ID_COL_NUM);
258     id_S = col.getString();
259     id = getUUIDFactory().recreateUUID(id_S);
260 
261     /* 2nd column is SchemaId */
262     col = row.getColumn(SCHEMA_ID_COL_NUM);
263     schemaId_S = col.getString();
264     schemaId = getUUIDFactory().recreateUUID(schemaId_S);
265     
266     schemaDescriptor = dd.getSchemaDescriptor(schemaId, null);
267     if (SanityManager.DEBUG)
268     {
269       if (schemaDescriptor == null)
270       {
271         SanityManager.THROWASSERT("Missing schema for FileInfo: "+id_S);
272       }
273     }
274 
275     /* 3nd column is NAME (varchar(128)) */
276     col = row.getColumn(NAME_COL_NUM);
277     name = col.getString();
278 
279     /* 4th column is generationId (long) */
280     col = row.getColumn(GENERATION_ID_COL_NUM);
281     generationId = col.getLong();
282 
283       result = ddg.newFileInfoDescriptor(id,schemaDescriptor,name,
284                        generationId);
285     return result;
286   }
287 
288   /**
289    * Builds a list of columns suitable for creating this Catalog.
290    *
291    *
292    * @return array of SystemColumn suitable for making this catalog.
293    */
294   public SystemColumn[]  buildColumnList()
295   {
296     int            index = 0;
297     SystemColumn[]      columnList = new SystemColumn[SYSFILES_COLUMN_COUNT];
298 
299     // describe columns
300 
301     columnList[index++] = new SystemColumnImpl(  
302                 convertIdCase( ID_COL_NAME),    // column name
303                 ID_COL_NUM,          // column number
304                 0,          // precision
305                 0,          // scale
306                 false,        // nullability
307                 "CHAR",        // dataType
308                 true,        // built-in type
309                 36          // maxLength
310                          );
311 
312     columnList[index++] = new SystemColumnImpl(  
313                 convertIdCase( SCHEMA_ID_COL_NAME),  // column name
314                 SCHEMA_ID_COL_NUM,  // schema number
315                 0,          // precision
316                 0,          // scale
317                 false,        // nullability
318                 "CHAR",        // dataType
319                 true,        // built-in type
320                 36          // maxLength
321                          );
322 
323     columnList[index++] = new SystemColumnImpl(  
324                 convertIdCase( NAME_COL_NAME),    // column name
325                 NAME_COL_NUM,       // column number
326                 false        // nullability
327                          );
328     columnList[index++] = 
329           new SystemColumnImpl(  
330               convertIdCase( GENERATION_ID_COL_NAME),    // column name
331               GENERATION_ID_COL_NUM,  // column number
332               0,          // precision
333               0,          // scale
334               false,        // nullability
335               "BIGINT",      // dataType
336               true,        // built-in type
337               TypeId.LONGINT_MAXWIDTH  // maxLength
338                      );
339     return  columnList;
340   }
341 
342 }