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

Query: Proper extensibility support for parameter value based SQL processor #20883

Merged
merged 1 commit into from
May 7, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static readonly IDictionary<Type, ServiceCharacteristics> RelationalServi
{ typeof(IRelationalTypeMappingSourcePlugin), new ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: true) },
{ typeof(IMethodCallTranslatorPlugin), new ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: true) },
{ typeof(IMemberTranslatorPlugin), new ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: true) },
{ typeof(IRelationalParameterBasedQueryTranslationPostprocessorFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) }
{ typeof(IRelationalParameterBasedSqlProcessorFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) }
};

/// <summary>
Expand Down Expand Up @@ -167,7 +167,7 @@ public override EntityFrameworkServicesBuilder TryAddCoreServices()
TryAdd<IRelationalSqlTranslatingExpressionVisitorFactory, RelationalSqlTranslatingExpressionVisitorFactory>();
TryAdd<ISqlExpressionFactory, SqlExpressionFactory>();
TryAdd<IQueryTranslationPreprocessorFactory, RelationalQueryTranslationPreprocessorFactory>();
TryAdd<IRelationalParameterBasedQueryTranslationPostprocessorFactory, RelationalParameterBasedQueryTranslationPostprocessorFactory>();
TryAdd<IRelationalParameterBasedSqlProcessorFactory, RelationalParameterBasedSqlProcessorFactory>();
TryAdd<IRelationalQueryStringFactory, RelationalQueryStringFactory>();

