Skip to content

Commit

Permalink
Query: Add support for TagWith in relational layer
Browse files Browse the repository at this point in the history
Close #15710
  • Loading branch information
smitpatel committed Jul 1, 2019
1 parent 34acab5 commit c3225b6
Show file tree
Hide file tree
Showing 20 changed files with 109 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,16 @@ public class CosmosShapedQueryCompilingExpressionVisitor : ShapedQueryCompilingE
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _logger;

public CosmosShapedQueryCompilingExpressionVisitor(
QueryCompilationContext queryCompilationContext,
IEntityMaterializerSource entityMaterializerSource,
ISqlExpressionFactory sqlExpressionFactory,
IQuerySqlGeneratorFactory querySqlGeneratorFactory,
Type contextType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger,
bool trackQueryResults,
bool async)
: base(entityMaterializerSource, trackQueryResults, async)
IQuerySqlGeneratorFactory querySqlGeneratorFactory)
: base(queryCompilationContext, entityMaterializerSource)
{
_sqlExpressionFactory = sqlExpressionFactory;
_querySqlGeneratorFactory = querySqlGeneratorFactory;
_contextType = contextType;
_logger = logger;
_contextType = queryCompilationContext.ContextType;
_logger = queryCompilationContext.Logger;
}

protected override Expression VisitShapedQueryExpression(ShapedQueryExpression shapedQueryExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ public CosmosShapedQueryCompilingExpressionVisitorFactory(IEntityMaterializerSou
public ShapedQueryCompilingExpressionVisitor Create(QueryCompilationContext queryCompilationContext)
{
return new CosmosShapedQueryCompilingExpressionVisitor(
queryCompilationContext,
_entityMaterializerSource,
_sqlExpressionFactory,
_querySqlGeneratorFactory,
queryCompilationContext.ContextType,
queryCompilationContext.Logger,
queryCompilationContext.TrackQueryResults,
queryCompilationContext.Async);
_querySqlGeneratorFactory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ public class InMemoryShapedQueryCompilingExpressionVisitor : ShapedQueryCompilin
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _logger;

public InMemoryShapedQueryCompilingExpressionVisitor(
IEntityMaterializerSource entityMaterializerSource,
Type contextType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger,
bool trackQueryResults, bool async)
: base(entityMaterializerSource, trackQueryResults, async)
QueryCompilationContext queryCompilationContext,
IEntityMaterializerSource entityMaterializerSource)
: base(queryCompilationContext, entityMaterializerSource)
{
_contextType = contextType;
_logger = logger;
_contextType = queryCompilationContext.ContextType;
_logger = queryCompilationContext.Logger;
}

protected override Expression VisitExtension(Expression extensionExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ public InMemoryShapedQueryCompilingExpressionVisitorFactory(IEntityMaterializerS
public ShapedQueryCompilingExpressionVisitor Create(QueryCompilationContext queryCompilationContext)
{
return new InMemoryShapedQueryCompilingExpressionVisitor(
_entityMaterializerSource,
queryCompilationContext.ContextType,
queryCompilationContext.Logger,
queryCompilationContext.TrackQueryResults,
queryCompilationContext.Async);
queryCompilationContext,
_entityMaterializerSource);
}
}

Expand Down
42 changes: 38 additions & 4 deletions src/EFCore.Relational/Query/Pipeline/QuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions;
Expand Down Expand Up @@ -55,14 +56,46 @@ public virtual IRelationalCommand GetCommand(SelectExpression selectExpression)
}
else
{
GenerateTagsHeaderComment(selectExpression);

VisitSelect(selectExpression);
}

return _relationalCommandBuilder.Build();
}

/// <summary>
/// The default alias separator.
/// </summary>
protected virtual string AliasSeparator { get; } = " AS ";

/// <summary>
/// The default single line comment prefix.
/// </summary>
protected virtual string SingleLineCommentToken { get; } = "--";

protected virtual IRelationalCommandBuilder Sql => _relationalCommandBuilder;

protected virtual void GenerateTagsHeaderComment(SelectExpression selectExpression)
{
if (selectExpression.Tags.Count > 0)
{
foreach (var tag in selectExpression.Tags)
{
using (var reader = new StringReader(tag))
{
string line;
while ((line = reader.ReadLine()) != null)
{
_relationalCommandBuilder.Append(SingleLineCommentToken).Append(" ").AppendLine(line);
}
}

_relationalCommandBuilder.AppendLine();
}
}
}

