1 21 22 package org.apache.derby.impl.sql.catalog; 23 24 import org.apache.derby.iapi.types.RowLocation; 25 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 28 import org.apache.derby.iapi.types.TypeId; 29 import org.apache.derby.iapi.sql.dictionary.SystemColumn; 30 import org.apache.derby.catalog.TypeDescriptor; 31 32 import org.apache.derby.iapi.types.DataValueDescriptor; 33 34 import org.apache.derby.iapi.types.DataValueFactory; 35 36 import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory; 37 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 38 import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor; 39 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator; 40 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 41 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext; 42 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 43 import org.apache.derby.iapi.sql.dictionary.SubCheckConstraintDescriptor; 44 import org.apache.derby.iapi.sql.dictionary.SubConstraintDescriptor; 45 import org.apache.derby.iapi.sql.dictionary.SubKeyConstraintDescriptor; 46 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 47 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor; 48 49 import org.apache.derby.iapi.services.uuid.UUIDFactory; 50 import org.apache.derby.catalog.UUID; 51 52 import org.apache.derby.iapi.sql.execute.ExecIndexRow; 53 import org.apache.derby.iapi.sql.execute.ExecRow; 54 import org.apache.derby.iapi.sql.execute.ExecutionContext; 55 import org.apache.derby.iapi.sql.execute.ExecutionFactory; 56 57 import org.apache.derby.iapi.error.StandardException; 58 59 import org.apache.derby.catalog.IndexDescriptor; 60 61 66 67 public class SYSCONSTRAINTSRowFactory extends CatalogRowFactory 68 { 69 private static final String TABLENAME_STRING = "SYSCONSTRAINTS"; 70 71 protected static final int SYSCONSTRAINTS_COLUMN_COUNT = 7; 72 protected static final int SYSCONSTRAINTS_CONSTRAINTID = 1; 73 protected static final int SYSCONSTRAINTS_TABLEID = 2; 74 protected static final int SYSCONSTRAINTS_CONSTRAINTNAME = 3; 75 protected static final int SYSCONSTRAINTS_TYPE = 4; 76 protected static final int SYSCONSTRAINTS_SCHEMAID = 5; 77 protected static final int SYSCONSTRAINTS_STATE = ConstraintDescriptor.SYSCONSTRAINTS_STATE_FIELD; 78 protected static final int SYSCONSTRAINTS_REFERENCECOUNT = 7; 79 80 protected static final int SYSCONSTRAINTS_INDEX1_ID = 0; 81 protected static final int SYSCONSTRAINTS_INDEX2_ID = 1; 82 protected static final int SYSCONSTRAINTS_INDEX3_ID = 2; 83 84 private static final boolean[] uniqueness = { 85 true, 86 true, 87 false 88 }; 89 90 private static final int[][] indexColumnPositions = 91 { 92 {SYSCONSTRAINTS_CONSTRAINTID}, 93 {SYSCONSTRAINTS_CONSTRAINTNAME, SYSCONSTRAINTS_SCHEMAID}, 94 {SYSCONSTRAINTS_TABLEID} 95 }; 96 97 private static final String [] uuids = 98 { 99 "8000002f-00d0-fd77-3ed8-000a0a0b1900" ,"80000036-00d0-fd77-3ed8-000a0a0b1900" ,"80000031-00d0-fd77-3ed8-000a0a0b1900" ,"80000033-00d0-fd77-3ed8-000a0a0b1900" ,"80000035-00d0-fd77-3ed8-000a0a0b1900" }; 105 106 112 public SYSCONSTRAINTSRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf, 113 boolean convertIdToLower) 114 { 115 super(uuidf,ef,dvf,convertIdToLower); 116 initInfo(SYSCONSTRAINTS_COLUMN_COUNT, TABLENAME_STRING, 117 indexColumnPositions, uniqueness, uuids ); 118 } 119 120 126 133 public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) 134 throws StandardException 135 { 136 DataValueDescriptor col; 137 ExecRow row; 138 int constraintIType; 139 UUID oid; 140 String constraintSType = null; 141 String constraintID = null; 142 String tableID = null; 143 String constraintName = null; 144 String schemaID = null; 145 boolean isEnabled = true; 146 int referenceCount = 0; 147 148 if (td != null) 149 { 150 ConstraintDescriptor constraint = (ConstraintDescriptor)td; 151 155 oid = constraint.getUUID(); 156 constraintID = oid.toString(); 157 158 oid = constraint.getTableId(); 159 tableID = oid.toString(); 160 161 constraintName = constraint.getConstraintName(); 162 163 constraintIType = constraint.getConstraintType(); 164 switch (constraintIType) 165 { 166 case DataDictionary.PRIMARYKEY_CONSTRAINT: 167 constraintSType = "P"; 168 break; 169 170 case DataDictionary.UNIQUE_CONSTRAINT: 171 constraintSType = "U"; 172 break; 173 174 case DataDictionary.CHECK_CONSTRAINT: 175 constraintSType = "C"; 176 break; 177 178 case DataDictionary.FOREIGNKEY_CONSTRAINT: 179 constraintSType = "F"; 180 break; 181 182 default: 183 if (SanityManager.DEBUG) 184 { 185 SanityManager.THROWASSERT("invalid constraint type"); 186 } 187 } 188 189 schemaID = constraint.getSchemaDescriptor().getUUID().toString(); 190 isEnabled = constraint.isEnabled(); 191 referenceCount = constraint.getReferenceCount(); 192 } 193 194 195 196 199 200 201 row = getExecutionFactory().getValueRow(SYSCONSTRAINTS_COLUMN_COUNT); 202 203 204 row.setColumn(SYSCONSTRAINTS_CONSTRAINTID, dvf.getCharDataValue(constraintID)); 205 206 207 row.setColumn(SYSCONSTRAINTS_TABLEID, dvf.getCharDataValue(tableID)); 208 209 210 row.setColumn(SYSCONSTRAINTS_CONSTRAINTNAME, dvf.getVarcharDataValue(constraintName)); 211 212 213 row.setColumn(SYSCONSTRAINTS_TYPE, dvf.getCharDataValue(constraintSType)); 214 215 216 row.setColumn(SYSCONSTRAINTS_SCHEMAID, dvf.getCharDataValue(schemaID)); 217 218 219 row.setColumn(SYSCONSTRAINTS_STATE, dvf.getCharDataValue(isEnabled ? "E" : "D")); 220 221 222 row.setColumn(SYSCONSTRAINTS_REFERENCECOUNT, dvf.getDataValue(referenceCount)); 223 224 return row; 225 } 226 227 228 234 243 public TupleDescriptor buildDescriptor( 244 ExecRow row, 245 TupleDescriptor parentTupleDescriptor, 246 DataDictionary dd ) 247 throws StandardException 248 { 249 ConstraintDescriptor constraintDesc = null; 250 251 if (SanityManager.DEBUG) 252 { 253 SanityManager.ASSERT( 254 row.nColumns() == SYSCONSTRAINTS_COLUMN_COUNT, 255 "Wrong number of columns for a SYSCONSTRAINTS row"); 256 } 257 258 DataValueDescriptor col; 259 ConglomerateDescriptor conglomDesc; 260 DataDescriptorGenerator ddg; 261 TableDescriptor td = null; 262 int constraintIType = -1; 263 int[] keyColumns = null; 264 UUID constraintUUID; 265 UUID schemaUUID; 266 UUID tableUUID; 267 UUID referencedConstraintId = null; 268 SchemaDescriptor schema; 269 String tableUUIDString; 270 String constraintName; 271 String constraintSType; 272 String constraintStateStr; 273 boolean constraintEnabled; 274 int referenceCount; 275 String constraintUUIDString; 276 String schemaUUIDString; 277 SubConstraintDescriptor scd; 278 279 if (SanityManager.DEBUG) 280 { 281 if (!(parentTupleDescriptor instanceof SubConstraintDescriptor)) 282 { 283 SanityManager.THROWASSERT( 284 "parentTupleDescriptor expected to be instanceof " + 285 "SubConstraintDescriptor, not " + 286 parentTupleDescriptor.getClass().getName()); 287 } 288 } 289 290 scd = (SubConstraintDescriptor) parentTupleDescriptor; 291 292 ddg = dd.getDataDescriptorGenerator(); 293 294 295 col = row.getColumn(SYSCONSTRAINTS_CONSTRAINTID); 296 constraintUUIDString = col.getString(); 297 constraintUUID = getUUIDFactory().recreateUUID(constraintUUIDString); 298 299 300 col = row.getColumn(SYSCONSTRAINTS_TABLEID); 301 tableUUIDString = col.getString(); 302 tableUUID = getUUIDFactory().recreateUUID(tableUUIDString); 303 304 309 if (scd != null) 310 { 311 td = scd.getTableDescriptor(); 312 } 313 if (td == null) 314 { 315 td = dd.getTableDescriptor(tableUUID); 316 } 317 318 319 col = row.getColumn(SYSCONSTRAINTS_CONSTRAINTNAME); 320 constraintName = col.getString(); 321 322 323 col = row.getColumn(SYSCONSTRAINTS_TYPE); 324 constraintSType = col.getString(); 325 if (SanityManager.DEBUG) 326 { 327 SanityManager.ASSERT(constraintSType.length() == 1, 328 "Fourth column type incorrect"); 329 } 330 331 boolean typeSet = false; 332 switch (constraintSType.charAt(0)) 333 { 334 case 'P' : 335 constraintIType = DataDictionary.PRIMARYKEY_CONSTRAINT; 336 typeSet = true; 337 339 case 'U' : 340 if (! typeSet) 341 { 342 constraintIType = DataDictionary.UNIQUE_CONSTRAINT; 343 typeSet = true; 344 } 345 347 case 'F' : 348 if (! typeSet) 349 constraintIType = DataDictionary.FOREIGNKEY_CONSTRAINT; 350 if (SanityManager.DEBUG) 351 { 352 if (!(parentTupleDescriptor instanceof SubKeyConstraintDescriptor)) 353 { 354 SanityManager.THROWASSERT( 355 "parentTupleDescriptor expected to be instanceof " + 356 "SubKeyConstraintDescriptor, not " + 357 parentTupleDescriptor.getClass().getName()); 358 } 359 } 360 conglomDesc = td.getConglomerateDescriptor( 361 ((SubKeyConstraintDescriptor) 362 parentTupleDescriptor).getIndexId()); 363 373 if (conglomDesc == null) 374 { 375 td = dd.getTableDescriptor(tableUUID); 381 if (scd != null) 382 scd.setTableDescriptor(td); 383 conglomDesc = td.getConglomerateDescriptor( 385 ((SubKeyConstraintDescriptor) 386 parentTupleDescriptor).getIndexId()); 387 } 388 389 if (SanityManager.DEBUG) 390 { 391 SanityManager.ASSERT(conglomDesc != null, 392 "conglomDesc is expected to be non-null for backing index"); 393 } 394 keyColumns = conglomDesc.getIndexDescriptor().baseColumnPositions(); 395 referencedConstraintId = ((SubKeyConstraintDescriptor) 396 parentTupleDescriptor).getKeyConstraintId(); 397 keyColumns = conglomDesc.getIndexDescriptor().baseColumnPositions(); 398 break; 399 400 case 'C' : 401 constraintIType = DataDictionary.CHECK_CONSTRAINT; 402 if (SanityManager.DEBUG) 403 { 404 if (!(parentTupleDescriptor instanceof SubCheckConstraintDescriptor)) 405 { 406 SanityManager.THROWASSERT("parentTupleDescriptor expected to be instanceof " + 407 "SubCheckConstraintDescriptor, not " + 408 parentTupleDescriptor.getClass().getName()); 409 } 410 } 411 break; 412 413 default: 414 if (SanityManager.DEBUG) 415 { 416 SanityManager.THROWASSERT("Fourth column value invalid"); 417 } 418 } 419 420 421 col = row.getColumn(SYSCONSTRAINTS_SCHEMAID); 422 schemaUUIDString = col.getString(); 423 schemaUUID = getUUIDFactory().recreateUUID(schemaUUIDString); 424 425 schema = dd.getSchemaDescriptor(schemaUUID, null); 426 427 428 col = row.getColumn(SYSCONSTRAINTS_STATE); 429 constraintStateStr = col.getString(); 430 if (SanityManager.DEBUG) 431 { 432 SanityManager.ASSERT(constraintStateStr.length() == 1, 433 "Sixth column (state) type incorrect"); 434 } 435 436 switch (constraintStateStr.charAt(0)) 437 { 438 case 'E': 439 constraintEnabled = true; 440 break; 441 case 'D': 442 constraintEnabled = false; 443 break; 444 default: 445 constraintEnabled = true; 446 if (SanityManager.DEBUG) 447 { 448 SanityManager.THROWASSERT("Invalidate state value '" 449 +constraintStateStr+ "' for constraint"); 450 } 451 } 452 453 454 col = row.getColumn(SYSCONSTRAINTS_REFERENCECOUNT); 455 referenceCount = col.getInt(); 456 457 458 459 switch (constraintIType) 460 { 461 case DataDictionary.PRIMARYKEY_CONSTRAINT : 462 constraintDesc = ddg.newPrimaryKeyConstraintDescriptor( 463 td, 464 constraintName, 465 false, false, keyColumns, constraintUUID, 469 ((SubKeyConstraintDescriptor) 470 parentTupleDescriptor).getIndexId(), 471 schema, 472 constraintEnabled, 473 referenceCount); 474 break; 475 476 case DataDictionary.UNIQUE_CONSTRAINT : 477 constraintDesc = ddg.newUniqueConstraintDescriptor( 478 td, 479 constraintName, 480 false, false, keyColumns, constraintUUID, 484 ((SubKeyConstraintDescriptor) 485 parentTupleDescriptor).getIndexId(), 486 schema, 487 constraintEnabled, 488 referenceCount); 489 break; 490 491 case DataDictionary.FOREIGNKEY_CONSTRAINT : 492 if (SanityManager.DEBUG) 493 { 494 SanityManager.ASSERT(referenceCount == 0, 495 "REFERENCECOUNT column is nonzero for fk constraint"); 496 } 497 498 constraintDesc = ddg.newForeignKeyConstraintDescriptor( 499 td, 500 constraintName, 501 false, false, keyColumns, constraintUUID, 505 ((SubKeyConstraintDescriptor) 506 parentTupleDescriptor).getIndexId(), 507 schema, 508 referencedConstraintId, 509 constraintEnabled, 510 ((SubKeyConstraintDescriptor) 511 parentTupleDescriptor).getRaDeleteRule(), 512 ((SubKeyConstraintDescriptor) 513 parentTupleDescriptor).getRaUpdateRule() 514 ); 515 break; 516 517 case DataDictionary.CHECK_CONSTRAINT : 518 if (SanityManager.DEBUG) 519 { 520 SanityManager.ASSERT(referenceCount == 0, 521 "REFERENCECOUNT column is nonzero for check constraint"); 522 } 523 524 constraintDesc = ddg.newCheckConstraintDescriptor( 525 td, 526 constraintName, 527 false, false, constraintUUID, 530 ((SubCheckConstraintDescriptor) 531 parentTupleDescriptor).getConstraintText(), 532 ((SubCheckConstraintDescriptor) 533 parentTupleDescriptor).getReferencedColumnsDescriptor(), 534 schema, 535 constraintEnabled); 536 break; 537 } 538 return constraintDesc; 539 } 540 541 550 protected UUID getConstraintId(ExecRow row) 551 throws StandardException 552 { 553 DataValueDescriptor col; 554 String constraintUUIDString; 555 556 557 col = row.getColumn(SYSCONSTRAINTS_CONSTRAINTID); 558 constraintUUIDString = col.getString(); 559 return getUUIDFactory().recreateUUID(constraintUUIDString); 560 } 561 562 571 protected String getConstraintName(ExecRow row) 572 throws StandardException 573 { 574 DataValueDescriptor col; 575 String constraintName; 576 577 578 col = row.getColumn(SYSCONSTRAINTS_CONSTRAINTNAME); 579 constraintName = col.getString(); 580 return constraintName; 581 } 582 583 592 protected UUID getSchemaId(ExecRow row) 593 throws StandardException 594 { 595 DataValueDescriptor col; 596 String schemaUUIDString; 597 598 599 col = row.getColumn(SYSCONSTRAINTS_SCHEMAID); 600 schemaUUIDString =col.getString(); 601 return getUUIDFactory().recreateUUID(schemaUUIDString); 602 } 603 604 613 protected UUID getTableId(ExecRow row) 614 throws StandardException 615 { 616 DataValueDescriptor col; 617 String tableUUIDString; 618 619 620 col = row.getColumn(SYSCONSTRAINTS_TABLEID); 621 tableUUIDString = col.getString(); 622 return getUUIDFactory().recreateUUID(tableUUIDString); 623 } 624 625 634 protected int getConstraintType(ExecRow row) 635 throws StandardException 636 { 637 DataValueDescriptor col; 638 int constraintIType; 639 String constraintSType; 640 641 642 col = row.getColumn(SYSCONSTRAINTS_TYPE); 643 constraintSType = col.getString(); 644 if (SanityManager.DEBUG) 645 { 646 SanityManager.ASSERT(constraintSType.length() == 1, 647 "Fourth column type incorrect"); 648 } 649 650 switch (constraintSType.charAt(0)) 651 { 652 case 'P' : 653 constraintIType = DataDictionary.PRIMARYKEY_CONSTRAINT; 654 break; 655 656 case 'U' : 657 constraintIType = DataDictionary.UNIQUE_CONSTRAINT; 658 break; 659 660 case 'C' : 661 constraintIType = DataDictionary.CHECK_CONSTRAINT; 662 break; 663 664 case 'F' : 665 constraintIType = DataDictionary.FOREIGNKEY_CONSTRAINT; 666 break; 667 668 default: 669 if (SanityManager.DEBUG) 670 { 671 SanityManager.THROWASSERT("Fourth column value invalid"); 672 } 673 constraintIType = -1; 674 } 675 676 return constraintIType; 677 } 678 679 685 public SystemColumn[] buildColumnList() 686 { 687 int index = 0; 688 SystemColumn[] columnList = new SystemColumn[SYSCONSTRAINTS_COLUMN_COUNT]; 689 690 692 columnList[index++] = 693 new SystemColumnImpl( 694 convertIdCase( "CONSTRAINTID"), SYSCONSTRAINTS_CONSTRAINTID, 0, 0, false, "CHAR", true, 36 ); 703 704 columnList[index++] = 705 new SystemColumnImpl( 706 convertIdCase( "TABLEID"), SYSCONSTRAINTS_TABLEID, 0, 0, false, "CHAR", true, 36 ); 715 716 columnList[index++] = 717 new SystemColumnImpl( convertIdCase( "CONSTRAINTNAME"), SYSCONSTRAINTS_CONSTRAINTNAME, 720 false ); 722 723 columnList[index++] = 724 new SystemColumnImpl( 725 convertIdCase( "TYPE"), SYSCONSTRAINTS_TYPE, 0, 0, false, "CHAR", true, 1 ); 734 735 736 columnList[index++] = 737 new SystemColumnImpl( 738 convertIdCase( "SCHEMAID"), SYSCONSTRAINTS_SCHEMAID, 0, 0, false, "CHAR", true, 36 ); 747 748 columnList[index++] = 749 new SystemColumnImpl( 750 convertIdCase( "STATE"), SYSCONSTRAINTS_STATE, 0, 0, false, "CHAR", true, 1 ); 759 760 columnList[index++] = 761 new SystemColumnImpl( 762 convertIdCase( "REFERENCECOUNT"), SYSCONSTRAINTS_REFERENCECOUNT, 0, 0, false, "INTEGER", true, 1 ); 771 return columnList; 772 } 773 774 } 775 | Popular Tags |