示例#1
0
        internal override bool IsConstraintViolated()
        {
            Index childIndex = childKey.GetSortIndex();

            object[] uniqueChildKeys = childIndex.GetUniqueKeyValues();
            bool     errors          = false;

            Index parentIndex = parentKey.GetSortIndex();

            for (int i = 0; i < uniqueChildKeys.Length; i++)
            {
                object[] childValues = (object[])uniqueChildKeys[i];

                if (!IsKeyNull(childValues))
                {
                    if (!parentIndex.IsKeyInIndex(childValues))
                    {
                        DataRow[] rows  = childIndex.GetRows(childIndex.FindRecords(childValues));
                        string    error = Res.GetString(Res.DataConstraint_ForeignKeyViolation, ConstraintName, ExceptionBuilder.KeysToString(childValues));
                        for (int j = 0; j < rows.Length; j++)
                        {
                            rows[j].RowError = error;
                        }
                        errors = true;
                    }
                }
            }
            return(errors);
        }
示例#2
0
        internal override bool CanEnableConstraint()
        {
            if (Table.DataSet == null || !Table.DataSet.EnforceConstraints)
            {
                return(true);
            }

            Index childIndex = childKey.GetSortIndex();

            object[] uniqueChildKeys = childIndex.GetUniqueKeyValues();

            Index parentIndex = parentKey.GetSortIndex();

            for (int i = 0; i < uniqueChildKeys.Length; i++)
            {
                object[] childValues = (object[])uniqueChildKeys[i];

                if (!IsKeyNull(childValues) && !parentIndex.IsKeyInIndex(childValues))
                {
#if DEBUG
                    if (CompModSwitches.Data_Constraints.TraceVerbose)
                    {
                        string result = "Constraint violation found.  Child values: ";
                        for (int j = 0; j < childValues.Length; j++)
                        {
                            result += Convert.ToString(childValues[j]) + (j < childValues.Length - 1 ? ", " : "");
                        }
                        Debug.WriteLine(result);
                    }
#endif
                    return(false);
                }
            }
            return(true);
        }
示例#3
0
        internal override void CheckConstraint(DataRow row, DataRowAction action)
        {
            if (Table.EnforceConstraints &&
                (action == DataRowAction.Add ||
                 action == DataRowAction.Change ||
                 (action == DataRowAction.Rollback && row.tempRecord != -1)))
            {
                if (row.HaveValuesChanged(Columns))
                {
                    Index    index  = Key.GetSortIndex();
                    object[] values = row.GetColumnValues(Columns);
                    if (index.IsKeyInIndex(values))
                    {
#if DEBUG
                        if (CompModSwitches.Data_Constraints.TraceVerbose)
                        {
                            Debug.WriteLine("UniqueConstraint violation...");
                            string valuesText = "";
                            for (int i = 0; i < values.Length; i++)
                            {
                                valuesText = Convert.ToString(values[i]) + (i < values.Length - 1 ? ", " : "");
                            }
                            Debug.WriteLine("   constraint: " + this.GetDebugString());
                            Debug.WriteLine("   key values: " + valuesText);
                            Range range  = index.FindRecords(values);
                            int   record = index.GetRecord(range.Min);
                            Debug.WriteLine("   conflicting record: " + record.ToString());
                        }
#endif
                        throw ExceptionBuilder.ConstraintViolation(Columns, values);
                    }
                }
            }
        }
示例#4
0
        internal override void CheckConstraint(DataRow childRow, DataRowAction action)
        {
            if ((action == DataRowAction.Change ||
                 action == DataRowAction.Add ||
                 action == DataRowAction.Rollback) &&
                Table.DataSet != null && Table.DataSet.EnforceConstraints &&
                childRow.HasKeyChanged(_childKey))
            {
                // This branch is for cascading case verification.
                DataRowVersion version        = (action == DataRowAction.Rollback) ? DataRowVersion.Original : DataRowVersion.Current;
                object[]       childKeyValues = childRow.GetKeyValues(_childKey);
                // check to see if this is just a change to my parent's proposed value.
                if (childRow.HasVersion(version))
                {
                    // this is the new proposed value for the parent.
                    DataRow parentRow = DataRelation.GetParentRow(ParentKey, ChildKey, childRow, version);
                    if (parentRow != null && parentRow._inCascade)
                    {
                        object[] parentKeyValues = parentRow.GetKeyValues(_parentKey, action == DataRowAction.Rollback ? version : DataRowVersion.Default);

                        int parentKeyValuesRecord = childRow.Table.NewRecord();
                        childRow.Table.SetKeyValues(_childKey, parentKeyValues, parentKeyValuesRecord);
                        if (_childKey.RecordsEqual(childRow._tempRecord, parentKeyValuesRecord))
                        {
                            return;
                        }
                    }
                }

                // now check to see if someone exists... it will have to be in a parent row's current, not a proposed.
                object[] childValues = childRow.GetKeyValues(_childKey);
                if (!IsKeyNull(childValues))
                {
                    Index parentIndex = _parentKey.GetSortIndex();
                    if (!parentIndex.IsKeyInIndex(childValues))
                    {
                        // could be self-join constraint
                        if (_childKey.Table == _parentKey.Table && childRow._tempRecord != -1)
                        {
                            int lo = 0;
                            for (lo = 0; lo < childValues.Length; lo++)
                            {
                                DataColumn column = _parentKey.ColumnsReference[lo];
                                object     value  = column.ConvertValue(childValues[lo]);
                                if (0 != column.CompareValueTo(childRow._tempRecord, value))
                                {
                                    break;
                                }
                            }
                            if (lo == childValues.Length)
                            {
                                return;
                            }
                        }
                        throw ExceptionBuilder.ForeignKeyViolation(ConstraintName, childKeyValues);
                    }
                }
            }
        }
