|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using System.Text; |
| 6 | +using NHibernate.Linq.Visitors; |
| 7 | +using NUnit.Framework; |
| 8 | +using Remotion.Linq; |
| 9 | +using Remotion.Linq.Clauses; |
| 10 | +using Remotion.Linq.Parsing; |
| 11 | + |
| 12 | +namespace NHibernate.Test.Linq |
| 13 | +{ |
| 14 | + public class CustomQueryModelRewriterTests : LinqTestCase |
| 15 | + { |
| 16 | + protected override void Configure(Cfg.Configuration configuration) |
| 17 | + { |
| 18 | + configuration.Properties[Cfg.Environment.QueryModelRewriterFactory] = typeof(QueryModelRewriterFactory).AssemblyQualifiedName; |
| 19 | + } |
| 20 | + |
| 21 | + [Test] |
| 22 | + public void RewriteNullComparison() |
| 23 | + { |
| 24 | + // This example shows how to use the query model rewriter to |
| 25 | + // make radical changes to the query. In this case, we rewrite |
| 26 | + // a null comparison (which would translate into a IS NULL) |
| 27 | + // into a comparison to "Thomas Hardy" (which translates to a = "Thomas Hardy"). |
| 28 | + |
| 29 | + var contacts = (from c in db.Customers where c.ContactName == null select c).ToList(); |
| 30 | + Assert.Greater(contacts.Count, 0); |
| 31 | + Assert.IsTrue(contacts.Select(customer => customer.ContactName).All(c => c == "Thomas Hardy")); |
| 32 | + } |
| 33 | + |
| 34 | + [Serializable] |
| 35 | + public class QueryModelRewriterFactory : IQueryModelRewriterFactory |
| 36 | + { |
| 37 | + public QueryModelVisitorBase CreateVisitor(VisitorParameters parameters) |
| 38 | + { |
| 39 | + return new CustomVisitor(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public class CustomVisitor : QueryModelVisitorBase |
| 44 | + { |
| 45 | + public override void VisitWhereClause(WhereClause whereClause, QueryModel queryModel, int index) |
| 46 | + { |
| 47 | + whereClause.TransformExpressions(new Visitor().VisitExpression); |
| 48 | + } |
| 49 | + |
| 50 | + private class Visitor : ExpressionTreeVisitor |
| 51 | + { |
| 52 | + protected override Expression VisitBinaryExpression(BinaryExpression expression) |
| 53 | + { |
| 54 | + if ( |
| 55 | + expression.NodeType == ExpressionType.Equal || |
| 56 | + expression.NodeType == ExpressionType.NotEqual |
| 57 | + ) |
| 58 | + { |
| 59 | + var left = expression.Left; |
| 60 | + var right = expression.Right; |
| 61 | + bool reverse = false; |
| 62 | + |
| 63 | + if (!(left is ConstantExpression) && right is ConstantExpression) |
| 64 | + { |
| 65 | + var tmp = left; |
| 66 | + left = right; |
| 67 | + right = tmp; |
| 68 | + reverse = true; |
| 69 | + } |
| 70 | + |
| 71 | + var constant = left as ConstantExpression; |
| 72 | + |
| 73 | + if (constant != null && constant.Value == null) |
| 74 | + { |
| 75 | + left = Expression.Constant("Thomas Hardy"); |
| 76 | + |
| 77 | + expression = Expression.MakeBinary( |
| 78 | + expression.NodeType, |
| 79 | + reverse ? right : left, |
| 80 | + reverse ? left : right |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return base.VisitBinaryExpression(expression); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments