/src/NHibernate.Spatial/src/NHibernate.Spatial/Linq/Functions/SpatialMethodGenerator.cs
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
- using System;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using NHibernate.Hql.Ast;
- using NHibernate.Linq;
- using NHibernate.Linq.Functions;
- using NHibernate.Linq.Visitors;
- using NHibernate.Spatial.Dialect;
-
- namespace NHibernate.Spatial.Linq.Functions
- {
- public abstract class SpatialMethodGenerator<TSource, TResult> : BaseHqlGeneratorForMethod
- {
- private readonly string methodName;
-
- protected SpatialMethodGenerator(string methodName, params Expression<Action<TSource>>[] expressions)
- {
- this.methodName = methodName;
- SupportedMethods = expressions.Select(o => ReflectionHelper.GetMethodDefinition(o)).ToArray();
- }
-
- protected SpatialMethodGenerator(params Expression<Action<TSource>>[] expressions)
- : this(null, expressions)
- {
- }
-
- public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
- {
- var isExtensionMethod = (targetObject == null);
- var expressions = isExtensionMethod ? arguments : new[] { targetObject }.Concat(arguments);
- var parameters = expressions.Select(o => visitor.Visit(o).AsExpression());
- var methodCall = treeBuilder.MethodCall(SpatialDialect.HqlPrefix + (methodName ?? method.Name), parameters);
- if (typeof(TResult) == typeof(bool))
- {
- return treeBuilder.Equality(methodCall, treeBuilder.True());
- }
- return methodCall;
- }
- }
- }