示例#5
0
 internal override bool CanEnableConstraint()
 {
     if ((this.Table.DataSet != null) && this.Table.DataSet.EnforceConstraints)
     {
         object[] uniqueKeyValues = this.childKey.GetSortIndex().GetUniqueKeyValues();
         Index    sortIndex       = this.parentKey.GetSortIndex();
         for (int i = 0; i < uniqueKeyValues.Length; i++)
         {
             object[] values = (object[])uniqueKeyValues[i];
             if (!this.IsKeyNull(values) && !sortIndex.IsKeyInIndex(values))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
示例#6
0
        internal override bool CanEnableConstraint()
        {
            if (Table.DataSet == null || !Table.DataSet.EnforceConstraints)
            {
                return(true);
            }

            Index childIndex = _childKey.GetSortIndex();

            object[] uniqueChildKeys = childIndex.GetUniqueKeyValues();

            Index parentIndex = _parentKey.GetSortIndex();

            for (int i = 0; i < uniqueChildKeys.Length; i++)
            {
                object[] childValues = (object[])uniqueChildKeys[i];

                if (!IsKeyNull(childValues) && !parentIndex.IsKeyInIndex(childValues))
                {
                    return(false);
                }
            }
            return(true);
        }
示例#7
0
        internal override bool IsConstraintViolated()
        {
            Index sortIndex = this.childKey.GetSortIndex();

            object[] uniqueKeyValues = sortIndex.GetUniqueKeyValues();
            bool     flag            = false;
            Index    index2          = this.parentKey.GetSortIndex();

            for (int i = 0; i < uniqueKeyValues.Length; i++)
            {
                object[] values = (object[])uniqueKeyValues[i];
                if (!this.IsKeyNull(values) && !index2.IsKeyInIndex(values))
                {
                    DataRow[] rows = sortIndex.GetRows(sortIndex.FindRecords(values));
                    string    str  = Res.GetString("DataConstraint_ForeignKeyViolation", new object[] { this.ConstraintName, ExceptionBuilder.KeysToString(values) });
                    for (int j = 0; j < rows.Length; j++)
                    {
                        rows[j].RowError = str;
                    }
                    flag = true;
                }
            }
            return(flag);
        }
示例#8
0
        internal override void CheckConstraint(DataRow childRow, DataRowAction action)
        {
            Debug.Assert(Table.DataSet != null, "Relation " + ConstraintName + " isn't part of a DataSet, so this check shouldn't be happening.");
            if ((action == DataRowAction.Change ||
                 action == DataRowAction.Add ||
                 action == DataRowAction.Rollback) &&
                Table.DataSet != null && Table.DataSet.EnforceConstraints &&
                childRow.HasKeyChanged(childKey))
            {
                // This branch is for cascading case verification.
                DataRowVersion version        = (action == DataRowAction.Rollback) ? DataRowVersion.Original : DataRowVersion.Current;
                object[]       childKeyValues = childRow.GetKeyValues(childKey);
                // check to see if this is just a change to my parent's proposed value.
                if (childRow.HasVersion(version))
                {
                    // this is the new proposed value for the parent.
                    DataRow parentRow = DataRelation.GetParentRow(this.ParentKey, this.ChildKey, childRow, version);
                    if (parentRow != null && parentRow.inCascade)
                    {
                        object[] parentKeyValues = parentRow.GetKeyValues(parentKey, action == DataRowAction.Rollback ? version : DataRowVersion.Default);

#if DEBUG
                        if (CompModSwitches.Data_Constraints.TraceVerbose)
                        {
                            Debug.WriteLine("Parent and Child values on constraint check.");
                            for (int i = 0; i < childKeyValues.Length; i++)
                            {
                                Debug.WriteLine("... " + i.ToString() + ": " + Convert.ToString(parentKeyValues[i]) +
                                                ", " + Convert.ToString(childKeyValues[i]));
                            }
                        }
#endif

                        int parentKeyValuesRecord = childRow.Table.NewRecord();
                        childRow.Table.SetKeyValues(childKey, parentKeyValues, parentKeyValuesRecord);
                        if (childKey.RecordsEqual(childRow.tempRecord, parentKeyValuesRecord))
                        {
                            return;
                        }
                    }
                }

                // now check to see if someone exists... it will have to be in a parent row's current, not a proposed.
                object[] childValues = childRow.GetKeyValues(childKey);
                if (!IsKeyNull(childValues))
                {
                    Index parentIndex = parentKey.GetSortIndex();
                    if (!parentIndex.IsKeyInIndex(childValues))
                    {
                        // could be self-join constraint
                        if (childKey.Table == parentKey.Table && childRow.tempRecord != -1)
                        {
                            int lo = 0;
                            for (lo = 0; lo < childValues.Length; lo++)
                            {
                                if (parentKey.Columns[lo].CompareToValue(childRow.tempRecord, childValues[lo]) != 0)
                                {
                                    break;
                                }
                            }
                            if (lo == childValues.Length)
                            {
                                return;
                            }
                        }

                        throw ExceptionBuilder.ForeignKeyViolation(ConstraintName, childKeyValues);
                    }
                }
            }
        }