/src/NHibernate.Test/Async/Linq/CustomQueryModelRewriterTests.cs
C# | 103 lines | 78 code | 13 blank | 12 comment | 11 complexity | 95f695ebda23a236cb082aa9f4ee2419 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, CC-BY-SA-3.0
- //------------------------------------------------------------------------------
- // <auto-generated>
- // This code was generated by AsyncGenerator.
- //
- // Changes to this file may cause incorrect behavior and will be lost if
- // the code is regenerated.
- // </auto-generated>
- //------------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using NHibernate.Linq.Visitors;
- using NUnit.Framework;
- using Remotion.Linq;
- using Remotion.Linq.Clauses;
- using Remotion.Linq.Parsing;
- using NHibernate.Linq;
- namespace NHibernate.Test.Linq
- {
- using System.Threading.Tasks;
- [TestFixture]
- public class CustomQueryModelRewriterTestsAsync : LinqTestCase
- {
- protected override void Configure(Cfg.Configuration configuration)
- {
- configuration.Properties[Cfg.Environment.QueryModelRewriterFactory] = typeof(QueryModelRewriterFactory).AssemblyQualifiedName;
- }
- [Test]
- public async Task RewriteNullComparisonAsync()
- {
- // This example shows how to use the query model rewriter to
- // make radical changes to the query. In this case, we rewrite
- // a null comparison (which would translate into a IS NULL)
- // into a comparison to "Thomas Hardy" (which translates to a = "Thomas Hardy").
- var contacts = await ((from c in db.Customers where c.ContactName == null select c).ToListAsync());
- Assert.Greater(contacts.Count, 0);
- Assert.IsTrue(contacts.Select(customer => customer.ContactName).All(c => c == "Thomas Hardy"));
- }
- [Serializable]
- public class QueryModelRewriterFactory : IQueryModelRewriterFactory
- {
- public QueryModelVisitorBase CreateVisitor(VisitorParameters parameters)
- {
- return new CustomVisitor();
- }
- }
- public class CustomVisitor : NhQueryModelVisitorBase
- {
- public override void VisitWhereClause(WhereClause whereClause, QueryModel queryModel, int index)
- {
- whereClause.TransformExpressions(new Visitor().Visit);
- }
- private class Visitor : RelinqExpressionVisitor
- {
- protected override Expression VisitBinary(BinaryExpression expression)
- {
- if (
- expression.NodeType == ExpressionType.Equal ||
- expression.NodeType == ExpressionType.NotEqual
- )
- {
- var left = expression.Left;
- var right = expression.Right;
- bool reverse = false;
- if (!(left is ConstantExpression) && right is ConstantExpression)
- {
- var tmp = left;
- left = right;
- right = tmp;
- reverse = true;
- }
- var constant = left as ConstantExpression;
- if (constant != null && constant.Value == null)
- {
- left = Expression.Constant("Thomas Hardy");
- expression = Expression.MakeBinary(
- expression.NodeType,
- reverse ? right : left,
- reverse ? left : right
- );
- }
- }
- return base.VisitBinary(expression);
- }
- }
- }
- }
- }