1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 25 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 26 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor; 27 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList; 28 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 29 import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor; 30 import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList; 31 import org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor; 32 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 33 import org.apache.derby.iapi.store.access.TransactionController; 34 35 import org.apache.derby.iapi.sql.depend.DependencyManager; 36 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 37 38 import org.apache.derby.iapi.reference.SQLState; 39 40 import org.apache.derby.iapi.sql.Activation; 41 import org.apache.derby.iapi.sql.StatementType; 42 43 import org.apache.derby.iapi.sql.execute.ConstantAction; 44 45 import org.apache.derby.iapi.error.StandardException; 46 47 import org.apache.derby.iapi.services.sanity.SanityManager; 48 49 import org.apache.derby.catalog.UUID; 50 51 import org.apache.derby.iapi.services.io.FormatableBitSet; 52 53 54 60 61 class RenameConstantAction extends DDLSingleTableConstantAction 62 { 63 64 65 private String fullTableName; 66 private String tableName; 67 private String newTableName; 68 private String oldObjectName; 69 private String newObjectName; 70 private UUID schemaId; 71 private SchemaDescriptor sd; 72 76 private boolean usedAlterTable; 77 81 private int renamingWhat; 82 83 85 101 public RenameConstantAction 102 ( 103 String fullTableName, 104 String tableName, 105 String oldObjectName, 106 String newObjectName, 107 SchemaDescriptor sd, 108 UUID tableId, 109 boolean usedAlterTable, 110 int renamingWhat) 111 { 112 super(tableId); 113 this.fullTableName = fullTableName; 114 this.tableName = tableName; 115 this.sd = sd; 116 this.usedAlterTable = usedAlterTable; 117 this.renamingWhat = renamingWhat; 118 119 switch (this.renamingWhat) 120 { 121 case StatementType.RENAME_TABLE: 122 this.newTableName = newObjectName; 123 this.oldObjectName = null; 124 this.newObjectName=newObjectName; 125 break; 126 127 case StatementType.RENAME_COLUMN: 128 case StatementType.RENAME_INDEX: 129 this.oldObjectName = oldObjectName; 130 this.newObjectName = newObjectName; 131 break; 132 133 default: 134 if (SanityManager.DEBUG) { 135 SanityManager.THROWASSERT( 136 "Unexpected rename action in RenameConstantAction"); 137 } 138 } 139 if (SanityManager.DEBUG) 140 { 141 SanityManager.ASSERT(sd != null, "SchemaDescriptor is null"); 142 } 143 } 144 145 147 public String toString() 148 { 149 String renameString; 150 if (usedAlterTable) 151 renameString = "ALTER TABLE "; 152 else 153 renameString = "RENAME "; 154 155 switch (this.renamingWhat) 156 { 157 case StatementType.RENAME_TABLE: 158 if(usedAlterTable) 159 renameString = renameString + fullTableName + " RENAME TO " + newTableName; 160 else 161 renameString = renameString + " TABLE " + fullTableName + " TO " + newTableName; 162 break; 163 164 case StatementType.RENAME_COLUMN: 165 if(usedAlterTable) 166 renameString = renameString + fullTableName + " RENAME " + oldObjectName + " TO " + newObjectName; 167 else 168 renameString = renameString + " COLUMN " + fullTableName + "." + oldObjectName + " TO " + newObjectName; 169 break; 170 171 case StatementType.RENAME_INDEX: 172 renameString = renameString + " INDEX " + oldObjectName + " TO " + newObjectName; 173 break; 174 175 default: 176 if (SanityManager.DEBUG) { 177 SanityManager.THROWASSERT( 178 "Unexpected rename action in RenameConstantAction"); 179 } 180 break; 181 } 182 183 return renameString; 184 } 185 186 188 189 196 public void executeConstantAction 197 ( 198 Activation activation) 199 throws StandardException 200 { 201 TableDescriptor td; 202 UUID tableID; 203 204 LanguageConnectionContext lcc = activation.getLanguageConnectionContext(); 205 DataDictionary dd = lcc.getDataDictionary(); 206 DependencyManager dm = dd.getDependencyManager(); 207 TransactionController tc = lcc.getTransactionExecute(); 208 209 210 219 dd.startWriting(lcc); 220 221 td = dd.getTableDescriptor(tableId); 222 223 if (td == null) 224 { 225 throw StandardException.newException( 226 SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, fullTableName); 227 } 228 229 235 if (sd == null) 236 { 237 sd = getAndCheckSchemaDescriptor(dd, schemaId, "RENAME TABLE"); 238 } 239 240 long heapId = td.getHeapConglomerateId(); 241 242 244 lockTableForDDL(tc, heapId, true); 245 246 248 td = dd.getTableDescriptor(tableId); 249 if (td == null) 250 { 251 throw StandardException.newException( 252 SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, fullTableName); 253 } 254 255 switch (renamingWhat) 256 { 257 case StatementType.RENAME_TABLE: 258 execGutsRenameTable(td, activation); 259 break; 260 261 case StatementType.RENAME_COLUMN: 262 execGutsRenameColumn(td, activation); 263 break; 264 265 case StatementType.RENAME_INDEX: 266 execGutsRenameIndex(td, activation); 267 break; 268 269 default: 270 if (SanityManager.DEBUG) { 271 SanityManager.THROWASSERT( 272 "Unexpected rename action in RenameConstantAction"); 273 } 274 break; 275 } 276 } 277 278 private void execGutsRenameTable 280 ( 281 TableDescriptor td, Activation activation) 282 throws StandardException 283 { 284 ConstraintDescriptorList constraintDescriptorList; 285 ConstraintDescriptor constraintDescriptor; 286 287 LanguageConnectionContext lcc = activation.getLanguageConnectionContext(); 288 DataDictionary dd = lcc.getDataDictionary(); 289 DependencyManager dm = dd.getDependencyManager(); 290 TransactionController tc = lcc.getTransactionExecute(); 291 dm.invalidateFor(td, DependencyManager.RENAME, lcc); 292 293 296 constraintDescriptorList = dd.getConstraintDescriptors(td); 297 for(int index=0; index<constraintDescriptorList.size(); index++) 298 { 299 constraintDescriptor = constraintDescriptorList.elementAt(index); 300 if (constraintDescriptor instanceof ReferencedKeyConstraintDescriptor) 301 dm.invalidateFor(constraintDescriptor, DependencyManager.RENAME, lcc); 302 } 303 304 dd.dropTableDescriptor(td, sd, tc); 306 td.setTableName(newTableName); 308 dd.addDescriptor(td, sd, DataDictionary.SYSTABLES_CATALOG_NUM, 310 false, tc); 311 } 312 313 private void execGutsRenameColumn 315 ( 316 TableDescriptor td, Activation activation) 317 throws StandardException 318 { 319 ColumnDescriptor columnDescriptor = null; 320 int columnPosition = 0; 321 ConstraintDescriptorList constraintDescriptorList; 322 ConstraintDescriptor constraintDescriptor; 323 LanguageConnectionContext lcc = activation.getLanguageConnectionContext(); 324 DataDictionary dd = lcc.getDataDictionary(); 325 DependencyManager dm = dd.getDependencyManager(); 326 TransactionController tc = lcc.getTransactionExecute(); 327 328 334 columnDescriptor = td.getColumnDescriptor(oldObjectName); 335 columnPosition = columnDescriptor.getPosition(); 336 FormatableBitSet toRename = new FormatableBitSet(td.getColumnDescriptorList().size() + 1); 337 toRename.set(columnPosition); 338 td.setReferencedColumnMap(toRename); 339 340 dm.invalidateFor(td, DependencyManager.RENAME, lcc); 341 342 constraintDescriptorList = dd.getConstraintDescriptors(td); 344 for(int index=0; index<constraintDescriptorList.size(); index++) 345 { 346 constraintDescriptor = constraintDescriptorList.elementAt(index); 347 int[] referencedColumns = constraintDescriptor.getReferencedColumns(); 348 int numRefCols = referencedColumns.length; 349 for (int j = 0; j < numRefCols; j++) 350 { 351 if ((referencedColumns[j] == columnPosition) && 352 (constraintDescriptor instanceof ReferencedKeyConstraintDescriptor)) 353 dm.invalidateFor(constraintDescriptor, DependencyManager.RENAME, lcc); 354 } 355 } 356 357 dd.dropColumnDescriptor(td.getUUID(), oldObjectName, tc); 359 columnDescriptor.setColumnName(newObjectName); 360 dd.addDescriptor(columnDescriptor, td, 361 DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc); 362 363 td = dd.getTableDescriptor(td.getObjectID()); 366 } 367 368 private void execGutsRenameIndex 370 ( 371 TableDescriptor td, Activation activation) 372 throws StandardException 373 { 374 LanguageConnectionContext lcc = activation.getLanguageConnectionContext(); 375 DataDictionary dd = lcc.getDataDictionary(); 376 DependencyManager dm = dd.getDependencyManager(); 377 TransactionController tc = lcc.getTransactionExecute(); 378 dm.invalidateFor(td, DependencyManager.RENAME_INDEX, lcc); 380 381 ConglomerateDescriptor conglomerateDescriptor = 382 dd.getConglomerateDescriptor(oldObjectName, sd, true); 383 384 if (conglomerateDescriptor == null) 385 throw StandardException.newException(SQLState.LANG_INDEX_NOT_FOUND_DURING_EXECUTION, 386 oldObjectName); 387 388 389 dd.dropConglomerateDescriptor(conglomerateDescriptor, tc); 390 conglomerateDescriptor.setConglomerateName(newObjectName); 392 dd.addDescriptor(conglomerateDescriptor, sd, 394 DataDictionary.SYSCONGLOMERATES_CATALOG_NUM, false, tc); 395 } 396 397 399 public String getTableName() { return tableName; } 400 401 } 402 | Popular Tags |