public override DbExpression Visit(DbScanExpression expression)
        {
            var column = SoftDeleteAttribute.GetSoftDeleteColumnName(expression.Target.ElementType);

            if (column != null)
            {
                // Just because the entity has the soft delete annotation doesn't mean that
                // this particular table has the column. This occurs in situation like TPT
                // inheritance mapping and entity splitting where one type maps to multiple
                // tables.
                // We only apply the filter if the column is actually present in this table.
                // If not, then the query is going to be joining to the table that does have
                // the column anyway, so the filter will still be applied.
                var table = (EntityType)expression.Target.ElementType;
                if (table.Properties.Any(p => p.Name == column))
                {
                    var binding = DbExpressionBuilder.Bind(expression);
                    return(DbExpressionBuilder.Filter(
                               binding,
                               DbExpressionBuilder.NotEqual(
                                   DbExpressionBuilder.Property(
                                       DbExpressionBuilder.Variable(binding.VariableType, binding.VariableName),
                                       column),
                                   DbExpression.FromBoolean(true))));
                }
            }

            return(base.Visit(expression));
        }
        public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
        {
            if (interceptionContext.OriginalResult.DataSpace != DataSpace.SSpace)
            {
                return;
            }

            if (interceptionContext.Result is DbQueryCommandTree queryCommand)
            {
                var newQuery = queryCommand.Query.Accept(new DeleteQueryVisitor());
                interceptionContext.Result = new DbQueryCommandTree(
                    queryCommand.MetadataWorkspace,
                    queryCommand.DataSpace,
                    newQuery);
            }

            if (!(interceptionContext.OriginalResult is DbDeleteCommandTree deleteCommand))
            {
                return;
            }

            var column = SoftDeleteAttribute.GetSoftDeleteColumnName(deleteCommand.Target.VariableType.EdmType);

            if (column == null)
            {
                return;
            }

            var setClauses = new List <DbModificationClause>();
            var table      = (EntityType)deleteCommand.Target.VariableType.EdmType;

            if (table.Properties.Any(p => p.Name == column))
            {
                setClauses.Add(DbExpressionBuilder.SetClause(
                                   deleteCommand.Target.VariableType.Variable(deleteCommand.Target.VariableName).Property(column),
                                   DbExpression.FromBoolean(true)));
            }

            var update = new DbUpdateCommandTree(
                deleteCommand.MetadataWorkspace,
                deleteCommand.DataSpace,
                deleteCommand.Target,
                deleteCommand.Predicate,
                setClauses.AsReadOnly(),
                null);

            interceptionContext.Result = update;
        }
Esempio n. 3
0
        public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
        {
            if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace)
            {
                var queryCommand = interceptionContext.Result as DbQueryCommandTree;
                if (queryCommand != null)
                {
                    var context = (DataContext)interceptionContext.DbContexts.Single(c => c is DataContext);

                    if (context.SoftDeleteFilterIsActive)
                    {
                        var newQuery = queryCommand.Query.Accept(new SoftDeleteQueryVisitor());
                        interceptionContext.Result = new DbQueryCommandTree(
                            queryCommand.MetadataWorkspace,
                            queryCommand.DataSpace,
                            newQuery);
                    }
                }

                var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree;
                if (deleteCommand != null)
                {
                    var column = SoftDeleteAttribute.GetSoftDeleteColumnName(deleteCommand.Target.VariableType.EdmType);
                    if (column != null)
                    {
                        var setClause =
                            DbExpressionBuilder.SetClause(
                                DbExpressionBuilder.Property(
                                    DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName),
                                    column),
                                DbExpression.FromBoolean(true));

                        var update = new DbUpdateCommandTree(
                            deleteCommand.MetadataWorkspace,
                            deleteCommand.DataSpace,
                            deleteCommand.Target,
                            deleteCommand.Predicate,
                            new List <DbModificationClause> {
                            setClause
                        }.AsReadOnly(),
                            null);

                        interceptionContext.Result = update;
                    }
                }
            }
        }
Esempio n. 4
0
        public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
        {
            if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace)
            {
                //This was Rowan Miller's example for filtering out IsDeleteds, but I'm going to try Jimmy Bogard's method instead.
                var queryCommand = interceptionContext.Result as DbQueryCommandTree;
                if (queryCommand != null)
                {
                    var newQuery = queryCommand.Query.Accept(new SoftDeleteQueryVisitor());
                    interceptionContext.Result = new DbQueryCommandTree(
                        queryCommand.MetadataWorkspace,
                        queryCommand.DataSpace,
                        newQuery);
                }

                var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree;
                if (deleteCommand != null)
                {
                    var column = SoftDeleteAttribute.GetSoftDeleteColumnName(deleteCommand.Target.VariableType.EdmType);
                    if (column != null)
                    {
                        var setClause =
                            DbExpressionBuilder.SetClause(
                                DbExpressionBuilder.Property(
                                    DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName),
                                    column),
                                DbExpression.FromBoolean(true));

                        var update = new DbUpdateCommandTree(
                            deleteCommand.MetadataWorkspace,
                            deleteCommand.DataSpace,
                            deleteCommand.Target,
                            deleteCommand.Predicate,
                            new List <DbModificationClause> {
                            setClause
                        }.AsReadOnly(),
                            null);

                        interceptionContext.Result = update;
                    }
                }
            }
        }
        public override DbExpression Visit(DbScanExpression expression)
        {
            var column = SoftDeleteAttribute.GetSoftDeleteColumnName(expression.Target.ElementType);

            if (column == null)
            {
                return(base.Visit(expression));
            }

            var table = (EntityType)expression.Target.ElementType;

            if (table.Properties.All(p => p.Name != column))
            {
                return(base.Visit(expression));
            }

            var binding = expression.Bind();

            return(binding.Filter(binding.VariableType.Variable(binding.VariableName).Property(column)
                                  .NotEqual(DbExpression.FromBoolean(true))));
        }
        public override DbExpression Visit(DbScanExpression expression)
        {
            var column = SoftDeleteAttribute.GetSoftDeleteColumnName(expression.Target.ElementType);

            if (column != null)
            {
                var table = (EntityType)expression.Target.ElementType;
                if (table.Properties.Any(p => p.Name == column))
                {
                    var binding = DbExpressionBuilder.Bind(expression);
                    return(DbExpressionBuilder.Filter(
                               binding,
                               DbExpressionBuilder.NotEqual(
                                   DbExpressionBuilder.Property(
                                       DbExpressionBuilder.Variable(binding.VariableType, binding.VariableName),
                                       column),
                                   DbExpression.FromBoolean(true))));
                }
            }

            return(base.Visit(expression));
        }