Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: Add CompileQuery overloads taking upto 15 parameters #20621

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,106 changes: 1,060 additions & 46 deletions src/EFCore/EF.CompileAsyncQuery.cs

Large diffs are not rendered by default.

130 changes: 130 additions & 0 deletions src/EFCore/EF.CompileAsyncQuery.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
// 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.
// <auto-generated />

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.Internal;

namespace Microsoft.EntityFrameworkCore
{
public static partial class EF
{
/// <summary>
/// Creates a compiled query delegate that when invoked will execute the specified LINQ query.
/// </summary>
/// <typeparam name="TContext">The target DbContext type.</typeparam>
/// <typeparam name="TResult">The query result type.</typeparam>
/// <param name="queryExpression">The LINQ query expression.</param>
/// <returns>A delegate that can be invoked to execute the compiled query.</returns>
public static Func<TContext, IAsyncEnumerable<TResult>> CompileAsyncQuery<TContext, TResult>(
[NotNull] Expression<Func<TContext, DbSet<TResult>>> queryExpression)
where TContext : DbContext
where TResult : class
=> new CompiledAsyncEnumerableQuery<TContext, TResult>(queryExpression).Execute;
<#
var ordinal = new[] { "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth" };

var maxParameters = 15;

for (var parameterCount = 0; parameterCount <= maxParameters; parameterCount++)
{
var paramsList = string.Join("", Enumerable.Range(1, parameterCount).Select(e => "TParam" + e + ", "));
#>

/// <summary>
/// Creates a compiled query delegate that when invoked will execute the specified LINQ query.
/// </summary>
/// <typeparam name="TContext">The target DbContext type.</typeparam>
<# for (var i = 1; i <= parameterCount; i++) { #>
/// <typeparam name="TParam<#= i #>">The type of the <#= ordinal[i - 1] #> query parameter.</typeparam>
<# } #>
/// <typeparam name="TResult">The query result type.</typeparam>
/// <typeparam name="TProperty">The included property type.</typeparam>
/// <param name="queryExpression">The LINQ query expression.</param>
/// <returns>A delegate that can be invoked to execute the compiled query.</returns>
<# if (parameterCount <= 2 ) { #>
public static Func<TContext, <#= paramsList #>IAsyncEnumerable<TResult>> CompileAsyncQuery<TContext, <#= paramsList #>TResult, TProperty>(
<# } else { #>
public static Func<TContext, <#= paramsList #>IAsyncEnumerable<TResult>> CompileAsyncQuery<
TContext, <#= paramsList #>TResult, TProperty>(
<# } #>
[NotNull] Expression<Func<TContext, <#= paramsList #>IIncludableQueryable<TResult, TProperty>>> queryExpression)
where TContext : DbContext
=> new CompiledAsyncEnumerableQuery<TContext, TResult>(queryExpression).Execute;

/// <summary>
/// Creates a compiled query delegate that when invoked will execute the specified LINQ query.
/// </summary>
/// <typeparam name="TContext">The target DbContext type.</typeparam>
<# for (var i = 1; i <= parameterCount; i++) { #>
/// <typeparam name="TParam<#= i #>">The type of the <#= ordinal[i - 1] #> query parameter.</typeparam>
<# } #>
/// <typeparam name="TResult">The query result type.</typeparam>
/// <param name="queryExpression">The LINQ query expression.</param>
/// <returns>A delegate that can be invoked to execute the compiled query.</returns>
<# if (parameterCount <= 2 ) { #>
public static Func<TContext, <#= paramsList #>IAsyncEnumerable<TResult>> CompileAsyncQuery<TContext, <#= paramsList #>TResult>(
<# } else { #>
public static Func<TContext, <#= paramsList #>IAsyncEnumerable<TResult>> CompileAsyncQuery<
TContext, <#= paramsList #>TResult>(
<# } #>
[NotNull] Expression<Func<TContext, <#= paramsList #>IQueryable<TResult>>> queryExpression)
where TContext : DbContext
=> new CompiledAsyncEnumerableQuery<TContext, TResult>(queryExpression).Execute;

/// <summary>
/// Creates a compiled query delegate that when invoked will execute the specified LINQ query.
/// </summary>
/// <typeparam name="TContext">The target DbContext type.</typeparam>
<# for (var i = 1; i <= parameterCount; i++) { #>
/// <typeparam name="TParam<#= i #>">The type of the <#= ordinal[i - 1] #> query parameter.</typeparam>
<# } #>
/// <typeparam name="TResult">The query result type.</typeparam>
/// <param name="queryExpression">The LINQ query expression.</param>
/// <returns>A delegate that can be invoked to execute the compiled query.</returns>
<# if (parameterCount <= 3 ) { #>
public static Func<TContext, <#= paramsList #>Task<TResult>> CompileAsyncQuery<TContext, <#= paramsList #>TResult>(
<# } else { #>
public static Func<TContext, <#= paramsList #>Task<TResult>> CompileAsyncQuery<
TContext, <#= paramsList #>TResult>(
<# } #>
[NotNull] Expression<Func<TContext, <#= paramsList #>TResult>> queryExpression)
where TContext : DbContext
=> new CompiledAsyncTaskQuery<TContext, TResult>(queryExpression).ExecuteAsync;
<# if (parameterCount < 15) { #>

/// <summary>
/// Creates a compiled query delegate that when invoked will execute the specified LINQ query.
/// </summary>
/// <typeparam name="TContext">The target DbContext type.</typeparam>
<# for (var i = 1; i <= parameterCount; i++) { #>
/// <typeparam name="TParam<#= i #>">The type of the <#= ordinal[i - 1] #> query parameter.</typeparam>
<# } #>
/// <typeparam name="TResult">The query result type.</typeparam>
/// <param name="queryExpression">The LINQ query expression.</param>
/// <returns>A delegate that can be invoked to execute the compiled query.</returns>
<# if (parameterCount <= 3 ) { #>
public static Func<TContext, <#= paramsList #>CancellationToken, Task<TResult>> CompileAsyncQuery<TContext, <#= paramsList #>TResult>(
<# } else { #>
public static Func<TContext, <#= paramsList #>CancellationToken, Task<TResult>> CompileAsyncQuery<
TContext, <#= paramsList #>TResult>(
<# } #>
[NotNull] Expression<Func<TContext, <#= paramsList #>CancellationToken, TResult>> queryExpression)
where TContext : DbContext
=> new CompiledAsyncTaskQuery<TContext, TResult>(queryExpression).ExecuteAsync;
<# } #>
<#
}
#>
}
}
Loading