Skip to content

Commit

Permalink
Better support for inline primitive collections with parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed May 8, 2023
1 parent 2edfca8 commit ba83a63
Show file tree
Hide file tree
Showing 33 changed files with 738 additions and 580 deletions.
20 changes: 14 additions & 6 deletions src/EFCore.Relational/Properties/RelationalStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 34 additions & 31 deletions src/EFCore.Relational/Properties/RelationalStrings.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -130,6 +130,9 @@
<data name="CannotChangeWhenOpen" xml:space="preserve">
<value>The instance of DbConnection is currently in use. The connection can only be changed when the existing connection is not being used.</value>
</data>
<data name="CannotTranslateNonConstantNewArrayExpression" xml:space="preserve">
<value>The query contained a new array expression containing non-constant elements, which could not be translated: '{newArrayExpression}'.</value>
</data>
<data name="CannotConfigureTriggerNonRootTphEntity" xml:space="preserve">
<value>Can't configure a trigger on entity type '{entityType}', which is in a TPH hierarchy and isn't the root. Configure the trigger on the TPH root entity type '{rootEntityType}' instead.</value>
</data>
Expand Down Expand Up @@ -346,8 +349,8 @@
<data name="DuplicateSeedDataSensitive" xml:space="preserve">
<value>A seed entity for entity type '{entityType}' has the same key value {keyValue} as another seed entity mapped to the same table '{table}'. Key values should be unique across seed entities.</value>
</data>
<data name="EitherOfTwoValuesMustBeNull" xml:space="preserve">
<value>Either {param1} or {param2} must be null.</value>
<data name="OneOfThreeValuesMustBeSet" xml:space="preserve">
<value>Exactly one of '{param1}', '{param2}' or '{param3}' must be set.</value>
</data>
<data name="EmptyCollectionNotSupportedAsInlineQueryRoot" xml:space="preserve">
<value>Empty collections are not supported as inline query roots.</value>
Expand Down Expand Up @@ -911,8 +914,8 @@
<data name="NoDbCommand" xml:space="preserve">
<value>Cannot create a DbCommand for a non-relational query.</value>
</data>
<data name="NonConstantOrParameterAsInExpressionValues" xml:space="preserve">
<value>Expression of type '{type}' isn't supported as the Values of an InExpression; only constants and parameters are supported.</value>
<data name="NonConstantOrParameterAsInExpressionValue" xml:space="preserve">
<value>Expression of type '{type}' isn't supported in the values of an InExpression; only constants and parameters are supported.</value>
</data>
<data name="NoneRelationalTypeMappingOnARelationalTypeMappingSource" xml:space="preserve">
<value>'FindMapping' was called on a 'RelationalTypeMappingSource' with a non-relational 'TypeMappingInfo'.</value>
Expand Down
17 changes: 13 additions & 4 deletions src/EFCore.Relational/Query/ISqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,16 @@ SqlFunctionExpression NiladicFunction(
/// <param name="subquery">A subquery to check existence of.</param>
/// <param name="negated">A value indicating if the existence check is negated.</param>
/// <returns>An expression representing an EXISTS operation in a SQL tree.</returns>
ExistsExpression Exists(SelectExpression subquery, bool negated);
ExistsExpression Exists(SelectExpression subquery, bool negated = false);

/// <summary>
/// Creates a new <see cref="InExpression" /> which represents an IN operation in a SQL tree.
/// </summary>
/// <param name="item">An item to look into values.</param>
/// <param name="subquery">A subquery in which item is searched.</param>
/// <param name="negated">A value indicating if the item should be present in the values or absent.</param>
/// <returns>An expression representing an IN operation in a SQL tree.</returns>
InExpression In(SqlExpression item, SelectExpression subquery, bool negated = false);

/// <summary>
/// Creates a new <see cref="InExpression" /> which represents an IN operation in a SQL tree.
Expand All @@ -410,16 +419,16 @@ SqlFunctionExpression NiladicFunction(
/// <param name="values">A list of values in which item is searched.</param>
/// <param name="negated">A value indicating if the item should be present in the values or absent.</param>
/// <returns>An expression representing an IN operation in a SQL tree.</returns>
InExpression In(SqlExpression item, SqlExpression values, bool negated);
InExpression In(SqlExpression item, IReadOnlyList<SqlExpression> values, bool negated = false);

/// <summary>
/// Creates a new <see cref="InExpression" /> which represents an IN operation in a SQL tree.
/// </summary>
/// <param name="item">An item to look into values.</param>
/// <param name="subquery">A subquery in which item is searched.</param>
/// <param name="valuesParameter">A parameterized list of values in which the item is searched.</param>
/// <param name="negated">A value indicating if the item should be present in the values or absent.</param>
/// <returns>An expression representing an IN operation in a SQL tree.</returns>
InExpression In(SqlExpression item, SelectExpression subquery, bool negated);
InExpression In(SqlExpression item, SqlParameterExpression valuesParameter, bool negated = false);

/// <summary>
/// Creates a new <see cref="InExpression" /> which represents a LIKE in a SQL tree.
Expand Down
31 changes: 27 additions & 4 deletions src/EFCore.Relational/Query/Internal/ContainsTranslator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;

namespace Microsoft.EntityFrameworkCore.Query.Internal;
Expand Down Expand Up @@ -38,26 +39,48 @@ public ContainsTranslator(ISqlExpressionFactory sqlExpressionFactory)
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
SqlExpression? itemExpression = null, valuesExpression = null;

// Identify static Enumerable.Contains and instance List.Contains
if (method.IsGenericMethod
&& method.GetGenericMethodDefinition().Equals(EnumerableMethods.Contains)
&& method.GetGenericMethodDefinition() == EnumerableMethods.Contains
&& ValidateValues(arguments[0]))
{
return _sqlExpressionFactory.In(RemoveObjectConvert(arguments[1]), arguments[0], negated: false);
(itemExpression, valuesExpression) = (RemoveObjectConvert(arguments[1]), arguments[0]);
}

if (arguments.Count == 1
&& method.IsContainsMethod()
&& instance != null
&& ValidateValues(instance))
{
return _sqlExpressionFactory.In(RemoveObjectConvert(arguments[0]), instance, negated: false);
(itemExpression, valuesExpression) = (RemoveObjectConvert(arguments[0]), instance);
}

if (itemExpression is not null && valuesExpression is not null)
{
switch (valuesExpression)
{
case SqlParameterExpression parameter:
return _sqlExpressionFactory.In(itemExpression, parameter);

case SqlConstantExpression { Value: IEnumerable values }:
var valuesExpressions = new List<SqlExpression>();

foreach (var value in values)
{
valuesExpressions.Add(_sqlExpressionFactory.Constant(value));
}

return _sqlExpressionFactory.In(itemExpression, valuesExpressions);
}
}

return null;
}

private static bool ValidateValues(SqlExpression values)
=> values is SqlConstantExpression || values is SqlParameterExpression;
=> values is SqlConstantExpression or SqlParameterExpression;

private static SqlExpression RemoveObjectConvert(SqlExpression expression)
=> expression is SqlUnaryExpression sqlUnaryExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,38 +130,58 @@ public virtual Expression Expand(
return _visitedFromSqlExpressions[fromSql] = fromSql.Update(
Expression.Constant(new CompositeRelationalParameter(parameterExpression.Name!, subParameters)));

case ConstantExpression constantExpression:
var existingValues = constantExpression.GetConstantValue<object?[]>();
case ConstantExpression { Value: object?[] existingValues }:
{
var constantValues = new object?[existingValues.Length];
for (var i = 0; i < existingValues.Length; i++)
{
var value = existingValues[i];
if (value is DbParameter dbParameter)
{
var parameterName = _parameterNameGenerator.GenerateNext();
if (string.IsNullOrEmpty(dbParameter.ParameterName))
{
dbParameter.ParameterName = parameterName;
}
else
{
parameterName = dbParameter.ParameterName;
}
constantValues[i] = existingValues[i];
}

constantValues[i] = new RawRelationalParameter(parameterName, dbParameter);
}
else
return _visitedFromSqlExpressions[fromSql] = fromSql.Update(Expression.Constant(constantValues, typeof(object[])));
}

case NewArrayExpression { Expressions: var expressions }:
{
var constantValues = new object?[expressions.Count];
for (var i = 0; i < constantValues.Length; i++)
{
if (expressions[i] is not SqlConstantExpression { Value: var existingValue })
{
constantValues[i] = _sqlExpressionFactory.Constant(
value, _typeMappingSource.GetMappingForValue(value));
Check.DebugFail("FromSql.Arguments must be Constant/ParameterExpression");
throw new InvalidOperationException();
}

constantValues[i] = ProcessConstantValue(existingValue);
}

return _visitedFromSqlExpressions[fromSql] = fromSql.Update(Expression.Constant(constantValues, typeof(object[])));
}

default:
Check.DebugFail("FromSql.Arguments must be Constant/ParameterExpression");
return null;
}

object ProcessConstantValue(object existingConstantValue)
{
if (existingConstantValue is DbParameter dbParameter)
{
var parameterName = _parameterNameGenerator.GenerateNext();
if (string.IsNullOrEmpty(dbParameter.ParameterName))
{
dbParameter.ParameterName = parameterName;
}
else
{
parameterName = dbParameter.ParameterName;
}

return new RawRelationalParameter(parameterName, dbParameter);
}

return _sqlExpressionFactory.Constant(
existingConstantValue, _typeMappingSource.GetMappingForValue(existingConstantValue));
}
}
}
Loading

0 comments on commit ba83a63

Please sign in to comment.