protected override Expression VisitSqlFragment(SqlFragmentExpression sqlFragmentExpression)
{
_relationalCommandBuilder.Append(sqlFragmentExpression.Sql);
Expand Down Expand Up @@ -94,7 +127,7 @@ protected override Expression VisitSelect(SelectExpression selectExpression)
subQueryIndent.Dispose();

_relationalCommandBuilder.AppendLine()
.Append(") AS " + _sqlGenerationHelper.DelimitIdentifier(selectExpression.Alias));
.Append(")" + AliasSeparator + _sqlGenerationHelper.DelimitIdentifier(selectExpression.Alias));
}

return selectExpression;
Expand Down Expand Up @@ -193,7 +226,7 @@ protected override Expression VisitProjection(ProjectionExpression projectionExp
&& !(projectionExpression.Expression is ColumnExpression column
&& string.Equals(column.Name, projectionExpression.Alias)))
{
_relationalCommandBuilder.Append(" AS " + _sqlGenerationHelper.DelimitIdentifier(projectionExpression.Alias));
_relationalCommandBuilder.Append(AliasSeparator + _sqlGenerationHelper.DelimitIdentifier(projectionExpression.Alias));
}

return projectionExpression;
Expand Down Expand Up @@ -248,7 +281,7 @@ protected override Expression VisitTable(TableExpression tableExpression)
{
_relationalCommandBuilder
.Append(_sqlGenerationHelper.DelimitIdentifier(tableExpression.Table, tableExpression.Schema))
.Append(" AS ")
.Append(AliasSeparator)
.Append(_sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias));

return tableExpression;
Expand Down Expand Up @@ -297,7 +330,8 @@ protected override Expression VisitFromSql(FromSqlExpression fromSqlExpression)
GenerateFromSql(fromSqlExpression);
}

_relationalCommandBuilder.Append(") AS ")
_relationalCommandBuilder.Append(")")
.Append(AliasSeparator)
.Append(_sqlGenerationHelper.DelimitIdentifier(fromSqlExpression.Alias));

return fromSqlExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ public partial class RelationalShapedQueryCompilingExpressionVisitor : ShapedQue
private readonly IParameterNameGeneratorFactory _parameterNameGeneratorFactory;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _logger;
private readonly ISet<string> _tags;

public RelationalShapedQueryCompilingExpressionVisitor(
QueryCompilationContext queryCompilationContext,
IEntityMaterializerSource entityMaterializerSource,
IQuerySqlGeneratorFactory querySqlGeneratorFactory,
ISqlExpressionFactory sqlExpressionFactory,
IParameterNameGeneratorFactory parameterNameGeneratorFactory,
Type contextType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger,
bool trackQueryResults,
bool async)
: base(entityMaterializerSource, trackQueryResults, async)
IParameterNameGeneratorFactory parameterNameGeneratorFactory)
: base(queryCompilationContext, entityMaterializerSource)
{
_querySqlGeneratorFactory = querySqlGeneratorFactory;
_sqlExpressionFactory = sqlExpressionFactory;
_parameterNameGeneratorFactory = parameterNameGeneratorFactory;
_contextType = contextType;
_logger = logger;
_contextType = queryCompilationContext.ContextType;
_logger = queryCompilationContext.Logger;
_tags = queryCompilationContext.Tags;
}

