Skip to content

Commit

Permalink
DRY some extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Jan 22, 2024
1 parent 3b98401 commit 9776d35
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 34 deletions.
20 changes: 8 additions & 12 deletions src/EFCore.InMemory/Query/Internal/InMemoryQueryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public virtual void ReplaceProjection(IReadOnlyDictionary<ProjectionMember, Expr
EntityProjectionExpression AddEntityProjection(EntityProjectionExpression entityProjectionExpression)
{
var readExpressionMap = new Dictionary<IProperty, MethodCallExpression>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
foreach (var property in entityProjectionExpression.EntityType.GetAllPropertiesInHierarchy())
{
var expression = entityProjectionExpression.BindProperty(property);
selectorExpressions.Add(expression);
Expand Down Expand Up @@ -266,7 +266,7 @@ public virtual void ApplyProjection()
case EntityProjectionExpression entityProjectionExpression:
{
var indexMap = new Dictionary<IProperty, int>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
foreach (var property in entityProjectionExpression.EntityType.GetAllPropertiesInHierarchy())
{
selectorExpressions.Add(entityProjectionExpression.BindProperty(property));
indexMap[property] = selectorExpressions.Count - 1;
Expand Down Expand Up @@ -307,7 +307,7 @@ public virtual void ApplyProjection()
if (expression is EntityProjectionExpression entityProjectionExpression)
{
var indexMap = new Dictionary<IProperty, int>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
foreach (var property in entityProjectionExpression.EntityType.GetAllPropertiesInHierarchy())
{
selectorExpressions.Add(entityProjectionExpression.BindProperty(property));
indexMap[property] = selectorExpressions.Count - 1;
Expand Down Expand Up @@ -384,7 +384,7 @@ public virtual void ApplySetOperation(MethodInfo setOperationMethodInfo, InMemor
&& value2 is EntityProjectionExpression entityProjection2)
{
var map = new Dictionary<IProperty, MethodCallExpression>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjection1.EntityType))
foreach (var property in entityProjection1.EntityType.GetAllPropertiesInHierarchy())
{
var expressionToAdd1 = entityProjection1.BindProperty(property);
var expressionToAdd2 = entityProjection2.BindProperty(property);
Expand Down Expand Up @@ -674,7 +674,7 @@ public virtual StructuralTypeShaperExpression AddNavigationToWeakEntityType(
var outerIndex = selectorExpressions.Count;
var innerEntityProjection = (EntityProjectionExpression)innerQueryExpression._projectionMapping[new ProjectionMember()];
var innerReadExpressionMap = new Dictionary<IProperty, MethodCallExpression>();
foreach (var property in GetAllPropertiesInHierarchy(innerEntityProjection.EntityType))
foreach (var property in innerEntityProjection.EntityType.GetAllPropertiesInHierarchy())
{
var propertyExpression = innerEntityProjection.BindProperty(property);
propertyExpression = MakeReadValueNullable(propertyExpression);
Expand Down Expand Up @@ -874,7 +874,7 @@ private static Expression GetGroupingKey(Expression key, List<Expression> groupi
(EntityProjectionExpression)((InMemoryQueryExpression)projectionBindingExpression.QueryExpression)
.GetProjection(projectionBindingExpression);
var readExpressions = new Dictionary<IProperty, MethodCallExpression>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
foreach (var property in entityProjectionExpression.EntityType.GetAllPropertiesInHierarchy())
{
readExpressions[property] = (MethodCallExpression)GetGroupingKey(
entityProjectionExpression.BindProperty(property),
Expand Down Expand Up @@ -1156,10 +1156,6 @@ private void ConvertToEnumerable()
private MethodCallExpression CreateReadValueExpression(Type type, int index, IPropertyBase? property)
=> (MethodCallExpression)_valueBufferParameter.CreateValueBufferReadValueExpression(type, index, property);

private static IEnumerable<IProperty> GetAllPropertiesInHierarchy(IEntityType entityType)
=> entityType.GetAllBaseTypes().Concat(entityType.GetDerivedTypesInclusive())
.SelectMany(t => t.GetDeclaredProperties());

private static IPropertyBase? InferPropertyFromInner(Expression expression)
=> expression is MethodCallExpression { Method.IsGenericMethod: true } methodCallExpression
&& methodCallExpression.Method.GetGenericMethodDefinition() == ExpressionExtensions.ValueBufferTryReadValueMethod
Expand All @@ -1169,7 +1165,7 @@ private static IEnumerable<IProperty> GetAllPropertiesInHierarchy(IEntityType en
private static EntityProjectionExpression MakeEntityProjectionNullable(EntityProjectionExpression entityProjectionExpression)
{
var readExpressionMap = new Dictionary<IProperty, MethodCallExpression>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
foreach (var property in entityProjectionExpression.EntityType.GetAllPropertiesInHierarchy())
{
readExpressionMap[property] = MakeReadValueNullable(entityProjectionExpression.BindProperty(property));
}
Expand Down Expand Up @@ -1277,7 +1273,7 @@ private EntityProjectionExpression TraverseEntityProjection(
bool makeNullable)
{
var readExpressionMap = new Dictionary<IProperty, MethodCallExpression>();
foreach (var property in GetAllPropertiesInHierarchy(entityProjectionExpression.EntityType))
foreach (var property in entityProjectionExpression.EntityType.GetAllPropertiesInHierarchy())
{
var expression = entityProjectionExpression.BindProperty(property);
if (makeNullable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpr
var columnInfos = new List<SqlServerOpenJsonExpression.ColumnInfo>();

// We're only interested in properties which actually exist in the JSON, filter out uninteresting shadow keys
foreach (var property in GetAllPropertiesInHierarchy(jsonQueryExpression.EntityType))
foreach (var property in jsonQueryExpression.EntityType.GetAllPropertiesInHierarchy())
{
if (property.GetJsonPropertyName() is string jsonPropertyName)
{
Expand All @@ -351,7 +351,7 @@ protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpr
}
}

foreach (var navigation in GetAllNavigationsInHierarchy(jsonQueryExpression.EntityType)
foreach (var navigation in jsonQueryExpression.EntityType.GetAllNavigationsInHierarchy()
.Where(
n => n.ForeignKey.IsOwnership
&& n.TargetEntityType.IsMappedToJson()
Expand Down Expand Up @@ -405,15 +405,6 @@ protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpr
new ProjectionMember(),
typeof(ValueBuffer)),
false));

// TODO: Move these to IEntityType?
static IEnumerable<IProperty> GetAllPropertiesInHierarchy(IEntityType entityType)
=> entityType.GetAllBaseTypes().Concat(entityType.GetDerivedTypesInclusive())
.SelectMany(t => t.GetDeclaredProperties());

static IEnumerable<INavigation> GetAllNavigationsInHierarchy(IEntityType entityType)
=> entityType.GetAllBaseTypes().Concat(entityType.GetDerivedTypesInclusive())
.SelectMany(t => t.GetDeclaredNavigations());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpr
// (SELECT value ->> 'a' AS a, value ->> 'b' AS b FROM json_each(c."JsonColumn", '$.Something.SomeCollection')

// We're only interested in properties which actually exist in the JSON, filter out uninteresting shadow keys
foreach (var property in GetAllPropertiesInHierarchy(entityType))
foreach (var property in entityType.GetAllPropertiesInHierarchy())
{
if (property.GetJsonPropertyName() is string jsonPropertyName)
{
Expand All @@ -369,7 +369,7 @@ protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpr
}
}

foreach (var navigation in GetAllNavigationsInHierarchy(jsonQueryExpression.EntityType)
foreach (var navigation in jsonQueryExpression.EntityType.GetAllNavigationsInHierarchy()
.Where(
n => n.ForeignKey.IsOwnership
&& n.TargetEntityType.IsMappedToJson()
Expand Down Expand Up @@ -426,15 +426,6 @@ protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpr
new ProjectionMember(),
typeof(ValueBuffer)),
false));

// TODO: Move these to IEntityType?
static IEnumerable<IProperty> GetAllPropertiesInHierarchy(IEntityType entityType)
=> entityType.GetAllBaseTypes().Concat(entityType.GetDerivedTypesInclusive())
.SelectMany(t => t.GetDeclaredProperties());

static IEnumerable<INavigation> GetAllNavigationsInHierarchy(IEntityType entityType)
=> entityType.GetAllBaseTypes().Concat(entityType.GetDerivedTypesInclusive())
.SelectMany(t => t.GetDeclaredNavigations());
}

/// <summary>
Expand Down

0 comments on commit 9776d35

Please sign in to comment.