|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// <auto-generated> |
| 3 | +// This code was generated by AsyncGenerator. |
| 4 | +// |
| 5 | +// Changes to this file may cause incorrect behavior and will be lost if |
| 6 | +// the code is regenerated. |
| 7 | +// </auto-generated> |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | + |
| 10 | + |
| 11 | +using System; |
| 12 | +using System.Collections.Generic; |
| 13 | +using System.Linq; |
| 14 | +using System.Linq.Expressions; |
| 15 | +using System.Reflection; |
| 16 | +using NHibernate.Linq; |
| 17 | +using NHibernate.Linq.Visitors; |
| 18 | +using NHibernate.Util; |
| 19 | +using NUnit.Framework; |
| 20 | +using Remotion.Linq.Parsing.ExpressionVisitors.Transformation; |
| 21 | + |
| 22 | +namespace NHibernate.Test.Linq |
| 23 | +{ |
| 24 | + using System.Threading.Tasks; |
| 25 | + [TestFixture] |
| 26 | + public class CustomPreTransformRegistrarTestsAsync : LinqTestCase |
| 27 | + { |
| 28 | + protected override void Configure(Cfg.Configuration configuration) |
| 29 | + { |
| 30 | + configuration.Properties[Cfg.Environment.PreTransformerRegistrar] = typeof(PreTransformerRegistrar).AssemblyQualifiedName; |
| 31 | + } |
| 32 | + |
| 33 | + [Test] |
| 34 | + public async Task RewriteLikeAsync() |
| 35 | + { |
| 36 | + // This example shows how to use the pre-transformer registrar to rewrite the |
| 37 | + // query so that StartsWith, EndsWith and Contains methods will generate the same sql. |
| 38 | + var queryPlanCache = GetQueryPlanCache(); |
| 39 | + queryPlanCache.Clear(); |
| 40 | + await (db.Customers.Where(o => o.ContactName.StartsWith("A")).ToListAsync()); |
| 41 | + await (db.Customers.Where(o => o.ContactName.EndsWith("A")).ToListAsync()); |
| 42 | + await (db.Customers.Where(o => o.ContactName.Contains("A")).ToListAsync()); |
| 43 | + |
| 44 | + Assert.That(queryPlanCache.Count, Is.EqualTo(1)); |
| 45 | + } |
| 46 | + |
| 47 | + [Serializable] |
| 48 | + public class PreTransformerRegistrar : IExpressionTransformerRegistrar |
| 49 | + { |
| 50 | + public void Register(ExpressionTransformerRegistry expressionTransformerRegistry) |
| 51 | + { |
| 52 | + expressionTransformerRegistry.Register(new LikeTransformer()); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private class LikeTransformer : IExpressionTransformer<MethodCallExpression> |
| 57 | + { |
| 58 | + private static readonly MethodInfo Like = ReflectHelper.GetMethodDefinition(() => SqlMethods.Like(null, null)); |
| 59 | + private static readonly MethodInfo EndsWith = ReflectHelper.GetMethodDefinition<string>(x => x.EndsWith(null)); |
| 60 | + private static readonly MethodInfo StartsWith = ReflectHelper.GetMethodDefinition<string>(x => x.StartsWith(null)); |
| 61 | + private static readonly MethodInfo Contains = ReflectHelper.GetMethodDefinition<string>(x => x.Contains(null)); |
| 62 | + private static readonly Dictionary<MethodInfo, Func<object, string>> ValueTransformers = |
| 63 | + new Dictionary<MethodInfo, Func<object, string>> |
| 64 | + { |
| 65 | + {StartsWith, s => $"{s}%"}, |
| 66 | + {EndsWith, s => $"%{s}"}, |
| 67 | + {Contains, s => $"%{s}%"}, |
| 68 | + }; |
| 69 | + |
| 70 | + public Expression Transform(MethodCallExpression expression) |
| 71 | + { |
| 72 | + if (ValueTransformers.TryGetValue(expression.Method, out var valueTransformer) && |
| 73 | + expression.Arguments[0] is ConstantExpression constantExpression) |
| 74 | + { |
| 75 | + return Expression.Call( |
| 76 | + Like, |
| 77 | + expression.Object, |
| 78 | + Expression.Constant(valueTransformer(constantExpression.Value)) |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + return expression; |
| 83 | + } |
| 84 | + |
| 85 | + public ExpressionType[] SupportedExpressionTypes { get; } = {ExpressionType.Call}; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments