I'm having issues when trying to define an 'IsBetween' extension method per
the attached files. NHibernate ends up throwing the following error and
I'm at a loss about it:
NHibernate.Hql.Ast.ANTLR.QuerySyntaxExceptionA recognition error occurred.
[.Where[GpisReplacement.Domain.Event](NHibernate.Linq.NhQueryable`1[GpisReplacement.Domain.Event],
Quote((x, ) => (.IsBetween(x.FromDate, p1, p2, ))), )]
at NHibernate.Hql.Ast.ANTLR.ErrorCounter.ThrowQueryException()
at NHibernate.Hql.Ast.ANTLR.HqlSqlTranslator.Translate()
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.DoCompile(IDictionary`2
replacements, Boolean shallow, String collectionRole)
at
NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode
ast, String queryIdentifier, String collectionRole, Boolean shallow,
IDictionary`2 filters, ISessionFactoryImplementor factory)
at
NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression
queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression
queryExpression, Boolean shallow)
at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression
queryExpression)
at NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression
expression, ref IQuery query, ref NhLinqExpression nhQuery)
at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
at Remotion.Linq.QueryableBase`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList(IEnumerable`1 source)
at GpisReplacement.Test.TestBetween.CanBetween() in TestBetween.cs: line
25
The other two extension methods come off without a hitch.
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to nhusers+unsubscr...@googlegroups.com.
To post to this group, send email to nhusers@googlegroups.com.
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using GpisReplacement.Arch.QueryExtensions;
using NHibernate.Hql.Ast;
using NHibernate.Linq;
using NHibernate.Linq.Functions;
using NHibernate.Linq.Visitors;
namespace GpisReplacement.Arch.Implementation.QueryExtensions
{
public class IsBetweenGenerator : BaseHqlGeneratorForMethod
{
public IsBetweenGenerator()
{
SupportedMethods = new[]
{
ReflectionHelper.GetMethodDefinition(() => QueryableExtensions.IsBetween(default(DateTime), default(DateTime), default(DateTime))),
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments,
HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
var subject = visitor.Visit(arguments[0]).AsExpression();
var lowerBound = visitor.Visit(arguments[1]).AsExpression();
var upperBound = visitor.Visit(arguments[2]).AsExpression();
var min = treeBuilder.LessThanOrEqual(lowerBound, subject);
var max = treeBuilder.LessThanOrEqual(subject, upperBound);
return treeBuilder.BooleanAnd(min, max);
}
}
}
using System;
using System.Text.RegularExpressions;
namespace GpisReplacement.Arch.QueryExtensions
{
public static class QueryableExtensions
{
public static bool IsLike(this string source, string pattern)
{
pattern = Regex.Escape(pattern);
pattern = pattern.Replace("%", ".*?").Replace("_", ".");
pattern = pattern.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");
return Regex.IsMatch(source, pattern, RegexOptions.IgnoreCase);
}
public static bool IsNullOrEmpty(this string source)
{
return String.IsNullOrEmpty(source);
}
public static bool IsBetween(this DateTime subject, DateTime lower, DateTime upper)
{
return lower.CompareTo(subject) < 1 && subject.CompareTo(upper) < 1;
}
}
}