Skip to content

Commit

Permalink
Use correct parameter in query based on type mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Nov 22, 2022
1 parent 6b3b852 commit 16660ca
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 14 deletions.
23 changes: 16 additions & 7 deletions src/EFCore.Relational/Query/QuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,21 +598,30 @@ protected override Expression VisitSqlParameter(SqlParameterExpression sqlParame
var invariantName = sqlParameterExpression.Name;
var parameterName = sqlParameterExpression.Name;

if (_relationalCommandBuilder.Parameters
.All(
p => p.InvariantName != parameterName
|| (p is TypeMappedRelationalParameter typeMappedRelationalParameter
&& (typeMappedRelationalParameter.RelationalTypeMapping.StoreType != sqlParameterExpression.TypeMapping!.StoreType
|| typeMappedRelationalParameter.RelationalTypeMapping.Converter
!= sqlParameterExpression.TypeMapping!.Converter))))
// Try to see if a parameter already exists - if so, just integrate the same placeholder into the SQL instead of sending the same
// data twice.
// Note that if the type mapping differs, we do send the same data twice (e.g. the same string may be sent once as Unicode, once as
// non-Unicode).
var parameter = _relationalCommandBuilder.Parameters.FirstOrDefault(p =>
p.InvariantName == parameterName
&& p is TypeMappedRelationalParameter typeMappedRelationalParameter
&& typeMappedRelationalParameter.RelationalTypeMapping.StoreType == sqlParameterExpression.TypeMapping!.StoreType
&& typeMappedRelationalParameter.RelationalTypeMapping.Converter == sqlParameterExpression.TypeMapping!.Converter);

if (parameter is null)
{
parameterName = GetUniqueParameterName(parameterName);

_relationalCommandBuilder.AddParameter(
invariantName,
_sqlGenerationHelper.GenerateParameterName(parameterName),
sqlParameterExpression.TypeMapping!,
sqlParameterExpression.IsNullable);
}
else
{
parameterName = ((TypeMappedRelationalParameter)parameter).Name;
}

_relationalCommandBuilder
.Append(_sqlGenerationHelper.GenerateParameterNamePlaceholder(parameterName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public virtual Task Parameter_used_multiple_times_take_appropriate_inferred_type
var place = "Seattle";
return AssertQuery(
async,
ss => ss.Set<City>().Where(e => e.Nation == place || e.Location == place));
ss => ss.Set<City>().Where(e => e.Nation == place || e.Location == place || e.Location == place));
}

public override async Task Correlated_collection_with_distinct_not_projecting_identifier_column_also_projecting_complex_expressions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9413,7 +9413,7 @@ public override async Task Parameter_used_multiple_times_take_appropriate_inferr

SELECT [c].[Name], [c].[Location], [c].[Nation]
FROM [Cities] AS [c]
WHERE [c].[Nation] = @__place_0 OR [c].[Location] = @__place_0_1
WHERE [c].[Nation] = @__place_0 OR [c].[Location] = @__place_0_1 OR [c].[Location] = @__place_0_1
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11939,7 +11939,7 @@ public override async Task Parameter_used_multiple_times_take_appropriate_inferr

SELECT [c].[Name], [c].[Location], [c].[Nation]
FROM [Cities] AS [c]
WHERE [c].[Nation] = @__place_0 OR [c].[Location] = @__place_0_1
WHERE [c].[Nation] = @__place_0 OR [c].[Location] = @__place_0_1 OR [c].[Location] = @__place_0_1
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10225,7 +10225,7 @@ public override async Task Parameter_used_multiple_times_take_appropriate_inferr

SELECT [c].[Name], [c].[Location], [c].[Nation]
FROM [Cities] AS [c]
WHERE [c].[Nation] = @__place_0 OR [c].[Location] = @__place_0_1
WHERE [c].[Nation] = @__place_0 OR [c].[Location] = @__place_0_1 OR [c].[Location] = @__place_0_1
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3252,7 +3252,7 @@ public override async Task Parameter_used_multiple_times_take_appropriate_inferr

SELECT [c].[Name], [c].[Location], [c].[Nation], [c].[PeriodEnd], [c].[PeriodStart]
FROM [Cities] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [c]
WHERE [c].[Nation] = @__place_0 OR [c].[Location] = @__place_0_1
WHERE [c].[Nation] = @__place_0 OR [c].[Location] = @__place_0_1 OR [c].[Location] = @__place_0_1
""");
}

Expand Down Expand Up @@ -9794,7 +9794,7 @@ public override async Task EF_Property_based_Include_navigation_on_derived_type(
""");
}

// Sequence contains no elements due to temporal filter
// Sequence contains no elements due to temporal filter
public override Task ElementAt_basic_with_OrderBy(bool async)
=> Task.CompletedTask;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2903,7 +2903,7 @@ public override async Task Parameter_used_multiple_times_take_appropriate_inferr

SELECT "c"."Name", "c"."Location", "c"."Nation"
FROM "Cities" AS "c"
WHERE "c"."Nation" = @__place_0 OR "c"."Location" = @__place_0
WHERE "c"."Nation" = @__place_0 OR "c"."Location" = @__place_0 OR "c"."Location" = @__place_0
""");
}

Expand Down

0 comments on commit 16660ca

Please sign in to comment.