Skip to content

Commit

Permalink
Rename defining query APIs (#21848)
Browse files Browse the repository at this point in the history
Rename InMemoryEntityTypeBuilderExtensions.ToQuery to ToInMemoryQuery
Rename ToQuerySql to ToSqlQuery

Part of #20409
  • Loading branch information
smitpatel committed Jul 30, 2020
1 parent 7a6fca0 commit b6dfdcc
Show file tree
Hide file tree
Showing 30 changed files with 97 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public static class InMemoryEntityTypeBuilderExtensions
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param>
/// <param name="query"> The query that will provide the underlying data for the entity type. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static EntityTypeBuilder<TEntity> ToQuery<TEntity>(
public static EntityTypeBuilder<TEntity> ToInMemoryQuery<TEntity>(
[NotNull] this EntityTypeBuilder<TEntity> entityTypeBuilder,
[NotNull] Expression<Func<IQueryable<TEntity>>> query)
where TEntity : class
{
Check.NotNull(query, nameof(query));

InMemoryEntityTypeExtensions.SetDefiningQuery(entityTypeBuilder.Metadata, query);
entityTypeBuilder.Metadata.SetInMemoryQuery(query);

return entityTypeBuilder;
}
Expand All @@ -44,14 +44,14 @@ public static EntityTypeBuilder<TEntity> ToQuery<TEntity>(
/// <returns>
/// The same builder instance if the query was set, <see langword="null" /> otherwise.
/// </returns>
public static IConventionEntityTypeBuilder ToQuery(
public static IConventionEntityTypeBuilder ToInMemoryQuery(
[NotNull] this IConventionEntityTypeBuilder entityTypeBuilder,
[CanBeNull] LambdaExpression query,
bool fromDataAnnotation = false)
{
if (CanSetDefiningQuery(entityTypeBuilder, query, fromDataAnnotation))
if (CanSetInMemoryQuery(entityTypeBuilder, query, fromDataAnnotation))
{
InMemoryEntityTypeExtensions.SetDefiningQuery(entityTypeBuilder.Metadata, query, fromDataAnnotation);
entityTypeBuilder.Metadata.SetInMemoryQuery(query, fromDataAnnotation);

return entityTypeBuilder;
}
Expand All @@ -60,13 +60,13 @@ public static IConventionEntityTypeBuilder ToQuery(
}

/// <summary>
/// Returns a value indicating whether the given defining query can be set from the current configuration source.
/// Returns a value indicating whether the given in-memory query can be set from the current configuration source.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param>
/// <param name="query"> The query that will provide the underlying data for the keyless entity type. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <see langword="true" /> if the given defining query can be set. </returns>
public static bool CanSetDefiningQuery(
/// <returns> <see langword="true" /> if the given in-memory query can be set. </returns>
public static bool CanSetInMemoryQuery(
[NotNull] this IConventionEntityTypeBuilder entityTypeBuilder,
[CanBeNull] LambdaExpression query,
bool fromDataAnnotation = false)
Expand Down
24 changes: 12 additions & 12 deletions src/EFCore.InMemory/Extensions/InMemoryEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public static class InMemoryEntityTypeExtensions
/// <summary>
/// Gets the LINQ query used as the default source for queries of this type.
/// </summary>
/// <param name="entityType"> The entity type to get the defining query for. </param>
/// <param name="entityType"> The entity type to get the in-memory query for. </param>
/// <returns> The LINQ query used as the default source. </returns>
public static LambdaExpression GetDefiningQuery([NotNull] this IEntityType entityType)
public static LambdaExpression GetInMemoryQuery([NotNull] this IEntityType entityType)
#pragma warning disable EF1001 // Internal EF Core API usage.
#pragma warning disable CS0612 // Type or member is obsolete
=> (LambdaExpression)Check.NotNull(entityType, nameof(entityType))[CoreAnnotationNames.DefiningQuery];
Expand All @@ -31,41 +31,41 @@ public static LambdaExpression GetDefiningQuery([NotNull] this IEntityType entit
/// Sets the LINQ query used as the default source for queries of this type.
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <param name="definingQuery"> The LINQ query used as the default source. </param>
public static void SetDefiningQuery(
/// <param name="inMemoryQuery"> The LINQ query used as the default source. </param>
public static void SetInMemoryQuery(
[NotNull] this IMutableEntityType entityType,
[CanBeNull] LambdaExpression definingQuery)
[CanBeNull] LambdaExpression inMemoryQuery)
=> Check.NotNull(entityType, nameof(entityType))
#pragma warning disable EF1001 // Internal EF Core API usage.
#pragma warning disable CS0612 // Type or member is obsolete
.SetOrRemoveAnnotation(CoreAnnotationNames.DefiningQuery, definingQuery);
.SetOrRemoveAnnotation(CoreAnnotationNames.DefiningQuery, inMemoryQuery);
#pragma warning restore CS0612 // Type or member is obsolete
#pragma warning restore EF1001 // Internal EF Core API usage.

/// <summary>
/// Sets the LINQ query used as the default source for queries of this type.
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <param name="definingQuery"> The LINQ query used as the default source. </param>
/// <param name="inMemoryQuery"> The LINQ query used as the default source. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The configured entity type. </returns>
public static LambdaExpression SetDefiningQuery(
public static LambdaExpression SetInMemoryQuery(
[NotNull] this IConventionEntityType entityType,
[CanBeNull] LambdaExpression definingQuery,
[CanBeNull] LambdaExpression inMemoryQuery,
bool fromDataAnnotation = false)
=> (LambdaExpression)Check.NotNull(entityType, nameof(entityType))
#pragma warning disable EF1001 // Internal EF Core API usage.
#pragma warning disable CS0612 // Type or member is obsolete
.SetOrRemoveAnnotation(CoreAnnotationNames.DefiningQuery, definingQuery, fromDataAnnotation)
.SetOrRemoveAnnotation(CoreAnnotationNames.DefiningQuery, inMemoryQuery, fromDataAnnotation)
#pragma warning restore CS0612 // Type or member is obsolete
#pragma warning restore EF1001 // Internal EF Core API usage.
?.Value;

/// <summary>
/// Returns the configuration source for <see cref="EntityTypeExtensions.GetDefiningQuery" />.
/// Returns the configuration source for <see cref="GetInMemoryQuery" />.
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <returns> The configuration source for <see cref="EntityTypeExtensions.GetDefiningQuery" />. </returns>
/// <returns> The configuration source for <see cref="GetInMemoryQuery" />. </returns>
public static ConfigurationSource? GetDefiningQueryConfigurationSource([NotNull] this IConventionEntityType entityType)
#pragma warning disable EF1001 // Internal EF Core API usage.
#pragma warning disable CS0612 // Type or member is obsolete
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.InMemory/Internal/InMemoryModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected virtual void ValidateDefiningQuery(

foreach (var entityType in model.GetEntityTypes())
{
if (InMemoryEntityTypeExtensions.GetDefiningQuery(entityType) != null)
if (entityType.GetInMemoryQuery() != null)
{
if (entityType.BaseType != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ public override void ProcessModelFinalizing(
{
foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
{
var definingQuery = InMemoryEntityTypeExtensions.GetDefiningQuery(entityType);
var definingQuery = entityType.GetInMemoryQuery();
if (definingQuery != null)
{
InMemoryEntityTypeExtensions.SetDefiningQuery(
entityType, (LambdaExpression)DbSetAccessRewriter.Rewrite(modelBuilder.Metadata, definingQuery));
entityType.SetInMemoryQuery((LambdaExpression)DbSetAccessRewriter.Rewrite(modelBuilder.Metadata, definingQuery));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,14 @@ public static bool CanSetViewSchema(
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param>
/// <param name="query"> The SQL query that will provide the underlying data for the entity type. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static EntityTypeBuilder<TEntity> ToQuerySql<TEntity>(
public static EntityTypeBuilder<TEntity> ToSqlQuery<TEntity>(
[NotNull] this EntityTypeBuilder<TEntity> entityTypeBuilder,
[NotNull] string query)
where TEntity : class
{
Check.NotNull(query, nameof(query));

entityTypeBuilder.Metadata.SetQuerySql(query);
entityTypeBuilder.Metadata.SetSqlQuery(query);

return entityTypeBuilder;
}
Expand All @@ -624,16 +624,16 @@ public static EntityTypeBuilder<TEntity> ToQuerySql<TEntity>(
/// <returns>
/// The same builder instance if the configuration was applied, <see langword="null" /> otherwise.
/// </returns>
public static IConventionEntityTypeBuilder ToQuerySql(
public static IConventionEntityTypeBuilder ToSqlQuery(
[NotNull] this IConventionEntityTypeBuilder entityTypeBuilder, [CanBeNull] string name, bool fromDataAnnotation = false)
{
if (!entityTypeBuilder.CanSetQuerySql(name, fromDataAnnotation))
if (!entityTypeBuilder.CanSetSqlQuery(name, fromDataAnnotation))
{
return null;
}

var entityType = entityTypeBuilder.Metadata;
entityType.SetQuerySql(name, fromDataAnnotation);
entityType.SetSqlQuery(name, fromDataAnnotation);

return entityTypeBuilder;
}
Expand All @@ -646,12 +646,12 @@ public static IConventionEntityTypeBuilder ToQuerySql(
/// <param name="name"> The SQL query that will provide the underlying data for the entity type. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <see langword="true" /> if the configuration can be applied. </returns>
public static bool CanSetQuerySql(
public static bool CanSetSqlQuery(
[NotNull] this IConventionEntityTypeBuilder entityTypeBuilder, [CanBeNull] string name, bool fromDataAnnotation = false)
{
Check.NullButNotEmpty(name, nameof(name));

return entityTypeBuilder.CanSetAnnotation(RelationalAnnotationNames.QuerySql, name, fromDataAnnotation);
return entityTypeBuilder.CanSetAnnotation(RelationalAnnotationNames.SqlQuery, name, fromDataAnnotation);
}

/// <summary>
Expand Down
24 changes: 12 additions & 12 deletions src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static string GetTableName([NotNull] this IEntityType entityType)
#pragma warning disable CS0618 // Type or member is obsolete
&& ((entityType as IConventionEntityType)?.GetDefiningQueryConfigurationSource() == null)
#pragma warning restore CS0618 // Type or member is obsolete
&& ((entityType as IConventionEntityType)?.GetQuerySqlConfigurationSource() == null)
&& ((entityType as IConventionEntityType)?.GetSqlQueryConfigurationSource() == null)
? GetDefaultTableName(entityType)
: null;
}
Expand Down Expand Up @@ -263,7 +263,7 @@ public static string GetViewName([NotNull] this IEntityType entityType)
#pragma warning disable CS0618 // Type or member is obsolete
&& (entityType as IConventionEntityType)?.GetDefiningQueryConfigurationSource() == null
#pragma warning restore CS0618 // Type or member is obsolete
&& ((entityType as IConventionEntityType)?.GetQuerySqlConfigurationSource() == null)
&& ((entityType as IConventionEntityType)?.GetSqlQueryConfigurationSource() == null)
? GetDefaultViewName(entityType)
: null;
}
Expand Down Expand Up @@ -402,7 +402,7 @@ public static IEnumerable<IViewMapping> GetViewMappings([NotNull] this IEntityTy

/// <summary>
/// Gets the default SQL query name that would be used for this entity type when mapped using
/// <see cref="M:RelationalEntityTypeBuilderExtensions.ToQuerySql" />.
/// <see cref="M:RelationalEntityTypeBuilderExtensions.ToSqlQuery" />.
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <returns> Gets the default SQL query name. </returns>
Expand All @@ -414,17 +414,17 @@ public static string GetDefaultSqlQueryName([NotNull] this IEntityType entityTyp
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <returns> The SQL string used to provide data for the entity type. </returns>
public static string GetQuerySql([NotNull] this IEntityType entityType)
public static string GetSqlQuery([NotNull] this IEntityType entityType)
{
var nameAnnotation = (string)entityType[RelationalAnnotationNames.QuerySql];
var nameAnnotation = (string)entityType[RelationalAnnotationNames.SqlQuery];
if (nameAnnotation != null)
{
return nameAnnotation;
}

if (entityType.BaseType != null)
{
return entityType.GetRootType().GetQuerySql();
return entityType.GetRootType().GetSqlQuery();
}

return null;
Expand All @@ -435,9 +435,9 @@ public static string GetQuerySql([NotNull] this IEntityType entityType)
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <param name="name"> The SQL string to set. </param>
public static void SetQuerySql([NotNull] this IMutableEntityType entityType, [CanBeNull] string name)
public static void SetSqlQuery([NotNull] this IMutableEntityType entityType, [CanBeNull] string name)
=> entityType.SetAnnotation(
RelationalAnnotationNames.QuerySql,
RelationalAnnotationNames.SqlQuery,
Check.NullButNotEmpty(name, nameof(name)));

/// <summary>
Expand All @@ -447,10 +447,10 @@ public static void SetQuerySql([NotNull] this IMutableEntityType entityType, [Ca
/// <param name="name"> The SQL string to set. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The configured value. </returns>
public static string SetQuerySql(
public static string SetSqlQuery(
[NotNull] this IConventionEntityType entityType, [CanBeNull] string name, bool fromDataAnnotation = false)
=> (string)entityType.SetAnnotation(
RelationalAnnotationNames.QuerySql,
RelationalAnnotationNames.SqlQuery,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation)?.Value;

Expand All @@ -459,8 +459,8 @@ public static string SetQuerySql(
/// </summary>
/// <param name="entityType"> The entity type to find configuration source for. </param>
/// <returns> The <see cref="ConfigurationSource" /> for the query SQL string. </returns>
public static ConfigurationSource? GetQuerySqlConfigurationSource([NotNull] this IConventionEntityType entityType)
=> entityType.FindAnnotation(RelationalAnnotationNames.QuerySql)
public static ConfigurationSource? GetSqlQueryConfigurationSource([NotNull] this IConventionEntityType entityType)
=> entityType.FindAnnotation(RelationalAnnotationNames.SqlQuery)
?.GetConfigurationSource();

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ protected virtual void ValidateSqlQueries(
{
foreach (var entityType in model.GetEntityTypes())
{
var sqlQuery = entityType.GetQuerySql();
var sqlQuery = entityType.GetSqlQuery();
if (sqlQuery == null)
{
continue;
}

if (entityType.BaseType != null
&& (entityType.GetDiscriminatorProperty() == null
|| sqlQuery != entityType.BaseType.GetQuerySql()))
|| sqlQuery != entityType.BaseType.GetSqlQuery()))
{
throw new InvalidOperationException(
RelationalStrings.InvalidMappedSqlQueryDerivedType(
Expand Down
22 changes: 12 additions & 10 deletions src/EFCore.Relational/Metadata/Internal/RelationalModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,8 @@ private static void AddViews(RelationalModel databaseModel, IConventionEntityTyp

private static void AddSqlQueries(RelationalModel databaseModel, IConventionEntityType entityType)
{
var querySql = entityType.GetQuerySql();
if (querySql == null)
var entityTypeSqlQuery = entityType.GetSqlQuery();
if (entityTypeSqlQuery == null)
{
return;
}
Expand All @@ -491,10 +491,10 @@ private static void AddSqlQueries(RelationalModel databaseModel, IConventionEnti
var definingType = entityType;
while (definingType != null)
{
var mappedSql = definingType.GetQuerySql();
if (mappedSql == null
var definingTypeSqlQuery = definingType.GetSqlQuery();
if (definingTypeSqlQuery == null
|| definingType.BaseType == null
|| (mappedSql == querySql
|| (definingTypeSqlQuery == entityTypeSqlQuery
&& definingType != entityType))
{
break;
Expand All @@ -506,9 +506,9 @@ private static void AddSqlQueries(RelationalModel databaseModel, IConventionEnti
var mappedType = entityType;
while (mappedType != null)
{
var mappedSql = mappedType.GetQuerySql();
if (mappedSql == null
|| (mappedSql == querySql
var mappedTypeSqlQuery = mappedType.GetSqlQuery();
if (mappedTypeSqlQuery == null
|| (mappedTypeSqlQuery == entityTypeSqlQuery
&& mappedType != entityType))
{
break;
Expand All @@ -517,8 +517,10 @@ private static void AddSqlQueries(RelationalModel databaseModel, IConventionEnti
var mappedQuery = StoreObjectIdentifier.SqlQuery(definingType);
if (!databaseModel.Queries.TryGetValue(mappedQuery.Name, out var sqlQuery))
{
sqlQuery = new SqlQuery(mappedQuery.Name, databaseModel);
sqlQuery.Sql = mappedSql;
sqlQuery = new SqlQuery(mappedQuery.Name, databaseModel)
{
Sql = mappedTypeSqlQuery
};
databaseModel.Queries.Add(mappedQuery.Name, sqlQuery);
}

Expand Down
Loading

0 comments on commit b6dfdcc

Please sign in to comment.