Skip to content

Commit

Permalink
Compiled query for 3.0
Browse files Browse the repository at this point in the history
Fixes #14551
  • Loading branch information
ajcvickers committed Jul 24, 2019
1 parent f40ddff commit a60392c
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/EFCore/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ public virtual DbSet<TEntity> Set<TEntity>()
/// <typeparam name="TQuery"> The type of query for which a DbQuery should be returned. </typeparam>
/// <returns> A DbQuery for the given keyless entity type. </returns>
[Obsolete("Use Set() for entity types without keys")]
public virtual IQueryable<TQuery> Query<TQuery>()
public virtual DbQuery<TQuery> Query<TQuery>()
where TQuery : class
=> Set<TQuery>().AsNoTracking();
=> (DbQuery<TQuery>)((IDbSetCache)this).GetOrAddSet(DbContextDependencies.SetSource, typeof(TQuery));

private IEntityFinder Finder(Type type)
{
Expand Down
16 changes: 14 additions & 2 deletions src/EFCore/Internal/InternalDbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ namespace Microsoft.EntityFrameworkCore.Internal
/// </summary>
public class InternalDbSet<TEntity> :
#pragma warning disable CS0618 // Type or member is obsolete
DbQuery<TEntity>, IQueryable<TEntity>, IAsyncEnumerable<TEntity>, IInfrastructure<IServiceProvider>, IResettableService
DbQuery<TEntity>,
#pragma warning restore CS0618 // Type or member is obsolete
IQueryable<TEntity>, IAsyncEnumerable<TEntity>, IInfrastructure<IServiceProvider>, IResettableService
where TEntity : class
{
private readonly DbContext _context;
Expand Down Expand Up @@ -110,7 +111,18 @@ private EntityQueryable<TEntity> EntityQueryable
}

private EntityQueryable<TEntity> CreateEntityQueryable()
=> new EntityQueryable<TEntity>(_context.GetDependencies().QueryProvider);
{
var queryable = new EntityQueryable<TEntity>(_context.GetDependencies().QueryProvider);

#pragma warning disable 618
if (_entityType.FindPrimaryKey() == null)
#pragma warning restore 618
{
queryable = (EntityQueryable<TEntity>)queryable.AsNoTracking();
}

return queryable;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
3 changes: 1 addition & 2 deletions src/EFCore/Query/Internal/CompiledAsyncTaskQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public virtual Task<TResult> ExecuteAsync<TParam1, TParam2, TParam3, TParam4, TP
/// </summary>
protected override Func<QueryContext, Task<TResult>> CreateCompiledQuery(
IQueryCompiler queryCompiler, Expression expression)
=> throw new NotImplementedException();
//queryCompiler.CreateCompiledAsyncSingletonQuery<TResult>(expression);
=> queryCompiler.CreateCompiledAsyncQuery<Task<TResult>>(expression);
}
}
6 changes: 2 additions & 4 deletions src/EFCore/Query/Internal/QueryCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ public virtual Func<QueryContext, TResult> CreateCompiledQuery<TResult>(Expressi

query = ExtractParameters(query, _queryContextFactory.Create(), _logger, parameterize: false);

//return CompileQueryCore<TResult>(query, _model, _queryModelGenerator, _database, _logger, _contextType);
throw new NotImplementedException();
return CompileQueryCore<TResult>(_database, query, _model, false);
}

