1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import org.apache.derby.iapi.services.io.FormatableHashtable; 27 28 import org.apache.derby.catalog.UUID; 29 import org.apache.derby.iapi.services.uuid.UUIDFactory; 30 31 import org.apache.derby.iapi.error.StandardException; 32 33 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 34 35 import org.apache.derby.iapi.sql.StatementType; 36 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.ConstraintDescriptorList; 40 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator; 41 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 42 import org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor; 43 import org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor; 44 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 45 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 46 47 import org.apache.derby.iapi.types.DataValueFactory; 48 49 import org.apache.derby.iapi.sql.depend.DependencyManager; 50 51 import org.apache.derby.iapi.reference.SQLState; 52 53 import org.apache.derby.iapi.sql.execute.ConstantAction; 54 55 import org.apache.derby.iapi.sql.Activation; 56 57 import org.apache.derby.iapi.store.access.TransactionController; 58 59 import org.apache.derby.catalog.UUID; 60 61 68 69 public class DropConstraintConstantAction extends ConstraintConstantAction 70 { 71 72 private boolean cascade; private String constraintSchemaName; 74 private int verifyType; 75 76 78 89 DropConstraintConstantAction( 90 String constraintName, 91 String constraintSchemaName, 92 String tableName, 93 UUID tableId, 94 String tableSchemaName, 95 IndexConstantAction indexAction, 96 int behavior, 97 int verifyType) 98 { 99 super(constraintName, DataDictionary.DROP_CONSTRAINT, tableName, 100 tableId, tableSchemaName, indexAction); 101 102 cascade = (behavior == StatementType.DROP_CASCADE); 103 this.constraintSchemaName = constraintSchemaName; 104 this.verifyType = verifyType; 105 } 106 107 109 public String toString() 110 { 111 if (constraintName == null) 114 return "DROP PRIMARY KEY"; 115 116 String ss = constraintSchemaName == null ? schemaName : constraintSchemaName; 117 return "DROP CONSTRAINT " + ss + "." + constraintName; 118 } 119 120 122 123 130 public void executeConstantAction( Activation activation ) 131 throws StandardException 132 { 133 ConstraintDescriptor conDesc = null; 134 TableDescriptor td; 135 UUID indexId = null; 136 String indexUUIDString; 137 138 LanguageConnectionContext lcc = activation.getLanguageConnectionContext(); 139 DataDictionary dd = lcc.getDataDictionary(); 140 DependencyManager dm = dd.getDependencyManager(); 141 TransactionController tc = lcc.getTransactionExecute(); 142 143 144 153 dd.startWriting(lcc); 154 155 td = dd.getTableDescriptor(tableId); 156 157 if (td == null) 158 { 159 throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, tableName); 160 } 161 162 163 164 170 171 SchemaDescriptor tdSd = td.getSchemaDescriptor(); 172 SchemaDescriptor constraintSd = 173 constraintSchemaName == null ? tdSd : dd.getSchemaDescriptor(constraintSchemaName, tc, true); 174 175 176 181 if (constraintName == null) conDesc = dd.getConstraintDescriptors(td).getPrimaryKey(); 183 else 184 conDesc = dd.getConstraintDescriptorByName(td, constraintSd, constraintName, true); 185 186 if (conDesc == null) 188 { 189 String errorName = constraintName == null ? "PRIMARY KEY" : 190 (constraintSd.getSchemaName() + "."+ constraintName); 191 192 throw StandardException.newException(SQLState.LANG_DROP_NON_EXISTENT_CONSTRAINT, 193 errorName, 194 td.getQualifiedName()); 195 } 196 switch( verifyType) 197 { 198 case DataDictionary.UNIQUE_CONSTRAINT: 199 if( conDesc.getConstraintType() != verifyType) 200 throw StandardException.newException(SQLState.LANG_DROP_CONSTRAINT_TYPE, 201 constraintName, "UNIQUE"); 202 break; 203 204 case DataDictionary.CHECK_CONSTRAINT: 205 if( conDesc.getConstraintType() != verifyType) 206 throw StandardException.newException(SQLState.LANG_DROP_CONSTRAINT_TYPE, 207 constraintName, "CHECK"); 208 break; 209 210 case DataDictionary.FOREIGNKEY_CONSTRAINT: 211 if( conDesc.getConstraintType() != verifyType) 212 throw StandardException.newException(SQLState.LANG_DROP_CONSTRAINT_TYPE, 213 constraintName, "FOREIGN KEY"); 214 break; 215 } 216 217 boolean cascadeOnRefKey = (cascade && 218 conDesc instanceof ReferencedKeyConstraintDescriptor); 219 if (!cascadeOnRefKey) 220 { 221 dm.invalidateFor(conDesc, DependencyManager.DROP_CONSTRAINT, lcc); 222 } 223 224 231 dropConstraintAndIndex(dm, td, dd, conDesc, tc, lcc, !cascadeOnRefKey); 232 233 if (cascadeOnRefKey) 234 { 235 ForeignKeyConstraintDescriptor fkcd; 236 ReferencedKeyConstraintDescriptor cd; 237 ConstraintDescriptorList cdl; 238 239 cd = (ReferencedKeyConstraintDescriptor)conDesc; 240 cdl = cd.getForeignKeyConstraints(ReferencedKeyConstraintDescriptor.ALL); 241 int cdlSize = cdl.size(); 242 243 for(int index = 0; index < cdlSize; index++) 244 { 245 fkcd = (ForeignKeyConstraintDescriptor) cdl.elementAt(index); 246 dm.invalidateFor(fkcd, DependencyManager.DROP_CONSTRAINT, lcc); 247 dropConstraintAndIndex(dm, fkcd.getTableDescriptor(), dd, fkcd, 248 tc, lcc, true); 249 } 250 251 256 dm.invalidateFor(conDesc, DependencyManager.DROP_CONSTRAINT, lcc); 257 dm.clearDependencies(lcc, conDesc); 258 } 259 } 260 261 267 public static void dropConstraintAndIndex(DependencyManager dm, 268 TableDescriptor td, 269 DataDictionary dd, 270 ConstraintDescriptor conDesc, 271 TransactionController tc, 272 LanguageConnectionContext lcc, 273 boolean clearDependencies) 274 throws StandardException 275 { 276 if (SanityManager.DEBUG) 277 { 278 SanityManager.ASSERT(tc != null, "tc is null"); 279 SanityManager.ASSERT(td != null, "td is null"); 280 } 281 282 if (clearDependencies) 283 { 284 dm.clearDependencies(lcc, conDesc); 285 } 286 287 292 dd.dropConstraintDescriptor(td, conDesc, tc); 293 294 299 if (conDesc.hasBackingIndex()) 300 { 301 ConglomerateDescriptor[] conglomDescs; 302 303 conglomDescs = dd.getConglomerateDescriptors( 308 conDesc.getConglomerateId()); 309 310 if (conglomDescs.length != 0) 311 { 312 for (int i = 0; i < conglomDescs.length; i++) 313 { 314 if (conglomDescs[i].isConstraint()) 315 { 316 DropIndexConstantAction.dropIndex(dm, dd, tc, 317 conglomDescs[i], td, 318 lcc); 319 break; 320 } 321 } 322 } 323 } 324 325 td.removeConstraintDescriptor(conDesc); 326 } 327 } 328 | Popular Tags |