Skip to content

Commit

Permalink
Query: Cleanup regions in Expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
smitpatel committed Jun 5, 2019
1 parent 7f7130a commit 17cc277
Show file tree
Hide file tree
Showing 26 changed files with 298 additions and 705 deletions.
13 changes: 0 additions & 13 deletions src/EFCore.Relational/Query/Pipeline/ISqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline
{
public interface ISqlExpressionFactory
{
#region TypeMapping
SqlExpression ApplyTypeMapping(SqlExpression sqlExpression, RelationalTypeMapping typeMapping);
SqlExpression ApplyDefaultTypeMapping(SqlExpression sqlExpression);
RelationalTypeMapping GetTypeMappingForValue(object value);
RelationalTypeMapping FindMapping(Type type);
#endregion

#region Binary
SqlBinaryExpression MakeBinary(ExpressionType operatorType, SqlExpression left, SqlExpression right, RelationalTypeMapping typeMapping);
// Comparison
SqlBinaryExpression Equal(SqlExpression left, SqlExpression right);
Expand All @@ -42,22 +39,16 @@ public interface ISqlExpressionFactory
SqlBinaryExpression Or(SqlExpression left, SqlExpression right, RelationalTypeMapping typeMapping = null);
// Other
SqlBinaryExpression Coalesce(SqlExpression left, SqlExpression right, RelationalTypeMapping typeMapping = null);
#endregion

#region Unary
SqlUnaryExpression IsNull(SqlExpression operand);
SqlUnaryExpression IsNotNull(SqlExpression operand);
SqlUnaryExpression Convert(SqlExpression operand, Type type, RelationalTypeMapping typeMapping = null);
SqlUnaryExpression Not(SqlExpression operand);
SqlUnaryExpression Negate(SqlExpression operand);
#endregion

#region Case block
CaseExpression Case(SqlExpression operand, params CaseWhenClause[] whenClauses);
CaseExpression Case(IReadOnlyList<CaseWhenClause> whenClauses, SqlExpression elseResult);
#endregion

#region Functions
SqlFunctionExpression Function(
string functionName, IEnumerable<SqlExpression> arguments, Type returnType, RelationalTypeMapping typeMapping = null);
SqlFunctionExpression Function(
Expand All @@ -70,9 +61,7 @@ SqlFunctionExpression Function(
string schema, string functionName, bool niladic, Type returnType, RelationalTypeMapping typeMapping = null);
SqlFunctionExpression Function(
SqlExpression instance, string functionName, bool niladic, Type returnType, RelationalTypeMapping typeMapping = null);
#endregion

#region Other Sql specific constructs
ExistsExpression Exists(SelectExpression subquery, bool negated);
InExpression In(SqlExpression item, SqlExpression values, bool negated);
InExpression In(SqlExpression item, SelectExpression subquery, bool negated);
Expand All @@ -83,7 +72,5 @@ SqlFunctionExpression Function(
SelectExpression Select(SqlExpression projection);
SelectExpression Select(IEntityType entityType);
SelectExpression Select(IEntityType entityType, string sql, Expression sqlArguments);

#endregion
}
}
13 changes: 0 additions & 13 deletions src/EFCore.Relational/Query/Pipeline/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public SqlExpressionFactory(IRelationalTypeMappingSource typeMappingSource)
_boolTypeMapping = typeMappingSource.FindMapping(typeof(bool));
}

#region TypeMapping
public SqlExpression ApplyDefaultTypeMapping(SqlExpression sqlExpression)
{
if (sqlExpression == null
Expand Down Expand Up @@ -210,9 +209,7 @@ public virtual RelationalTypeMapping FindMapping(Type type)
{
return _typeMappingSource.FindMapping(type);
}
#endregion

#region Binary
public SqlBinaryExpression MakeBinary(
ExpressionType operatorType, SqlExpression left, SqlExpression right, RelationalTypeMapping typeMapping)
{
Expand Down Expand Up @@ -315,9 +312,7 @@ public SqlBinaryExpression Coalesce(SqlExpression left, SqlExpression right, Rel
return MakeBinary(ExpressionType.Coalesce, left, right, typeMapping);
}

#endregion

#region Unary
private SqlUnaryExpression MakeUnary(
ExpressionType operatorType, SqlExpression operand, Type type, RelationalTypeMapping typeMapping = null)
{
Expand Down Expand Up @@ -347,9 +342,7 @@ public SqlUnaryExpression Negate(SqlExpression operand)
{
return MakeUnary(ExpressionType.Negate, operand, operand.Type, operand.TypeMapping);
}
#endregion

#region Case block
public CaseExpression Case(SqlExpression operand, params CaseWhenClause[] whenClauses)
{
var operandTypeMapping = operand.TypeMapping
Expand Down Expand Up @@ -391,9 +384,7 @@ public CaseExpression Case(IReadOnlyList<CaseWhenClause> whenClauses, SqlExpress

return new CaseExpression(typeMappedWhenClauses, elseResult);
}
#endregion

#region Functions
public SqlFunctionExpression Function(
string functionName, IEnumerable<SqlExpression> arguments, Type returnType, RelationalTypeMapping typeMapping = null)
{
Expand Down Expand Up @@ -467,9 +458,7 @@ public SqlFunctionExpression Function(
}


#endregion

#region Other Sql specific constructs
public ExistsExpression Exists(SelectExpression subquery, bool negated)
{
return new ExistsExpression(subquery, negated, _boolTypeMapping);
Expand Down Expand Up @@ -577,7 +566,5 @@ private void AddDiscriminator(SelectExpression selectExpression, IEntityType ent
In(discriminatorColumn, Constant(concreteEntityTypes.Select(et => et.GetDiscriminatorValue()).ToList()), negated: false));
}
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions
{
public class CaseExpression : SqlExpression
{
#region Fields & Constructors

private readonly List<CaseWhenClause> _whenClauses = new List<CaseWhenClause>();

public CaseExpression(
Expand Down Expand Up @@ -40,18 +38,10 @@ private CaseExpression(
ElseResult = elseResult;
}

#endregion

#region Public Properties

public SqlExpression Operand { get; }
public IReadOnlyList<CaseWhenClause> WhenClauses => _whenClauses;
public SqlExpression ElseResult { get; }

#endregion

#region Expression-based methods

protected override Expression VisitChildren(ExpressionVisitor visitor)
{
var operand = (SqlExpression)visitor.Visit(Operand);
Expand Down Expand Up @@ -85,43 +75,9 @@ public virtual CaseExpression Update(
SqlExpression operand,
IReadOnlyList<CaseWhenClause> whenClauses,
SqlExpression elseResult)
{
return operand != Operand || !whenClauses.SequenceEqual(WhenClauses) || elseResult != ElseResult
=> operand != Operand || !whenClauses.SequenceEqual(WhenClauses) || elseResult != ElseResult
? new CaseExpression(operand, whenClauses, elseResult)
: this;
}

#endregion

#region Equality & HashCode

public override bool Equals(object obj)
=> obj != null
&& (ReferenceEquals(this, obj)
|| obj is CaseExpression caseExpression
&& Equals(caseExpression));

private bool Equals(CaseExpression caseExpression)
=> base.Equals(caseExpression)
&& (Operand == null ? caseExpression.Operand == null : Operand.Equals(caseExpression.Operand))
&& WhenClauses.SequenceEqual(caseExpression.WhenClauses)
&& (ElseResult == null ? caseExpression.ElseResult == null : ElseResult.Equals(caseExpression.ElseResult));

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ (Operand?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ WhenClauses.Aggregate(
0, (current, value) => current + ((current * 397) ^ value.GetHashCode()));
hashCode = (hashCode * 397) ^ (ElseResult?.GetHashCode() ?? 0);

return hashCode;
}
}

#endregion

public override void Print(ExpressionPrinter expressionPrinter)
{
Expand Down Expand Up @@ -151,5 +107,31 @@ public override void Print(ExpressionPrinter expressionPrinter)

expressionPrinter.StringBuilder.AppendLine().Append("END");
}

public override bool Equals(object obj)
=> obj != null
&& (ReferenceEquals(this, obj)
|| obj is CaseExpression caseExpression
&& Equals(caseExpression));

private bool Equals(CaseExpression caseExpression)
=> base.Equals(caseExpression)
&& (Operand == null ? caseExpression.Operand == null : Operand.Equals(caseExpression.Operand))
&& WhenClauses.SequenceEqual(caseExpression.WhenClauses)
&& (ElseResult == null ? caseExpression.ElseResult == null : ElseResult.Equals(caseExpression.ElseResult));

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ (Operand?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ WhenClauses.Aggregate(
0, (current, value) => current + ((current * 397) ^ value.GetHashCode()));
hashCode = (hashCode * 397) ^ (ElseResult?.GetHashCode() ?? 0);

return hashCode;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,15 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions
{
public class CaseWhenClause
{
#region Fields & Constructors

public CaseWhenClause(SqlExpression test, SqlExpression result)
{
Test = test;
Result = result;
}

#endregion

#region Public Properties

public SqlExpression Test { get; }
public SqlExpression Result { get; }

#endregion

#region Equality & HashCode

public override bool Equals(object obj)
=> obj != null
&& (ReferenceEquals(this, obj)
Expand All @@ -44,7 +34,5 @@ public override int GetHashCode()
return hashCode;
}
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions
{
public class ColumnExpression : SqlExpression
{
#region Fields & Constructors

internal ColumnExpression(IProperty property, TableExpressionBase table, bool nullable)
: this(property.GetColumnName(), table, property.ClrType, property.FindRelationalMapping(),
nullable || property.IsNullable || property.DeclaringEntityType.BaseType != null)
Expand All @@ -32,18 +30,10 @@ private ColumnExpression(string name, TableExpressionBase table, Type type, Rela
Nullable = nullable;
}

#endregion

#region Public Properties

public string Name { get; }
public TableExpressionBase Table { get; }
public bool Nullable { get; }

#endregion

#region Expression-based methods

protected override Expression VisitChildren(ExpressionVisitor visitor)
{
var newTable = (TableExpressionBase)visitor.Visit(Table);
Expand All @@ -54,13 +44,11 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
}

public ColumnExpression MakeNullable()
{
return new ColumnExpression(Name, Table, Type.MakeNullable(), TypeMapping, true);
}
=> new ColumnExpression(Name, Table, Type.MakeNullable(), TypeMapping, true);

#endregion

#region Equality & HashCode
public override void Print(ExpressionPrinter expressionPrinter)
=> expressionPrinter.StringBuilder.Append(Table.Alias).Append(".").Append(Name);

public override bool Equals(object obj)
=> obj != null
Expand All @@ -86,12 +74,5 @@ public override int GetHashCode()
return hashCode;
}
}

#endregion

public override void Print(ExpressionPrinter expressionPrinter)
{
expressionPrinter.StringBuilder.Append(Table.Alias).Append(".").Append(Name);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query.Internal;

namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions
{
public class CrossJoinExpression : JoinExpressionBase
{
public CrossJoinExpression(TableExpressionBase table)
: base(table)
{
}

protected override Expression VisitChildren(ExpressionVisitor visitor)
=> Update((TableExpressionBase)visitor.Visit(Table));

public CrossJoinExpression Update(TableExpressionBase table)
=> table != Table
? new CrossJoinExpression(table)
: this;

public override void Print(ExpressionPrinter expressionPrinter)
{
expressionPrinter.StringBuilder.Append("CROSS JOIN ");
expressionPrinter.Visit(Table);
}

public override bool Equals(object obj)
=> obj != null
&& (ReferenceEquals(this, obj)
|| obj is CrossJoinExpression crossJoinExpression
&& Equals(crossJoinExpression));

private bool Equals(CrossJoinExpression crossJoinExpression)
=> base.Equals(crossJoinExpression);

public override int GetHashCode() => base.GetHashCode();
}
}
Loading

0 comments on commit 17cc277

Please sign in to comment.