Skip to content

Commit

Permalink
Add hooks for expression fragments and visitors
Browse files Browse the repository at this point in the history
Creates Npgsql implementations of `IExpressionFragmentTranslator`
and `IQueryOptimizer` to support more complex translations.

These classes make it easier to inject new fragment translations
and expression rewrites into the EF Core translation process.

This also includes documentation and cleanup of the composite
method call and member translators.

This is split out from npgsql#431. Working examples can be found there.
  • Loading branch information
austindrenski committed Jun 1, 2018
1 parent c383f1e commit e17c5ea
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/EFCore.PG/Extensions/NpgsqlServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
{
// ReSharper disable once UnusedMember.Global
public static class NpgsqlEntityFrameworkServicesBuilderExtensions
{
/// <summary>
Expand Down Expand Up @@ -106,6 +107,8 @@ public static IServiceCollection AddEntityFrameworkNpgsql([NotNull] this IServic
.TryAdd<IQueryCompilationContextFactory, NpgsqlQueryCompilationContextFactory>()
.TryAdd<IMemberTranslator, NpgsqlCompositeMemberTranslator>()
.TryAdd<ICompositeMethodCallTranslator, NpgsqlCompositeMethodCallTranslator>()
.TryAdd<IExpressionFragmentTranslator, NpgsqlCompositeExpressionFragmentTranslator>()
.TryAdd<IQueryOptimizer, NpgsqlQueryOptimizer>()
.TryAdd<IQuerySqlGeneratorFactory, NpgsqlQuerySqlGeneratorFactory>()
.TryAdd<ISqlTranslatingExpressionVisitorFactory, NpgsqlSqlTranslatingExpressionVisitorFactory>()
.TryAdd<ISingletonOptions, INpgsqlOptions>(p => p.GetService<INpgsqlOptions>())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#region License

// The PostgreSQL License
//
// Copyright (C) 2016 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

#endregion

using System.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators;

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal
{
/// <summary>
/// A composite expression fragment translator that dispatches to multiple specialized translators specific to Npgsql.
/// </summary>
public class NpgsqlCompositeExpressionFragmentTranslator : RelationalCompositeExpressionFragmentTranslator
{
/// <summary>
/// The default expression fragment translators registered by the Npgsql provider.
/// </summary>
[NotNull] [ItemNotNull] static readonly IExpressionFragmentTranslator[] ExpressionFragmentTranslators = {};

/// <inheritdoc />
public NpgsqlCompositeExpressionFragmentTranslator(
[NotNull] RelationalCompositeExpressionFragmentTranslatorDependencies dependencies)
: base(dependencies)
{
// ReSharper disable once DoNotCallOverridableMethodsInConstructor
AddTranslators(ExpressionFragmentTranslators);
}

/// <summary>
/// Adds additional dispatches to the translators list.
/// </summary>
/// <param name="translators">The translators.</param>
public new virtual void AddTranslators([NotNull] IEnumerable<IExpressionFragmentTranslator> translators)
=> base.AddTranslators(translators);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#region License

// The PostgreSQL License
//
// Copyright (C) 2016 The Npgsql Development Team
Expand All @@ -19,6 +20,7 @@
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

#endregion

using System.Collections.Generic;
Expand All @@ -28,23 +30,37 @@

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal
{
/// <summary>
/// A composite member translator that dispatches to multiple specialized member translators specific to Npgsql.
/// </summary>
public class NpgsqlCompositeMemberTranslator : RelationalCompositeMemberTranslator
{
/// <summary>
/// The default member translators registered by the Npgsql provider.
/// </summary>
[NotNull] [ItemNotNull] static readonly IMemberTranslator[] MemberTranslators =
{
new NpgsqlStringLengthTranslator(),
new NpgsqlDateTimeMemberTranslator()
};

/// <inheritdoc />
public NpgsqlCompositeMemberTranslator(
[NotNull] RelationalCompositeMemberTranslatorDependencies dependencies,
[NotNull] INpgsqlOptions npgsqlOptions)
: base(dependencies)
{
AddTranslators(new List<IMemberTranslator>
{
new NpgsqlStringLengthTranslator(),
new NpgsqlDateTimeMemberTranslator()
});
// ReSharper disable once VirtualMemberCallInConstructor
AddTranslators(MemberTranslators);

foreach (var plugin in npgsqlOptions.Plugins)
plugin.AddMemberTranslators(this);
}

/// <summary>
/// Adds additional dispatches to the translators list.
/// </summary>
/// <param name="translators">The translators.</param>
public new virtual void AddTranslators([NotNull] IEnumerable<IMemberTranslator> translators)
=> base.AddTranslators(translators);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#region License

// The PostgreSQL License
//
// Copyright (C) 2016 The Npgsql Development Team
Expand All @@ -19,6 +20,7 @@
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

#endregion

using System.Collections.Generic;
Expand All @@ -28,9 +30,15 @@

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal
{
/// <summary>
/// A composite method call translator that dispatches to multiple specialized method call translators specific to Npgsql.
/// </summary>
public class NpgsqlCompositeMethodCallTranslator : RelationalCompositeMethodCallTranslator
{
static readonly IMethodCallTranslator[] _methodCallTranslators =
/// <summary>
/// The default method call translators registered by the Npgsql provider.
/// </summary>
[NotNull] [ItemNotNull] static readonly IMethodCallTranslator[] MethodCallTranslators =
{
new NpgsqlArraySequenceEqualTranslator(),
new NpgsqlConvertTranslator(),
Expand All @@ -55,18 +63,23 @@ public class NpgsqlCompositeMethodCallTranslator : RelationalCompositeMethodCall
new NpgsqlRangeTranslator()
};

/// <inheritdoc />
public NpgsqlCompositeMethodCallTranslator(
[NotNull] RelationalCompositeMethodCallTranslatorDependencies dependencies,
[NotNull] INpgsqlOptions npgsqlOptions)
: base(dependencies)
{
// ReSharper disable once DoNotCallOverridableMethodsInConstructor
AddTranslators(_methodCallTranslators);
AddTranslators(MethodCallTranslators);

foreach (var plugin in npgsqlOptions.Plugins)
plugin.AddMethodCallTranslators(this);
}

/// <summary>
/// Adds additional dispatches to the translators list.
/// </summary>
/// <param name="translators">The translators.</param>
public new virtual void AddTranslators([NotNull] IEnumerable<IMethodCallTranslator> translators)
=> base.AddTranslators(translators);
}
Expand Down
55 changes: 55 additions & 0 deletions src/EFCore.PG/Query/Internal/NpgsqlQueryOptimizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#region License

// The PostgreSQL License
//
// Copyright (C) 2016 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

#endregion

using System.Linq.Expressions;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Remotion.Linq;

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal
{
/// <summary>
/// The default relational LINQ query optimizer for Npgsql.
/// </summary>
public class NpgsqlQueryOptimizer : QueryOptimizer
{
/// <summary>
/// The default expression visitors registered by the Npgsql provider.
/// </summary>
[NotNull] [ItemNotNull] static readonly ExpressionVisitor[] ExpressionVisitors = {};

/// <inheritdoc />
public override void Optimize(QueryCompilationContext queryCompilationContext, QueryModel queryModel)
{
base.Optimize(queryCompilationContext, queryModel);

for (int i = 0; i < ExpressionVisitors.Length; i++)
{
queryModel.TransformExpressions(ExpressionVisitors[i].Visit);
}
}
}
}

0 comments on commit e17c5ea

Please sign in to comment.