Skip to content

Commit

Permalink
Can we run it? Yes we can!
Browse files Browse the repository at this point in the history
Design meeting feedback on #19334 for #6482

* Formatting of the relational query string can now be overridden by providers
* SQL Server overrides this to create runnable `Parameter` declarations
* Tested by:
  * Executing the DbCommand we create when using `CreateDbCommand`
  * Executing a DbCommand created externally and given the generated query string
  • Loading branch information
ajcvickers committed Dec 23, 2019
1 parent 47b6c70 commit 8e018ca
Show file tree
Hide file tree
Showing 35 changed files with 558 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class EntityFrameworkRelationalServicesBuilder : EntityFrameworkServicesB
{ typeof(IMethodCallTranslatorProvider), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMemberTranslatorProvider), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ISqlExpressionFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRelationalQueryStringFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ICommandBatchPreparer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IModificationCommandBatchFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationsModelDiffer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
Expand Down Expand Up @@ -167,6 +168,7 @@ public override EntityFrameworkServicesBuilder TryAddCoreServices()
TryAdd<ISqlExpressionFactory, SqlExpressionFactory>();
TryAdd<IQueryTranslationPreprocessorFactory, RelationalQueryTranslationPreprocessorFactory>();
TryAdd<IRelationalParameterBasedQueryTranslationPostprocessorFactory, RelationalParameterBasedQueryTranslationPostprocessorFactory>();
TryAdd<IRelationalQueryStringFactory, RelationalQueryStringFactory>();

ServiceCollectionMap.GetInfrastructure()
.AddDependencySingleton<RelationalSqlGenerationHelperDependencies>()
Expand Down
27 changes: 27 additions & 0 deletions src/EFCore.Relational/Query/IRelationalQueryStringFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 System.Data.Common;
using JetBrains.Annotations;

namespace Microsoft.EntityFrameworkCore.Query
{
/// <summary>
/// <para>
/// Implemented by database providers to generate the query string for <see cref="EntityFrameworkQueryableExtensions.ToQueryString" />.
/// </para>
/// <para>
/// This interface is typically used by database providers (and other extensions). It is generally
/// not used in application code.
/// </para>
/// </summary>
public interface IRelationalQueryStringFactory
{
/// <summary>
/// Returns a formatted query string for the given command.
/// </summary>
/// <param name="command"> The command that represents the query. </param>
/// <returns> The formatted string. </returns>
string Create([NotNull] DbCommand command);
}
}
38 changes: 0 additions & 38 deletions src/EFCore.Relational/Query/IRelationalQueryingEnumerable.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 System.Data.Common;

namespace Microsoft.EntityFrameworkCore.Query.Internal
{
/// <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 interface IRelationalQueryingEnumerable : IQueryingEnumerable
{
/// <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>
DbCommand CreateDbCommand();
}
}
19 changes: 1 addition & 18 deletions src/EFCore.Relational/Query/Internal/QueryingEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;

namespace Microsoft.EntityFrameworkCore.Query.Internal
{
Expand Down Expand Up @@ -107,22 +105,7 @@ public virtual DbCommand CreateDbCommand()
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string ToQueryString()
{
using var command = CreateDbCommand();

if (command.Parameters.Count == 0)
{
return command.CommandText;
}

var builder = new StringBuilder();
foreach (var parameter in command.Parameters.FormatParameterList(logParameterValues: true))
{
builder.Append("-- ").AppendLine(parameter);
}

return builder.Append(command.CommandText).ToString();
}
=> _relationalQueryContext.RelationalQueryStringFactory.Create(CreateDbCommand());

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 System.Data.Common;
using System.Text;
using Microsoft.EntityFrameworkCore.Storage.Internal;

namespace Microsoft.EntityFrameworkCore.Query.Internal
{
/// <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 class RelationalQueryStringFactory : IRelationalQueryStringFactory
{
/// <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 virtual string Create(DbCommand command)
{
if (command.Parameters.Count == 0)
{
return command.CommandText;
}

var builder = new StringBuilder();
foreach (DbParameter parameter in command.Parameters)
{
builder.Append("-- ").AppendLine(parameter.FormatParameter(logParameterValues: true));
}

return builder.Append(command.CommandText).ToString();
}
}
}
7 changes: 7 additions & 0 deletions src/EFCore.Relational/Query/RelationalQueryContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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 System.Data.Common;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
Expand Down Expand Up @@ -38,6 +39,12 @@ public class RelationalQueryContext : QueryContext
/// </summary>
protected virtual RelationalQueryContextDependencies RelationalDependencies { get; }

/// <summary>
/// A factory for creating a readable query string from a <see cref="DbCommand"/>
/// </summary>
public virtual IRelationalQueryStringFactory RelationalQueryStringFactory
=> RelationalDependencies.RelationalQueryStringFactory;

/// <summary>
/// Gets the active relational connection.
/// </summary>
Expand Down
21 changes: 19 additions & 2 deletions src/EFCore.Relational/Query/RelationalQueryContextDependencies.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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 System.Data.Common;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down Expand Up @@ -55,24 +56,40 @@ public sealed class RelationalQueryContextDependencies
/// </summary>
[EntityFrameworkInternal]
public RelationalQueryContextDependencies(
[NotNull] IRelationalConnection relationalConnection)
[NotNull] IRelationalConnection relationalConnection,
[NotNull] IRelationalQueryStringFactory relationalQueryStringFactory)
{
Check.NotNull(relationalConnection, nameof(relationalConnection));
Check.NotNull(relationalQueryStringFactory, nameof(relationalQueryStringFactory));

RelationalConnection = relationalConnection;
RelationalQueryStringFactory = relationalQueryStringFactory;
}

