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 support for non entity query roots #28472

Merged
merged 1 commit into from
Jul 25, 2022
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
6 changes: 3 additions & 3 deletions src/EFCore.Cosmos/Extensions/CosmosQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ public static IQueryable<TEntity> FromSqlRaw<TEntity>(
Check.NotNull(parameters, nameof(parameters));

var queryableSource = (IQueryable)source;
var queryRootExpression = (QueryRootExpression)queryableSource.Expression;
var entityQueryRootExpression = (EntityQueryRootExpression)queryableSource.Expression;

var entityType = queryRootExpression.EntityType;
var entityType = entityQueryRootExpression.EntityType;

Check.DebugAssert(
(entityType.BaseType is null && !entityType.GetDirectlyDerivedTypes().Any())
|| entityType.FindDiscriminatorProperty() is not null,
"Found FromSql on a TPT entity type, but TPT isn't supported on Cosmos");

var fromSqlQueryRootExpression = new FromSqlQueryRootExpression(
queryRootExpression.QueryProvider!,
entityQueryRootExpression.QueryProvider!,
entityType,
sql,
Expression.Constant(parameters));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public override Expression Visit(Expression expression)
&& methodCallExpression.Method.IsGenericMethod
&& queryRootMethodCallExpression.Method.GetGenericMethodDefinition() == QueryableMethods.Where)
{
if (queryRootMethodCallExpression.Arguments[0] is QueryRootExpression queryRootExpression)
if (queryRootMethodCallExpression.Arguments[0] is EntityQueryRootExpression entityQueryRootExpression)
{
var entityType = queryRootExpression.EntityType;
var entityType = entityQueryRootExpression.EntityType;

if (queryRootMethodCallExpression.Arguments[1] is UnaryExpression unaryExpression
&& unaryExpression.Operand is LambdaExpression lambdaExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class FromSqlQueryRootExpression : QueryRootExpression
public class FromSqlQueryRootExpression : EntityQueryRootExpression
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -75,7 +75,7 @@ public override Expression DetachQueryProvider()
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override QueryRootExpression UpdateEntityType(IEntityType entityType)
public override EntityQueryRootExpression UpdateEntityType(IEntityType entityType)
=> entityType.ClrType != EntityType.ClrType
|| entityType.Name != EntityType.Name
? throw new InvalidOperationException(CoreStrings.QueryRootDifferentEntityType(entityType.DisplayName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Conventions;

/// <summary>
/// Convention that converts accesses of <see cref="DbSet{TEntity}" /> inside query filters and defining queries into
/// <see cref="QueryRootExpression" />.
/// <see cref="EntityQueryRootExpression" />.
/// This makes them consistent with how DbSet accesses in the actual queries are represented, which allows for easier processing in the
/// query pipeline.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,17 @@ private static FromSqlQueryRootExpression GenerateFromSqlQueryRoot(
object?[] arguments,
[CallerMemberName] string memberName = null!)
{
var queryRootExpression = (QueryRootExpression)source.Expression;
var entityQueryRootExpression = (EntityQueryRootExpression)source.Expression;

var entityType = queryRootExpression.EntityType;
var entityType = entityQueryRootExpression.EntityType;
if ((entityType.BaseType != null || entityType.GetDirectlyDerivedTypes().Any())
&& entityType.FindDiscriminatorProperty() == null)
{
throw new InvalidOperationException(RelationalStrings.MethodOnNonTphRootNotSupported(memberName, entityType.DisplayName()));
}

return new FromSqlQueryRootExpression(
queryRootExpression.QueryProvider!,
entityQueryRootExpression.QueryProvider!,
entityType,
sql,
Expression.Constant(arguments));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
&& (methodName == nameof(RelationalQueryableExtensions.FromSqlRaw)
|| methodName == nameof(RelationalQueryableExtensions.FromSqlInterpolated)))
{
var newSource = (QueryRootExpression)Visit(methodCallExpression.Arguments[0]);
var newSource = (EntityQueryRootExpression)Visit(methodCallExpression.Arguments[0]);

string sql;
Expression argument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.EntityFrameworkCore.Query.Internal;
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public sealed class FromSqlQueryRootExpression : QueryRootExpression
public sealed class FromSqlQueryRootExpression : EntityQueryRootExpression
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -75,7 +75,7 @@ public override Expression DetachQueryProvider()
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override QueryRootExpression UpdateEntityType(IEntityType entityType)
public override EntityQueryRootExpression UpdateEntityType(IEntityType entityType)
=> entityType.ClrType != EntityType.ClrType
|| entityType.Name != EntityType.Name
? throw new InvalidOperationException(CoreStrings.QueryRootDifferentEntityType(entityType.DisplayName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.EntityFrameworkCore.Query.Internal;
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public sealed class TableValuedFunctionQueryRootExpression : QueryRootExpression
public sealed class TableValuedFunctionQueryRootExpression : EntityQueryRootExpression
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -72,7 +72,7 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override QueryRootExpression UpdateEntityType(IEntityType entityType)
public override EntityQueryRootExpression UpdateEntityType(IEntityType entityType)
=> entityType.ClrType != EntityType.ClrType
|| entityType.Name != EntityType.Name
? throw new InvalidOperationException(CoreStrings.QueryRootDifferentEntityType(entityType.DisplayName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,16 @@ protected override Expression VisitExtension(Expression extensionExpression)

return CreateShapedQueryExpression(entityType, queryExpression);

case QueryRootExpression queryRootExpression
when queryRootExpression.GetType() == typeof(QueryRootExpression)
&& queryRootExpression.EntityType.GetSqlQueryMappings().FirstOrDefault(m => m.IsDefaultSqlQueryMapping)?.SqlQuery is
case EntityQueryRootExpression entityQueryRootExpression
when entityQueryRootExpression.GetType() == typeof(EntityQueryRootExpression)
&& entityQueryRootExpression.EntityType.GetSqlQueryMappings().FirstOrDefault(m => m.IsDefaultSqlQueryMapping)?.SqlQuery is
ISqlQuery sqlQuery:
return CreateShapedQueryExpression(
queryRootExpression.EntityType,
entityQueryRootExpression.EntityType,
_sqlExpressionFactory.Select(
queryRootExpression.EntityType,
entityQueryRootExpression.EntityType,
new FromSqlExpression(
queryRootExpression.EntityType.GetDefaultMappings().Single().Table,
entityQueryRootExpression.EntityType.GetDefaultMappings().Single().Table,
sqlQuery.Sql,
Expression.Constant(Array.Empty<object>(), typeof(object[])))));

Expand Down
30 changes: 15 additions & 15 deletions src/EFCore.SqlServer/Extensions/SqlServerDbSetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public static IQueryable<TEntity> TemporalAsOf<TEntity>(
where TEntity : class
{
var queryableSource = (IQueryable)source;
var queryRootExpression = (QueryRootExpression)queryableSource.Expression;
var entityType = queryRootExpression.EntityType;
var entityQueryRootExpression = (EntityQueryRootExpression)queryableSource.Expression;
var entityType = entityQueryRootExpression.EntityType;

return queryableSource.Provider.CreateQuery<TEntity>(
new TemporalAsOfQueryRootExpression(
queryRootExpression.QueryProvider!,
entityQueryRootExpression.QueryProvider!,
entityType,
utcPointInTime)).AsNoTracking();
}
Expand Down Expand Up @@ -83,12 +83,12 @@ public static IQueryable<TEntity> TemporalFromTo<TEntity>(
where TEntity : class
{
var queryableSource = (IQueryable)source;
var queryRootExpression = (QueryRootExpression)queryableSource.Expression;
var entityType = queryRootExpression.EntityType;
var entityQueryRootExpression = (EntityQueryRootExpression)queryableSource.Expression;
var entityType = entityQueryRootExpression.EntityType;

return queryableSource.Provider.CreateQuery<TEntity>(
new TemporalFromToQueryRootExpression(
queryRootExpression.QueryProvider!,
entityQueryRootExpression.QueryProvider!,
entityType,
utcFrom,
utcTo)).AsNoTracking();
Expand Down Expand Up @@ -130,12 +130,12 @@ public static IQueryable<TEntity> TemporalBetween<TEntity>(
where TEntity : class
{
var queryableSource = (IQueryable)source;
var queryRootExpression = (QueryRootExpression)queryableSource.Expression;
var entityType = queryRootExpression.EntityType;
var entityQueryRootExpression = (EntityQueryRootExpression)queryableSource.Expression;
var entityType = entityQueryRootExpression.EntityType;

return queryableSource.Provider.CreateQuery<TEntity>(
new TemporalBetweenQueryRootExpression(
queryRootExpression.QueryProvider!,
entityQueryRootExpression.QueryProvider!,
entityType,
utcFrom,
utcTo)).AsNoTracking();
Expand Down Expand Up @@ -177,12 +177,12 @@ public static IQueryable<TEntity> TemporalContainedIn<TEntity>(
where TEntity : class
{
var queryableSource = (IQueryable)source;
var queryRootExpression = (QueryRootExpression)queryableSource.Expression;
var entityType = queryRootExpression.EntityType;
var entityQueryRootExpression = (EntityQueryRootExpression)queryableSource.Expression;
var entityType = entityQueryRootExpression.EntityType;

return queryableSource.Provider.CreateQuery<TEntity>(
new TemporalContainedInQueryRootExpression(
queryRootExpression.QueryProvider!,
entityQueryRootExpression.QueryProvider!,
entityType,
utcFrom,
utcTo)).AsNoTracking();
Expand All @@ -208,11 +208,11 @@ public static IQueryable<TEntity> TemporalAll<TEntity>(
where TEntity : class
{
var queryableSource = (IQueryable)source;
var queryRootExpression = (QueryRootExpression)queryableSource.Expression;
var entityType = queryRootExpression.EntityType;
var entityQueryRootExpression = (EntityQueryRootExpression)queryableSource.Expression;
var entityType = entityQueryRootExpression.EntityType;

return queryableSource.Provider.CreateQuery<TEntity>(
new TemporalAllQueryRootExpression(
queryRootExpression.QueryProvider!, entityType)).AsNoTracking();
entityQueryRootExpression.QueryProvider!, entityType)).AsNoTracking();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SqlServerNavigationExpansionExtensibilityHelper(NavigationExpansionExtens
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override QueryRootExpression CreateQueryRoot(IEntityType entityType, QueryRootExpression? source)
public override EntityQueryRootExpression CreateQueryRoot(IEntityType entityType, EntityQueryRootExpression? source)
{
if (source is TemporalAsOfQueryRootExpression asOf)
{
Expand All @@ -49,7 +49,7 @@ public override QueryRootExpression CreateQueryRoot(IEntityType entityType, Quer
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override void ValidateQueryRootCreation(IEntityType entityType, QueryRootExpression? source)
public override void ValidateQueryRootCreation(IEntityType entityType, EntityQueryRootExpression? source)
{
if (source is TemporalQueryRootExpression)
{
Expand All @@ -75,7 +75,7 @@ public override void ValidateQueryRootCreation(IEntityType entityType, QueryRoot
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override bool AreQueryRootsCompatible(QueryRootExpression? first, QueryRootExpression? second)
public override bool AreQueryRootsCompatible(EntityQueryRootExpression? first, EntityQueryRootExpression? second)
{
if (!base.AreQueryRootsCompatible(first, second))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override Expression DetachQueryProvider()
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override QueryRootExpression UpdateEntityType(IEntityType entityType)
public override EntityQueryRootExpression UpdateEntityType(IEntityType entityType)
=> entityType.ClrType != EntityType.ClrType
|| entityType.Name != EntityType.Name
? throw new InvalidOperationException(CoreStrings.QueryRootDifferentEntityType(entityType.DisplayName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override Expression DetachQueryProvider()
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override QueryRootExpression UpdateEntityType(IEntityType entityType)
public override EntityQueryRootExpression UpdateEntityType(IEntityType entityType)
=> entityType.ClrType != EntityType.ClrType
|| entityType.Name != EntityType.Name
? throw new InvalidOperationException(CoreStrings.QueryRootDifferentEntityType(entityType.DisplayName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override Expression DetachQueryProvider()
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override QueryRootExpression UpdateEntityType(IEntityType entityType)
public override EntityQueryRootExpression UpdateEntityType(IEntityType entityType)
=> entityType.ClrType != EntityType.ClrType
|| entityType.Name != EntityType.Name
? throw new InvalidOperationException(CoreStrings.QueryRootDifferentEntityType(entityType.DisplayName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override Expression DetachQueryProvider()
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override QueryRootExpression UpdateEntityType(IEntityType entityType)
public override EntityQueryRootExpression UpdateEntityType(IEntityType entityType)
=> entityType.ClrType != EntityType.ClrType
|| entityType.Name != EntityType.Name
? throw new InvalidOperationException(CoreStrings.QueryRootDifferentEntityType(entityType.DisplayName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override Expression DetachQueryProvider()
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override QueryRootExpression UpdateEntityType(IEntityType entityType)
public override EntityQueryRootExpression UpdateEntityType(IEntityType entityType)
=> entityType.ClrType != EntityType.ClrType
|| entityType.Name != EntityType.Name
? throw new InvalidOperationException(CoreStrings.QueryRootDifferentEntityType(entityType.DisplayName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public abstract class TemporalQueryRootExpression : QueryRootExpression
public abstract class TemporalQueryRootExpression : EntityQueryRootExpression
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Loading