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.catalog.UUID; 27 import org.apache.derby.iapi.services.uuid.UUIDFactory; 28 29 import org.apache.derby.iapi.error.StandardException; 30 31 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 32 33 import org.apache.derby.iapi.sql.dictionary.CheckConstraintDescriptor; 34 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 35 import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor; 36 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator; 37 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 38 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext; 39 import org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor; 40 import org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor; 41 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 42 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 43 44 import org.apache.derby.iapi.types.DataValueFactory; 45 46 import org.apache.derby.iapi.reference.SQLState; 47 48 import org.apache.derby.iapi.sql.execute.ConstantAction; 49 import org.apache.derby.iapi.sql.execute.ExecRow; 50 51 import org.apache.derby.iapi.sql.ResultSet; 52 import org.apache.derby.iapi.sql.Statement; 53 import org.apache.derby.iapi.sql.PreparedStatement; 54 55 import org.apache.derby.iapi.types.NumberDataValue; 56 57 import org.apache.derby.iapi.store.access.ConglomerateController; 58 import org.apache.derby.iapi.store.access.GroupFetchScanController; 59 import org.apache.derby.iapi.store.access.ScanController; 60 import org.apache.derby.iapi.store.access.TransactionController; 61 62 import org.apache.derby.iapi.types.DataValueDescriptor; 63 64 import org.apache.derby.catalog.UUID; 65 66 import org.apache.derby.iapi.services.io.FormatableBitSet; 67 74 75 public abstract class ConstraintConstantAction extends DDLSingleTableConstantAction 76 { 77 78 protected String constraintName; 79 protected int constraintType; 80 protected String tableName; 81 protected String schemaName; 82 protected UUID schemaId; 83 protected IndexConstantAction indexAction; 84 85 98 ConstraintConstantAction( 99 String constraintName, 100 int constraintType, 101 String tableName, 102 UUID tableId, 103 String schemaName, 104 IndexConstantAction indexAction) 105 { 106 super(tableId); 107 this.constraintName = constraintName; 108 this.constraintType = constraintType; 109 this.tableName = tableName; 110 this.indexAction = indexAction; 111 this.schemaName = schemaName; 112 113 if (SanityManager.DEBUG) 114 { 115 SanityManager.ASSERT(schemaName != null, "Constraint schema name is null"); 116 } 117 } 118 119 121 126 public int getConstraintType() 127 { 128 return constraintType; 129 } 130 131 136 public String getConstraintName() { return constraintName; } 137 138 143 public IndexConstantAction getIndexAction() { return indexAction; } 144 145 161 static void validateFKConstraint 162 ( 163 TransactionController tc, 164 DataDictionary dd, 165 ForeignKeyConstraintDescriptor fk, 166 ReferencedKeyConstraintDescriptor refcd, 167 ExecRow indexTemplateRow 168 ) 169 throws StandardException 170 { 171 172 GroupFetchScanController refScan = null; 173 174 GroupFetchScanController fkScan = 175 tc.openGroupFetchScan( 176 fk.getIndexConglomerateDescriptor(dd).getConglomerateNumber(), 177 false, 0, tc.MODE_TABLE, tc.ISOLATION_READ_COMMITTED, (FormatableBitSet)null, (DataValueDescriptor[])null, ScanController.GE, null, (DataValueDescriptor[])null, ScanController.GT ); 188 189 try 190 { 191 197 if (!fkScan.next()) 198 { 199 fkScan.close(); 200 return; 201 } 202 203 fkScan.reopenScan( 204 (DataValueDescriptor[])null, ScanController.GE, null, (DataValueDescriptor[])null, ScanController.GT ); 210 211 224 refScan = 225 tc.openGroupFetchScan( 226 refcd.getIndexConglomerateDescriptor(dd).getConglomerateNumber(), 227 false, 0, tc.MODE_RECORD, 230 tc.ISOLATION_READ_COMMITTED, (FormatableBitSet)null, (DataValueDescriptor[])null, ScanController.GE, null, (DataValueDescriptor[])null, ScanController.GT ); 238 239 RIBulkChecker riChecker = new RIBulkChecker(refScan, 240 fkScan, 241 indexTemplateRow, 242 true, (ConglomerateController)null, 244 (ExecRow)null); 245 246 int numFailures = riChecker.doCheck(); 247 if (numFailures > 0) 248 { 249 StandardException se = StandardException.newException(SQLState.LANG_ADD_FK_CONSTRAINT_VIOLATION, 250 fk.getConstraintName(), 251 fk.getTableDescriptor().getName()); 252 throw se; 253 } 254 } 255 finally 256 { 257 if (fkScan != null) 258 { 259 fkScan.close(); 260 fkScan = null; 261 } 262 if (refScan != null) 263 { 264 refScan.close(); 265 refScan = null; 266 } 267 } 268 } 269 270 287 static boolean validateConstraint 288 ( 289 String constraintName, 290 String constraintText, 291 TableDescriptor td, 292 LanguageConnectionContext lcc, 293 boolean isCheckConstraint 294 ) 295 throws StandardException 296 { 297 StringBuffer checkStmt = new StringBuffer (); 298 302 checkStmt.append("SELECT COUNT(*) FROM "); 303 checkStmt.append(td.getQualifiedName()); 304 checkStmt.append(" WHERE NOT("); 305 checkStmt.append(constraintText); 306 checkStmt.append(")"); 307 308 ResultSet rs = null; 309 try 310 { 311 PreparedStatement ps = lcc.prepareInternalStatement(checkStmt.toString()); 312 313 rs = ps.execute(lcc, false, 0L); 317 ExecRow row = rs.getNextRow(); 318 if (SanityManager.DEBUG) 319 { 320 if (row == null) 321 { 322 SanityManager.THROWASSERT("did not get any rows back from query: "+checkStmt.toString()); 323 } 324 } 325 326 DataValueDescriptor[] rowArray = row.getRowArray(); 327 Number value = ((Number )((NumberDataValue)row.getRowArray()[0]).getObject()); 328 332 if ((value != null) && (value.longValue() != 0)) 333 { 334 if (isCheckConstraint) 336 throw StandardException.newException(SQLState.LANG_ADD_CHECK_CONSTRAINT_FAILED, 337 constraintName, td.getQualifiedName(), value.toString()); 338 343 return false; 344 } 345 } 346 finally 347 { 348 if (rs != null) 349 { 350 rs.close(); 351 } 352 } 353 return true; 354 } 355 } 356 | Popular Tags |