1 21 22 package org.apache.derby.iapi.sql.dictionary; 23 24 import org.apache.derby.iapi.error.StandardException; 25 26 import org.apache.derby.iapi.reference.SQLState; 27 import org.apache.derby.iapi.services.sanity.SanityManager; 28 import org.apache.derby.iapi.sql.StatementType; 29 import org.apache.derby.iapi.services.io.StoredFormatIds; 30 import org.apache.derby.catalog.UUID; 31 37 public class ReferencedKeyConstraintDescriptor extends KeyConstraintDescriptor 38 { 39 51 52 private final int constraintType; 54 55 int referenceCount; 56 57 private ConstraintDescriptorList fkEnabledConstraintList; 59 private ConstraintDescriptorList fkConstraintList; 61 62 private boolean checkedSelfReferencing; 63 private boolean hasSelfReferencing; 64 65 81 protected ReferencedKeyConstraintDescriptor(int constraintType, 82 DataDictionary dataDictionary, 83 TableDescriptor table, 84 String constraintName, 85 boolean deferrable, 86 boolean initiallyDeferred, 87 int[] columns, 88 UUID constraintId, 89 UUID indexId, 90 SchemaDescriptor schemaDesc, 91 boolean isEnabled, 92 int referenceCount 93 ) 94 { 95 super(dataDictionary, table, constraintName, deferrable, 96 initiallyDeferred, columns, 97 constraintId, indexId, schemaDesc, isEnabled); 98 this.referenceCount = referenceCount; 99 this.constraintType = constraintType; 100 } 101 102 public final int getConstraintType() { 103 return constraintType; 104 } 105 106 116 public boolean hasSelfReferencingFK(ConstraintDescriptorList cdl, int type) 117 throws StandardException 118 { 119 if (SanityManager.DEBUG) 120 { 121 checkType(type); 122 } 123 124 if (checkedSelfReferencing) 125 { 126 return hasSelfReferencing; 127 } 128 129 ConstraintDescriptor cd; 130 ForeignKeyConstraintDescriptor fkcd; 131 134 if (cdl == null) 135 { 136 cdl = getForeignKeyConstraints(type); 137 } 138 int cdlSize = cdl.size(); 139 140 for (int index = 0; index < cdlSize; index++) 141 { 142 cd = (ConstraintDescriptor) cdl.elementAt(index); 143 if (! (cd instanceof ForeignKeyConstraintDescriptor)) 144 { 145 continue; 146 } 147 148 fkcd = (ForeignKeyConstraintDescriptor) cd; 149 if (fkcd.getReferencedConstraintId().equals(getUUID())) 150 { 151 hasSelfReferencing = true; 152 break; 153 } 154 } 155 return hasSelfReferencing; 156 } 157 158 159 165 public boolean hasNonSelfReferencingFK(int type) 166 throws StandardException 167 { 168 169 boolean hasNonSelfReferenceFk = false; 170 171 if (SanityManager.DEBUG) 172 { 173 checkType(type); 174 } 175 176 ConstraintDescriptor cd; 177 ForeignKeyConstraintDescriptor fkcd; 178 ConstraintDescriptorList cdl = getForeignKeyConstraints(type); 180 int cdlSize = cdl.size(); 181 182 for (int index = 0; index < cdlSize; index++) 183 { 184 cd = (ConstraintDescriptor) cdl.elementAt(index); 185 if (! (cd instanceof ForeignKeyConstraintDescriptor)) 186 { 187 continue; 188 } 189 190 fkcd = (ForeignKeyConstraintDescriptor) cd; 191 if(!(fkcd.getTableId().equals(getTableId()))) 192 { 193 hasNonSelfReferenceFk = true; 194 break; 195 } 196 } 197 return hasNonSelfReferenceFk; 198 } 199 200 201 202 211 public ConstraintDescriptorList getForeignKeyConstraints(int type) 212 throws StandardException 213 { 214 if (SanityManager.DEBUG) 215 { 216 checkType(type); 217 } 218 219 if (type == ENABLED) 221 { 222 if (!isReferenced()) 225 { 226 return new ConstraintDescriptorList(); 227 } 228 else if (fkEnabledConstraintList != null) 229 { 230 return fkEnabledConstraintList; 231 } 232 else if (fkConstraintList == null) 233 { 234 fkConstraintList = getDataDictionary().getForeignKeys(constraintId); 235 } 236 fkEnabledConstraintList = fkConstraintList.getConstraintDescriptorList(true); 237 return fkEnabledConstraintList; 238 } 239 240 else if (type == DISABLED) 242 { 243 if (fkConstraintList == null) 244 { 245 fkConstraintList = getDataDictionary().getForeignKeys(constraintId); 246 } 247 return fkConstraintList.getConstraintDescriptorList(false); 248 } 249 else 250 { 251 if (fkConstraintList == null) 252 { 253 fkConstraintList = getDataDictionary().getForeignKeys(constraintId); 254 } 255 return fkConstraintList; 256 } 257 } 258 259 266 public boolean isReferenced() 267 { 268 return referenceCount != 0; 269 } 270 271 277 public int getReferenceCount() 278 { 279 return referenceCount; 280 } 281 282 287 public int incrementReferenceCount() 288 { 289 return referenceCount++; 290 } 291 292 297 public int decrementReferenceCount() 298 { 299 return referenceCount--; 300 } 301 302 314 public boolean needsToFire(int stmtType, int[] modifiedCols) 315 { 316 319 if (!isEnabled) 320 { 321 return false; 322 } 323 324 if (!isReferenced() || 325 (stmtType == StatementType.INSERT)) 326 { 327 return false; 328 } 329 330 if (stmtType == StatementType.DELETE || 331 stmtType == StatementType.BULK_INSERT_REPLACE) 332 { 333 return true; 334 } 335 336 return doColumnsIntersect(modifiedCols, getReferencedColumns()); 338 } 339 340 private void checkType(int type) throws StandardException 341 { 342 if (SanityManager.DEBUG) 343 { 344 switch (type) 345 { 346 case ENABLED: 347 case DISABLED: 348 case ALL: 349 break; 350 default: 351 SanityManager.THROWASSERT("constraint type "+type+" is invalid"); 352 } 353 } 354 } 355 356 } 357 | Popular Tags |