ServiceCollectionMap.GetInfrastructure()
Expand All @@ -191,7 +191,7 @@ public override EntityFrameworkServicesBuilder TryAddCoreServices()
.AddDependencySingleton<RelationalQueryTranslationPostprocessorDependencies>()
.AddDependencySingleton<RelationalEvaluatableExpressionFilterDependencies>()
.AddDependencySingleton<RelationalQueryTranslationPreprocessorDependencies>()
.AddDependencySingleton<RelationalParameterBasedQueryTranslationPostprocessorDependencies>()
.AddDependencySingleton<RelationalParameterBasedSqlProcessorDependencies>()
.AddDependencyScoped<MigrationsSqlGeneratorDependencies>()
.AddDependencyScoped<RelationalConventionSetBuilderDependencies>()
.AddDependencyScoped<ModificationCommandBatchFactoryDependencies>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.Query
{
/// <summary>
/// <para>
/// A factory for creating <see cref="RelationalParameterBasedQueryTranslationPostprocessor" /> instances.
/// A factory for creating <see cref="RelationalParameterBasedSqlProcessor" /> instances.
/// </para>
/// <para>
/// The service lifetime is <see cref="ServiceLifetime.Singleton" />. This means a single instance
/// is used by many <see cref="DbContext" /> instances. The implementation must be thread-safe.
/// This service cannot depend on services registered as <see cref="ServiceLifetime.Scoped" />.
/// </para>
/// </summary>
public interface IRelationalParameterBasedQueryTranslationPostprocessorFactory
public interface IRelationalParameterBasedSqlProcessorFactory
{
/// <summary>
/// Creates a new <see cref="RelationalParameterBasedQueryTranslationPostprocessor"/>.
/// Creates a new <see cref="RelationalParameterBasedSqlProcessor"/>.
/// </summary>
/// <param name="useRelationalNulls"> A bool value indicating if relational nulls should be used. </param>
/// <returns> A relational parameter based query translation postprocessor. </returns>
RelationalParameterBasedQueryTranslationPostprocessor Create(bool useRelationalNulls);
/// <returns> A relational parameter based sql processor. </returns>
RelationalParameterBasedSqlProcessor Create(bool useRelationalNulls);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,56 @@ namespace Microsoft.EntityFrameworkCore.Query.Internal
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class FromSqlParameterApplyingExpressionVisitor : ExpressionVisitor
public class FromSqlParameterExpandingExpressionVisitor : ExpressionVisitor
{
private readonly IDictionary<FromSqlExpression, Expression> _visitedFromSqlExpressions
= new Dictionary<FromSqlExpression, Expression>(LegacyReferenceEqualityComparer.Instance);

private readonly ISqlExpressionFactory _sqlExpressionFactory;
private readonly IRelationalTypeMappingSource _typeMappingSource;
private readonly ParameterNameGenerator _parameterNameGenerator;
private readonly IReadOnlyDictionary<string, object> _parametersValues;
private readonly IParameterNameGeneratorFactory _parameterNameGeneratorFactory;

private IReadOnlyDictionary<string, object> _parametersValues;
private ParameterNameGenerator _parameterNameGenerator;
private bool _canCache;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public FromSqlParameterExpandingExpressionVisitor(
[NotNull] RelationalParameterBasedSqlProcessorDependencies dependencies)
{
Check.NotNull(dependencies, nameof(dependencies));

_sqlExpressionFactory = dependencies.SqlExpressionFactory;
_typeMappingSource = dependencies.TypeMappingSource;
_parameterNameGeneratorFactory = dependencies.ParameterNameGeneratorFactory;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public FromSqlParameterApplyingExpressionVisitor(
[NotNull] ISqlExpressionFactory sqlExpressionFactory,
[NotNull] IRelationalTypeMappingSource typeMappingSource,
[NotNull] ParameterNameGenerator parameterNameGenerator,
[NotNull] IReadOnlyDictionary<string, object> parametersValues)
public virtual SelectExpression Expand(
[NotNull] SelectExpression selectExpression, [NotNull] IReadOnlyDictionary<string, object> parameterValues, out bool canCache)
{
Check.NotNull(sqlExpressionFactory, nameof(sqlExpressionFactory));
Check.NotNull(typeMappingSource, nameof(typeMappingSource));
Check.NotNull(parameterNameGenerator, nameof(parameterNameGenerator));
Check.NotNull(parametersValues, nameof(parametersValues));

_sqlExpressionFactory = sqlExpressionFactory;
_typeMappingSource = typeMappingSource;
_parameterNameGenerator = parameterNameGenerator;
_parametersValues = parametersValues;
Check.NotNull(selectExpression, nameof(selectExpression));
Check.NotNull(parameterValues, nameof(parameterValues));

_visitedFromSqlExpressions.Clear();
_parameterNameGenerator = _parameterNameGeneratorFactory.Create();
_parametersValues = parameterValues;
_canCache = true;

var result = (SelectExpression)Visit(selectExpression);
canCache = _canCache;

return result;
}

/// <summary>
Expand All @@ -69,6 +88,7 @@ public override Expression Visit(Expression expression)
{
case ParameterExpression parameterExpression:
var parameterValues = (object[])_parametersValues[parameterExpression.Name];
_canCache = false;

var subParameters = new List<IRelationalParameter>(parameterValues.Length);
// ReSharper disable once ForCanBeConvertedToForeach
Expand Down
10 changes: 4 additions & 6 deletions src/EFCore.Relational/Query/Internal/RelationalCommandCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private static readonly ConcurrentDictionary<object, object> _syncObjects
private readonly IMemoryCache _memoryCache;
private readonly IQuerySqlGeneratorFactory _querySqlGeneratorFactory;
private readonly SelectExpression _selectExpression;
private readonly RelationalParameterBasedQueryTranslationPostprocessor _relationalParameterBasedQueryTranslationPostprocessor;
private readonly RelationalParameterBasedSqlProcessor _relationalParameterBasedSqlProcessor;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -35,16 +35,15 @@ private static readonly ConcurrentDictionary<object, object> _syncObjects
/// </summary>
public RelationalCommandCache(
[NotNull] IMemoryCache memoryCache,
[NotNull] ISqlExpressionFactory sqlExpressionFactory,
[NotNull] IQuerySqlGeneratorFactory querySqlGeneratorFactory,
[NotNull] IRelationalParameterBasedQueryTranslationPostprocessorFactory relationalParameterBasedQueryTranslationPostprocessorFactory,
[NotNull] IRelationalParameterBasedSqlProcessorFactory relationalParameterBasedSqlProcessorFactory,
bool useRelationalNulls,
[NotNull] SelectExpression selectExpression)
{
_memoryCache = memoryCache;
_querySqlGeneratorFactory = querySqlGeneratorFactory;
_selectExpression = selectExpression;
_relationalParameterBasedQueryTranslationPostprocessor = relationalParameterBasedQueryTranslationPostprocessorFactory.Create(useRelationalNulls);
_relationalParameterBasedSqlProcessor = relationalParameterBasedSqlProcessorFactory.Create(useRelationalNulls);
}

/// <summary>
Expand All @@ -67,8 +66,7 @@ public virtual IRelationalCommand GetRelationalCommand([NotNull] IReadOnlyDictio

try
{
var selectExpression = _relationalParameterBasedQueryTranslationPostprocessor.Optimize(
_selectExpression, parameters, out var canCache);
var selectExpression = _relationalParameterBasedSqlProcessor.Optimize(_selectExpression, parameters, out var canCache);
relationalCommand = _querySqlGeneratorFactory.Create().GetCommand(selectExpression);

if (canCache)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -20,18 +21,18 @@ namespace Microsoft.EntityFrameworkCore.Query.Internal
/// This service cannot depend on services registered as <see cref="ServiceLifetime.Scoped" />.
/// </para>
/// </summary>
public class RelationalParameterBasedQueryTranslationPostprocessorFactory : IRelationalParameterBasedQueryTranslationPostprocessorFactory
public class RelationalParameterBasedSqlProcessorFactory : IRelationalParameterBasedSqlProcessorFactory
{
private readonly RelationalParameterBasedQueryTranslationPostprocessorDependencies _dependencies;
private readonly RelationalParameterBasedSqlProcessorDependencies _dependencies;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public RelationalParameterBasedQueryTranslationPostprocessorFactory(
[NotNull] RelationalParameterBasedQueryTranslationPostprocessorDependencies dependencies)
public RelationalParameterBasedSqlProcessorFactory(
[NotNull] RelationalParameterBasedSqlProcessorDependencies dependencies)
{
Check.NotNull(dependencies, nameof(dependencies));

Expand All @@ -44,7 +45,7 @@ public RelationalParameterBasedQueryTranslationPostprocessorFactory(
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual RelationalParameterBasedQueryTranslationPostprocessor Create(bool useRelationalNulls)
=> new RelationalParameterBasedQueryTranslationPostprocessor(_dependencies, useRelationalNulls);
public virtual RelationalParameterBasedSqlProcessor Create(bool useRelationalNulls)
=> new RelationalParameterBasedSqlProcessor(_dependencies, useRelationalNulls);
}
}

This file was deleted.

Loading