public virtual TResult ExecuteAsync<TResult>(Expression query, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -154,8 +153,7 @@ public virtual Func<QueryContext, TResult> CreateCompiledAsyncQuery<TResult>(Exp

query = ExtractParameters(query, _queryContextFactory.Create(), _logger, parameterize: false);

//return CompileAsyncQueryCore<TResult>(query, _queryModelGenerator, _database);
throw new NotImplementedException();
return CompileQueryCore<TResult>(_database, query, _model, true);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class InMemoryComplianceTest : ComplianceTestBase
typeof(ConcurrencyDetectorTestBase<>),
typeof(AsNoTrackingTestBase<>),
typeof(AsTrackingTestBase<>),
typeof(CompiledQueryTestBase<>),
typeof(ComplexNavigationsQueryTestBase<>),
typeof(GearsOfWarQueryTestBase<>),
typeof(IncludeAsyncTestBase<>),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@

namespace Microsoft.EntityFrameworkCore.Query
{
internal class CompiledQueryInMemoryTest : CompiledQueryTestBase<NorthwindQueryInMemoryFixture<NoopModelCustomizer>>
public class CompiledQueryInMemoryTest : CompiledQueryTestBase<NorthwindQueryInMemoryFixture<NoopModelCustomizer>>
{
public CompiledQueryInMemoryTest(NorthwindQueryInMemoryFixture<NoopModelCustomizer> fixture)
: base(fixture)
{
}

[ConditionalFact(Skip = "See issue#15318")]
public override void Query_ending_with_include()
{
base.Query_ending_with_include();
}

[ConditionalFact(Skip = "See issue#15318")]
public override void Query_with_single_parameter_with_include()
{
base.Query_ending_with_include();
}

[ConditionalFact(Skip = "See issue#13857")]
public override void DbQuery_query()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Microsoft.EntityFrameworkCore.Query
{
internal class CompiledQuerySqlServerTest : CompiledQueryTestBase<NorthwindQuerySqlServerFixture<NoopModelCustomizer>>
public class CompiledQuerySqlServerTest : CompiledQueryTestBase<NorthwindQuerySqlServerFixture<NoopModelCustomizer>>
{
public CompiledQuerySqlServerTest(NorthwindQuerySqlServerFixture<NoopModelCustomizer> fixture, ITestOutputHelper testOutputHelper)
: base(fixture)
Expand Down Expand Up @@ -42,29 +42,10 @@ public override void Query_ending_with_include()
base.Query_ending_with_include();

AssertSql(
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
ORDER BY [c].[CustomerID]",
//
@"SELECT [c.Orders].[OrderID], [c.Orders].[CustomerID], [c.Orders].[EmployeeID], [c.Orders].[OrderDate]
FROM [Orders] AS [c.Orders]
INNER JOIN (
SELECT [c0].[CustomerID]
FROM [Customers] AS [c0]
) AS [t] ON [c.Orders].[CustomerID] = [t].[CustomerID]
ORDER BY [t].[CustomerID]",
//
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Customers] AS [c]
ORDER BY [c].[CustomerID]",
//
@"SELECT [c.Orders].[OrderID], [c.Orders].[CustomerID], [c.Orders].[EmployeeID], [c.Orders].[OrderDate]
FROM [Orders] AS [c.Orders]
INNER JOIN (
SELECT [c0].[CustomerID]
FROM [Customers] AS [c0]
) AS [t] ON [c.Orders].[CustomerID] = [t].[CustomerID]
ORDER BY [t].[CustomerID]");
LEFT JOIN [Orders] AS [o] ON [c].[CustomerID] = [o].[CustomerID]
ORDER BY [c].[CustomerID], [o].[OrderID]");
}

public override void Untyped_context()
Expand All @@ -88,13 +69,7 @@ public override void Query_with_single_parameter()
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] = @__customerID",
//
@"@__customerID='ANATR' (Size = 5)
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] = @__customerID");
WHERE ([c].[CustomerID] = @__customerID) AND @__customerID IS NOT NULL");
}

public override void First_query_with_single_parameter()
Expand All @@ -106,13 +81,7 @@ public override void First_query_with_single_parameter()
SELECT TOP(1) [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] = @__customerID",
//
@"@__customerID='ANATR' (Size = 5)
SELECT TOP(1) [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] = @__customerID");
WHERE ([c].[CustomerID] = @__customerID) AND @__customerID IS NOT NULL");
}

public override void Query_with_two_parameters()
Expand All @@ -124,13 +93,7 @@ public override void Query_with_two_parameters()
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] = @__customerID",
//
@"@__customerID='ANATR' (Size = 5)
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] = @__customerID");
WHERE ([c].[CustomerID] = @__customerID) AND @__customerID IS NOT NULL");
}

public override void Query_with_three_parameters()
Expand All @@ -142,13 +105,7 @@ public override void Query_with_three_parameters()
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] = @__customerID",
//
@"@__customerID='ANATR' (Size = 5)
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] = @__customerID");
WHERE ([c].[CustomerID] = @__customerID) AND @__customerID IS NOT NULL");
}

public override void Query_with_array_parameter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Microsoft.EntityFrameworkCore.Query
{
internal class CompiledQuerySqliteTest : CompiledQueryTestBase<NorthwindQuerySqliteFixture<NoopModelCustomizer>>
public class CompiledQuerySqliteTest : CompiledQueryTestBase<NorthwindQuerySqliteFixture<NoopModelCustomizer>>
{
public CompiledQuerySqliteTest(NorthwindQuerySqliteFixture<NoopModelCustomizer> fixture)
: base(fixture)
Expand Down

0 comments on commit a60392c

Please sign in to comment.