support for rewriting null conditional access expressions

This commit is contained in:
Koen Bekkenutte
2021-10-18 22:36:06 +08:00
parent 654dcba6ff
commit 86223a11c9
29 changed files with 682 additions and 11 deletions
@@ -11,8 +11,8 @@ namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMethods
[Projectable]
public static int Foo(this Entity entity) => entity.Id + 1;
[Projectable]
public static int Foo2(this Entity entity) => entity.Foo() + 1;
[Projectable]
public static int Foo2(this Entity entity) => entity.Foo() + 1;
[Projectable]
public static Entity? LeadingEntity(this Entity entity, DbContext dbContext)
@@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace EntityFrameworkCore.Projectables.FunctionalTests.NullConditionals
{
public record Entity
{
public int Id { get; set; }
public string? Name { get; set; }
public List<Entity>? RelatedEntities { get; set; }
}
}
@@ -0,0 +1,31 @@
#nullable disable
namespace EntityFrameworkCore.Projectables.FunctionalTests.NullConditionals
{
public static class EntityExtensions
{
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
public static string GetNameIgnoreNulls(this Entity entity)
=> entity?.Name;
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
public static int? GetNameLengthIgnoreNulls(this Entity entity)
=> entity.GetNameIgnoreNulls()?.Length;
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
public static Entity GetFirstRelatedIgnoreNulls(this Entity entity)
=> entity?.RelatedEntities?[0];
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
public static string GetNameRewriteNulls(this Entity entity)
=> entity?.Name;
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
public static int? GetNameLengthRewriteNulls(this Entity entity)
=> entity.GetNameIgnoreNulls()?.Length;
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
public static Entity GetFirstRelatedRewriteNulls(this Entity entity)
=> entity?.RelatedEntities?[0];
}
}
@@ -0,0 +1,2 @@
SELECT CAST(LEN([e].[Name]) AS int)
FROM [Entity] AS [e]
@@ -0,0 +1,4 @@
SELECT [e].[Id], [e0].[Id], [e0].[EntityId], [e0].[Name]
FROM [Entity] AS [e]
LEFT JOIN [Entity] AS [e0] ON [e].[Id] = [e0].[EntityId]
ORDER BY [e].[Id]
@@ -0,0 +1,48 @@
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
using Microsoft.EntityFrameworkCore;
using VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.FunctionalTests.NullConditionals
{
[UsesVerify]
public class IngoreNullConditionalRewriteTests
{
[Fact]
public Task SimpleMemberExpression()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.GetNameIgnoreNulls());
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task ComplexMemberExpression()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.GetNameLengthIgnoreNulls());
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task RelationalExpression()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.GetFirstRelatedIgnoreNulls());
return Verifier.Verify(query.ToQueryString());
}
}
}
@@ -0,0 +1,2 @@
SELECT CAST(LEN([e].[Name]) AS int)
FROM [Entity] AS [e]
@@ -0,0 +1,5 @@
SELECT CAST(1 AS bit), [e].[Id], [e0].[Id], [e0].[EntityId], [e0].[Name], [e1].[Id], [e1].[EntityId], [e1].[Name]
FROM [Entity] AS [e]
LEFT JOIN [Entity] AS [e0] ON [e].[Id] = [e0].[EntityId]
LEFT JOIN [Entity] AS [e1] ON [e].[Id] = [e1].[EntityId]
ORDER BY [e].[Id], [e0].[Id]
@@ -0,0 +1,2 @@
SELECT CAST(LEN([e].[Name]) AS int)
FROM [Entity] AS [e]
@@ -0,0 +1,48 @@
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
using Microsoft.EntityFrameworkCore;
using VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.FunctionalTests.NullConditionals
{
[UsesVerify]
public class RewriteNullConditionalRewriteTests
{
[Fact]
public Task SimpleMemberExpression()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.GetNameLengthRewriteNulls());
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task ComplexMemberExpression()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.GetNameLengthRewriteNulls());
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task RelationalExpression()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.GetFirstRelatedRewriteNulls());
return Verifier.Verify(query.ToQueryString());
}
}
}