Expression Db2ApplyOptimizations(Expression expression, bool searchCondition, bool joinCondition = false) { var expression2 = (new NullComparisonTransformingVisitor_DB2(ParameterValues)).Visit(expression); var binaryExpression = (expression2 is BinaryExpression) ? (expression2 as BinaryExpression) : null; if (_relationalNullsExpandingVisitor == null) { _relationalNullsExpandingVisitor = new RelationalNullsExpandingVisitor(); } if (_predicateReductionExpressionOptimizer == null) { _predicateReductionExpressionOptimizer = new PredicateReductionExpressionOptimizer(); } if (_predicateNegationExpressionOptimizer == null) { _predicateNegationExpressionOptimizer = new PredicateNegationExpressionOptimizer(); } if (_reducingExpressionVisitor == null) { _reducingExpressionVisitor = new ReducingExpressionVisitor(); } if (_booleanExpressionTranslatingVisitor == null) { _booleanExpressionTranslatingVisitor = new BooleanExpressionTranslatingVisitor_DB2(); } expression2 = ((!joinCondition || binaryExpression == null || binaryExpression.NodeType != ExpressionType.Equal) ? Db2ApplyNullSemantics(expression2) : Expression.MakeBinary(binaryExpression.NodeType, Db2ApplyNullSemantics(binaryExpression.Left), Db2ApplyNullSemantics(binaryExpression.Right))); expression2 = _predicateReductionExpressionOptimizer.Visit(expression2); expression2 = _predicateNegationExpressionOptimizer.Visit(expression2); expression2 = _reducingExpressionVisitor.Visit(expression2); if (binaryExpression == null || (expression.NodeType != ExpressionType.Or && expression.NodeType != ExpressionType.And)) { expression2 = _booleanExpressionTranslatingVisitor.Translate(expression2, searchCondition); } return(expression2); }
private Expression ApplyNullSemantics(Expression expression) { var newExpression = new NullComparisonTransformingVisitor(_parametersValues) .Visit(expression); var relationalNullsOptimizedExpandingVisitor = new RelationalNullsOptimizedExpandingVisitor(); var optimizedExpression = relationalNullsOptimizedExpandingVisitor.Visit(newExpression); newExpression = relationalNullsOptimizedExpandingVisitor.IsOptimalExpansion ? optimizedExpression : new RelationalNullsExpandingVisitor().Visit(newExpression); newExpression = new PredicateNegationExpressionOptimizer().Visit(newExpression); newExpression = new ReducingExpressionVisitor().Visit(newExpression); return(newExpression); }
public virtual Expression VisitSelect(SelectExpression selectExpression) { Check.NotNull(selectExpression, nameof(selectExpression)); IDisposable subQueryIndent = null; if (selectExpression.Alias != null) { _relationalCommandBuilder.AppendLine("("); subQueryIndent = _relationalCommandBuilder.Indent(); } _relationalCommandBuilder.Append("SELECT "); if (selectExpression.IsDistinct) { _relationalCommandBuilder.Append("DISTINCT "); } GenerateTop(selectExpression); var projectionAdded = false; if (selectExpression.IsProjectStar) { _relationalCommandBuilder .Append(_sqlGenerationHelper.DelimitIdentifier(selectExpression.Tables.Single().Alias)) .Append(".*"); projectionAdded = true; } if (selectExpression.Projection.Any()) { if (selectExpression.IsProjectStar) { _relationalCommandBuilder.Append(", "); } VisitJoin(selectExpression.Projection); projectionAdded = true; } if (!projectionAdded) { _relationalCommandBuilder.Append("1"); } if (selectExpression.Tables.Any()) { _relationalCommandBuilder.AppendLine() .Append("FROM "); VisitJoin(selectExpression.Tables, sql => sql.AppendLine()); } if (selectExpression.Predicate != null) { _relationalCommandBuilder.AppendLine() .Append("WHERE "); var constantExpression = selectExpression.Predicate as ConstantExpression; if (constantExpression != null) { _relationalCommandBuilder.Append((bool)constantExpression.Value ? "1 = 1" : "1 = 0"); } else { var predicate = new NullComparisonTransformingVisitor(_parametersValues) .Visit(selectExpression.Predicate); var relationalNullsOptimizedExpandingVisitor = new RelationalNullsOptimizedExpandingVisitor(); var newPredicate = relationalNullsOptimizedExpandingVisitor.Visit(predicate); predicate = relationalNullsOptimizedExpandingVisitor.IsOptimalExpansion ? newPredicate : new RelationalNullsExpandingVisitor().Visit(predicate); predicate = new PredicateNegationExpressionOptimizer().Visit(predicate); predicate = new ReducingExpressionVisitor().Visit(predicate); Visit(predicate); if (selectExpression.Predicate is ParameterExpression || selectExpression.Predicate.IsAliasWithColumnExpression() || selectExpression.Predicate is SelectExpression) { _relationalCommandBuilder.Append(" = "); _relationalCommandBuilder.Append(TrueLiteral); } } } if (selectExpression.OrderBy.Any()) { _relationalCommandBuilder.AppendLine(); GenerateOrderBy(selectExpression.OrderBy); } GenerateLimitOffset(selectExpression); if (subQueryIndent != null) { subQueryIndent.Dispose(); _relationalCommandBuilder.AppendLine() .Append(")"); if (selectExpression.Alias.Length > 0) { _relationalCommandBuilder.Append(" AS ") .Append(_sqlGenerationHelper.DelimitIdentifier(selectExpression.Alias)); } } return(selectExpression); }