-
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: Add support for custom EntityQueryables
This makes look up for EntityQueryable in query tree bit more complex but it adds support for providers to add custom EntityQueryable to carry more metadata as needs to and allow rest of the pipeline to be transparent about it. Current FromSql has be to converted during compilation phase since, we want to parameterize the arguments array in the methodCall. If there is nothing to parameterize then providers can directly create custom Queryable. Resolves #16326
- Loading branch information
Showing
10 changed files
with
241 additions
and
14 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
46 changes: 46 additions & 0 deletions
46
src/EFCore.Relational/Query/Internal/FromSqlEntityQueryable.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,46 @@ | ||
// 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; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.Internal | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public class FromSqlEntityQueryable<TResult> : EntityQueryable<TResult> | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public FromSqlEntityQueryable(IAsyncQueryProvider queryProvider, string sql, Expression arguments) | ||
: base(queryProvider) | ||
{ | ||
Sql = sql; | ||
Arguments = arguments; | ||
Expression = Expression.Constant(this); | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public virtual string Sql { get; } | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public virtual Expression Arguments { get; } | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/EFCore.Relational/Query/Internal/FromSqlEntityQueryableInjectingExpressionVisitor.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,42 @@ | ||
// 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; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using JetBrains.Annotations; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.Internal | ||
{ | ||
public class FromSqlEntityQueryableInjectingExpressionVisitor : ExpressionVisitor | ||
{ | ||
protected override Expression VisitMethodCall(MethodCallExpression methodCallExpression) | ||
{ | ||
if (methodCallExpression.Method.DeclaringType == typeof(RelationalQueryableExtensions) | ||
&& methodCallExpression.Method.Name == nameof(RelationalQueryableExtensions.FromSqlOnQueryable)) | ||
{ | ||
var sql = (string)((ConstantExpression)methodCallExpression.Arguments[1]).Value; | ||
var queryable = (IQueryable)((ConstantExpression)methodCallExpression.Arguments[0]).Value; | ||
|
||
return Expression.Constant( | ||
_createFromSqlEntityQueryableMethod | ||
.MakeGenericMethod(queryable.ElementType) | ||
.Invoke(null, new object[] { queryable.Provider, sql, methodCallExpression.Arguments[2] })); | ||
} | ||
|
||
return base.VisitMethodCall(methodCallExpression); | ||
} | ||
|
||
private static readonly MethodInfo _createFromSqlEntityQueryableMethod | ||
= typeof(FromSqlEntityQueryableInjectingExpressionVisitor) | ||
.GetTypeInfo().GetDeclaredMethod(nameof(_CreateFromSqlEntityQueryable)); | ||
|
||
[UsedImplicitly] | ||
// ReSharper disable once InconsistentNaming | ||
private static FromSqlEntityQueryable<TResult> _CreateFromSqlEntityQueryable<TResult>( | ||
IAsyncQueryProvider entityQueryProvider, | ||
string sql, | ||
Expression arguments) | ||
=> new FromSqlEntityQueryable<TResult>(entityQueryProvider, sql, arguments); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/EFCore.Relational/Query/Internal/RelationalQueryOptimizerFactory.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,27 @@ | ||
// 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. | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.Internal | ||
{ | ||
public class RelationalQueryOptimizerFactory : IQueryOptimizerFactory | ||
{ | ||
private readonly QueryOptimizerDependencies _dependencies; | ||
private readonly RelationalQueryOptimizerDependencies _relationalDependencies; | ||
|
||
public RelationalQueryOptimizerFactory( | ||
QueryOptimizerDependencies dependencies, | ||
RelationalQueryOptimizerDependencies relationalDependencies) | ||
{ | ||
_dependencies = dependencies; | ||
_relationalDependencies = relationalDependencies; | ||
} | ||
|
||
public QueryOptimizer Create(QueryCompilationContext queryCompilationContext) | ||
{ | ||
return new RelationalQueryOptimizer( | ||
_dependencies, | ||
_relationalDependencies, | ||
queryCompilationContext); | ||
} | ||
} | ||
} |
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,29 @@ | ||
// 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.Query | ||
{ | ||
public class RelationalQueryOptimizer : QueryOptimizer | ||
{ | ||
public RelationalQueryOptimizer( | ||
QueryOptimizerDependencies dependencies, | ||
RelationalQueryOptimizerDependencies relationalDependencies, | ||
QueryCompilationContext queryCompilationContext) | ||
: base(dependencies, queryCompilationContext) | ||
{ | ||
RelationalDependencies = relationalDependencies; | ||
} | ||
|
||
protected virtual RelationalQueryOptimizerDependencies RelationalDependencies { get; } | ||
|
||
public override Expression Visit(Expression query) | ||
{ | ||
query = new FromSqlEntityQueryableInjectingExpressionVisitor().Visit(query); | ||
|
||
return base.Visit(query); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/EFCore.Relational/Query/RelationalQueryOptimizerDependencies.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,58 @@ | ||
// 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 Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// Service dependencies parameter class for <see cref="RelationalShapedQueryOptimizer" /> | ||
/// </para> | ||
/// <para> | ||
/// This type is typically used by database providers (and other extensions). It is generally | ||
/// not used in application code. | ||
/// </para> | ||
/// <para> | ||
/// Do not construct instances of this class directly from either provider or application code as the | ||
/// constructor signature may change as new dependencies are added. Instead, use this type in | ||
/// your constructor so that an instance will be created and injected automatically by the | ||
/// dependency injection container. To create an instance with some dependent services replaced, | ||
/// first resolve the object from the dependency injection container, then replace selected | ||
/// services using the 'With...' methods. Do not call the constructor at any point in this process. | ||
/// </para> | ||
/// <para> | ||
/// The service lifetime is <see cref="ServiceLifetime.Scoped"/>. This means that each | ||
/// <see cref="DbContext"/> instance will use its own instance of this service. | ||
/// The implementation may depend on other services registered with any lifetime. | ||
/// The implementation does not need to be thread-safe. | ||
/// </para> | ||
/// </summary> | ||
public sealed class RelationalQueryOptimizerDependencies | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// Creates the service dependencies parameter object for a <see cref="RelationalQueryOptimizer" />. | ||
/// </para> | ||
/// <para> | ||
/// Do not call this constructor directly from either provider or application code as it may change | ||
/// as new dependencies are added. Instead, use this type in your constructor so that an instance | ||
/// will be created and injected automatically by the dependency injection container. To create | ||
/// an instance with some dependent services replaced, first resolve the object from the dependency | ||
/// injection container, then replace selected services using the 'With...' methods. Do not call | ||
/// the constructor at any point in this process. | ||
/// </para> | ||
/// <para> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </para> | ||
/// </summary> | ||
[EntityFrameworkInternal] | ||
public RelationalQueryOptimizerDependencies() | ||
{ | ||
} | ||
} | ||
} |
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