1 21 22 package org.apache.derby.iapi.sql.dictionary; 23 24 import org.apache.derby.iapi.error.StandardException; 25 import org.apache.derby.catalog.UUID; 26 27 import org.apache.derby.iapi.reference.SQLState; 28 import org.apache.derby.iapi.services.sanity.SanityManager; 29 import org.apache.derby.iapi.sql.StatementType; 30 import org.apache.derby.iapi.services.io.StoredFormatIds; 31 import org.apache.derby.iapi.error.StandardException; 32 import org.apache.derby.iapi.sql.depend.DependencyManager; 33 import org.apache.derby.iapi.sql.depend.Dependent; 34 import org.apache.derby.iapi.sql.depend.Dependency; 35 import org.apache.derby.iapi.sql.depend.Provider; 36 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 37 import org.apache.derby.iapi.services.sanity.SanityManager; 38 39 44 public class ForeignKeyConstraintDescriptor extends KeyConstraintDescriptor 45 { 46 57 58 ReferencedKeyConstraintDescriptor referencedConstraintDescriptor; 60 UUID referencedConstraintId; 61 int raDeleteRule; 62 int raUpdateRule; 63 78 protected ForeignKeyConstraintDescriptor( 79 DataDictionary dataDictionary, 80 TableDescriptor table, 81 String constraintName, 82 boolean deferrable, 83 boolean initiallyDeferred, 84 int[] fkColumns, 85 UUID constraintId, 86 UUID indexId, 87 SchemaDescriptor schemaDesc, 88 ReferencedKeyConstraintDescriptor referencedConstraintDescriptor, 89 boolean isEnabled, 90 int raDeleteRule, 91 int raUpdateRule 92 ) 93 { 94 super(dataDictionary, table, constraintName, deferrable, 95 initiallyDeferred, fkColumns, 96 constraintId, indexId, schemaDesc, isEnabled); 97 98 this.referencedConstraintDescriptor = referencedConstraintDescriptor; 99 this.raDeleteRule = raDeleteRule; 100 this.raUpdateRule = raUpdateRule; 101 } 102 103 118 ForeignKeyConstraintDescriptor( 119 DataDictionary dataDictionary, 120 TableDescriptor table, 121 String constraintName, 122 boolean deferrable, 123 boolean initiallyDeferred, 124 int[] fkColumns, 125 UUID constraintId, 126 UUID indexId, 127 SchemaDescriptor schemaDesc, 128 UUID referencedConstraintId, 129 boolean isEnabled, 130 int raDeleteRule, 131 int raUpdateRule 132 ) 133 { 134 super(dataDictionary, table, constraintName, deferrable, 135 initiallyDeferred, fkColumns, 136 constraintId, indexId, schemaDesc, isEnabled); 137 this.referencedConstraintId = referencedConstraintId; 138 this.raDeleteRule = raDeleteRule; 139 this.raUpdateRule = raUpdateRule; 140 141 } 142 143 151 public ReferencedKeyConstraintDescriptor getReferencedConstraint() 152 throws StandardException 153 { 154 if (referencedConstraintDescriptor != null) 155 { 156 return referencedConstraintDescriptor; 157 } 158 159 if (referencedConstraintId == null) 160 { 161 getReferencedConstraintId(); 162 } 163 164 TableDescriptor refTd = getDataDictionary().getConstraintTableDescriptor(referencedConstraintId); 165 166 if (SanityManager.DEBUG) 167 { 168 if (refTd == null) 169 { 170 SanityManager.THROWASSERT("not able to find "+referencedConstraintId+ 171 " in SYS.SYSCONSTRAINTS"); 172 } 173 } 174 175 ConstraintDescriptorList cdl = getDataDictionary().getConstraintDescriptors(refTd); 176 referencedConstraintDescriptor = (ReferencedKeyConstraintDescriptor) 177 cdl.getConstraintDescriptorById(referencedConstraintId); 178 179 if (SanityManager.DEBUG) 180 { 181 if (referencedConstraintDescriptor == null) 182 { 183 SanityManager.THROWASSERT("not able to find " 184 +referencedConstraintDescriptor+ " off of table descriptor " 185 +refTd.getName()); 186 } 187 } 188 189 return referencedConstraintDescriptor; 190 } 191 192 193 201 public UUID getReferencedConstraintId() throws StandardException 202 { 203 if (referencedConstraintDescriptor != null) 204 { 205 return referencedConstraintDescriptor.getUUID(); 206 } 207 208 SubKeyConstraintDescriptor subKey; 209 subKey = getDataDictionary().getSubKeyConstraint(constraintId, 210 DataDictionary.FOREIGNKEY_CONSTRAINT); 211 if (SanityManager.DEBUG) 212 { 213 if (subKey == null) 214 { 215 SanityManager.THROWASSERT("not able to find "+constraintName+ 216 " in SYS.SYSFOREIGNKEYS"); 217 } 218 } 219 referencedConstraintId = subKey.getKeyConstraintId(); 220 return referencedConstraintId; 221 } 222 223 230 public int getConstraintType() 231 { 232 return DataDictionary.FOREIGNKEY_CONSTRAINT; 233 } 234 235 245 public boolean needsToFire(int stmtType, int[] modifiedCols) 246 { 247 250 if (!isEnabled) 251 { 252 return false; 253 } 254 255 if (stmtType == StatementType.DELETE) 256 { 257 return false; 258 } 259 if (stmtType == StatementType.INSERT) 260 { 261 return true; 262 } 263 264 return doColumnsIntersect(modifiedCols, getReferencedColumns()); 266 } 267 268 276 public boolean isSelfReferencingFK() 277 throws StandardException 278 { 279 ReferencedKeyConstraintDescriptor refcd = getReferencedConstraint(); 280 return (refcd.getTableId().equals(getTableId())); 281 } 282 283 288 public int getRaDeleteRule() 289 { 290 return raDeleteRule; 291 } 292 293 294 299 public int getRaUpdateRule() 300 { 301 return raUpdateRule; 302 } 303 304 } 305 306 307 308 309 310 311 312 | Popular Tags |