PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NHibernate.Test/Async/Linq/CustomQueryModelRewriterTests.cs

http://github.com/nhibernate/nhibernate-core
C# | 103 lines | 78 code | 13 blank | 12 comment | 11 complexity | 95f695ebda23a236cb082aa9f4ee2419 MD5 | raw file
  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
 11using System;
 12using System.Collections.Generic;
 13using System.Linq;
 14using System.Linq.Expressions;
 15using System.Text;
 16using NHibernate.Linq.Visitors;
 17using NUnit.Framework;
 18using Remotion.Linq;
 19using Remotion.Linq.Clauses;
 20using Remotion.Linq.Parsing;
 21using NHibernate.Linq;
 22
 23namespace NHibernate.Test.Linq
 24{
 25	using System.Threading.Tasks;
 26	[TestFixture]
 27	public class CustomQueryModelRewriterTestsAsync : LinqTestCase
 28	{
 29		protected override void Configure(Cfg.Configuration configuration)
 30		{
 31			configuration.Properties[Cfg.Environment.QueryModelRewriterFactory] = typeof(QueryModelRewriterFactory).AssemblyQualifiedName;
 32		}
 33
 34		[Test]
 35		public async Task RewriteNullComparisonAsync()
 36		{
 37			// This example shows how to use the query model rewriter to
 38			// make radical changes to the query. In this case, we rewrite
 39			// a null comparison (which would translate into a IS NULL)
 40			// into a comparison to "Thomas Hardy" (which translates to a = "Thomas Hardy").
 41
 42			var contacts = await ((from c in db.Customers where c.ContactName == null select c).ToListAsync());
 43			Assert.Greater(contacts.Count, 0);
 44			Assert.IsTrue(contacts.Select(customer => customer.ContactName).All(c => c == "Thomas Hardy"));
 45		}
 46
 47		[Serializable]
 48		public class QueryModelRewriterFactory : IQueryModelRewriterFactory
 49		{
 50			public QueryModelVisitorBase CreateVisitor(VisitorParameters parameters)
 51			{
 52				return new CustomVisitor();
 53			}
 54		}
 55
 56		public class CustomVisitor : NhQueryModelVisitorBase
 57		{
 58			public override void VisitWhereClause(WhereClause whereClause, QueryModel queryModel, int index)
 59			{
 60				whereClause.TransformExpressions(new Visitor().Visit);
 61			}
 62
 63			private class Visitor : RelinqExpressionVisitor
 64			{
 65				protected override Expression VisitBinary(BinaryExpression expression)
 66				{
 67					if (
 68						expression.NodeType == ExpressionType.Equal ||
 69						expression.NodeType == ExpressionType.NotEqual
 70					)
 71					{
 72						var left = expression.Left;
 73						var right = expression.Right;
 74						bool reverse = false;
 75
 76						if (!(left is ConstantExpression) && right is ConstantExpression)
 77						{
 78							var tmp = left;
 79							left = right;
 80							right = tmp;
 81							reverse = true;
 82						}
 83
 84						var constant = left as ConstantExpression;
 85
 86						if (constant != null && constant.Value == null)
 87						{
 88							left = Expression.Constant("Thomas Hardy");
 89
 90							expression = Expression.MakeBinary(
 91								expression.NodeType,
 92								reverse ? right : left,
 93								reverse ? left : right
 94							);
 95						}
 96					}
 97
 98					return base.VisitBinary(expression);
 99				}
100			}
101		}
102	}
103}