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

Generate multiple query tags without newlines between them #25963

Merged
merged 1 commit into from
Sep 11, 2021
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
5 changes: 2 additions & 3 deletions src/EFCore.Relational/Query/QuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,9 @@ protected virtual void GenerateTagsHeaderComment(SelectExpression selectExpressi
{
foreach (var tag in selectExpression.Tags)
{
_relationalCommandBuilder
.AppendLines(_sqlGenerationHelper.GenerateComment(tag))
.AppendLine();
_relationalCommandBuilder.AppendLines(_sqlGenerationHelper.GenerateComment(tag));
}
_relationalCommandBuilder.AppendLine();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private void VerifyReturnType(Expression expression, ParameterExpression lambdaP

var filePath = methodCallExpression.Arguments[1].GetConstantValue<string>();
var lineNumber = methodCallExpression.Arguments[2].GetConstantValue<int>();
_queryCompilationContext.AddTag($"file: {filePath}:{lineNumber}");
_queryCompilationContext.AddTag($"File: {filePath}:{lineNumber}");

return visitedExpression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ public virtual async Task From_sql_queryable_stored_procedure_with_tag(bool asyn
&& mep.UnitPrice == 263.50m);
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public virtual async Task From_sql_queryable_stored_procedure_with_tags(bool async)
{
using var context = CreateContext();
var query = context
.Set<MostExpensiveProduct>()
.FromSqlRaw(TenMostExpensiveProductsSproc, GetTenMostExpensiveProductsParameters())
.TagWith("One")
.TagWith("Two")
.TagWith("Three");

var actual = async
? await query.ToArrayAsync()
: query.ToArray();

Assert.Equal(10, actual.Length);

Assert.Contains(
actual, mep =>
mep.TenMostExpensiveProducts == "Côte de Blaye"
&& mep.UnitPrice == 263.50m);
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
Expand All @@ -83,7 +108,31 @@ public virtual async Task From_sql_queryable_stored_procedure_with_caller_info_t

var actual = query.ToQueryString().Split(Environment.NewLine).First();

Assert.Equal("-- file: SampleFileName:13", actual);
Assert.Equal("-- File: SampleFileName:13", actual);
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public virtual async Task From_sql_queryable_stored_procedure_with_caller_info_tag_and_other_tags(bool async)
{
using var context = CreateContext();
var query = context
.Set<MostExpensiveProduct>()
.FromSqlRaw(TenMostExpensiveProductsSproc, GetTenMostExpensiveProductsParameters())
.TagWith("Before")
.TagWithCallSite("SampleFileName", 13)
.TagWith("After");

var queryResult = async
? await query.ToArrayAsync()
: query.ToArray();

var tags = query.ToQueryString().Split(Environment.NewLine).ToList();

Assert.Equal("-- Before", tags[0]);
Assert.Equal("-- File: SampleFileName:13", tags[1]);
Assert.Equal("-- After", tags[2]);
}

[ConditionalTheory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ public override async Task From_sql_queryable_stored_procedure_with_tag(bool asy
AssertSql(
@"-- Stored Procedure

[dbo].[Ten Most Expensive Products]");
}

public override async Task From_sql_queryable_stored_procedure_with_tags(bool async)
{
await base.From_sql_queryable_stored_procedure_with_tags(async);

AssertSql(
@"-- One
-- Two
-- Three

[dbo].[Ten Most Expensive Products]");
}

Expand All @@ -36,7 +48,19 @@ public override async Task From_sql_queryable_stored_procedure_with_caller_info_
await base.From_sql_queryable_stored_procedure_with_caller_info_tag(async);

AssertSql(
@"-- file: SampleFileName:13
@"-- File: SampleFileName:13

[dbo].[Ten Most Expensive Products]");
}

public override async Task From_sql_queryable_stored_procedure_with_caller_info_tag_and_other_tags(bool async)
{
await base.From_sql_queryable_stored_procedure_with_caller_info_tag_and_other_tags(async);

AssertSql(
@"-- Before
-- File: SampleFileName:13
-- After

[dbo].[Ten Most Expensive Products]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public override void Single_query_multiple_tags()

AssertSql(
@"-- Yanni

-- Enya

SELECT TOP(1) [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
Expand All @@ -53,7 +52,6 @@ public override void Tags_on_subquery()

AssertSql(
@"-- Yanni

-- Laurel

SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
Expand Down Expand Up @@ -162,7 +160,6 @@ public override void Single_query_multiple_multiline_tag()
@"-- Yanni
-- AND
-- Laurel

-- Yet
-- Another
-- Multiline
Expand All @@ -179,9 +176,9 @@ public override void Single_query_multiline_tag_with_empty_lines()

AssertSql(
@"-- Yanni
--
-- " + @"
-- AND
--
-- " + @"
-- Laurel

SELECT TOP(1) [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
Expand Down