1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.sql.execute.ConstantAction; 25 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 import org.apache.derby.iapi.error.StandardException; 28 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 29 import org.apache.derby.iapi.sql.StatementType; 30 31 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor; 32 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList; 33 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 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.DataDictionary; 37 import org.apache.derby.iapi.sql.dictionary.DefaultDescriptor; 38 import org.apache.derby.iapi.sql.dictionary.GenericDescriptorList; 39 import org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor; 40 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 41 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 42 import org.apache.derby.iapi.sql.dictionary.TriggerDescriptor; 43 44 import org.apache.derby.iapi.sql.depend.DependencyManager; 45 46 import org.apache.derby.iapi.reference.SQLState; 47 48 import org.apache.derby.iapi.sql.Activation; 49 50 import org.apache.derby.iapi.store.access.TransactionController; 51 52 import org.apache.derby.catalog.UUID; 53 54 import java.util.Enumeration ; 55 56 62 63 class DropTableConstantAction extends DDLSingleTableConstantAction 64 { 65 66 private final long conglomerateNumber; 67 private final String fullTableName; 68 private final String tableName; 69 private final SchemaDescriptor sd; 70 private final boolean cascade; 71 72 74 75 87 DropTableConstantAction( 88 String fullTableName, 89 String tableName, 90 SchemaDescriptor sd, 91 long conglomerateNumber, 92 UUID tableId, 93 int behavior) 94 { 95 super(tableId); 96 this.fullTableName = fullTableName; 97 this.tableName = tableName; 98 this.sd = sd; 99 this.conglomerateNumber = conglomerateNumber; 100 this.cascade = (behavior == StatementType.DROP_CASCADE); 101 102 if (SanityManager.DEBUG) 103 { 104 SanityManager.ASSERT(sd != null, "SchemaDescriptor is null"); 105 } 106 } 107 108 110 public String toString() 111 { 112 return "DROP TABLE " + fullTableName; 115 } 116 117 119 120 127 public void executeConstantAction( Activation activation ) 128 throws StandardException 129 { 130 TableDescriptor td; 131 UUID tableID; 132 ConglomerateDescriptor[] cds; 133 134 LanguageConnectionContext lcc = activation.getLanguageConnectionContext(); 135 DataDictionary dd = lcc.getDataDictionary(); 136 DependencyManager dm = dd.getDependencyManager(); 137 TransactionController tc = lcc.getTransactionExecute(); 138 139 if ((sd != null) && sd.getSchemaName().equals(SchemaDescriptor.STD_DECLARED_GLOBAL_TEMPORARY_TABLES_SCHEMA_NAME)) { 140 td = lcc.getTableDescriptorForDeclaredGlobalTempTable(tableName); 142 if (td == null) td = dd.getTableDescriptor(tableName, sd); 144 145 if (td == null) { 147 throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, fullTableName); 148 } 149 150 if (td.getTableType() == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) { 151 dm.invalidateFor(td, DependencyManager.DROP_TABLE, lcc); 152 tc.dropConglomerate(td.getHeapConglomerateId()); 153 lcc.dropDeclaredGlobalTempTable(tableName); 154 return; 155 } 156 } 157 158 165 if ( conglomerateNumber != 0 ) { lockTableForDDL(tc, conglomerateNumber, true); } 166 167 176 dd.startWriting(lcc); 177 178 179 td = dd.getTableDescriptor(tableId); 180 181 if (td == null) 182 { 183 throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, fullTableName); 184 } 185 186 187 long heapId = td.getHeapConglomerateId(); 188 lockTableForDDL(tc, heapId, true); 189 190 191 GenericDescriptorList tdl = dd.getTriggerDescriptors(td); 192 Enumeration descs = tdl.elements(); 193 while (descs.hasMoreElements()) 194 { 195 TriggerDescriptor trd = (TriggerDescriptor) descs.nextElement(); 196 DropTriggerConstantAction.dropTriggerDescriptor(lcc, dm, dd, tc, trd, activation); 197 } 198 199 200 ColumnDescriptorList cdl = td.getColumnDescriptorList(); 201 int cdlSize = cdl.size(); 202 203 for (int index = 0; index < cdlSize; index++) 204 { 205 ColumnDescriptor cd = (ColumnDescriptor) cdl.elementAt(index); 206 207 if (cd.getDefaultInfo() != null) 210 { 211 DefaultDescriptor defaultDesc = cd.getDefaultDescriptor(dd); 212 dm.clearDependencies(lcc, defaultDesc); 213 } 214 } 215 216 217 dd.dropAllColumnDescriptors(tableId, tc); 218 219 220 dd.dropAllTableAndColPermDescriptors(tableId, tc); 221 222 223 dropAllConstraintDescriptors(td, activation); 224 225 229 cds = td.getConglomerateDescriptors(); 230 231 long[] dropped = new long[cds.length - 1]; 232 int numDropped = 0; 233 for (int index = 0; index < cds.length; index++) 234 { 235 ConglomerateDescriptor cd = cds[index]; 236 237 240 if (cd.getConglomerateNumber() != heapId) 241 { 242 long thisConglom = cd.getConglomerateNumber(); 243 244 int i; 245 for (i = 0; i < numDropped; i++) 246 { 247 if (dropped[i] == thisConglom) 248 break; 249 } 250 if (i == numDropped) { 252 dropped[numDropped++] = thisConglom; 253 tc.dropConglomerate(thisConglom); 254 dd.dropStatisticsDescriptors(td.getUUID(), cd.getUUID(), tc); 255 } 256 } 257 } 258 259 267 268 dm.invalidateFor(td, DependencyManager.DROP_TABLE, lcc); 269 270 271 dd.dropTableDescriptor(td, sd, tc); 272 273 274 dd.dropAllConglomerateDescriptors(td, tc); 275 276 279 tc.dropConglomerate(heapId); 280 281 } 282 283 private void dropAllConstraintDescriptors(TableDescriptor td, Activation activation) 284 throws StandardException 285 { 286 ConstraintDescriptor cd; 287 ConstraintDescriptorList cdl; 288 ConstraintDescriptor fkcd; 289 ConstraintDescriptorList fkcdl; 290 LanguageConnectionContext lcc = activation.getLanguageConnectionContext(); 291 DataDictionary dd = lcc.getDataDictionary(); 292 DependencyManager dm = dd.getDependencyManager(); 293 TransactionController tc = lcc.getTransactionExecute(); 294 295 cdl = dd.getConstraintDescriptors(td); 296 297 302 306 for(int index = 0; index < cdl.size(); ) 307 { 308 cd = cdl.elementAt(index); 309 if (cd instanceof ReferencedKeyConstraintDescriptor) 310 { 311 index++; 312 continue; 313 } 314 315 dm.invalidateFor(cd, DependencyManager.DROP_CONSTRAINT, lcc); 316 DropConstraintConstantAction.dropConstraintAndIndex(dm, td, dd, cd, 317 tc, lcc, true); 318 } 319 320 323 326 while (cdl.size() > 0) 327 { 328 cd = cdl.elementAt(0); 329 if (SanityManager.DEBUG) 330 { 331 if (!(cd instanceof ReferencedKeyConstraintDescriptor)) 332 { 333 SanityManager.THROWASSERT("Constraint descriptor not an instance of " + 334 "ReferencedKeyConstraintDescriptor as expected. Is a "+ cd.getClass().getName()); 335 } 336 } 337 338 348 DropConstraintConstantAction.dropConstraintAndIndex(dm, td, dd, cd, 349 tc, lcc, false); 350 351 355 if (cascade) 356 { 357 361 362 fkcdl = dd.getForeignKeys(cd.getUUID()); 363 364 368 for(int inner = 0; inner < fkcdl.size(); inner++) 369 { 370 fkcd = (ConstraintDescriptor) fkcdl.elementAt(inner); 371 dm.invalidateFor(fkcd, DependencyManager.DROP_CONSTRAINT, lcc); 372 DropConstraintConstantAction.dropConstraintAndIndex( 373 dm, fkcd.getTableDescriptor(), dd, fkcd, 374 tc, lcc, true); 375 activation.addWarning( 376 StandardException.newWarning(SQLState.LANG_CONSTRAINT_DROPPED, 377 fkcd.getConstraintName(), 378 fkcd.getTableDescriptor().getName())); 379 } 380 } 381 382 386 dm.invalidateFor(cd, DependencyManager.DROP_CONSTRAINT, lcc); 387 dm.clearDependencies(lcc, cd); 388 } 389 } 390 391 } 392 | Popular Tags |