1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.reference.SQLState; 25 26 import org.apache.derby.iapi.sql.compile.CompilerContext; 27 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 28 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 29 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor; 30 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 31 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 32 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor; 33 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList; 34 import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor; 35 import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList; 36 import org.apache.derby.iapi.sql.dictionary.CheckConstraintDescriptor; 37 import org.apache.derby.iapi.sql.execute.ConstantAction; 38 39 import org.apache.derby.iapi.error.StandardException; 40 41 import org.apache.derby.iapi.services.sanity.SanityManager; 42 43 import org.apache.derby.iapi.sql.StatementType; 44 45 51 52 public class RenameNode extends DDLStatementNode 53 { 54 protected TableName newTableName; 55 56 protected String oldObjectName; 58 protected String newObjectName; 60 61 protected TableDescriptor td; 62 private long conglomerateNumber; 63 64 68 protected boolean usedAlterTable; 69 70 74 protected int renamingWhat; 75 76 95 public void init(Object tableName, 96 Object oldObjectName, 97 Object newObjectName, 98 Object usedAlterTable, 99 Object renamingWhat) 100 throws StandardException 101 { 102 this.usedAlterTable = ((Boolean ) usedAlterTable).booleanValue(); 103 this.renamingWhat = ((Integer ) renamingWhat).intValue(); 104 105 switch (this.renamingWhat) 106 { 107 case StatementType.RENAME_TABLE: 108 initAndCheck((TableName) tableName); 109 this.newTableName = 110 makeTableName(getObjectName().getSchemaName(),(String )newObjectName); 111 this.oldObjectName = null; 112 this.newObjectName = this.newTableName.getTableName(); 113 break; 114 115 case StatementType.RENAME_COLUMN: 116 120 TableName actingObjectName; 121 if (tableName instanceof TableName) 122 actingObjectName = (TableName) tableName; 123 else 124 actingObjectName = makeTableName(null, 125 (String )tableName); 126 initAndCheck(actingObjectName); 127 128 129 this.oldObjectName = (String )oldObjectName; 130 this.newObjectName = (String )newObjectName; 131 break; 132 133 case StatementType.RENAME_INDEX: 134 this.oldObjectName = (String )oldObjectName; 135 this.newObjectName = (String )newObjectName; 136 break; 137 138 default: 139 if (SanityManager.DEBUG) 140 SanityManager.THROWASSERT( 141 "Unexpected rename action in RenameNode"); 142 } 143 } 144 145 151 152 public String toString() 153 { 154 if (SanityManager.DEBUG) { 155 156 switch (renamingWhat) 157 { 158 case StatementType.RENAME_TABLE: 159 return super.toString() + 160 "oldTableName: " + "\n" + getRelativeName() + "\n" + 161 "newTableName: " + "\n" + newTableName + "\n" ; 162 163 case StatementType.RENAME_COLUMN: 164 return super.toString() + 165 "oldTableName.oldColumnName:" + "\n" + 166 getRelativeName() + "." + oldObjectName + "\n" + 167 "newColumnName: " + "\n" + newObjectName + "\n" ; 168 169 case StatementType.RENAME_INDEX: 170 return super.toString() + 171 "oldIndexName:" + "\n" + oldObjectName + "\n" + 172 "newIndexName: " + "\n" + newObjectName + "\n" ; 173 174 default: 175 SanityManager.THROWASSERT( 176 "Unexpected rename action in RenameNode"); 177 return "UNKNOWN"; 178 } 179 } else { 180 return ""; 181 } 182 } 183 184 public String statementToString() 185 { 186 if (usedAlterTable) 187 return "ALTER TABLE"; 188 else { 189 switch (renamingWhat) 190 { 191 case StatementType.RENAME_TABLE: 192 return "RENAME TABLE"; 193 194 case StatementType.RENAME_COLUMN: 195 return "RENAME COLUMN"; 196 197 case StatementType.RENAME_INDEX: 198 return "RENAME INDEX"; 199 200 default: 201 if (SanityManager.DEBUG) 202 SanityManager.THROWASSERT( 203 "Unexpected rename action in RenameNode"); 204 return "UNKNOWN"; 205 } 206 } 207 } 208 209 211 231 232 public QueryTreeNode bind() throws StandardException 233 { 234 CompilerContext cc = getCompilerContext(); 235 DataDictionary dd = getDataDictionary(); 236 ConglomerateDescriptor cd; 237 SchemaDescriptor sd; 238 239 250 TableName baseTable; 251 252 if (renamingWhat == StatementType.RENAME_INDEX) { 253 254 sd = getSchemaDescriptor((String )null); 255 256 ConglomerateDescriptor indexDescriptor = 257 dd.getConglomerateDescriptor(oldObjectName, sd, false); 258 if (indexDescriptor == null) 259 throw StandardException.newException( 260 SQLState.LANG_INDEX_NOT_FOUND, oldObjectName); 261 262 td = dd.getTableDescriptor(indexDescriptor.getTableID()); 263 initAndCheck(makeTableName(td.getSchemaName(), 264 td.getName())); 265 } else 266 sd = getSchemaDescriptor(); 267 268 td = getTableDescriptor(); 269 270 if (td.getTableType() == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) 272 { 273 throw StandardException.newException(SQLState.LANG_NOT_ALLOWED_FOR_DECLARED_GLOBAL_TEMP_TABLE); 274 } 275 276 switch (this.renamingWhat) 277 { 278 case StatementType.RENAME_TABLE: 279 280 TableDescriptor td = getTableDescriptor(newObjectName, sd); 281 if (td != null) 282 throw descriptorExistsException(td, sd); 283 renameTableBind(dd); 284 break; 285 286 case StatementType.RENAME_COLUMN: 287 renameColumnBind(dd); 288 break; 289 290 case StatementType.RENAME_INDEX: 291 ConglomerateDescriptor conglomDesc = dd.getConglomerateDescriptor(newObjectName, sd, false); 292 if (conglomDesc != null) 293 throw descriptorExistsException(conglomDesc, sd); 294 break; 295 296 default: 297 if (SanityManager.DEBUG) 298 SanityManager.THROWASSERT( 299 "Unexpected rename action in RenameNode"); 300 break; 301 } 302 303 conglomerateNumber = td.getHeapConglomerateId(); 304 305 306 cd = td.getConglomerateDescriptor(conglomerateNumber); 307 308 309 cc.createDependency(td); 310 cc.createDependency(cd); 311 312 return this; 313 } 314 315 322 public boolean referencesSessionSchema() 323 throws StandardException 324 { 325 if (isSessionSchema(td.getSchemaName())) return true; 328 329 if (renamingWhat == StatementType.RENAME_TABLE && isSessionSchema(getSchemaDescriptor())) 331 return true; 332 333 return false; 334 } 335 336 private void renameTableBind(DataDictionary dd) 338 throws StandardException 339 { 340 341 ConstraintDescriptorList constraintDescriptorList = dd.getConstraintDescriptors(td); 342 int size = 343 constraintDescriptorList == null ? 0 : constraintDescriptorList.size(); 344 345 ConstraintDescriptor constraintDescriptor; 346 347 for (int index = 0; index < size; index++) 349 { 350 constraintDescriptor = constraintDescriptorList.elementAt(index); 351 if (constraintDescriptor.getConstraintType() == DataDictionary.CHECK_CONSTRAINT) 353 { 354 throw StandardException.newException( 355 SQLState.LANG_PROVIDER_HAS_DEPENDENT_OBJECT, 356 "RENAME", 357 td.getName(), 358 "CONSTRAINT", 359 constraintDescriptor.getConstraintName()); 360 } 361 } 362 } 363 364 private void renameColumnBind(DataDictionary dd) 366 throws StandardException 367 { 368 ColumnDescriptor columnDescriptor = td.getColumnDescriptor(oldObjectName); 369 370 371 if (columnDescriptor == null) 372 throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, 373 oldObjectName, getFullName()); 374 375 376 ColumnDescriptor cd = td.getColumnDescriptor(newObjectName); 377 if (cd != null) 378 throw descriptorExistsException(cd, td); 379 380 381 ConstraintDescriptorList constraintDescriptorList = 382 dd.getConstraintDescriptors(td); 383 int size = 384 constraintDescriptorList == null ? 0 : constraintDescriptorList.size(); 385 386 ConstraintDescriptor constraintDescriptor; 387 ColumnDescriptorList checkConstraintCDL; 388 int checkConstraintCDLSize; 389 390 for (int index = 0; index < size; index++) 392 { 393 constraintDescriptor = constraintDescriptorList.elementAt(index); 394 if (constraintDescriptor.getConstraintType() == DataDictionary.CHECK_CONSTRAINT) 397 { 398 checkConstraintCDL = constraintDescriptor.getColumnDescriptors(); 399 checkConstraintCDLSize = checkConstraintCDL.size(); 400 401 for (int index2 = 0; index2 < checkConstraintCDLSize; index2++) 402 if (checkConstraintCDL.elementAt( index2 ) == columnDescriptor) 403 throw StandardException.newException( 404 SQLState.LANG_RENAME_COLUMN_WILL_BREAK_CHECK_CONSTRAINT, 405 oldObjectName, 406 constraintDescriptor.getConstraintName()); 407 } 408 } 409 } 410 411 416 public ConstantAction makeConstantAction() 417 throws StandardException 418 { 419 return getGenericConstantActionFactory().getRenameConstantAction(getFullName(), 420 getRelativeName(), 421 oldObjectName, 422 newObjectName, 423 getSchemaDescriptor(), 424 td.getUUID(), 425 usedAlterTable, 426 renamingWhat); 427 } 428 429 private StandardException descriptorExistsException(TupleDescriptor tuple, 430 TupleDescriptor parent) 431 { 432 return 433 StandardException.newException(SQLState.LANG_OBJECT_ALREADY_EXISTS_IN_OBJECT, 434 tuple.getDescriptorType(), 435 tuple.getDescriptorName(), 436 parent.getDescriptorType(), 437 parent.getDescriptorName()); 438 } 439 } 440 | Popular Tags |