/// <summary>
/// The connection.
/// </summary>
public IRelationalConnection RelationalConnection { get; }

/// <summary>
/// A factory for creating a readable query string from a <see cref="DbCommand"/>
/// </summary>
public IRelationalQueryStringFactory RelationalQueryStringFactory { get; }

/// <summary>
/// Clones this dependency parameter object with one service replaced.
/// </summary>
/// <param name="relationalConnection"> A replacement for the current dependency of this type. </param>
/// <returns> A new parameter object with the given service replaced. </returns>
public RelationalQueryContextDependencies With([NotNull] IRelationalConnection relationalConnection)
=> new RelationalQueryContextDependencies(relationalConnection);
=> new RelationalQueryContextDependencies(relationalConnection, RelationalQueryStringFactory);

/// <summary>
/// Clones this dependency parameter object with one service replaced.
/// </summary>
/// <param name="relationalQueryStringFactory"> A replacement for the current dependency of this type. </param>
/// <returns> A new parameter object with the given service replaced. </returns>
public RelationalQueryContextDependencies With([NotNull] IRelationalQueryStringFactory relationalQueryStringFactory)
=> new RelationalQueryContextDependencies(RelationalConnection, relationalQueryStringFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,27 @@ public static class DbParameterCollectionExtensions
public static string FormatParameters(
[NotNull] this DbParameterCollection parameters,
bool logParameterValues)
=> FormatParameterList(parameters, logParameterValues).Join();
=> parameters
.Cast<DbParameter>()
.Select(p => FormatParameter(p, logParameterValues)).Join();

/// <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 static IEnumerable<string> FormatParameterList(
[NotNull] this DbParameterCollection parameters,
bool logParameterValues)
{
return parameters
.Cast<DbParameter>()
.Select(
p => FormatParameter(
p.ParameterName,
logParameterValues ? p.Value : "?",
logParameterValues,
p.Direction,
p.DbType,
p.IsNullable,
p.Size,
p.Precision,
p.Scale));
}
public static string FormatParameter([NotNull] this DbParameter parameter, bool logParameterValues)
=> FormatParameter(
parameter.ParameterName,
logParameterValues ? parameter.Value : "?",
logParameterValues,
parameter.Direction,
parameter.DbType,
parameter.IsNullable,
parameter.Size,
parameter.Precision,
parameter.Scale);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static IServiceCollection AddEntityFrameworkSqlServer([NotNull] this ISer
.TryAdd<IRelationalDatabaseCreator, SqlServerDatabaseCreator>()
.TryAdd<IHistoryRepository, SqlServerHistoryRepository>()
.TryAdd<IExecutionStrategyFactory, SqlServerExecutionStrategyFactory>()
.TryAdd<IRelationalQueryStringFactory, SqlServerQueryStringFactory>()

// New Query Pipeline
.TryAdd<IMethodCallTranslatorProvider, SqlServerMethodCallTranslatorProvider>()
Expand Down
Loading

0 comments on commit 8e018ca

Please sign in to comment.