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