PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NHibernate.Spatial/src/NHibernate.Spatial/Linq/Functions/SpatialMethodGenerator.cs

https://bitbucket.org/dabide/nhcontrib
C# | 42 lines | 38 code | 4 blank | 0 comment | 3 complexity | 6b78c3cd6f4580f15eee668f9fe42c3c MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Apache-2.0, LGPL-3.0, LGPL-2.1
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using NHibernate.Hql.Ast;
  7. using NHibernate.Linq;
  8. using NHibernate.Linq.Functions;
  9. using NHibernate.Linq.Visitors;
  10. using NHibernate.Spatial.Dialect;
  11. namespace NHibernate.Spatial.Linq.Functions
  12. {
  13. public abstract class SpatialMethodGenerator<TSource, TResult> : BaseHqlGeneratorForMethod
  14. {
  15. private readonly string methodName;
  16. protected SpatialMethodGenerator(string methodName, params Expression<Action<TSource>>[] expressions)
  17. {
  18. this.methodName = methodName;
  19. SupportedMethods = expressions.Select(o => ReflectionHelper.GetMethodDefinition(o)).ToArray();
  20. }
  21. protected SpatialMethodGenerator(params Expression<Action<TSource>>[] expressions)
  22. : this(null, expressions)
  23. {
  24. }
  25. public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
  26. {
  27. var isExtensionMethod = (targetObject == null);
  28. var expressions = isExtensionMethod ? arguments : new[] { targetObject }.Concat(arguments);
  29. var parameters = expressions.Select(o => visitor.Visit(o).AsExpression());
  30. var methodCall = treeBuilder.MethodCall(SpatialDialect.HqlPrefix + (methodName ?? method.Name), parameters);
  31. if (typeof(TResult) == typeof(bool))
  32. {
  33. return treeBuilder.Equality(methodCall, treeBuilder.True());
  34. }
  35. return methodCall;
  36. }
  37. }
  38. }