1 21 22 package org.apache.derby.impl.sql.catalog; 23 24 import org.apache.derby.iapi.reference.Property; 25 26 import org.apache.derby.iapi.types.DataTypeDescriptor; 27 import org.apache.derby.iapi.types.TypeId; 28 import org.apache.derby.iapi.types.DataValueDescriptor; 29 30 import org.apache.derby.iapi.store.raw.RawStoreFactory; 31 32 import org.apache.derby.iapi.types.DataValueFactory; 33 import org.apache.derby.iapi.sql.dictionary.SystemColumn; 34 import org.apache.derby.catalog.TypeDescriptor; 35 import org.apache.derby.catalog.types.BaseTypeIdImpl; 36 37 import org.apache.derby.iapi.types.DataValueDescriptor; 38 39 import org.apache.derby.iapi.types.TypeId; 40 import org.apache.derby.iapi.types.DataTypeDescriptor; 41 import org.apache.derby.iapi.types.DataValueFactory; 42 import org.apache.derby.iapi.types.RowLocation; 43 44 import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory; 45 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor; 46 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator; 47 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 48 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 49 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor; 50 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 51 import org.apache.derby.iapi.sql.dictionary.UniqueTupleDescriptor; 52 53 import org.apache.derby.iapi.sql.execute.ExecutionContext; 54 import org.apache.derby.iapi.sql.execute.ExecutionFactory; 55 import org.apache.derby.iapi.sql.execute.ExecIndexRow; 56 import org.apache.derby.iapi.sql.execute.ExecRow; 57 58 import org.apache.derby.iapi.error.StandardException; 59 60 import org.apache.derby.iapi.services.sanity.SanityManager; 61 62 import org.apache.derby.iapi.services.monitor.Monitor; 63 import org.apache.derby.catalog.types.TypeDescriptorImpl; 64 import org.apache.derby.catalog.DefaultInfo; 65 import org.apache.derby.iapi.services.uuid.UUIDFactory; 66 import org.apache.derby.catalog.UUID; 67 68 import org.apache.derby.impl.sql.compile.ColumnDefinitionNode; 69 70 import org.apache.derby.catalog.types.DefaultInfoImpl; 71 72 import org.apache.derby.iapi.types.*; 73 74 import java.io.Serializable ; 75 76 import java.util.Properties ; 77 78 85 86 public class SYSCOLUMNSRowFactory extends CatalogRowFactory 87 { 88 static final String TABLENAME_STRING = "SYSCOLUMNS"; 89 90 93 public static final String OLD_REFERENCEID_NAME = "TABLEID"; 94 95 protected static final int SYSCOLUMNS_COLUMN_COUNT = 9; 96 97 98 protected static final int SYSCOLUMNS_TABLEID = 1; 101 protected static final int SYSCOLUMNS_REFERENCEID = 1; 102 protected static final int SYSCOLUMNS_COLUMNNAME = 2; 103 protected static final int SYSCOLUMNS_COLUMNNUMBER = 3; 104 protected static final int SYSCOLUMNS_COLUMNDATATYPE = 4; 105 protected static final int SYSCOLUMNS_COLUMNDEFAULT = 5; 106 protected static final int SYSCOLUMNS_COLUMNDEFAULTID = 6; 107 protected static final int SYSCOLUMNS_AUTOINCREMENTVALUE = 7; 108 protected static final int SYSCOLUMNS_AUTOINCREMENTSTART = 8; 109 protected static final int SYSCOLUMNS_AUTOINCREMENTINC = 9; 110 111 protected static final int SYSCOLUMNS_INDEX1_ID = 0; 113 114 protected static final int SYSCOLUMNS_INDEX2_ID = 1; 116 117 protected static final String REFERENCEDID_STRING = "REFERENCEID"; 118 protected static final String COLUMNNAME_STRING = "COLUMNNAME"; 119 protected static final String COLUMNDEFAULTID_STRING = "COLUMNDEFAULTID"; 120 121 private static final boolean[] uniqueness = { 122 true, 123 false 124 }; 125 126 private static final String [] uuids = 127 { 128 "8000001e-00d0-fd77-3ed8-000a0a0b1900" ,"80000029-00d0-fd77-3ed8-000a0a0b1900" ,"80000020-00d0-fd77-3ed8-000a0a0b1900" ,"6839c016-00d9-2829-dfcd-000a0a411400" }; 133 134 private static final int[][] indexColumnPositions = 135 { 136 {SYSCOLUMNS_REFERENCEID, SYSCOLUMNS_COLUMNNAME}, 137 {SYSCOLUMNS_COLUMNDEFAULTID} 138 }; 139 140 146 private SystemColumn[] columnList; 147 148 154 public SYSCOLUMNSRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf, 155 boolean convertIdToLower) 156 { 157 this(uuidf, ef, dvf, convertIdToLower, TABLENAME_STRING); 158 } 159 160 public SYSCOLUMNSRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf, 161 boolean convertIdToLower, String myName ) 162 { 163 super(uuidf,ef,dvf,convertIdToLower); 164 initInfo(SYSCOLUMNS_COLUMN_COUNT, myName, indexColumnPositions, uniqueness, uuids); 165 } 166 167 173 180 181 public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) 182 throws StandardException 183 { 184 ExecRow row; 185 DataValueDescriptor col; 186 187 String colName = null; 188 String defaultID = null; 189 String tabID = null; 190 Integer colID = null; 191 TypeDescriptorImpl typeDesc = null; 192 Object defaultSerializable = null; 193 long autoincStart = 0; 194 long autoincInc = 0; 195 long autoinc_create_or_modify_Start_Increment = -1; 202 203 if (td != null) 204 { 205 ColumnDescriptor column = (ColumnDescriptor)td; 206 207 208 typeDesc = column.getType().getCatalogType(); 209 210 tabID = column.getReferencingUUID().toString(); 211 colName = column.getColumnName(); 212 colID = new Integer (column.getPosition() ); 213 autoincStart = column.getAutoincStart(); 214 autoincInc = column.getAutoincInc(); 215 autoinc_create_or_modify_Start_Increment = column.getAutoinc_create_or_modify_Start_Increment(); 216 if (column.getDefaultInfo() != null) 217 { 218 defaultSerializable = column.getDefaultInfo(); 219 } 220 else 221 { 222 defaultSerializable = column.getDefaultValue(); 223 } 224 if (column.getDefaultUUID() != null) 225 { 226 defaultID = column.getDefaultUUID().toString(); 227 } 228 } 229 230 231 232 236 237 238 row = getExecutionFactory().getValueRow(SYSCOLUMNS_COLUMN_COUNT); 239 240 241 row.setColumn(SYSCOLUMNS_REFERENCEID, dvf.getCharDataValue(tabID)); 242 243 244 row.setColumn(SYSCOLUMNS_COLUMNNAME, dvf.getVarcharDataValue(colName)); 245 246 247 row.setColumn(SYSCOLUMNS_COLUMNNUMBER, dvf.getDataValue(colID)); 248 249 250 row.setColumn(SYSCOLUMNS_COLUMNDATATYPE, 251 dvf.getDataValue(typeDesc)); 252 253 254 row.setColumn(SYSCOLUMNS_COLUMNDEFAULT, 255 dvf.getDataValue(defaultSerializable)); 256 257 258 row.setColumn(SYSCOLUMNS_COLUMNDEFAULTID, dvf.getCharDataValue(defaultID)); 259 260 if (autoinc_create_or_modify_Start_Increment == ColumnDefinitionNode.CREATE_AUTOINCREMENT || 261 autoinc_create_or_modify_Start_Increment == ColumnDefinitionNode.MODIFY_AUTOINCREMENT_INC_VALUE) 262 { row.setColumn(SYSCOLUMNS_AUTOINCREMENTVALUE, 264 new SQLLongint(autoincStart)); 265 row.setColumn(SYSCOLUMNS_AUTOINCREMENTSTART, 266 new SQLLongint(autoincStart)); 267 row.setColumn(SYSCOLUMNS_AUTOINCREMENTINC, 268 new SQLLongint(autoincInc)); 269 } else if (autoinc_create_or_modify_Start_Increment == ColumnDefinitionNode.MODIFY_AUTOINCREMENT_RESTART_VALUE) 270 { ColumnDescriptor column = (ColumnDescriptor)td; 274 row.setColumn(SYSCOLUMNS_AUTOINCREMENTVALUE, new SQLLongint(autoincStart)); 275 row.setColumn(SYSCOLUMNS_AUTOINCREMENTSTART, new SQLLongint(autoincStart)); 276 row.setColumn(SYSCOLUMNS_AUTOINCREMENTINC, new SQLLongint( 277 column.getTableDescriptor().getColumnDescriptor(colName).getAutoincInc())); 278 } 279 else 280 { 281 row.setColumn(SYSCOLUMNS_AUTOINCREMENTVALUE, 282 new SQLLongint()); 283 row.setColumn(SYSCOLUMNS_AUTOINCREMENTSTART, 284 new SQLLongint()); 285 row.setColumn(SYSCOLUMNS_AUTOINCREMENTINC, 286 new SQLLongint()); 287 } 288 return row; 289 } 290 291 296 public Properties getCreateHeapProperties() 297 { 298 Properties properties = new Properties (); 299 properties.put(Property.PAGE_SIZE_PARAMETER,"4096"); 301 properties.put(RawStoreFactory.PAGE_RESERVED_SPACE_PARAMETER,"0"); 303 properties.put(RawStoreFactory.MINIMUM_RECORD_SIZE_PARAMETER,"1"); 304 return properties; 305 } 306 307 314 public Properties getCreateIndexProperties(int indexNumber) 315 { 316 Properties properties = new Properties (); 317 properties.put(Property.PAGE_SIZE_PARAMETER,"4096"); 319 return properties; 320 } 321 322 328 340 public TupleDescriptor buildDescriptor( 341 ExecRow row, 342 TupleDescriptor parentTupleDescriptor, 343 DataDictionary dd ) 344 throws StandardException 345 { 346 if (SanityManager.DEBUG) 347 { 348 SanityManager.ASSERT(row.nColumns() == SYSCOLUMNS_COLUMN_COUNT, 349 "Wrong number of columns for a SYSCOLUMNS row"); 350 } 351 352 int columnNumber; 353 String columnName; 354 String defaultID; 355 DefaultInfoImpl defaultInfo = null; 356 ColumnDescriptor colDesc; 357 BaseTypeIdImpl typeId; 358 TypeId wrapperTypeId; 359 DataValueDescriptor defaultValue = null; 360 UUID defaultUUID = null; 361 UUID uuid = null; 362 UUIDFactory uuidFactory = getUUIDFactory(); 363 long autoincStart, autoincInc; 364 365 DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator(); 366 367 368 372 if (parentTupleDescriptor != null) 373 { 374 if (SanityManager.DEBUG) 375 { 376 if (!(parentTupleDescriptor instanceof UniqueTupleDescriptor)) 377 { 378 SanityManager.THROWASSERT(parentTupleDescriptor.getClass().getName() 379 + " not instanceof UniqueTupleDescriptor"); 380 } 381 } 382 uuid = ((UniqueTupleDescriptor)parentTupleDescriptor).getUUID(); 383 } 384 else 385 { 386 387 uuid = uuidFactory.recreateUUID(row.getColumn(SYSCOLUMNS_REFERENCEID). 388 getString()); 389 } 390 391 394 395 Object object = row.getColumn(SYSCOLUMNS_COLUMNDEFAULT).getObject(); 397 if (object instanceof DataValueDescriptor) 398 { 399 defaultValue = (DataValueDescriptor) object; 400 } 401 else if (object instanceof DefaultInfoImpl) 402 { 403 defaultInfo = (DefaultInfoImpl) object; 404 defaultValue = defaultInfo.getDefaultValue(); 405 } 406 407 408 defaultID = row.getColumn(SYSCOLUMNS_COLUMNDEFAULTID).getString(); 409 410 if (defaultID != null) 411 { 412 defaultUUID = uuidFactory.recreateUUID(defaultID); 413 } 414 415 416 columnName = row.getColumn(SYSCOLUMNS_COLUMNNAME).getString(); 417 418 419 columnNumber = row.getColumn(SYSCOLUMNS_COLUMNNUMBER).getInt(); 420 421 422 423 431 TypeDescriptorImpl typeDescriptor = (TypeDescriptorImpl) row.getColumn(SYSCOLUMNS_COLUMNDATATYPE). 432 getObject(); 433 typeId = typeDescriptor.getTypeId(); 434 435 439 wrapperTypeId = 440 (TypeId) Monitor.newInstanceFromIdentifier(typeId.wrapperTypeFormatId()); 441 442 wrapperTypeId.setNestedTypeId(typeId); 443 444 445 DataTypeDescriptor dataTypeServices = new DataTypeDescriptor(typeDescriptor, 446 wrapperTypeId); 447 448 449 450 451 autoincStart = row.getColumn(SYSCOLUMNS_AUTOINCREMENTSTART).getLong(); 452 453 454 autoincInc = row.getColumn(SYSCOLUMNS_AUTOINCREMENTINC).getLong(); 455 456 DataValueDescriptor col = row.getColumn(SYSCOLUMNS_AUTOINCREMENTSTART); 457 autoincStart = col.getLong(); 458 459 col = row.getColumn(SYSCOLUMNS_AUTOINCREMENTINC); 460 autoincInc = col.getLong(); 461 462 colDesc = new ColumnDescriptor(columnName, columnNumber, 463 dataTypeServices, defaultValue, defaultInfo, uuid, 464 defaultUUID, autoincStart, autoincInc); 465 return colDesc; 466 } 467 468 474 public int getPrimaryKeyIndexNumber() 475 { 476 return SYSCOLUMNS_INDEX1_ID; 477 } 478 479 485 public SystemColumn[] buildColumnList() 486 { 487 if ( columnList != null ) { return columnList; } 488 489 columnList = new SystemColumn[SYSCOLUMNS_COLUMN_COUNT]; 490 491 493 columnList[0] = 494 new SystemColumnImpl( 495 convertIdCase( REFERENCEDID_STRING), SYSCOLUMNS_REFERENCEID, 0, 0, false, "CHAR", true, 36 ); 504 505 columnList[1] = 506 new SystemColumnImpl( convertIdCase( COLUMNNAME_STRING), SYSCOLUMNS_COLUMNNAME, false ); 511 512 columnList[2] = 513 new SystemColumnImpl( 514 convertIdCase( "COLUMNNUMBER"), SYSCOLUMNS_COLUMNNUMBER, 0, 0, false, "INTEGER", true, 4 ); 523 524 columnList[3] = 525 new SystemColumnImpl( 526 convertIdCase( "COLUMNDATATYPE"), SYSCOLUMNS_COLUMNDATATYPE, 0, 0, false, "org.apache.derby.catalog.TypeDescriptor", false, TypeDescriptor.MAXIMUM_WIDTH_UNKNOWN ); 535 536 columnList[4] = 537 new SystemColumnImpl( 538 convertIdCase( "COLUMNDEFAULT"), SYSCOLUMNS_COLUMNDEFAULT, 0, 0, true, "java.io.Serializable", false, TypeDescriptor.MAXIMUM_WIDTH_UNKNOWN ); 547 548 columnList[5] = 549 new SystemColumnImpl( 550 convertIdCase( COLUMNDEFAULTID_STRING), SYSCOLUMNS_COLUMNDEFAULTID, 0, 0, true, "CHAR", true, 36 ); 559 560 columnList[6] = 562 new SystemColumnImpl( 563 convertIdCase( "AUTOINCREMENTVALUE"), SYSCOLUMNS_AUTOINCREMENTVALUE, 565 0, 566 0, 567 true, 568 "BIGINT", 569 true, 570 TypeId.LONGINT_MAXWIDTH 571 ); 572 573 columnList[7] = 574 new SystemColumnImpl( 575 convertIdCase( "AUTOINCREMENTSTART"), SYSCOLUMNS_AUTOINCREMENTSTART, 577 0, 578 0, 579 true, 580 "BIGINT", 581 true, 582 TypeId.LONGINT_MAXWIDTH 583 ); 584 585 columnList[8] = 586 new SystemColumnImpl( 587 convertIdCase( "AUTOINCREMENTINC"), SYSCOLUMNS_AUTOINCREMENTINC, 589 0, 590 0, 591 true, 592 "BIGINT", 593 true, 594 TypeId.LONGINT_MAXWIDTH 595 ); 596 597 return columnList; 598 } 599 } 600 | Popular Tags |