コード例 #1
0
        private void HandleFKConstraintConflicts(List <FKConstraintConflict> conflicts, ITransaction transaction)
        {
            HashSet <FKConstraintConflict> handled = new HashSet <FKConstraintConflict>();
            IEnumerable <IGrouping <Type, FKConstraintConflict> > constraintsByType = conflicts.GroupBy(conf => conf.ConflictType);

            foreach (IGrouping <Type, FKConstraintConflict> constraintGroup in constraintsByType)
            {
                SchemaObject foreignSchemaObject = Schema.Schema.GetSchemaObject(constraintGroup.Key);
                foreach (IGrouping <string, FKConstraintConflict> constraintGroupByField in constraintGroup.GroupBy(fk => fk.ForeignKeyName))
                {
                    List <long> autoDeleteReferenceKeys = new List <long>();
                    List <long> autoRemoveReferenceKeys = new List <long>();

                    foreach (FKConstraintConflict fKConstraintConflict in constraintGroup)
                    {
                        switch (fKConstraintConflict.ActionType)
                        {
                        case FKConstraintConflict.ActionTypes.AutoDeleteReference:
                            autoDeleteReferenceKeys.Add(fKConstraintConflict.ForeignKey.Value);
                            handled.Add(fKConstraintConflict);
                            break;

                        case FKConstraintConflict.ActionTypes.AutoRemoveReference:
                            autoRemoveReferenceKeys.Add(fKConstraintConflict.ForeignKey.Value);
                            handled.Add(fKConstraintConflict);
                            break;
                        }
                    }

                    if (autoDeleteReferenceKeys.Any())
                    {
                        IDeleteQuery deleteQuery = SQLProviderFactory.GetDeleteQuery();
                        deleteQuery.Table     = new Table(foreignSchemaObject.SchemaName, foreignSchemaObject.ObjectName);
                        deleteQuery.Condition = new Condition()
                        {
                            Left          = (Base.Data.Operand.Field)foreignSchemaObject.PrimaryKeyField.FieldName,
                            ConditionType = Condition.ConditionTypes.List,
                            Right         = (CSV <long>)autoDeleteReferenceKeys
                        };

                        deleteQuery.Execute(transaction);
                    }

                    if (autoRemoveReferenceKeys.Any())
                    {
                        IUpdateQuery updateQuery = SQLProviderFactory.GetUpdateQuery();
                        updateQuery.Table = new Table(foreignSchemaObject.SchemaName, foreignSchemaObject.ObjectName);
                        updateQuery.FieldValueList.Add(new FieldValue()
                        {
                            FieldName = constraintGroupByField.Key,
                            Value     = null
                        });
                        updateQuery.Condition = new Condition()
                        {
                            Left          = (Base.Data.Operand.Field)foreignSchemaObject.PrimaryKeyField.FieldName,
                            ConditionType = Condition.ConditionTypes.List,
                            Right         = (CSV <long>)autoRemoveReferenceKeys
                        };

                        updateQuery.Execute(transaction);
                    }
                }
            }

            conflicts.RemoveAll(fk => handled.Contains(fk));
        }
コード例 #2
0
        public bool Delete(ITransaction transaction = null, List <Guid> saveFlags = null)
        {
            ITransaction localTransaction = transaction == null?SQLProviderFactory.GenerateTransaction() : transaction;

            try
            {
                if (!isEditable)
                {
                    throw new System.Data.ReadOnlyException("Attempt to call Delete on a Read Only Data Object");
                }

                PreValidate();
                Validate(Validator.SaveModes.Delete, saveFlags);

                if (Errors.Any())
                {
                    return(false);
                }

                fKConstraintConflicts = GetFKConstraintConflicts(localTransaction);
                if (fKConstraintConflicts.Any())
                {
                    HandleFKConstraintConflicts(fKConstraintConflicts, localTransaction);
                    if (fKConstraintConflicts.Any())
                    {
                        return(false);
                    }
                }

                if (!PreDelete(localTransaction))
                {
                    return(false);
                }

                SchemaObject schemaObject = Schema.Schema.GetSchemaObject(GetType());

                IDeleteQuery deleteQuery = SQLProviderFactory.GetDeleteQuery();
                deleteQuery.Table     = new Table(schemaObject.SchemaName, schemaObject.ObjectName);
                deleteQuery.Condition = new Condition()
                {
                    Left          = (Base.Data.Operand.Field)schemaObject.PrimaryKeyField.FieldName,
                    ConditionType = Condition.ConditionTypes.Equal,
                    Right         = new Literal(schemaObject.PrimaryKeyField.GetValue(this))
                };

                deleteQuery.Execute(localTransaction);

                if (!PostDelete(localTransaction))
                {
                    return(false);
                }

                if (transaction == null)
                {
                    localTransaction.Commit();
                }
            }
            finally
            {
                if (transaction == null && localTransaction.IsActive)
                {
                    localTransaction.Rollback();
                }
            }

            return(true);
        }