internal void CascadeDelete(DataRow row)
{
if (-1 == row._newRecord)
{
return;
}
object[] currentKey = row.GetKeyValues(_parentKey, DataRowVersion.Current);
if (IsKeyNull(currentKey))
{
return;
}
Index childIndex = _childKey.GetSortIndex();
switch (DeleteRule)
{
case Rule.None:
{
if (row.Table.DataSet.EnforceConstraints)
{
// if we're not cascading deletes, we should throw if we're going to strand a child row under enforceConstraints.
Range range = childIndex.FindRecords(currentKey);
if (!range.IsNull)
{
if (range.Count == 1 && childIndex.GetRow(range.Min) == row)
return;
throw ExceptionBuilder.FailedCascadeDelete(ConstraintName);
}
}
break;
}
case Rule.Cascade:
{
object[] key = row.GetKeyValues(_parentKey, DataRowVersion.Default);
Range range = childIndex.FindRecords(key);
if (!range.IsNull)
{
DataRow[] rows = childIndex.GetRows(range);
for (int j = 0; j < rows.Length; j++)
{
DataRow r = rows[j];
if (r._inCascade)
continue;
r.Table.DeleteRow(r);
}
}
break;
}
case Rule.SetNull:
{
object[] proposedKey = new object[_childKey.ColumnsReference.Length];
for (int i = 0; i < _childKey.ColumnsReference.Length; i++)
proposedKey[i] = DBNull.Value;
Range range = childIndex.FindRecords(currentKey);
if (!range.IsNull)
{
DataRow[] rows = childIndex.GetRows(range);
for (int j = 0; j < rows.Length; j++)
{
// if (rows[j].inCascade)
// continue;
if (row != rows[j])
rows[j].SetKeyValues(_childKey, proposedKey);
}
}
break;
}
case Rule.SetDefault:
{
object[] proposedKey = new object[_childKey.ColumnsReference.Length];
for (int i = 0; i < _childKey.ColumnsReference.Length; i++)
proposedKey[i] = _childKey.ColumnsReference[i].DefaultValue;
Range range = childIndex.FindRecords(currentKey);
if (!range.IsNull)
{
DataRow[] rows = childIndex.GetRows(range);
for (int j = 0; j < rows.Length; j++)
{
// if (rows[j].inCascade)
// continue;
if (row != rows[j])
rows[j].SetKeyValues(_childKey, proposedKey);
}
}
break;
}
default:
{
Debug.Assert(false, "Unknown Rule value");
break;
}
}
}