Skip to content

Commit

Permalink
Remove ITableBasedExpression - Use annotation instead
Browse files Browse the repository at this point in the history
Resolves #28667
  • Loading branch information
smitpatel committed Aug 11, 2022
1 parent 30d4b22 commit 6da6e80
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 47 deletions.
7 changes: 7 additions & 0 deletions src/EFCore.Relational/Metadata/RelationalAnnotationNames.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Query.SqlExpressions;

namespace Microsoft.EntityFrameworkCore.Metadata;

/// <summary>
Expand Down Expand Up @@ -333,4 +335,9 @@ public static class RelationalAnnotationNames
/// The JSON property name for the element that the property/navigation maps to.
/// </summary>
public const string JsonPropertyName = Prefix + "JsonPropertyName";

/// <summary>
/// The <see cref="ITableBase"/> associated with the given <see cref="TableExpressionBase"/>.
/// </summary>
public const string QueryTableSource = Prefix + "QueryTableSource";
}
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Query/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ private void AddConditions(SelectExpression selectExpression, IEntityType entity
return;
}

var table = ((ITableBasedExpression)selectExpression.Tables[0]).Table;
var table = (ITableBase)selectExpression.Tables[0].FindAnnotation(RelationalAnnotationNames.QueryTableSource)!.Value!;
if (table.IsOptional(entityType))
{
var entityProjectionExpression = GetMappedEntityProjectionExpression(selectExpression);
Expand Down
19 changes: 6 additions & 13 deletions src/EFCore.Relational/Query/SqlExpressions/FromSqlExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions;
/// not used in application code.
/// </para>
/// </summary>
public class FromSqlExpression : TableExpressionBase, IClonableTableExpressionBase, ITableBasedExpression
public class FromSqlExpression : TableExpressionBase, IClonableTableExpressionBase
{
private readonly ITableBase _table;

/// <summary>
/// Creates a new instance of the <see cref="FromSqlExpression" /> class.
/// </summary>
/// <param name="defaultTableBase">A default table base associated with this table source.</param>
/// <param name="sql">A user-provided custom SQL for the table source.</param>
/// <param name="arguments">A user-provided parameters to pass to the custom SQL.</param>
public FromSqlExpression(ITableBase defaultTableBase, string sql, Expression arguments)
: this(defaultTableBase.Name[..1].ToLowerInvariant(), defaultTableBase, sql, arguments, annotations: null)
: this(defaultTableBase.Name[..1].ToLowerInvariant(), sql, arguments,
new[] { new Annotation(RelationalAnnotationNames.QueryTableSource, defaultTableBase)})
{
}

Expand All @@ -41,13 +40,11 @@ public FromSqlExpression(ITableBase defaultTableBase, string sql, Expression arg

private FromSqlExpression(
string alias,
ITableBase tableBase,
string sql,
Expression arguments,
IEnumerable<IAnnotation>? annotations)
: base(alias, annotations)
{
_table = tableBase;
Sql = sql;
Arguments = arguments;
}
Expand Down Expand Up @@ -80,23 +77,20 @@ public override string? Alias
/// <returns>This expression if no children changed, or an expression with the updated children.</returns>
public virtual FromSqlExpression Update(Expression arguments)
=> arguments != Arguments
? new FromSqlExpression(Alias, _table, Sql, arguments, GetAnnotations())
? new FromSqlExpression(Alias, Sql, arguments, GetAnnotations())
: this;

/// <inheritdoc />
protected override TableExpressionBase CreateWithAnnotations(IEnumerable<IAnnotation> annotations)
=> new FromSqlExpression(Alias, _table, Sql, Arguments, annotations);

/// <inheritdoc />
ITableBase ITableBasedExpression.Table => _table;
=> new FromSqlExpression(Alias, Sql, Arguments, annotations);

/// <inheritdoc />
protected override Expression VisitChildren(ExpressionVisitor visitor)
=> this;

/// <inheritdoc />
public virtual TableExpressionBase Clone()
=> new FromSqlExpression(Alias, _table, Sql, Arguments, GetAnnotations());
=> new FromSqlExpression(Alias, Sql, Arguments, GetAnnotations());

/// <inheritdoc />
protected override void Print(ExpressionPrinter expressionPrinter)
Expand All @@ -114,7 +108,6 @@ public override bool Equals(object? obj)

private bool Equals(FromSqlExpression fromSqlExpression)
=> base.Equals(fromSqlExpression)
&& _table == fromSqlExpression._table
&& Sql == fromSqlExpression.Sql
&& ExpressionEqualityComparer.Instance.Equals(Arguments, fromSqlExpression.Arguments);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ internal SelectExpression(IEntityType entityType, TableExpressionBase tableExpre
throw new InvalidOperationException(RelationalStrings.SelectExpressionNonTphWithCustomTable(entityType.DisplayName()));
}

var table = ((ITableBasedExpression)tableExpressionBase).Table;
var table = (ITableBase)tableExpressionBase.FindAnnotation(RelationalAnnotationNames.QueryTableSource)!.Value!;
var tableReferenceExpression = new TableReferenceExpression(this, tableExpressionBase.Alias!);
AddTable(tableExpressionBase, tableReferenceExpression);

Expand Down
7 changes: 2 additions & 5 deletions src/EFCore.Relational/Query/SqlExpressions/TableExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions;
/// application or database provider code. If this is a problem for your application or provider, then please file
/// an issue at <see href="https://github.com/dotnet/efcore">github.com/dotnet/efcore</see>.
/// </remarks>
public sealed class TableExpression : TableExpressionBase, IClonableTableExpressionBase, ITableBasedExpression
public sealed class TableExpression : TableExpressionBase, IClonableTableExpressionBase
{
internal TableExpression(ITableBase table)
: this(table, annotations: null)
: this(table, annotations: new[] { new Annotation(RelationalAnnotationNames.QueryTableSource, table) })
{
}

Expand Down Expand Up @@ -57,9 +57,6 @@ public override string? Alias
protected override TableExpressionBase CreateWithAnnotations(IEnumerable<IAnnotation> annotations)
=> new TableExpression(Table, annotations) { Alias = Alias };

/// <inheritdoc />
ITableBase ITableBasedExpression.Table => Table;

/// <inheritdoc />
protected override void Print(ExpressionPrinter expressionPrinter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,39 @@ public override bool Equals(object? obj)
&& Equals(tableExpressionBase));

private bool Equals(TableExpressionBase tableExpressionBase)
=> Alias == tableExpressionBase.Alias;
{
if (Alias != tableExpressionBase.Alias)
{
return false;
}

if (_annotations == null)
{
return tableExpressionBase._annotations == null;
}

if (tableExpressionBase._annotations == null)
{
return false;
}

if (_annotations.Count != tableExpressionBase._annotations.Count)
{
return false;
}

foreach (var kvp in _annotations)
{
if (!tableExpressionBase._annotations.TryGetValue(kvp.Key, out var value)
|| !Equals(value, kvp.Value))
{
return false;
}
}

return false;
}


/// <inheritdoc />
public override int GetHashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions;
/// not used in application code.
/// </para>
/// </summary>
public class TableValuedFunctionExpression : TableExpressionBase, ITableBasedExpression
public class TableValuedFunctionExpression : TableExpressionBase
{
/// <summary>
/// Creates a new instance of the <see cref="TableValuedFunctionExpression" /> class.
Expand All @@ -26,7 +26,7 @@ public TableValuedFunctionExpression(IStoreFunction storeFunction, IReadOnlyList
storeFunction.Name[..1].ToLowerInvariant(),
storeFunction,
arguments,
annotations: null)
annotations: new[] { new Annotation(RelationalAnnotationNames.QueryTableSource, storeFunction) })
{
}

Expand Down Expand Up @@ -61,9 +61,6 @@ public override string? Alias
/// </summary>
public virtual IReadOnlyList<SqlExpression> Arguments { get; }

/// <inheritdoc />
ITableBase ITableBasedExpression.Table => StoreFunction;

/// <inheritdoc />
protected override Expression VisitChildren(ExpressionVisitor visitor)
{
Expand Down

0 comments on commit 6da6e80

Please sign in to comment.