Source code: org/apache/derby/impl/sql/catalog/SYSCONGLOMERATESRowFactory.java
1 /*
2
3 Derby - Class org.apache.derby.impl.sql.catalog.SYSCONGLOMERATESRowFactory
4
5 Copyright 1997, 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.sanity.SanityManager;
24
25 import org.apache.derby.iapi.types.TypeId;
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
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.IndexRowGenerator;
38 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
39 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;
40
41 import org.apache.derby.iapi.types.RowLocation;
42
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.ExecutionContext;
46 import org.apache.derby.iapi.sql.execute.ExecutionFactory;
47
48 import org.apache.derby.iapi.error.StandardException;
49
50 import org.apache.derby.iapi.services.uuid.UUIDFactory;
51 import org.apache.derby.catalog.UUID;
52
53 import org.apache.derby.catalog.IndexDescriptor;
54
55 import java.util.Properties;
56
57 /**
58 * Factory for creating a SYSCONGLOMERATES row.
59 *
60 * @author ames
61 */
62
63 public class SYSCONGLOMERATESRowFactory extends CatalogRowFactory
64 {
65 private static final String TABLENAME_STRING = "SYSCONGLOMERATES";
66
67 protected static final int SYSCONGLOMERATES_COLUMN_COUNT = 8;
68 protected static final int SYSCONGLOMERATES_SCHEMAID = 1;
69 protected static final int SYSCONGLOMERATES_TABLEID = 2;
70 protected static final int SYSCONGLOMERATES_CONGLOMERATENUMBER = 3;
71 protected static final int SYSCONGLOMERATES_CONGLOMERATENAME = 4;
72 protected static final int SYSCONGLOMERATES_ISINDEX = 5;
73 protected static final int SYSCONGLOMERATES_DESCRIPTOR = 6;
74 protected static final int SYSCONGLOMERATES_ISCONSTRAINT = 7;
75 protected static final int SYSCONGLOMERATES_CONGLOMERATEID = 8;
76
77 protected static final int SYSCONGLOMERATES_INDEX1_ID = 0;
78 protected static final int SYSCONGLOMERATES_INDEX2_ID = 1;
79 protected static final int SYSCONGLOMERATES_INDEX3_ID = 2;
80
81 private static final boolean[] uniqueness = {
82 false,
83 true,
84 false
85 };
86
87 private static final int[][] indexColumnPositions =
88 {
89 {SYSCONGLOMERATES_CONGLOMERATEID},
90 {SYSCONGLOMERATES_CONGLOMERATENAME, SYSCONGLOMERATES_SCHEMAID},
91 {SYSCONGLOMERATES_TABLEID}
92 };
93
94 private static final String[][] indexColumnNames =
95 {
96 {"CONGLOMERATE_ID"},
97 {"CONGLOMERATE_NAME", "SCHEMAID"},
98 {"TABLEID"}
99 };
100
101 private static final String[] uuids =
102 {
103 "80000010-00d0-fd77-3ed8-000a0a0b1900" // catalog UUID
104 ,"80000027-00d0-fd77-3ed8-000a0a0b1900" // heap UUID
105 ,"80000012-00d0-fd77-3ed8-000a0a0b1900" // SYSCONGLOMERATES_INDEX1
106 ,"80000014-00d0-fd77-3ed8-000a0a0b1900" // SYSCONGLOMERATES_INDEX2
107 ,"80000016-00d0-fd77-3ed8-000a0a0b1900" // SYSCONGLOMERATES_INDEX3
108 };
109
110 SYSCONGLOMERATESRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf,
111 boolean convertIdToLower)
112 {
113 super(uuidf,ef,dvf,convertIdToLower);
114 initInfo(SYSCONGLOMERATES_COLUMN_COUNT,
115 TABLENAME_STRING, indexColumnPositions,
116 indexColumnNames, uniqueness, uuids );
117 }
118
119 /**
120 * Make a SYSCONGLOMERATES row
121 *
122 * @param emptyRow Make an empty row if this parameter is true
123 * @param sd Schema Descriptor
124 * @param conglomerate conglomerate descriptor
125 *
126 * @return Row suitable for inserting into SYSCONGLOMERATES.
127 *
128 * @exception StandardException thrown on failure
129 */
130 public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent)
131 throws StandardException
132 {
133 ExecRow row;
134 DataValueDescriptor col;
135 String tabID =null;
136 Long conglomNumber = null;
137 String conglomName = null;
138 Boolean supportsIndex = null;
139 IndexRowGenerator indexRowGenerator = null;
140 Boolean supportsConstraint = null;
141 String conglomUUIDString = null;
142 String schemaID = null;
143 ConglomerateDescriptor conglomerate = (ConglomerateDescriptor)td;
144
145 /* Insert info into sysconglomerates */
146
147 if (td != null)
148 {
149 /* Sometimes the SchemaDescriptor is non-null and sometimes it
150 * is null. (We can't just rely on getting the schema id from
151 * the ConglomerateDescriptor because it can be null when
152 * we are creating a new conglomerate.
153 */
154 if (parent != null)
155 {
156 SchemaDescriptor sd = (SchemaDescriptor)parent;
157 schemaID = sd.getUUID().toString();
158 }
159 else
160 {
161 schemaID = conglomerate.getSchemaID().toString();
162 }
163 tabID = conglomerate.getTableID().toString();
164 conglomNumber = new Long( conglomerate.getConglomerateNumber() );
165 conglomName = conglomerate.getConglomerateName();
166 conglomUUIDString = conglomerate.getUUID().toString();
167
168 supportsIndex = new Boolean( conglomerate.isIndex() );
169 indexRowGenerator = conglomerate.getIndexDescriptor();
170 supportsConstraint = new Boolean( conglomerate.isConstraint() );
171 }
172
173 /* RESOLVE - It would be nice to require less knowledge about sysconglomerates
174 * and have this be more table driven.
175 */
176
177 /* Build the row to insert */
178 row = getExecutionFactory().getValueRow(SYSCONGLOMERATES_COLUMN_COUNT);
179
180 /* 1st column is SCHEMAID (UUID - char(36)) */
181 row.setColumn(1, dvf.getCharDataValue(schemaID));
182
183 /* 2nd column is TABLEID (UUID - char(36)) */
184 row.setColumn(2, dvf.getCharDataValue(tabID));
185
186 /* 3rd column is CONGLOMERATENUMBER (long) */
187 row.setColumn(3, dvf.getDataValue(conglomNumber));
188
189 /* 4th column is CONGLOMERATENAME (varchar(128))
190 ** If null, use the tableid so we always
191 ** have a unique column
192 */
193 row.setColumn(4, (conglomName == null) ?
194 dvf.getVarcharDataValue(tabID):
195 dvf.getVarcharDataValue(conglomName));
196
197 /* 5th column is ISINDEX (boolean) */
198 row.setColumn(5, dvf.getDataValue(supportsIndex));
199
200 /* 6th column is DESCRIPTOR
201 * (user type org.apache.derby.catalog.IndexDescriptor)
202 */
203 row.setColumn(6,
204 dvf.getDataValue(
205 (indexRowGenerator == null ?
206 (IndexDescriptor) null :
207 indexRowGenerator.getIndexDescriptor()
208 )
209 )
210 );
211
212 /* 7th column is ISCONSTRAINT (boolean) */
213 row.setColumn(7, dvf.getDataValue(supportsConstraint));
214
215 /* 8th column is CONGLOMERATEID (UUID - char(36)) */
216 row.setColumn(8, dvf.getCharDataValue(conglomUUIDString));
217
218 return row;
219 }
220
221 public ExecRow makeEmptyRow() throws StandardException
222 {
223 return makeRow(null, null);
224 }
225
226 /**
227 * Builds an empty index row.
228 *
229 * @param indexNumber Index to build empty row for.
230 * @param rowLocation Row location for last column of index row
231 *
232 * @return corresponding empty index row
233 * @exception StandardException thrown on failure
234 */
235 public ExecIndexRow buildEmptyIndexRow( int indexNumber,
236 RowLocation rowLocation)
237 throws StandardException
238 {
239 int ncols = getIndexColumnCount(indexNumber);
240 ExecIndexRow row = getExecutionFactory().getIndexableRow(ncols + 1);
241
242 row.setColumn(ncols + 1, rowLocation);
243
244 switch( indexNumber )
245 {
246 case SYSCONGLOMERATES_INDEX1_ID:
247
248 /* 1st column is CONGLOMERATEID (char(36)) */
249 row.setColumn(1, getDataValueFactory().getCharDataValue((String) null));
250 break;
251
252 case SYSCONGLOMERATES_INDEX2_ID:
253
254 /* 1st column is CONGLOMERATENAME (varchar(128)) */
255 row.setColumn(1, getDataValueFactory().getVarcharDataValue((String) null));
256
257 /* 2nd column is SCHEMAID (char(36)) */
258 row.setColumn(2, getDataValueFactory().getCharDataValue((String) null));
259
260 break;
261
262 case SYSCONGLOMERATES_INDEX3_ID:
263
264 /* Build the row */
265 /* NOTE: this index is not unique, need extra column in template for
266 * drop method in DataDictionary.
267 */
268
269 /* 1st column is TABLEID (char(36)) */
270 row.setColumn(1, getDataValueFactory().getCharDataValue((String) null));
271
272 break;
273
274 } // end switch
275
276 return row;
277 }
278
279 /**
280 * Get the Properties associated with creating the heap.
281 *
282 * @return The Properties associated with creating the heap.
283 */
284 public Properties getCreateHeapProperties()
285 {
286 Properties properties = new Properties();
287 // keep page size at 4K since its a big table
288 properties.put("derby.storage.pageSize","4096");
289 // default properties for system tables:
290 properties.put("derby.storage.pageReservedSpace","0");
291 properties.put("derby.storage.minimumRecordSize","1");
292 return properties;
293 }
294
295 /**
296 * Get the Properties associated with creating the specified index.
297 *
298 * @param indexNumber The specified index number.
299 *
300 * @return The Properties associated with creating the specified index.
301 */
302 public Properties getCreateIndexProperties(int indexNumber)
303 {
304 Properties properties = new Properties();
305 // keep page size for all indexes at 4K since its a big table
306 properties.put("derby.storage.pageSize","4096");
307 return properties;
308 }
309
310 ///////////////////////////////////////////////////////////////////////////
311 //
312 // ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory
313 //
314 ///////////////////////////////////////////////////////////////////////////
315
316 /**
317 *
318 * @param row a SYSCOLUMNS row
319 * @param parentTupleDescriptor Null for this kind of descriptor.
320 * @param dd dataDictionary
321 *
322 * @return a conglomerate descriptor equivalent to a SYSCONGOMERATES row
323 *
324 * @exception StandardException thrown on failure
325 */
326
327 public TupleDescriptor buildDescriptor(
328 ExecRow row,
329 TupleDescriptor parentTupleDescriptor,
330 DataDictionary dd )
331 throws StandardException
332 {
333 if (SanityManager.DEBUG)
334 SanityManager.ASSERT(
335 row.nColumns() == SYSCONGLOMERATES_COLUMN_COUNT,
336 "Wrong number of columns for a SYSCONGLOMERATES row");
337
338 DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
339 long conglomerateNumber;
340 String name;
341 boolean isConstraint;
342 boolean isIndex;
343 IndexRowGenerator indexRowGenerator;
344 DataValueDescriptor col;
345 ConglomerateDescriptor conglomerateDesc;
346 String conglomUUIDString;
347 UUID conglomUUID;
348 String schemaUUIDString;
349 UUID schemaUUID;
350 String tableUUIDString;
351 UUID tableUUID;
352
353 /* 1st column is SCHEMAID (UUID - char(36)) */
354 col = row.getColumn(1);
355 schemaUUIDString = col.getString();
356 schemaUUID = getUUIDFactory().recreateUUID(schemaUUIDString);
357
358 /* 2nd column is TABLEID (UUID - char(36)) */
359 col = row.getColumn(2);
360 tableUUIDString = col.getString();
361 tableUUID = getUUIDFactory().recreateUUID(tableUUIDString);
362
363
364 /* 3nd column is CONGLOMERATENUMBER (long) */
365 col = row.getColumn(3);
366 conglomerateNumber = col.getLong();
367
368 /* 4rd column is CONGLOMERATENAME (varchar(128)) */
369 col = row.getColumn(4);
370 name = col.getString();
371
372 /* 5th column is ISINDEX (boolean) */
373 col = row.getColumn(5);
374 isIndex = col.getBoolean();
375
376 /* 6th column is DESCRIPTOR */
377 col = row.getColumn(6);
378 indexRowGenerator = new IndexRowGenerator(
379 (IndexDescriptor) col.getObject());
380
381 /* 7th column is ISCONSTRAINT (boolean) */
382 col = row.getColumn(7);
383 isConstraint = col.getBoolean();
384
385 /* 8th column is CONGLOMERATEID (UUID - char(36)) */
386 col = row.getColumn(8);
387 conglomUUIDString = col.getString();
388 conglomUUID = getUUIDFactory().recreateUUID(conglomUUIDString);
389
390 /* now build and return the descriptor */
391 conglomerateDesc = ddg.newConglomerateDescriptor(conglomerateNumber,
392 name,
393 isIndex,
394 indexRowGenerator,
395 isConstraint,
396 conglomUUID,
397 tableUUID,
398 schemaUUID);
399 return conglomerateDesc;
400 }
401
402 /**
403 * Get the conglomerate's UUID of the row.
404 *
405 * @param row The row from sysconglomerates
406 *
407 * @return UUID The conglomerates UUID
408 *
409 * @exception StandardException thrown on failure
410 */
411 protected UUID getConglomerateUUID(ExecRow row)
412 throws StandardException
413 {
414 DataValueDescriptor col;
415 String conglomerateUUIDString;
416
417 /* 8th column is CONGLOMERATEID (UUID - char(36)) */
418 col = row.getColumn(SYSCONGLOMERATES_CONGLOMERATEID);
419 conglomerateUUIDString = col.getString();
420 return getUUIDFactory().recreateUUID(conglomerateUUIDString);
421 }
422
423 /**
424 * Get the table's UUID from the row.
425 *
426 * @param row The row from sysconglomerates
427 *
428 * @return UUID The table's UUID
429 *
430 * @exception StandardException thrown on failure
431 */
432 protected UUID getTableUUID(ExecRow row)
433 throws StandardException
434 {
435 DataValueDescriptor col;
436 String tableUUIDString;
437
438 /* 2nd column is TABLEID (UUID - char(36)) */
439 col = row.getColumn(SYSCONGLOMERATES_TABLEID);
440 tableUUIDString = col.getString();
441 return getUUIDFactory().recreateUUID(tableUUIDString);
442 }
443
444 /**
445 * Get the schema's UUID from the row.
446 *
447 * @param row The row from sysconglomerates
448 *
449 * @return UUID The schema's UUID
450 *
451 * @exception StandardException thrown on failure
452 */
453 protected UUID getSchemaUUID(ExecRow row)
454 throws StandardException
455 {
456 DataValueDescriptor col;
457 String schemaUUIDString;
458
459 /* 1st column is SCHEMAID (UUID - char(36)) */
460 col = row.getColumn(SYSCONGLOMERATES_SCHEMAID);
461 schemaUUIDString = col.getString();
462 return getUUIDFactory().recreateUUID(schemaUUIDString);
463 }
464
465 /**
466 * Get the conglomerate's name of the row.
467 *
468 * @param row The row from sysconglomerates
469 *
470 * @return String The conglomerates name
471 *
472 * @exception StandardException thrown on failure
473 */
474 protected String getConglomerateName(ExecRow row)
475 throws StandardException
476 {
477 DataValueDescriptor col;
478
479 /* 4th column is CONGLOMERATENAME (varchar(128)) */
480 col = row.getColumn(SYSCONGLOMERATES_CONGLOMERATENAME);
481 return col.getString();
482 }
483
484 /**
485 * Builds a list of columns suitable for creating this Catalog.
486 *
487 *
488 * @return array of SystemColumn suitable for making this catalog.
489 */
490 public SystemColumn[] buildColumnList()
491 {
492 int index = 0;
493 SystemColumn[] columnList = new SystemColumn[SYSCONGLOMERATES_COLUMN_COUNT];
494
495 // describe columns
496
497
498 columnList[index++] =
499 new SystemColumnImpl(
500 convertIdCase( "SCHEMAID"), // column name
501 SYSCONGLOMERATES_SCHEMAID, // column number
502 0, // precision
503 0, // scale
504 false, // nullability
505 "CHAR", // dataType
506 true, // built-in type
507 36 // maxLength
508 );
509
510 columnList[index++] =
511 new SystemColumnImpl(
512 convertIdCase( "TABLEID"), // column name
513 SYSCONGLOMERATES_TABLEID, // column number
514 0, // precision
515 0, // scale
516 false, // nullability
517 "CHAR", // dataType
518 true, // built-in type
519 36 // maxLength
520 );
521 columnList[index++] =
522 new SystemColumnImpl(
523 convertIdCase( "CONGLOMERATENUMBER"), // column name
524 SYSCONGLOMERATES_CONGLOMERATENUMBER, // column number
525 0, // precision
526 0, // scale
527 false, // nullability
528 "BIGINT", // dataType
529 true, // built-in type
530 TypeId.LONGINT_MAXWIDTH // maxLength
531 );
532
533 columnList[index++] =
534 new SystemColumnImpl( // SQL IDENTIFIER
535 convertIdCase( "CONGLOMERATENAME"), // column name
536 SYSCONGLOMERATES_CONGLOMERATENAME,
537 true // nullability
538 );
539
540 columnList[index++] =
541 new SystemColumnImpl(
542 convertIdCase( "ISINDEX"), // column name
543 SYSCONGLOMERATES_ISINDEX, // column number
544 0, // precision
545 0, // scale
546 false, // nullability
547 "BOOLEAN", // dataType
548 true, // built-in type
549 1 // maxLength
550 );
551
552 columnList[index++] =
553 new SystemColumnImpl(
554 convertIdCase( "DESCRIPTOR"), // column name
555 SYSCONGLOMERATES_DESCRIPTOR,
556 0, // precision
557 0, // scale
558 true, // nullability
559 "org.apache.derby.catalog.IndexDescriptor", // datatype
560 false, // built-in type
561 TypeDescriptor.MAXIMUM_WIDTH_UNKNOWN
562 // maxLength
563 );
564
565 columnList[index++] =
566 new SystemColumnImpl(
567 convertIdCase( "ISCONSTRAINT"), // column name
568 SYSCONGLOMERATES_ISCONSTRAINT,
569 0, // precision
570 0, // scale
571 true, // nullability
572 "BOOLEAN", // datatype
573 true, // built-in type
574 1 // maxLength
575 );
576
577 columnList[index++] =
578 new SystemColumnImpl(
579 convertIdCase( "CONGLOMERATEID"), // column name
580 SYSCONGLOMERATES_CONGLOMERATEID,
581 0, // precision
582 0, // scale
583 false, // nullability
584 "CHAR", // datatype
585 true, // built-in type
586 36 // maxLength
587 );
588
589 return columnList;
590
591 }
592 }