From 17cc277e2d383a27150f54f72dd6b43d244703f8 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Wed, 5 Jun 2019 15:45:42 -0700 Subject: [PATCH] Query: Cleanup regions in Expressions --- .../Query/Pipeline/ISqlExpressionFactory.cs | 13 --- .../Query/Pipeline/SqlExpressionFactory.cs | 13 --- .../Pipeline/SqlExpressions/CaseExpression.cs | 72 ++++++---------- .../Pipeline/SqlExpressions/CaseWhenClause.cs | 12 --- .../SqlExpressions/ColumnExpression.cs | 25 +----- .../SqlExpressions/CrossJoinExpression.cs | 41 ++++++++++ .../SqlExpressions/ExistsExpression.cs | 55 ++++--------- .../SqlExpressions/FromSqlExpression.cs | 28 +------ .../Pipeline/SqlExpressions/InExpression.cs | 35 +++----- .../SqlExpressions/InnerJoinExpression.cs | 82 ++----------------- .../SqlExpressions/JoinExpressionBase.cs | 6 -- .../SqlExpressions/LeftJoinExpression.cs | 34 ++------ .../Pipeline/SqlExpressions/LikeExpression.cs | 42 +++------- .../SqlExpressions/OrderingExpression.cs | 33 ++------ .../PredicateJoinExpressionBase.cs | 6 -- .../SqlExpressions/ProjectionExpression.cs | 39 +++------ .../SqlExpressions/SqlBinaryExpression.cs | 81 +++++++----------- .../SqlExpressions/SqlConstantExpression.cs | 69 ++++++---------- .../Pipeline/SqlExpressions/SqlExpression.cs | 16 +--- .../SqlExpressions/SqlFragmentExpression.cs | 22 +---- .../SqlExpressions/SqlFunctionExpression.cs | 71 +++++++--------- .../SqlExpressions/SqlParameterExpression.cs | 26 +----- .../SqlExpressions/SqlUnaryExpression.cs | 71 ++++++---------- .../SqlExpressions/SubSelectExpression.cs | 38 +++------ .../SqlExpressions/TableExpression.cs | 50 ++++------- .../SqlExpressions/TableExpressionBase.cs | 23 +----- 26 files changed, 298 insertions(+), 705 deletions(-) create mode 100644 src/EFCore.Relational/Query/Pipeline/SqlExpressions/CrossJoinExpression.cs diff --git a/src/EFCore.Relational/Query/Pipeline/ISqlExpressionFactory.cs b/src/EFCore.Relational/Query/Pipeline/ISqlExpressionFactory.cs index 6666ac27cff..902cbc9e1a7 100644 --- a/src/EFCore.Relational/Query/Pipeline/ISqlExpressionFactory.cs +++ b/src/EFCore.Relational/Query/Pipeline/ISqlExpressionFactory.cs @@ -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); @@ -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 whenClauses, SqlExpression elseResult); - #endregion - #region Functions SqlFunctionExpression Function( string functionName, IEnumerable arguments, Type returnType, RelationalTypeMapping typeMapping = null); SqlFunctionExpression Function( @@ -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); @@ -83,7 +72,5 @@ SqlFunctionExpression Function( SelectExpression Select(SqlExpression projection); SelectExpression Select(IEntityType entityType); SelectExpression Select(IEntityType entityType, string sql, Expression sqlArguments); - - #endregion } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressionFactory.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressionFactory.cs index 27c98e91117..c150c5391df 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressionFactory.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressionFactory.cs @@ -24,7 +24,6 @@ public SqlExpressionFactory(IRelationalTypeMappingSource typeMappingSource) _boolTypeMapping = typeMappingSource.FindMapping(typeof(bool)); } - #region TypeMapping public SqlExpression ApplyDefaultTypeMapping(SqlExpression sqlExpression) { if (sqlExpression == null @@ -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) { @@ -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) { @@ -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 @@ -391,9 +384,7 @@ public CaseExpression Case(IReadOnlyList whenClauses, SqlExpress return new CaseExpression(typeMappedWhenClauses, elseResult); } - #endregion - #region Functions public SqlFunctionExpression Function( string functionName, IEnumerable arguments, Type returnType, RelationalTypeMapping typeMapping = null) { @@ -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); @@ -577,7 +566,5 @@ private void AddDiscriminator(SelectExpression selectExpression, IEntityType ent In(discriminatorColumn, Constant(concreteEntityTypes.Select(et => et.GetDiscriminatorValue()).ToList()), negated: false)); } } - - #endregion } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/CaseExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/CaseExpression.cs index 3328c37b66e..74d2d77bcdc 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/CaseExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/CaseExpression.cs @@ -11,8 +11,6 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class CaseExpression : SqlExpression { - #region Fields & Constructors - private readonly List _whenClauses = new List(); public CaseExpression( @@ -40,18 +38,10 @@ private CaseExpression( ElseResult = elseResult; } - #endregion - - #region Public Properties - public SqlExpression Operand { get; } public IReadOnlyList WhenClauses => _whenClauses; public SqlExpression ElseResult { get; } - #endregion - - #region Expression-based methods - protected override Expression VisitChildren(ExpressionVisitor visitor) { var operand = (SqlExpression)visitor.Visit(Operand); @@ -85,43 +75,9 @@ public virtual CaseExpression Update( SqlExpression operand, IReadOnlyList 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) { @@ -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; + } + } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/CaseWhenClause.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/CaseWhenClause.cs index 78aaab2a7ef..f03815396d4 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/CaseWhenClause.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/CaseWhenClause.cs @@ -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) @@ -44,7 +34,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/ColumnExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/ColumnExpression.cs index a19095d8b04..008a8b83405 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/ColumnExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/ColumnExpression.cs @@ -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) @@ -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); @@ -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 @@ -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); - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/CrossJoinExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/CrossJoinExpression.cs new file mode 100644 index 00000000000..26af574c769 --- /dev/null +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/CrossJoinExpression.cs @@ -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(); + } +} diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/ExistsExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/ExistsExpression.cs index 462b3287aa6..4d7a0ceb83f 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/ExistsExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/ExistsExpression.cs @@ -9,8 +9,6 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class ExistsExpression : SqlExpression { - #region Fields & Constructors - public ExistsExpression(SelectExpression subquery, bool negated, RelationalTypeMapping typeMapping) : base(typeof(bool), typeMapping) { @@ -18,34 +16,33 @@ public ExistsExpression(SelectExpression subquery, bool negated, RelationalTypeM Negated = negated; } - #endregion - - #region Public Properties - public SelectExpression Subquery { get; } public bool Negated { get; } - #endregion - - #region Expression-based methods - protected override Expression VisitChildren(ExpressionVisitor visitor) - { - var newSubquery = (SelectExpression)visitor.Visit(Subquery); - - return Update(newSubquery); - } + => Update((SelectExpression)visitor.Visit(Subquery)); public ExistsExpression Update(SelectExpression subquery) - { - return subquery != Subquery + => subquery != Subquery ? new ExistsExpression(subquery, Negated, TypeMapping) : this; - } - #endregion - #region Equality & HashCode + public override void Print(ExpressionPrinter expressionPrinter) + { + if (Negated) + { + expressionPrinter.StringBuilder.Append("NOT "); + } + + expressionPrinter.StringBuilder.AppendLine("EXISTS ("); + using (expressionPrinter.StringBuilder.Indent()) + { + expressionPrinter.Visit(Subquery); + } + + expressionPrinter.StringBuilder.Append(")"); + } public override bool Equals(object obj) => obj != null @@ -69,23 +66,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public override void Print(ExpressionPrinter expressionPrinter) - { - if (Negated) - { - expressionPrinter.StringBuilder.Append("NOT "); - } - - expressionPrinter.StringBuilder.AppendLine("EXISTS ("); - using (expressionPrinter.StringBuilder.Indent()) - { - expressionPrinter.Visit(Subquery); - } - - expressionPrinter.StringBuilder.Append(")"); - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/FromSqlExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/FromSqlExpression.cs index 7024e9b6a94..50acee0534f 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/FromSqlExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/FromSqlExpression.cs @@ -9,43 +9,21 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class FromSqlExpression : TableExpressionBase { - #region Fields & Constructors - public FromSqlExpression( - [NotNull] string sql, - Expression arguments, - [NotNull] string alias) + public FromSqlExpression([NotNull] string sql, Expression arguments, [NotNull] string alias) : base(alias) { Sql = sql; Arguments = arguments; } - #endregion - #region Public Properties - - /// - /// Gets the SQL. - /// - /// - /// The SQL. - /// public string Sql { get; } public Expression Arguments { get; } - #endregion - - #region Expression-based methods - - protected override Expression VisitChildren(ExpressionVisitor visitor) - => this; + protected override Expression VisitChildren(ExpressionVisitor visitor) => this; public override void Print(ExpressionPrinter expressionPrinter) => expressionPrinter.StringBuilder.Append(Sql); - #endregion - - #region Equality & HashCode - public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -66,7 +44,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/InExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/InExpression.cs index 28e8b0f1cd3..4d4ff97765f 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/InExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/InExpression.cs @@ -9,7 +9,6 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class InExpression : SqlExpression { - #region Fields & Constructors public InExpression(SqlExpression item, bool negated, SelectExpression subquery, RelationalTypeMapping typeMapping) : this(item, negated, null, subquery, typeMapping) { @@ -29,17 +28,12 @@ private InExpression(SqlExpression item, bool negated, SqlExpression values, Sel Subquery = subquery; Values = values; } - #endregion - - #region Public Properties public SqlExpression Item { get; } public bool Negated { get; } public SqlExpression Values { get; } public SelectExpression Subquery { get; } - #endregion - #region Expression-based methods protected override Expression VisitChildren(ExpressionVisitor visitor) { var newItem = (SqlExpression)visitor.Visit(Item); @@ -49,20 +43,22 @@ protected override Expression VisitChildren(ExpressionVisitor visitor) return Update(newItem, values, subquery); } - public InExpression Negate() - { - return new InExpression(Item, !Negated, Values, Subquery, TypeMapping); - } + public InExpression Negate() => new InExpression(Item, !Negated, Values, Subquery, TypeMapping); public InExpression Update(SqlExpression item, SqlExpression values, SelectExpression subquery) - { - return item != Item || subquery != Subquery || values != Values + => item != Item || subquery != Subquery || values != Values ? new InExpression(item, Negated, values, subquery, TypeMapping) : this; + + public override void Print(ExpressionPrinter expressionPrinter) + { + expressionPrinter.Visit(Item); + expressionPrinter.StringBuilder.Append(Negated ? " NOT IN " : " IN "); + expressionPrinter.StringBuilder.Append("("); + expressionPrinter.Visit(Values); + expressionPrinter.StringBuilder.Append(")"); } - #endregion - #region Equality & HashCode public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -89,16 +85,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public override void Print(ExpressionPrinter expressionPrinter) - { - expressionPrinter.Visit(Item); - expressionPrinter.StringBuilder.Append(Negated ? " NOT IN " : " IN "); - expressionPrinter.StringBuilder.Append("("); - expressionPrinter.Visit(Values); - expressionPrinter.StringBuilder.Append(")"); - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/InnerJoinExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/InnerJoinExpression.cs index ff8d0d94446..559affd7f14 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/InnerJoinExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/InnerJoinExpression.cs @@ -8,14 +8,11 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class InnerJoinExpression : PredicateJoinExpressionBase { - #region Fields & Constructors public InnerJoinExpression(TableExpressionBase table, SqlExpression joinPredicate) : base(table, joinPredicate) { } - #endregion - #region Expression-based methods protected override Expression VisitChildren(ExpressionVisitor visitor) { var table = (TableExpressionBase)visitor.Visit(Table); @@ -25,34 +22,9 @@ protected override Expression VisitChildren(ExpressionVisitor visitor) } public InnerJoinExpression Update(TableExpressionBase table, SqlExpression joinPredicate) - { - return table != Table || joinPredicate != JoinPredicate + => table != Table || joinPredicate != JoinPredicate ? new InnerJoinExpression(table, joinPredicate) : this; - } - #endregion - - #region Equality & HashCode - public override bool Equals(object obj) - => obj != null - && (ReferenceEquals(this, obj) - || obj is InnerJoinExpression innerJoinExpression - && Equals(innerJoinExpression)); - - private bool Equals(InnerJoinExpression innerJoinExpression) - => base.Equals(innerJoinExpression); - - public override int GetHashCode() - { - unchecked - { - var hashCode = base.GetHashCode(); - - return hashCode; - } - } - - #endregion public override void Print(ExpressionPrinter expressionPrinter) { @@ -61,58 +33,16 @@ public override void Print(ExpressionPrinter expressionPrinter) expressionPrinter.StringBuilder.Append(" ON "); expressionPrinter.Visit(JoinPredicate); } - } - public class CrossJoinExpression : JoinExpressionBase - { - #region Fields & Constructors - public CrossJoinExpression(TableExpressionBase table) - : base(table) - { - } - #endregion - - #region Expression-based methods - protected override Expression VisitChildren(ExpressionVisitor visitor) - { - var table = (TableExpressionBase)visitor.Visit(Table); - - return Update(table); - } - - public CrossJoinExpression Update(TableExpressionBase table) - { - return table != Table - ? new CrossJoinExpression(table) - : this; - } - #endregion - - #region Equality & HashCode 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() - { - unchecked - { - var hashCode = base.GetHashCode(); + || obj is InnerJoinExpression innerJoinExpression + && Equals(innerJoinExpression)); - return hashCode; - } - } - #endregion + private bool Equals(InnerJoinExpression innerJoinExpression) + => base.Equals(innerJoinExpression); - public override void Print(ExpressionPrinter expressionPrinter) - { - expressionPrinter.StringBuilder.Append("CROSS JOIN "); - expressionPrinter.Visit(Table); - } + public override int GetHashCode() => base.GetHashCode(); } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/JoinExpressionBase.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/JoinExpressionBase.cs index c8c9efa82f2..6c620f65930 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/JoinExpressionBase.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/JoinExpressionBase.cs @@ -5,19 +5,14 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public abstract class JoinExpressionBase : TableExpressionBase { - #region Fields & Constructors protected JoinExpressionBase(TableExpressionBase table) : base("") { Table = table; } - #endregion - #region Public Properties public TableExpressionBase Table { get; } - #endregion - #region Equality & HashCode public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -38,6 +33,5 @@ public override int GetHashCode() return hashCode; } } - #endregion } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/LeftJoinExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/LeftJoinExpression.cs index 7c954de08fa..3b68836061c 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/LeftJoinExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/LeftJoinExpression.cs @@ -8,14 +8,11 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class LeftJoinExpression : PredicateJoinExpressionBase { - #region Fields & Constructors public LeftJoinExpression(TableExpressionBase table, SqlExpression joinPredicate) : base(table, joinPredicate) { } - #endregion - #region Expression-based methods protected override Expression VisitChildren(ExpressionVisitor visitor) { var table = (TableExpressionBase)visitor.Visit(Table); @@ -25,14 +22,18 @@ protected override Expression VisitChildren(ExpressionVisitor visitor) } public LeftJoinExpression Update(TableExpressionBase table, SqlExpression joinPredicate) - { - return table != Table || joinPredicate != JoinPredicate + => table != Table || joinPredicate != JoinPredicate ? new LeftJoinExpression(table, joinPredicate) : this; + + public override void Print(ExpressionPrinter expressionPrinter) + { + expressionPrinter.StringBuilder.Append("LEFT JOIN "); + expressionPrinter.Visit(Table); + expressionPrinter.StringBuilder.Append(" ON "); + expressionPrinter.Visit(JoinPredicate); } - #endregion - #region Equality & HashCode public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -42,23 +43,6 @@ public override bool Equals(object obj) private bool Equals(LeftJoinExpression leftJoinExpression) => base.Equals(leftJoinExpression); - public override int GetHashCode() - { - unchecked - { - var hashCode = base.GetHashCode(); - - return hashCode; - } - } - #endregion - - public override void Print(ExpressionPrinter expressionPrinter) - { - expressionPrinter.StringBuilder.Append("LEFT JOIN "); - expressionPrinter.Visit(Table); - expressionPrinter.StringBuilder.Append(" ON "); - expressionPrinter.Visit(JoinPredicate); - } + public override int GetHashCode() => base.GetHashCode(); } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/LikeExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/LikeExpression.cs index 6b8668a0903..9a3c6fb8ad9 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/LikeExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/LikeExpression.cs @@ -9,8 +9,6 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class LikeExpression : SqlExpression { - #region Fields & Constructors - public LikeExpression(SqlExpression match, SqlExpression pattern, SqlExpression escapeChar, RelationalTypeMapping typeMapping) : base(typeof(bool), typeMapping) { @@ -19,18 +17,10 @@ public LikeExpression(SqlExpression match, SqlExpression pattern, SqlExpression EscapeChar = escapeChar; } - #endregion - - #region Public Properties - public SqlExpression Match { get; } public SqlExpression Pattern { get; } public SqlExpression EscapeChar { get; } - #endregion - - #region Expression-based methods - protected override Expression VisitChildren(ExpressionVisitor visitor) { var match = (SqlExpression)visitor.Visit(Match); @@ -41,15 +31,22 @@ protected override Expression VisitChildren(ExpressionVisitor visitor) } public LikeExpression Update(SqlExpression match, SqlExpression pattern, SqlExpression escapeChar) - { - return match != Match || pattern != Pattern || escapeChar != EscapeChar + => match != Match || pattern != Pattern || escapeChar != EscapeChar ? new LikeExpression(match, pattern, escapeChar, TypeMapping) : this; - } - #endregion + public override void Print(ExpressionPrinter expressionPrinter) + { + expressionPrinter.Visit(Match); + expressionPrinter.StringBuilder.Append(" LIKE "); + expressionPrinter.Visit(Pattern); - #region Equality & HashCode + if (EscapeChar != null) + { + expressionPrinter.StringBuilder.Append(" ESCAPE "); + expressionPrinter.Visit(EscapeChar); + } + } public override bool Equals(object obj) => obj != null @@ -75,20 +72,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public override void Print(ExpressionPrinter expressionPrinter) - { - expressionPrinter.Visit(Match); - expressionPrinter.StringBuilder.Append(" LIKE "); - expressionPrinter.Visit(Pattern); - - if (EscapeChar != null) - { - expressionPrinter.StringBuilder.Append(" ESCAPE "); - expressionPrinter.Visit(EscapeChar); - } - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/OrderingExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/OrderingExpression.cs index 01b31b42b9b..c428acc8489 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/OrderingExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/OrderingExpression.cs @@ -10,40 +10,32 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class OrderingExpression : Expression, IPrintable { - #region Fields & Constructors public OrderingExpression(SqlExpression expression, bool ascending) { Expression = expression; Ascending = ascending; } - #endregion - #region Public Properties public SqlExpression Expression { get; } public bool Ascending { get; } - #endregion - - #region Expression-based properties/methods - public override ExpressionType NodeType => ExpressionType.Extension; public override Type Type => Expression.Type; protected override Expression VisitChildren(ExpressionVisitor visitor) - { - var expression = (SqlExpression)visitor.Visit(Expression); - - return Update(expression); - } + => Update((SqlExpression)visitor.Visit(Expression)); public OrderingExpression Update(SqlExpression expression) - { - return expression != Expression + => expression != Expression ? new OrderingExpression(expression, Ascending) : this; + + public void Print(ExpressionPrinter expressionPrinter) + { + expressionPrinter.Visit(Expression); + + expressionPrinter.StringBuilder.Append(Ascending ? "ASC" : "DESC"); } - #endregion - #region Equality & HashCode public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -64,14 +56,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public void Print(ExpressionPrinter expressionPrinter) - { - expressionPrinter.Visit(Expression); - - expressionPrinter.StringBuilder.Append(Ascending ? "ASC" : "DESC"); - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/PredicateJoinExpressionBase.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/PredicateJoinExpressionBase.cs index 449cfdfe6c4..a08f0b23c7e 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/PredicateJoinExpressionBase.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/PredicateJoinExpressionBase.cs @@ -5,19 +5,14 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public abstract class PredicateJoinExpressionBase : JoinExpressionBase { - #region Fields & Constructors protected PredicateJoinExpressionBase(TableExpressionBase table, SqlExpression joinPredicate) : base(table) { JoinPredicate = joinPredicate; } - #endregion - #region Public Properties public SqlExpression JoinPredicate { get; } - #endregion - #region Equality & HashCode public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -38,6 +33,5 @@ public override int GetHashCode() return hashCode; } } - #endregion } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/ProjectionExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/ProjectionExpression.cs index 98d1beaeada..dbc1ca8ddc2 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/ProjectionExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/ProjectionExpression.cs @@ -10,39 +10,37 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class ProjectionExpression : Expression, IPrintable { - #region Fields & Constructors public ProjectionExpression(SqlExpression expression, string alias) { Expression = expression; Alias = alias; } - #endregion - #region Public Properties public string Alias { get; } public SqlExpression Expression { get; } - #endregion - #region Expression-based methods/properties public override Type Type => Expression.Type; public override ExpressionType NodeType => ExpressionType.Extension; protected override Expression VisitChildren(ExpressionVisitor visitor) - { - var expression = (SqlExpression)visitor.Visit(Expression); - - return Update(expression); - } + => Update((SqlExpression)visitor.Visit(Expression)); public ProjectionExpression Update(SqlExpression expression) - { - return expression != Expression + => expression != Expression ? new ProjectionExpression(expression, Alias) : this; + + public void Print(ExpressionPrinter expressionPrinter) + { + expressionPrinter.Visit(Expression); + if (!string.Equals(string.Empty, Alias) + && !(Expression is ColumnExpression column + && string.Equals(column.Name, Alias))) + { + expressionPrinter.StringBuilder.Append(" AS " + Alias); + } } - #endregion - #region Equality & HashCode public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -64,18 +62,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public void Print(ExpressionPrinter expressionPrinter) - { - expressionPrinter.Visit(Expression); - if (!string.Equals(string.Empty, Alias) - && !(Expression is ColumnExpression column - && string.Equals(column.Name, Alias))) - { - expressionPrinter.StringBuilder.Append(" AS " + Alias); - } - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlBinaryExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlBinaryExpression.cs index 642b5e13587..87e2d95866b 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlBinaryExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlBinaryExpression.cs @@ -12,15 +12,11 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class SqlBinaryExpression : SqlExpression { - #region Fields & Constructors private static ISet _allowedOperators = new HashSet { ExpressionType.Add, - //ExpressionType.AddChecked, ExpressionType.Subtract, - //ExpressionType.SubtractChecked, ExpressionType.Multiply, - //ExpressionType.MultiplyChecked, ExpressionType.Divide, ExpressionType.Modulo, //ExpressionType.Power, @@ -41,6 +37,11 @@ public class SqlBinaryExpression : SqlExpression //ExpressionType.LeftShift, }; + private static ExpressionType VerifyOperator(ExpressionType operatorType) + => _allowedOperators.Contains(operatorType) + ? operatorType + : throw new InvalidOperationException("Unsupported Binary operator type specified."); + public SqlBinaryExpression( ExpressionType operatorType, SqlExpression left, @@ -58,22 +59,10 @@ public SqlBinaryExpression( Right = right; } - private static ExpressionType VerifyOperator(ExpressionType operatorType) - { - return _allowedOperators.Contains(operatorType) - ? operatorType - : throw new InvalidOperationException("Unsupported Binary operator type specified."); - } - - #endregion - - #region Public Properties public ExpressionType OperatorType { get; } public SqlExpression Left { get; } public SqlExpression Right { get; } - #endregion - #region Expression-based methods protected override Expression VisitChildren(ExpressionVisitor visitor) { var left = (SqlExpression)visitor.Visit(Left); @@ -83,42 +72,9 @@ protected override Expression VisitChildren(ExpressionVisitor visitor) } public SqlBinaryExpression Update(SqlExpression left, SqlExpression right) - { - return left != Left || right != Right + => left != Left || right != Right ? new SqlBinaryExpression(OperatorType, left, right, Type, TypeMapping) : this; - } - - #endregion - - #region Equality & HashCode - - public override bool Equals(object obj) - => obj != null - && (ReferenceEquals(this, obj) - || obj is SqlBinaryExpression sqlBinaryExpression - && Equals(sqlBinaryExpression)); - - private bool Equals(SqlBinaryExpression sqlBinaryExpression) - => base.Equals(sqlBinaryExpression) - && OperatorType == sqlBinaryExpression.OperatorType - && Left.Equals(sqlBinaryExpression.Left) - && Right.Equals(sqlBinaryExpression.Right); - - public override int GetHashCode() - { - unchecked - { - var hashCode = base.GetHashCode(); - hashCode = (hashCode * 397) ^ OperatorType.GetHashCode(); - hashCode = (hashCode * 397) ^ Left.GetHashCode(); - hashCode = (hashCode * 397) ^ Right.GetHashCode(); - - return hashCode; - } - } - - #endregion public override void Print(ExpressionPrinter expressionPrinter) { @@ -159,5 +115,30 @@ private bool RequiresBrackets(SqlExpression expression) && sqlBinary.OperatorType != ExpressionType.Coalesce || expression is LikeExpression; } + + public override bool Equals(object obj) + => obj != null + && (ReferenceEquals(this, obj) + || obj is SqlBinaryExpression sqlBinaryExpression + && Equals(sqlBinaryExpression)); + + private bool Equals(SqlBinaryExpression sqlBinaryExpression) + => base.Equals(sqlBinaryExpression) + && OperatorType == sqlBinaryExpression.OperatorType + && Left.Equals(sqlBinaryExpression.Left) + && Right.Equals(sqlBinaryExpression.Right); + + public override int GetHashCode() + { + unchecked + { + var hashCode = base.GetHashCode(); + hashCode = (hashCode * 397) ^ OperatorType.GetHashCode(); + hashCode = (hashCode * 397) ^ Left.GetHashCode(); + hashCode = (hashCode * 397) ^ Right.GetHashCode(); + + return hashCode; + } + } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlConstantExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlConstantExpression.cs index 447c4e40042..389d1d11b3b 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlConstantExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlConstantExpression.cs @@ -10,7 +10,6 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class SqlConstantExpression : SqlExpression { - #region Fields & Constructors private readonly ConstantExpression _constantExpression; public SqlConstantExpression(ConstantExpression constantExpression, RelationalTypeMapping typeMapping) @@ -18,25 +17,39 @@ public SqlConstantExpression(ConstantExpression constantExpression, RelationalTy { _constantExpression = constantExpression; } - #endregion - #region Public Properties public object Value => _constantExpression.Value; - #endregion - #region Expression-based methods public SqlExpression ApplyTypeMapping(RelationalTypeMapping typeMapping) - { - return new SqlConstantExpression(_constantExpression, typeMapping); - } + => new SqlConstantExpression(_constantExpression, typeMapping); + protected override Expression VisitChildren(ExpressionVisitor visitor) => this; + public override void Print(ExpressionPrinter expressionPrinter) => Print(Value, expressionPrinter); - protected override Expression VisitChildren(ExpressionVisitor visitor) + private void Print( + object value, + ExpressionPrinter expressionPrinter) { - return this; + if (value is IEnumerable enumerable + && !(value is string)) + { + bool first = true; + foreach (var item in enumerable) + { + if (!first) + { + expressionPrinter.StringBuilder.Append(", "); + } + + first = false; + Print(item, expressionPrinter); + } + } + else + { + expressionPrinter.StringBuilder.Append(TypeMapping?.GenerateSqlLiteral(value) ?? Value?.ToString() ?? "NULL"); + } } - #endregion - #region Equality & HashCode public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -59,37 +72,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public override void Print(ExpressionPrinter expressionPrinter) - { - Print(Value, expressionPrinter); - } - - private void Print( - object value, - ExpressionPrinter expressionPrinter) - { - if (value is IEnumerable enumerable - && !(value is string)) - { - bool first = true; - foreach (var item in enumerable) - { - if (!first) - { - expressionPrinter.StringBuilder.Append(", "); - } - - first = false; - Print(item, expressionPrinter); - } - } - else - { - expressionPrinter.StringBuilder.Append(TypeMapping?.GenerateSqlLiteral(value) ?? Value?.ToString() ?? "NULL"); - } - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlExpression.cs index 98d9d4428c2..e7ae3bab292 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlExpression.cs @@ -11,29 +11,21 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public abstract class SqlExpression : Expression, IPrintable { - #region Fields & Constructors protected SqlExpression(Type type, RelationalTypeMapping typeMapping) { Type = type; TypeMapping = typeMapping; } - #endregion - #region Public Properties public override Type Type { get; } public RelationalTypeMapping TypeMapping { get; } - #endregion - #region Expression-based methods/properties protected override Expression VisitChildren(ExpressionVisitor visitor) - { - throw new InvalidOperationException("VisitChildren must be overriden in class deriving from SqlExpression"); - } + => throw new InvalidOperationException("VisitChildren must be overriden in class deriving from SqlExpression"); public override ExpressionType NodeType => ExpressionType.Extension; - #endregion + public abstract void Print(ExpressionPrinter expressionPrinter); - #region Equality & HashCode public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -54,9 +46,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public abstract void Print(ExpressionPrinter expressionPrinter); } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlFragmentExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlFragmentExpression.cs index de6f156a00d..714cde9acb4 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlFragmentExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlFragmentExpression.cs @@ -8,26 +8,17 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class SqlFragmentExpression : SqlExpression { - #region Fields & Constructors public SqlFragmentExpression(string sql) : base(typeof(string), null) { Sql = sql; } - #endregion - #region Public Properties public string Sql { get; } - #endregion - - #region Expression-based methods - protected override Expression VisitChildren(ExpressionVisitor visitor) - { - return this; - } - #endregion + protected override Expression VisitChildren(ExpressionVisitor visitor) => this; + public override void Print(ExpressionPrinter expressionPrinter) + => expressionPrinter.StringBuilder.Append(Sql); - #region Equality & HashCode public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -48,12 +39,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public override void Print(ExpressionPrinter expressionPrinter) - { - expressionPrinter.StringBuilder.Append(Sql); - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlFunctionExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlFunctionExpression.cs index 3bdce32f63b..cc21ba9d636 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlFunctionExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlFunctionExpression.cs @@ -12,7 +12,6 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class SqlFunctionExpression : SqlExpression { - #region Fields & Constructors public SqlFunctionExpression( string functionName, bool niladic, @@ -87,17 +86,13 @@ private SqlFunctionExpression( IsNiladic = niladic; Arguments = (arguments ?? Array.Empty()).ToList(); } - #endregion - #region Public Properties public string FunctionName { get; } public string Schema { get; } public bool IsNiladic { get; } public IReadOnlyList Arguments { get; } public Expression Instance { get; } - #endregion - #region Expression-based methods protected override Expression VisitChildren(ExpressionVisitor visitor) { var changed = false; @@ -123,8 +118,7 @@ protected override Expression VisitChildren(ExpressionVisitor visitor) } public SqlFunctionExpression ApplyTypeMapping(RelationalTypeMapping typeMapping) - { - return new SqlFunctionExpression( + => new SqlFunctionExpression( Instance, Schema, FunctionName, @@ -132,17 +126,40 @@ public SqlFunctionExpression ApplyTypeMapping(RelationalTypeMapping typeMapping) Arguments, Type, typeMapping ?? TypeMapping); - } public SqlFunctionExpression Update(SqlExpression instance, IReadOnlyList arguments) - { - return instance != Instance || !arguments.SequenceEqual(Arguments) + => instance != Instance || !arguments.SequenceEqual(Arguments) ? new SqlFunctionExpression(instance, Schema, FunctionName, IsNiladic, arguments, Type, TypeMapping) : this; + + public override void Print(ExpressionPrinter expressionPrinter) + { + if (!string.IsNullOrEmpty(Schema)) + { + expressionPrinter.StringBuilder + .Append(Schema) + .Append(".") + .Append(FunctionName); + } + else + { + if (Instance != null) + { + expressionPrinter.Visit(Instance); + expressionPrinter.StringBuilder.Append("."); + } + + expressionPrinter.StringBuilder.Append(FunctionName); + } + + if (!IsNiladic) + { + expressionPrinter.StringBuilder.Append("("); + expressionPrinter.VisitList(Arguments); + expressionPrinter.StringBuilder.Append(")"); + } } - #endregion - #region Equality & HashCode public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -172,35 +189,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public override void Print(ExpressionPrinter expressionPrinter) - { - if (!string.IsNullOrEmpty(Schema)) - { - expressionPrinter.StringBuilder - .Append(Schema) - .Append(".") - .Append(FunctionName); - } - else - { - if (Instance != null) - { - expressionPrinter.Visit(Instance); - expressionPrinter.StringBuilder.Append("."); - } - - expressionPrinter.StringBuilder.Append(FunctionName); - } - - if (!IsNiladic) - { - expressionPrinter.StringBuilder.Append("("); - expressionPrinter.VisitList(Arguments); - expressionPrinter.StringBuilder.Append(")"); - } - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlParameterExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlParameterExpression.cs index cc85ffe2940..9e9b7cdbfa9 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlParameterExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlParameterExpression.cs @@ -9,7 +9,6 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class SqlParameterExpression : SqlExpression { - #region Fields & Constructors private readonly ParameterExpression _parameterExpression; internal SqlParameterExpression(ParameterExpression parameterExpression, RelationalTypeMapping typeMapping) @@ -17,25 +16,15 @@ internal SqlParameterExpression(ParameterExpression parameterExpression, Relatio { _parameterExpression = parameterExpression; } - #endregion - #region Public Properties public string Name => _parameterExpression.Name; - #endregion - #region Expression-based methods public SqlExpression ApplyTypeMapping(RelationalTypeMapping typeMapping) - { - return new SqlParameterExpression(_parameterExpression, typeMapping); - } - - protected override Expression VisitChildren(ExpressionVisitor visitor) - { - return this; - } - #endregion + => new SqlParameterExpression(_parameterExpression, typeMapping); + protected override Expression VisitChildren(ExpressionVisitor visitor) => this; + public override void Print(ExpressionPrinter expressionPrinter) + => expressionPrinter.StringBuilder.Append("@" + _parameterExpression.Name); - #region Equality & HashCode public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -56,12 +45,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public override void Print(ExpressionPrinter expressionPrinter) - { - expressionPrinter.StringBuilder.Append("@" + _parameterExpression.Name); - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlUnaryExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlUnaryExpression.cs index 523c1fb90db..728baa2ae4a 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlUnaryExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SqlUnaryExpression.cs @@ -12,7 +12,6 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class SqlUnaryExpression : SqlExpression { - #region Fields & Constructors private static ISet _allowedOperators = new HashSet { ExpressionType.Equal, @@ -21,6 +20,10 @@ public class SqlUnaryExpression : SqlExpression ExpressionType.Not, ExpressionType.Negate, }; + private static ExpressionType VerifyOperator(ExpressionType operatorType) + => _allowedOperators.Contains(operatorType) + ? operatorType + : throw new InvalidOperationException("Unsupported Unary operator type specified."); public SqlUnaryExpression( ExpressionType operatorType, @@ -34,38 +37,36 @@ public SqlUnaryExpression( Operand = operand; } - private static ExpressionType VerifyOperator(ExpressionType operatorType) - { - return _allowedOperators.Contains(operatorType) - ? operatorType - : throw new InvalidOperationException("Unsupported Unary operator type specified."); - - } - #endregion - - #region Public Properties - public ExpressionType OperatorType { get; } public SqlExpression Operand { get; } - #endregion - #region Expression-based methods protected override Expression VisitChildren(ExpressionVisitor visitor) - { - var operand = (SqlExpression)visitor.Visit(Operand); - - return Update(operand); - } + => Update((SqlExpression)visitor.Visit(Operand)); public SqlUnaryExpression Update(SqlExpression operand) - { - return operand != Operand + => operand != Operand ? new SqlUnaryExpression(OperatorType, operand, Type, TypeMapping) : this; - } - #endregion - #region Equality & HashCode + public override void Print(ExpressionPrinter expressionPrinter) + { + if (OperatorType == ExpressionType.Convert) + { + expressionPrinter.StringBuilder.Append("CAST("); + expressionPrinter.Visit(Operand); + expressionPrinter.StringBuilder.Append(")"); + expressionPrinter.StringBuilder.Append(" AS "); + expressionPrinter.StringBuilder.Append(TypeMapping.StoreType); + expressionPrinter.StringBuilder.Append(")"); + } + else + { + expressionPrinter.StringBuilder.Append(OperatorType); + expressionPrinter.StringBuilder.Append("("); + expressionPrinter.Visit(Operand); + expressionPrinter.StringBuilder.Append(")"); + } + } public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -88,27 +89,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public override void Print(ExpressionPrinter expressionPrinter) - { - if (OperatorType == ExpressionType.Convert) - { - expressionPrinter.StringBuilder.Append("CAST("); - expressionPrinter.Visit(Operand); - expressionPrinter.StringBuilder.Append(")"); - expressionPrinter.StringBuilder.Append(" AS "); - expressionPrinter.StringBuilder.Append(TypeMapping.StoreType); - expressionPrinter.StringBuilder.Append(")"); - } - else - { - expressionPrinter.StringBuilder.Append(OperatorType); - expressionPrinter.StringBuilder.Append("("); - expressionPrinter.Visit(Operand); - expressionPrinter.StringBuilder.Append(")"); - } - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SubSelectExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SubSelectExpression.cs index b31c2c9beb4..4733e650d15 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SubSelectExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SubSelectExpression.cs @@ -9,7 +9,6 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class SubSelectExpression : SqlExpression { - #region Fields & Constructors public SubSelectExpression(SelectExpression subquery) : base(Verify(subquery).Projection[0].Type, subquery.Projection[0].Expression.TypeMapping) { @@ -24,29 +23,26 @@ private static SelectExpression Verify(SelectExpression selectExpression) return selectExpression; } - #endregion - #region Public Properties public SelectExpression Subquery { get; } - #endregion - #region Expression-based methods protected override Expression VisitChildren(ExpressionVisitor visitor) - { - var subquery = (SelectExpression)visitor.Visit(Subquery); - - return Update(subquery); - } + => Update((SelectExpression)visitor.Visit(Subquery)); public SubSelectExpression Update(SelectExpression subquery) - { - return subquery != Subquery + => subquery != Subquery ? new SubSelectExpression(subquery) : this; - } - #endregion - #region Equality & HashCode + public override void Print(ExpressionPrinter expressionPrinter) + { + expressionPrinter.StringBuilder.Append("("); + using (expressionPrinter.StringBuilder.Indent()) + { + expressionPrinter.Visit(Subquery); + } + expressionPrinter.StringBuilder.Append(")"); + } public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -67,17 +63,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public override void Print(ExpressionPrinter expressionPrinter) - { - expressionPrinter.StringBuilder.Append("("); - using (expressionPrinter.StringBuilder.Indent()) - { - expressionPrinter.Visit(Subquery); - } - expressionPrinter.StringBuilder.Append(")"); - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/TableExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/TableExpression.cs index 35a73db2050..40de31805cb 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/TableExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/TableExpression.cs @@ -7,32 +7,34 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public class TableExpression : TableExpressionBase { - #region Fields & Constructors - public TableExpression(string table, string schema, string alias) + internal TableExpression(string table, string schema, string alias) : base(alias) { Table = table; Schema = schema; } - #endregion - #region Public Properties + public override void Print(ExpressionPrinter expressionPrinter) + { + if (!string.IsNullOrEmpty(Schema)) + { + expressionPrinter.StringBuilder + .Append(Schema) + .Append("."); + } + + expressionPrinter.StringBuilder + .Append(Table) + .Append(" AS ") + .Append(Alias); + } + public string Table { get; } public string Schema { get; } - #endregion - - #region Equality & HashCode public override bool Equals(object obj) - => obj != null - && (ReferenceEquals(this, obj) - || obj is TableExpression tableExpression - && Equals(tableExpression)); - - private bool Equals(TableExpression tableExpression) - => base.Equals(tableExpression) - && string.Equals(Table, tableExpression.Table) - && string.Equals(Schema, tableExpression.Schema); + // This should be reference equal only. + => obj != null && ReferenceEquals(this, obj); public override int GetHashCode() { @@ -45,21 +47,5 @@ public override int GetHashCode() return hashCode; } } - #endregion - - public override void Print(ExpressionPrinter expressionPrinter) - { - if (!string.IsNullOrEmpty(Schema)) - { - expressionPrinter.StringBuilder - .Append(Schema) - .Append("."); - } - - expressionPrinter.StringBuilder - .Append(Table) - .Append(" AS ") - .Append(Alias); - } } } diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/TableExpressionBase.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/TableExpressionBase.cs index a4a4b9fd99c..2580d14d973 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/TableExpressionBase.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/TableExpressionBase.cs @@ -10,28 +10,15 @@ namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions { public abstract class TableExpressionBase : Expression, IPrintable { - #region Fields & Constructors - protected TableExpressionBase(string alias) - { - Alias = alias; - } - #endregion + protected TableExpressionBase(string alias) => Alias = alias; - #region Public Properties public string Alias { get; internal set; } - #endregion - #region Expression-based methods/properties - protected override Expression VisitChildren(ExpressionVisitor visitor) - { - return this; - } + protected override Expression VisitChildren(ExpressionVisitor visitor) => this; public override Type Type => typeof(object); public override ExpressionType NodeType => ExpressionType.Extension; - #endregion - - #region Equality & HashCode + public abstract void Print(ExpressionPrinter expressionPrinter); public override bool Equals(object obj) => obj != null && (ReferenceEquals(this, obj) @@ -50,9 +37,5 @@ public override int GetHashCode() return hashCode; } } - - #endregion - - public abstract void Print(ExpressionPrinter expressionPrinter); } }