-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query: Convert FromSql methods to custom query roots
- Avoids any specific processing in core provider - Takes care of parameterization correctly - Also improved parameter extraction by creating parameter for any expression outside of lambda even when not a method call argument Part of #20146
- Loading branch information
Showing
10 changed files
with
153 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/EFCore.Relational/Query/Internal/FromSqlQueryRootExpression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// 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; | ||
using System.Linq.Expressions; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.Internal | ||
{ | ||
public class FromSqlQueryRootExpression : QueryRootExpression | ||
{ | ||
public FromSqlQueryRootExpression( | ||
[NotNull] IAsyncQueryProvider queryProvider, [NotNull] IEntityType entityType, [NotNull] string sql, [NotNull] Expression argument) | ||
: base(queryProvider, entityType) | ||
{ | ||
Check.NotEmpty(sql, nameof(sql)); | ||
Check.NotNull(argument, nameof(argument)); | ||
|
||
Sql = sql; | ||
Argument = argument; | ||
} | ||
|
||
public FromSqlQueryRootExpression( | ||
[NotNull] IEntityType entityType, [NotNull] string sql, [NotNull] Expression argument) | ||
: base(entityType) | ||
{ | ||
Check.NotEmpty(sql, nameof(sql)); | ||
Check.NotNull(argument, nameof(argument)); | ||
|
||
Sql = sql; | ||
Argument = argument; | ||
} | ||
|
||
public virtual string Sql { get; } | ||
public virtual Expression Argument { get; } | ||
|
||
public override Expression DetachQueryProvider() => new FromSqlQueryRootExpression(EntityType, Sql, Argument); | ||
|
||
protected override Expression VisitChildren(ExpressionVisitor visitor) | ||
{ | ||
var argument = visitor.Visit(Argument); | ||
|
||
return argument != Argument | ||
? new FromSqlQueryRootExpression(EntityType, Sql, argument) | ||
: this; | ||
} | ||
|
||
public override void Print(ExpressionPrinter expressionPrinter) | ||
{ | ||
base.Print(expressionPrinter); | ||
expressionPrinter.Append($".FromSql({Sql}, "); | ||
expressionPrinter.Visit(Argument); | ||
expressionPrinter.AppendLine(")"); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
=> obj != null | ||
&& (ReferenceEquals(this, obj) | ||
|| obj is FromSqlQueryRootExpression queryRootExpression | ||
&& Equals(queryRootExpression)); | ||
|
||
private bool Equals(FromSqlQueryRootExpression queryRootExpression) | ||
=> base.Equals(queryRootExpression) | ||
&& string.Equals(Sql, queryRootExpression.Sql, StringComparison.OrdinalIgnoreCase) | ||
&& ExpressionEqualityComparer.Instance.Equals(Argument, queryRootExpression.Argument); | ||
|
||
public override int GetHashCode() | ||
=> HashCode.Combine(base.GetHashCode(), Sql, ExpressionEqualityComparer.Instance.GetHashCode(Argument)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.