예제 #1
0
        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.Columns.Length];
                for (int i = 0; i < childKey.Columns.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.Columns.Length];
                for (int i = 0; i < childKey.Columns.Length; i++)
                {
                    proposedKey[i] = childKey.Columns[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;
            }
            }
        }
예제 #2
0
        internal void CascadeDelete(DataRow row)
        {
            if (-1 != row.newRecord)
            {
                object[] keyValues = row.GetKeyValues(this.parentKey, DataRowVersion.Current);
                if (!this.IsKeyNull(keyValues))
                {
                    Index sortIndex = this.childKey.GetSortIndex();
                    switch (this.DeleteRule)
                    {
                    case Rule.None:
                        if (row.Table.DataSet.EnforceConstraints)
                        {
                            Range range4 = sortIndex.FindRecords(keyValues);
                            if (range4.IsNull)
                            {
                                return;
                            }
                            if ((range4.Count != 1) || (sortIndex.GetRow(range4.Min) != row))
                            {
                                throw ExceptionBuilder.FailedCascadeDelete(this.ConstraintName);
                            }
                        }
                        return;

                    case Rule.Cascade:
                    {
                        object[] key    = row.GetKeyValues(this.parentKey, DataRowVersion.Default);
                        Range    range3 = sortIndex.FindRecords(key);
                        if (!range3.IsNull)
                        {
                            foreach (DataRow row2 in sortIndex.GetRows(range3))
                            {
                                if (!row2.inCascade)
                                {
                                    row2.Table.DeleteRow(row2);
                                }
                            }
                        }
                        return;
                    }

                    case Rule.SetNull:
                    {
                        object[] objArray3 = new object[this.childKey.ColumnsReference.Length];
                        for (int i = 0; i < this.childKey.ColumnsReference.Length; i++)
                        {
                            objArray3[i] = DBNull.Value;
                        }
                        Range range2 = sortIndex.FindRecords(keyValues);
                        if (!range2.IsNull)
                        {
                            DataRow[] rows = sortIndex.GetRows(range2);
                            for (int j = 0; j < rows.Length; j++)
                            {
                                if (row != rows[j])
                                {
                                    rows[j].SetKeyValues(this.childKey, objArray3);
                                }
                            }
                        }
                        return;
                    }

                    case Rule.SetDefault:
                    {
                        object[] objArray2 = new object[this.childKey.ColumnsReference.Length];
                        for (int k = 0; k < this.childKey.ColumnsReference.Length; k++)
                        {
                            objArray2[k] = this.childKey.ColumnsReference[k].DefaultValue;
                        }
                        Range range = sortIndex.FindRecords(keyValues);
                        if (!range.IsNull)
                        {
                            DataRow[] rowArray = sortIndex.GetRows(range);
                            for (int m = 0; m < rowArray.Length; m++)
                            {
                                if (row != rowArray[m])
                                {
                                    rowArray[m].SetKeyValues(this.childKey, objArray2);
                                }
                            }
                        }
                        return;
                    }
                    }
                }
            }
        }