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

Solution of Compute - Include - Unit test #105

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Detail of supported commands
============
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:15
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:45

This file documents what linq commands **DelegateDecompiler** supports when
working with [Entity Framework v6.1](http://msdn.microsoft.com/en-us/data/aa937723) (EF).
Expand All @@ -22,6 +22,12 @@ That will make it much easier to diagnose your issue.
More will appear as we move forward.*


### Group: Relationship
#### [Include](../TestGroup20Relationship/Test01Include.cs):
- **Not Supported**
* Include (line 42)


### Group: Basic Features
#### [Select](../TestGroup05BasicFeatures/Test01Select.cs):
- Supported
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Detail With Sql of supported commands
============
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:15
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:45

This file documents what linq commands **DelegateDecompiler** supports when
working with [Entity Framework v6.1](http://msdn.microsoft.com/en-us/data/aa937723) (EF).
Expand All @@ -22,6 +22,12 @@ That will make it much easier to diagnose your issue.
More will appear as we move forward.*


### Group: Relationship
#### [Include](../TestGroup20Relationship/Test01Include.cs):
- **Not Supported**
* Include (line 42)


### Group: Basic Features
#### [Select](../TestGroup05BasicFeatures/Test01Select.cs):
- Supported
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Summary of supported commands
============
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:15
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:45

This file documents what linq commands **DelegateDecompiler** supports when
working with [Entity Framework v6.1](http://msdn.microsoft.com/en-us/data/aa937723) (EF).
Expand All @@ -22,6 +22,10 @@ That will make it much easier to diagnose your issue.
More will appear as we move forward.*


### Group: Relationship
- **Not Supported**
* [Include](../TestGroup20Relationship/Test01Include.cs) (1 tests)

### Group: Basic Features
- Supported
* [Select](../TestGroup05BasicFeatures/Test01Select.cs) (8 tests)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Contributed by @JonPSmith (GitHub) www.thereformedprogrammer.com

using System.Linq;
using DelegateDecompiler.EntityFramework.Tests.Helpers;
using NUnit.Framework;
using DelegateDecompiler.EntityFramework.Tests.EfItems;
#if EF_CORE
using Microsoft.EntityFrameworkCore;
#else
using System.Data.Entity;
#endif

namespace DelegateDecompiler.EntityFramework.Tests.TestGroup05BasicFeatures
{
class Test01Include
{
private ClassEnvironment classEnv;

[OneTimeSetUp]
public void SetUpFixture()
{
classEnv = new ClassEnvironment();
}

[Computed]
private static bool ComputedSample() { return true; }

[Test]
public void TestInclude()
{
using (var db = new EfTestDbContext())
using (var env = new MethodEnvironment(classEnv))
{
//SETUP
var linq = db.EfParents.Where(p => true).Include(p => p.Children).ToList();

//ATTEMPT
env.AboutToUseDelegateDecompiler();
var dd = env.Db.EfParents.Where(p => ComputedSample()).Include(p => p.Children).Decompile().ToList();

//VERIFY
env.CompareAndLogList(linq, dd);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Detail of supported commands
============
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:15
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:45

This file documents what linq commands **DelegateDecompiler** supports when
working with [Entity Framework Core](https://docs.microsoft.com/en-us/ef/core/) (EF).
Expand All @@ -22,6 +22,12 @@ That will make it much easier to diagnose your issue.
More will appear as we move forward.*


### Group: Relationship
#### [Include](../TestGroup20Relationship/Test01Include.cs):
- **Not Supported**
* Include (line 42)


### Group: Basic Features
#### [Select](../TestGroup05BasicFeatures/Test01Select.cs):
- Supported
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Detail With Sql of supported commands
============
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:15
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:45

This file documents what linq commands **DelegateDecompiler** supports when
working with [Entity Framework Core](https://docs.microsoft.com/en-us/ef/core/) (EF).
Expand All @@ -22,6 +22,12 @@ That will make it much easier to diagnose your issue.
More will appear as we move forward.*


### Group: Relationship
#### [Include](../TestGroup20Relationship/Test01Include.cs):
- **Not Supported**
* Include (line 42)


### Group: Basic Features
#### [Select](../TestGroup05BasicFeatures/Test01Select.cs):
- Supported
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Summary of supported commands
============
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:15
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:45

This file documents what linq commands **DelegateDecompiler** supports when
working with [Entity Framework Core](https://docs.microsoft.com/en-us/ef/core/) (EF).
Expand All @@ -22,6 +22,10 @@ That will make it much easier to diagnose your issue.
More will appear as we move forward.*


### Group: Relationship
- **Not Supported**
* [Include](../TestGroup20Relationship/Test01Include.cs) (1 tests)

### Group: Basic Features
- Supported
* [Select](../TestGroup05BasicFeatures/Test01Select.cs) (10 tests)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Detail of supported commands
============
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:15
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:45

This file documents what linq commands **DelegateDecompiler** supports when
working with [Entity Framework Core](https://docs.microsoft.com/en-us/ef/core/) (EF).
Expand All @@ -22,6 +22,12 @@ That will make it much easier to diagnose your issue.
More will appear as we move forward.*


### Group: Relationship
#### [Include](../TestGroup20Relationship/Test01Include.cs):
- **Not Supported**
* Include (line 42)


### Group: Basic Features
#### [Select](../TestGroup05BasicFeatures/Test01Select.cs):
- Supported
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Detail With Sql of supported commands
============
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:15
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:45

This file documents what linq commands **DelegateDecompiler** supports when
working with [Entity Framework Core](https://docs.microsoft.com/en-us/ef/core/) (EF).
Expand All @@ -22,6 +22,12 @@ That will make it much easier to diagnose your issue.
More will appear as we move forward.*


### Group: Relationship
#### [Include](../TestGroup20Relationship/Test01Include.cs):
- **Not Supported**
* Include (line 42)


### Group: Basic Features
#### [Select](../TestGroup05BasicFeatures/Test01Select.cs):
- Supported
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Summary of supported commands
============
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:15
## Documentation produced for DelegateDecompiler, version 0.34.0.0 on Sunday, 05 May 2024 23:45

This file documents what linq commands **DelegateDecompiler** supports when
working with [Entity Framework Core](https://docs.microsoft.com/en-us/ef/core/) (EF).
Expand All @@ -22,6 +22,10 @@ That will make it much easier to diagnose your issue.
More will appear as we move forward.*


### Group: Relationship
- **Not Supported**
* [Include](../TestGroup20Relationship/Test01Include.cs) (1 tests)

### Group: Basic Features
- Supported
* [Select](../TestGroup05BasicFeatures/Test01Select.cs) (10 tests)
Expand Down
46 changes: 45 additions & 1 deletion src/DelegateDecompiler/DecompileExpressionVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand All @@ -12,6 +12,50 @@ public static Expression Decompile(Expression expression)
return new DecompileExpressionVisitor().Visit(expression);
}

private static readonly object NULL = new object(); // for use as a dictionary key
private readonly Dictionary<object, Expression> visitedConstants;

private bool hasAnyChanges = false;
public override Expression Visit(Expression node)
{
var result = base.Visit(node);
if (result != node)
hasAnyChanges = true;
return result;
}

private DecompileExpressionVisitor(Dictionary<object, Expression> sharedVisitedConstants = null)
{
this.visitedConstants = sharedVisitedConstants ?? new Dictionary<object, Expression>();
}

protected override Expression VisitConstant(ConstantExpression node)
{
Expression result;
if (visitedConstants.TryGetValue(node.Value ?? NULL, out result))
{
return result; // avoid infinite recursion
}

if (typeof(IQueryable).IsAssignableFrom(node.Type))
{
visitedConstants.Add(node.Value ?? NULL, node);

var value = (IQueryable)node.Value;
var childVisitor = new DecompileExpressionVisitor(visitedConstants);
result = childVisitor.Visit(value.Expression);

if (childVisitor.hasAnyChanges)
{
result = Expression.Constant(value.Provider.CreateQuery(result), node.Type);
visitedConstants[node.Value ?? NULL] = result;
return result;
}
}

return node;
}

protected override Expression VisitMember(MemberExpression node)
{
if (ShouldDecompile(node.Member) && node.Member is PropertyInfo property)
Expand Down