--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.execute.ForeignKeyRIChecker\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.sql.execute;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import org.apache.derby.iapi.sql.StatementUtil;\r
+import org.apache.derby.iapi.sql.execute.ExecRow;\r
+import org.apache.derby.iapi.sql.execute.ExecIndexRow;\r
+import org.apache.derby.iapi.reference.SQLState;\r
+import org.apache.derby.iapi.store.access.ScanController;\r
+import org.apache.derby.iapi.store.access.TransactionController;\r
+\r
+/**\r
+ * A Referential Integrity checker for a foreign\r
+ * key constraint. It makes sure the foreign key is\r
+ * intact. This is used for a change to a foreign\r
+ * key column. see ReferencedKeyRIChecker for the code\r
+ * that validates changes to referenced keys.\r
+ */\r
+public class ForeignKeyRIChecker extends GenericRIChecker\r
+{\r
+ /**\r
+ * @param tc the xact controller\r
+ * @param fkinfo the foreign key information \r
+ *\r
+ * @exception StandardException Thrown on failure\r
+ */\r
+ ForeignKeyRIChecker(TransactionController tc, FKInfo fkinfo)\r
+ throws StandardException\r
+ {\r
+ super(tc, fkinfo);\r
+\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ if (fkInfo.type != FKInfo.FOREIGN_KEY)\r
+ {\r
+ SanityManager.THROWASSERT("invalid type "+fkInfo.type+" for a ForeignKeyRIChecker");\r
+ }\r
+ } \r
+ }\r
+\r
+ /**\r
+ * Check that the row either has a null column(s), or\r
+ * corresponds to a row in the referenced key.\r
+ * <p> \r
+ * If the referenced key is found, then it is locked\r
+ * when this method returns. The lock is held until\r
+ * the next call to doCheck() or close().\r
+ *\r
+ * @param row the row to check\r
+ *\r
+ * @exception StandardException on unexped error, or\r
+ * on a foreign key violation\r
+ */\r
+ void doCheck(ExecRow row, boolean restrictCheckOnly) throws StandardException\r
+ {\r
+\r
+ if(restrictCheckOnly) //RESTRICT rule checks are not valid here.\r
+ return; \r
+\r
+ /*\r
+ ** If any of the columns are null, then the\r
+ ** check always succeeds.\r
+ */\r
+ if (isAnyFieldNull(row))\r
+ {\r
+ return;\r
+ }\r
+\r
+ /*\r
+ ** Otherwise, we had better find this row in the\r
+ ** referenced key\r
+ */\r
+ ScanController scan = getScanController(fkInfo.refConglomNumber, refScoci, refDcoci, row);\r
+ if (!scan.next())\r
+ {\r
+ close();\r
+ StandardException se = StandardException.newException(SQLState.LANG_FK_VIOLATION, fkInfo.fkConstraintNames[0],\r
+ fkInfo.tableName,\r
+ StatementUtil.typeName(fkInfo.stmtType),\r
+ RowUtil.toString(row, fkInfo.colArray));\r
+\r
+ throw se;\r
+ }\r
+ \r
+ /*\r
+ ** If we found the row, we are currently positioned on\r
+ ** the row when we leave this method. So we hold the\r
+ ** lock on the referenced key, which is very important.\r
+ */ \r
+ }\r
+\r
+ /**\r
+ * Get the isolation level for the scan for\r
+ * the RI check.\r
+ *\r
+ * NOTE: The level will eventually be instantaneous\r
+ * locking once the implemenation changes.\r
+ *\r
+ * @return The isolation level for the scan for\r
+ * the RI check.\r
+ */\r
+ int getRICheckIsolationLevel()\r
+ {\r
+ return TransactionController.ISOLATION_READ_COMMITTED;\r
+ }\r
+}\r