protected override Expression VisitShapedQueryExpression(ShapedQueryExpression shapedQueryExpression)
{
var selectExpression = (SelectExpression)shapedQueryExpression.QueryExpression;
selectExpression.ApplyTags(_tags);

var dataReaderParameter = Expression.Parameter(typeof(DbDataReader), "dataReader");
var resultCoordinatorParameter = Expression.Parameter(typeof(ResultCoordinator), "resultCoordinator");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ public RelationalShapedQueryCompilingExpressionVisitorFactory(
public ShapedQueryCompilingExpressionVisitor Create(QueryCompilationContext queryCompilationContext)
{
return new RelationalShapedQueryCompilingExpressionVisitor(
queryCompilationContext,
_entityMaterializerSource,
_querySqlGeneratorFactory,
_sqlExpressionFactory,
_parameterNameGeneratorFactory,
queryCompilationContext.ContextType,
queryCompilationContext.Logger,
queryCompilationContext.TrackQueryResults,
queryCompilationContext.Async);
_parameterNameGeneratorFactory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
Expand All @@ -34,11 +31,17 @@ private readonly IDictionary<EntityProjectionExpression, IDictionary<IProperty,
public IReadOnlyList<ProjectionExpression> Projection => _projection;
public IReadOnlyList<TableExpressionBase> Tables => _tables;
public IReadOnlyList<OrderingExpression> Orderings => _orderings;
public ISet<string> Tags { get; private set; } = new HashSet<string>();
public SqlExpression Predicate { get; private set; }
public SqlExpression Limit { get; private set; }
public SqlExpression Offset { get; private set; }
public bool IsDistinct { get; private set; }

public void ApplyTags(ISet<string> tags)
{
Tags = tags;
}

/// <summary>
/// Marks this <see cref="SelectExpression"/> as representing an SQL set operation, such as a UNION.
/// For regular SQL SELECT expressions, contains <c>None</c>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ private NavigationExpansionExpressionState ReplaceNavigationExpansionExpressionS
newPendingOrderings,
newPendingIncludeChain,
State.PendingCardinalityReducingOperator,
State.PendingTags,
State.CustomRootMappings,
State.MaterializeCollectionNavigation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public NavigationExpansionExpressionState(
List<(MethodInfo method, LambdaExpression keySelector)> pendingOrderings,
NavigationBindingExpression pendingIncludeChain,
MethodInfo pendingCardinalityReducingOperator,
List<string> pendingTags,
List<List<string>> customRootMappings,
INavigation materializeCollectionNavigation)
{
Expand All @@ -29,7 +28,6 @@ public NavigationExpansionExpressionState(
PendingOrderings = pendingOrderings;
PendingIncludeChain = pendingIncludeChain;
PendingCardinalityReducingOperator = pendingCardinalityReducingOperator;
PendingTags = pendingTags;
CustomRootMappings = customRootMappings;
MaterializeCollectionNavigation = materializeCollectionNavigation;
}
Expand All @@ -41,7 +39,6 @@ public NavigationExpansionExpressionState(
public virtual List<(MethodInfo method, LambdaExpression keySelector)> PendingOrderings { get; set; }
public virtual NavigationBindingExpression PendingIncludeChain { get; set; }
public virtual MethodInfo PendingCardinalityReducingOperator { get; set; }
public virtual List<string> PendingTags { get; set; }
public virtual List<List<string>> CustomRootMappings { get; set; }
public virtual INavigation MaterializeCollectionNavigation { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public static NavigationExpansionExpression CreateNavigationExpansionRoot(
new List<(MethodInfo method, LambdaExpression keySelector)>(),
pendingIncludeChain: null,
pendingCardinalityReducingOperator: null,
pendingTags: new List<string>(),
customRootMappings: new List<List<string>>(),
materializeCollectionNavigation),
materializeCollectionNavigation?.ClrType ?? operand.Type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ private Expression ProcessMemberPushdown(
navigationExpansionExpression.State.PendingIncludeChain,
// we need to remap cardinality reducing operator since it's source type has now changed
navigationExpansionExpression.State.PendingCardinalityReducingOperator.GetGenericMethodDefinition().MakeGenericMethod(newPendingSelector.Type),
navigationExpansionExpression.State.PendingTags,
navigationExpansionExpression.State.CustomRootMappings,
navigationExpansionExpression.State.MaterializeCollectionNavigation);

Expand Down Expand Up @@ -160,7 +159,6 @@ private Expression ProcessMemberPushdown(
navigationExpansionExpression.State.PendingIncludeChain,
// we need to remap cardinality reducing operator since it's source type has now changed
navigationExpansionExpression.State.PendingCardinalityReducingOperator.GetGenericMethodDefinition().MakeGenericMethod(combinedKeySelectorBody.Type),
navigationExpansionExpression.State.PendingTags,
navigationExpansionExpression.State.CustomRootMappings,
materializeCollectionNavigation: null);

Expand Down Expand Up @@ -200,7 +198,6 @@ private Expression ProcessMemberPushdown(
navigationExpansionExpression.State.PendingIncludeChain,
// we need to remap cardinality reducing operator since it's source type has now changed
navigationExpansionExpression.State.PendingCardinalityReducingOperator.GetGenericMethodDefinition().MakeGenericMethod(boundSelectorBody.Type),
navigationExpansionExpression.State.PendingTags,
navigationExpansionExpression.State.CustomRootMappings,
navigationExpansionExpression.State.MaterializeCollectionNavigation);

Expand Down Expand Up @@ -364,7 +361,6 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
navigationExpansionExpression.State.PendingIncludeChain,
// we need to remap cardinality reducing operator since it's source type has now changed
navigationExpansionExpression.State.PendingCardinalityReducingOperator?.GetGenericMethodDefinition().MakeGenericMethod(navigationKeyAccessExpression.Type),
navigationExpansionExpression.State.PendingTags,
navigationExpansionExpression.State.CustomRootMappings,
navigationExpansionExpression.State.MaterializeCollectionNavigation);

Expand Down
Loading

0 comments on commit c3225b6

Please sign in to comment.