1 21 22 package org.apache.derby.impl.sql.catalog; 23 24 import org.apache.derby.iapi.services.monitor.Monitor; 25 import org.apache.derby.iapi.services.sanity.SanityManager; 26 import org.apache.derby.iapi.services.io.Formatable; 27 import org.apache.derby.iapi.error.StandardException; 28 import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory; 29 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 30 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 31 import org.apache.derby.iapi.sql.dictionary.SPSDescriptor; 32 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 33 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 34 import org.apache.derby.iapi.sql.dictionary.AliasDescriptor; 35 import org.apache.derby.iapi.types.DataValueFactory; 36 import org.apache.derby.iapi.types.RowLocation; 37 import org.apache.derby.iapi.reference.SQLState; 38 import org.apache.derby.iapi.store.access.ConglomerateController; 39 import org.apache.derby.iapi.store.access.TransactionController; 40 import org.apache.derby.iapi.sql.dictionary.IndexRowGenerator; 41 import org.apache.derby.iapi.store.access.ScanController; 42 import org.apache.derby.iapi.sql.execute.ExecRow; 43 import org.apache.derby.iapi.sql.execute.ExecIndexRow; 44 import org.apache.derby.iapi.services.io.StoredFormatIds; 45 46 import org.apache.derby.iapi.services.io.FormatableBitSet; 47 import org.apache.derby.iapi.services.info.ProductGenusNames; 48 import org.apache.derby.iapi.services.info.ProductVersionHolder; 49 import org.apache.derby.iapi.reference.JDBC30Translation; 50 import org.apache.derby.iapi.reference.Limits; 51 import org.apache.derby.iapi.util.IdUtil; 52 53 import org.apache.derby.iapi.services.uuid.UUIDFactory; 54 import org.apache.derby.catalog.UUID; 55 import org.apache.derby.catalog.types.RoutineAliasInfo; 56 import org.apache.derby.catalog.AliasInfo; 57 import org.apache.derby.catalog.TypeDescriptor; 58 import org.apache.derby.iapi.types.DataTypeDescriptor; 59 import java.io.IOException ; 60 import java.io.ObjectInput ; 61 import java.io.ObjectOutput ; 62 import java.sql.Types ; 63 import java.util.Enumeration ; 64 import java.util.Properties ; 65 66 80 81 public class DD_Version implements Formatable 82 { 83 89 private transient DataDictionaryImpl bootingDictionary; 90 91 int majorVersionNumber; 92 private int minorVersionNumber; 93 94 100 103 public DD_Version() {} 104 105 106 112 DD_Version( DataDictionaryImpl bootingDictionary, int majorVersionNumber) 113 { 114 this.majorVersionNumber = majorVersionNumber; 115 this.minorVersionNumber = getJBMSMinorVersionNumber(); 116 this.bootingDictionary = bootingDictionary; 117 } 118 119 125 130 public String toString() 131 { 132 return DD_Version.majorToString(majorVersionNumber); 133 } 134 135 private static String majorToString(int majorVersionNumber) { 136 switch (majorVersionNumber) { 137 case DataDictionary.DD_VERSION_CS_5_0: 138 return "5.0"; 139 case DataDictionary.DD_VERSION_CS_5_1: 140 return "5.1"; 141 case DataDictionary.DD_VERSION_CS_5_2: 142 return "5.2"; 143 case DataDictionary.DD_VERSION_CS_8_1: 144 return "8.1"; 145 case DataDictionary.DD_VERSION_CS_10_0: 146 return "10.0"; 147 case DataDictionary.DD_VERSION_DERBY_10_1: 148 return "10.1"; 149 case DataDictionary.DD_VERSION_DERBY_10_2: 150 return "10.2"; 151 default: 152 return null; 153 } 154 } 155 156 162 169 void upgradeIfNeeded(DD_Version dictionaryVersion, 170 TransactionController tc, Properties startParams) 171 throws StandardException 172 { 173 if (dictionaryVersion.majorVersionNumber > majorVersionNumber) { 175 throw StandardException.newException(SQLState.LANG_CANT_UPGRADE_CATALOGS, 176 dictionaryVersion, this); 177 } 178 179 180 boolean minorOnly = false; 181 boolean performMajorUpgrade = false; 182 boolean softUpgradeRun = false; 183 boolean isReadOnly = bootingDictionary.af.isReadOnly(); 184 185 if (dictionaryVersion.majorVersionNumber == majorVersionNumber) { 186 187 if (dictionaryVersion.minorVersionNumber == minorVersionNumber) 189 return; 190 191 minorOnly = true; 193 194 } else { 195 196 if (Monitor.isFullUpgrade(startParams, dictionaryVersion.toString())) { 197 performMajorUpgrade = true; 198 } else { 199 softUpgradeRun = true; 200 } 201 } 202 203 tc.commit(); 205 206 if (performMajorUpgrade) { 207 String userName = IdUtil.getUserNameFromURLProps(startParams); 209 doFullUpgrade(tc, dictionaryVersion.majorVersionNumber,IdUtil.getUserAuthorizationId(userName)); 210 } 211 212 if (!minorOnly && !isReadOnly) { 213 216 DD_Version softUpgradeVersion = (DD_Version) tc.getProperty( 218 DataDictionary.SOFT_DATA_DICTIONARY_VERSION); 219 220 int softUpgradeMajorVersion = 0; 223 if (softUpgradeVersion != null) 224 softUpgradeMajorVersion = softUpgradeVersion.majorVersionNumber; 225 226 if (softUpgradeMajorVersion < majorVersionNumber) { 227 applySafeChanges( tc, dictionaryVersion.majorVersionNumber, softUpgradeMajorVersion); 228 } 229 } 230 231 handleMinorRevisionChange(tc, dictionaryVersion, softUpgradeRun); 235 236 tc.commit(); 238 } 239 240 260 private void applySafeChanges(TransactionController tc, int fromMajorVersionNumber, int lastSoftUpgradeVersion) 261 throws StandardException 262 { 263 264 284 285 tc.setProperty(DataDictionary.SOFT_DATA_DICTIONARY_VERSION, this, true); 286 } 287 288 310 private void doFullUpgrade(TransactionController tc, int fromMajorVersionNumber, String aid) 311 throws StandardException 312 { 313 if (fromMajorVersionNumber < DataDictionary.DD_VERSION_CS_10_0) 315 { 316 throw StandardException.newException(SQLState.UPGRADE_UNSUPPORTED, 317 DD_Version.majorToString(fromMajorVersionNumber), this); 318 } 319 320 dropJDBCMetadataSPSes(tc, false); 324 bootingDictionary.createSystemSps(tc); 325 326 347 348 if (fromMajorVersionNumber == DataDictionary.DD_VERSION_CS_10_0) 349 { 350 bootingDictionary.create_10_1_system_procedures( 354 tc, 355 bootingDictionary.getSystemUtilSchemaDescriptor().getUUID()); 356 } 357 358 if (fromMajorVersionNumber <= DataDictionary.DD_VERSION_DERBY_10_1) 359 { 360 bootingDictionary.create_10_2_system_procedures( 363 tc, 364 bootingDictionary.getSystemUtilSchemaDescriptor().getUUID()); 365 366 if (SanityManager.DEBUG) 367 SanityManager.ASSERT((aid != null), "Failed to get new Database Owner authorization"); 368 369 bootingDictionary.upgradeMakeCatalog(tc, DataDictionary.SYSTABLEPERMS_CATALOG_NUM); 371 bootingDictionary.upgradeMakeCatalog(tc, DataDictionary.SYSCOLPERMS_CATALOG_NUM); 372 bootingDictionary.upgradeMakeCatalog(tc, DataDictionary.SYSROUTINEPERMS_CATALOG_NUM); 373 374 bootingDictionary.updateSystemSchemaAuthorization(aid, tc); 376 377 bootingDictionary.grantPublicAccessToSystemRoutines(tc, aid); 379 } 380 381 } 382 383 395 private void handleMinorRevisionChange(TransactionController tc, DD_Version fromVersion, boolean softUpgradeRun) 396 throws StandardException 397 { 398 boolean isReadOnly = bootingDictionary.af.isReadOnly(); 399 400 if (!isReadOnly) { 401 bootingDictionary.clearSPSPlans(); 402 403 DD_Version lastRun; 404 405 if (softUpgradeRun) 406 { 407 fromVersion.minorVersionNumber = 1; lastRun = fromVersion; 411 } 412 else 413 { 414 lastRun = this; 416 417 fromVersion.majorVersionNumber = majorVersionNumber; 419 fromVersion.minorVersionNumber = minorVersionNumber; 420 } 421 422 tc.setProperty(DataDictionary.CORE_DATA_DICTIONARY_VERSION, fromVersion, true); 423 } 424 else 425 { 426 bootingDictionary.readOnlyUpgrade = true; 432 } 433 434 bootingDictionary.clearCaches(); 435 } 436 437 452 protected void dropJDBCMetadataSPSes(TransactionController tc, boolean removeSYSIBMonly) 453 throws StandardException 454 { 455 for (java.util.Iterator it = bootingDictionary.getAllSPSDescriptors().iterator(); it.hasNext(); ) 456 { 457 SPSDescriptor spsd = (SPSDescriptor) it.next(); 458 SchemaDescriptor sd = spsd.getSchemaDescriptor(); 459 boolean isSYSIBM = sd.getSchemaName().equals(SchemaDescriptor.IBM_SYSTEM_SCHEMA_NAME); 461 462 if (!sd.isSystemSchema() && !isSYSIBM) { 464 continue; 465 } 466 467 if (removeSYSIBMonly && !isSYSIBM) { 470 continue; 471 } 472 473 bootingDictionary.dropSPSDescriptor(spsd, tc); 474 bootingDictionary.dropDependentsStoredDependencies(spsd.getUUID(), 475 tc); 476 } 477 } 478 479 484 protected void makeSystemCatalog(TransactionController tc, 485 TabInfoImpl ti) 486 throws StandardException 487 { 488 SchemaDescriptor sd = bootingDictionary.getSystemSchemaDescriptor(); 489 bootingDictionary.makeCatalog(ti,sd,tc); 490 } 491 492 500 protected void 501 dropSystemCatalogDescription(TransactionController tc, TableDescriptor td) 502 throws StandardException 503 { 504 505 bootingDictionary.dropAllColumnDescriptors(td.getUUID(), tc); 506 507 508 bootingDictionary.dropAllConglomerateDescriptors(td, tc); 509 510 511 bootingDictionary.dropTableDescriptor( td, td.getSchemaDescriptor(), tc ); 512 bootingDictionary.clearCaches(); 513 } 514 515 521 protected void dropSystemCatalog(TransactionController tc, 522 CatalogRowFactory crf) 523 throws StandardException 524 { 525 SchemaDescriptor sd = bootingDictionary.getSystemSchemaDescriptor(); 526 TableDescriptor td = bootingDictionary.getTableDescriptor( 527 crf.getCatalogName(), 528 sd); 529 ConglomerateDescriptor[] cds = td.getConglomerateDescriptors(); 530 for (int index = 0; index < cds.length; index++) 531 { 532 tc.dropConglomerate(cds[index].getConglomerateNumber()); 533 } 534 dropSystemCatalogDescription(tc,td); 535 } 536 537 538 549 protected void fillIndex 550 ( 551 TransactionController tc, 552 long heapConglomerateNumber, 553 TabInfoImpl tabInfo, 554 int indexNumber 555 ) 556 throws StandardException 557 { 558 long indexConglomerateNumber = tabInfo.getIndexConglomerate( indexNumber ); 559 IndexRowGenerator indexRowGenerator = tabInfo.getIndexRowGenerator( indexNumber ); 560 CatalogRowFactory rowFactory = tabInfo.getCatalogRowFactory(); 561 ExecRow heapRow = rowFactory.makeEmptyRow(); 562 ExecIndexRow indexableRow = indexRowGenerator.getIndexRowTemplate(); 563 564 ScanController heapScan = 565 tc.openScan( 566 heapConglomerateNumber, false, 0, TransactionController.MODE_TABLE, 570 TransactionController.ISOLATION_REPEATABLE_READ, 571 (FormatableBitSet) null, null, ScanController.GE, null, null, ScanController.GT); 578 RowLocation heapLocation = 579 heapScan.newRowLocationTemplate(); 580 581 ConglomerateController indexController = 582 tc.openConglomerate( 583 indexConglomerateNumber, 584 false, 585 TransactionController.OPENMODE_FORUPDATE, 586 TransactionController.MODE_TABLE, 587 TransactionController.ISOLATION_REPEATABLE_READ); 588 589 while ( heapScan.fetchNext(heapRow.getRowArray()) ) 590 { 591 heapScan.fetchLocation( heapLocation ); 592 593 indexRowGenerator.getIndexRow( heapRow, heapLocation, indexableRow, (FormatableBitSet) null ); 594 595 indexController.insert(indexableRow.getRowArray()); 596 } 597 598 indexController.close(); 599 heapScan.close(); 600 } 601 602 618 public int getTypeFormatId() { 619 return majorVersionNumber == DataDictionary.DD_VERSION_CS_5_1 ? 620 StoredFormatIds.DD_ARWEN_VERSION_ID : StoredFormatIds.DD_DB2J72_VERSION_ID; 621 } 622 630 public final void readExternal( ObjectInput in ) throws IOException 631 { 632 majorVersionNumber = in.readInt(); 633 minorVersionNumber = in.readInt(); 634 } 635 636 647 public final void writeExternal( ObjectOutput out ) throws IOException 648 { 649 out.writeInt(majorVersionNumber); 650 out.writeInt(minorVersionNumber); 651 } 652 681 private int getJBMSMinorVersionNumber() 682 { 683 ProductVersionHolder jbmsVersion = Monitor.getMonitor().getEngineVersion(); 684 685 return jbmsVersion.getMinorVersion()*100 +jbmsVersion.getMaintVersion() + (jbmsVersion.isBeta() ? 0 : 1) + 2; 686 } 687 688 704 705 725 734 boolean checkVersion(int requiredMajorVersion, String feature) throws StandardException { 735 736 if (majorVersionNumber < requiredMajorVersion) { 737 738 if (feature != null) 739 throw StandardException.newException(SQLState.LANG_STATEMENT_UPGRADE_REQUIRED, feature, 740 DD_Version.majorToString(majorVersionNumber), 741 DD_Version.majorToString(requiredMajorVersion)); 742 743 return false; 744 } 745 746 return true; 747 } 748 749 } 750 | Popular Tags |