protected override Expression VisitConditional(ConditionalExpression expr) { var visitorTrue = new SelectArgumentsVisitor(_rootCriteria, _session); visitorTrue.Visit(expr.IfTrue); var visitorFalse = new SelectArgumentsVisitor(_rootCriteria, _session); visitorFalse.Visit(expr.IfFalse); var visitorCondition = new WhereArgumentsVisitor(_rootCriteria, _session); visitorCondition.Visit(expr.Test); Conjunction conjunction = NHibernate.Criterion.Expression.Conjunction(); foreach (var criterion in visitorCondition.CurrentCriterions) { conjunction.Add(criterion); } _projections.Add( NHibernate.Criterion.Projections .Conditional(conjunction, visitorTrue.Projection, visitorFalse.Projection) ); return(expr); }
public static IEnumerable <ICriterion> GetCriterion(ICriteria rootCriteria, ISession session, Expression expression) { var visitor = new WhereArgumentsVisitor(rootCriteria, session); visitor.Visit(expression); return(visitor.CurrentCriterions); }
private IList <T> GetElementList(MethodCallExpression call, int count) { if (call.Arguments.Count > 1) { rootCriteria.Add(WhereArgumentsVisitor.GetCriterion(rootCriteria, session, call.Arguments[1])); } return(rootCriteria.SetFirstResult(0).SetMaxResults(count).List <T>()); }
private T HandleAnyCall(MethodCallExpression call) { rootCriteria.SetProjection(Projections.RowCount()); if (call.Arguments.Count > 1) { rootCriteria.Add(WhereArgumentsVisitor.GetCriterion(rootCriteria, session, call.Arguments[1])); } int count = (int)rootCriteria.UniqueResult(); //HACK: the Any method always returns bool - maybe need to make this class non-generic return((T)(object)(count > 0)); }
private Expression VisitCriterionExpression(Expression expr) { IEnumerable <ICriterion> criterion = WhereArgumentsVisitor.GetCriterion(_rootCriteria, _session, expr); if (criterion.Count() > 0) { Conjunction conjunction = new Conjunction(); foreach (ICriterion crit in criterion) { conjunction.Add(crit); } _projections.Add(Projections.Conditional( conjunction, Projections.Constant(true), Projections.Constant(false)) ); } return(expr); }
private ICriterion GetExistsCriteria(MethodCallExpression expr) { EntityExpression rootEntity = EntityExpressionVisitor.FirstEntity(expr); string propertyName = MemberNameVisitor.GetMemberName(rootCriteria, expr); var rootEntityType = rootEntity.Type; if (rootEntity.MetaData.HasProxy) { rootEntityType = rootEntity.MetaData.GetMappedClass(EntityMode.Poco); } DetachedCriteria query = DetachedCriteria.For(rootEntityType) .SetProjection(Projections.Id()) .Add(Restrictions.IsNotEmpty(propertyName)); if (expr.Arguments.Count > 1) { var arg = (LambdaExpression)LinqUtil.StripQuotes(expr.Arguments[1]); string alias = arg.Parameters[0].Name; DetachedCriteria subquery = query.CreateCriteria(propertyName, alias); var temp = new WhereArgumentsVisitor(subquery.Adapt(session), session); temp.Visit(arg.Body); foreach (ICriterion c in temp.CurrentCriterions) { subquery.Add(c); } } string identifierName = rootEntity.GetAliasedIdentifierPropertyName(); return(Subqueries.PropertyIn(identifierName, query)); }
protected override Expression VisitMethodCall(MethodCallExpression expr) { if (WhereArgumentsVisitor.SupportsMethod(expr.Method.Name)) { return(VisitCriterionExpression(expr)); } //TODO: this needs to be refactored... //create any collection subcriteria and get the collection access expression MemberNameVisitor memberVisitor = new MemberNameVisitor(_rootCriteria, true); memberVisitor.Visit(expr.Arguments[0]); CollectionAccessExpression collectionExpr = (CollectionAccessExpression)memberVisitor.CurrentExpression; string propertyName = null; IProjection projection = null; PropertyProjection currentProjection; if (expr.Arguments.Count > 1) { propertyName = MemberNameVisitor.GetMemberName(_rootCriteria, expr.Arguments[1]); } else if ((currentProjection = _rootCriteria.GetProjection() as PropertyProjection) != null) { propertyName = currentProjection.PropertyName; } switch (expr.Method.Name) { case "Average": projection = NHProjections.Avg(propertyName); break; case "Count": case "LongCount": if (expr.Arguments.Count > 1) { _rootCriteria.Add(WhereArgumentsVisitor.GetCriterion(_rootCriteria, _session, expr.Arguments[1])); } if (collectionExpr != null) { //get count on collection element's identifier property propertyName = memberVisitor.MemberName + "." + collectionExpr.ElementExpression.MetaData.IdentifierPropertyName; projection = NHProjections.Count(propertyName); } else { projection = NHProjections.RowCount(); } break; case "Max": projection = NHProjections.Max(propertyName); break; case "Min": projection = NHProjections.Min(propertyName); break; case "Sum": projection = NHProjections.Sum(propertyName); break; default: throw new NotImplementedException("The method '" + expr.Method.Name + "' is not implemented."); } _projections.Add(projection); return(expr); }
private void HandleWhereCall(MethodCallExpression call) { IEnumerable <ICriterion> criterion = WhereArgumentsVisitor.GetCriterion(rootCriteria, session, call.Arguments[1]); rootCriteria.Add(criterion); }
public static IEnumerable<ICriterion> GetCriterion(ICriteria rootCriteria, ISession session, Expression expression) { var visitor = new WhereArgumentsVisitor(rootCriteria, session); visitor.Visit(expression); return visitor.CurrentCriterions; }
private ICriterion GetExistsCriteria(MethodCallExpression expr) { EntityExpression rootEntity = EntityExpressionVisitor.FirstEntity(expr); string propertyName = MemberNameVisitor.GetMemberName(rootCriteria, expr); var rootEntityType = rootEntity.Type; if (rootEntity.MetaData.HasProxy) { rootEntityType = rootEntity.MetaData.GetMappedClass(EntityMode.Poco); } DetachedCriteria query = DetachedCriteria.For(rootEntityType) .SetProjection(Projections.Id()) .Add(Restrictions.IsNotEmpty(propertyName)); if (expr.Arguments.Count > 1) { var arg = (LambdaExpression)LinqUtil.StripQuotes(expr.Arguments[1]); string alias = arg.Parameters[0].Name; DetachedCriteria subquery = query.CreateCriteria(propertyName, alias); var temp = new WhereArgumentsVisitor(subquery.Adapt(session), session); temp.Visit(arg.Body); foreach (ICriterion c in temp.CurrentCriterions) { subquery.Add(c); } } string identifierName = rootEntity.GetAliasedIdentifierPropertyName(); return Subqueries.PropertyIn(identifierName, query); }
protected override Expression VisitConditional(ConditionalExpression expr) { var visitorTrue = new SelectArgumentsVisitor(_rootCriteria, _session); visitorTrue.Visit(expr.IfTrue); var visitorFalse = new SelectArgumentsVisitor(_rootCriteria, _session); visitorFalse.Visit(expr.IfFalse); var visitorCondition = new WhereArgumentsVisitor(_rootCriteria, _session); visitorCondition.Visit(expr.Test); Conjunction conjunction = NHibernate.Criterion.Expression.Conjunction(); foreach (var criterion in visitorCondition.CurrentCriterions) { conjunction.Add(criterion); } _projections.Add( NHibernate.Criterion.Projections .Conditional(conjunction, visitorTrue.Projection, visitorFalse.Projection) ); return expr; }
protected override Expression VisitMethodCall(MethodCallExpression m) { if (m.Method.DeclaringType == typeof(Queryable) || m.Method.DeclaringType == typeof(Enumerable)) { switch (m.Method.Name) { case "Where": WhereArgumentsVisitor vst = new WhereArgumentsVisitor(rootCriteria); System.Linq.Expressions.Expression exp = vst.Visit(m.Arguments[1]); rootCriteria.Add(vst.CurrentCriterions[0]); break; case "Select": HandleSelect(m); break; case "First": TranslateFirst(m, false); break; case "FirstOrDefault": TranslateFirst(m, true); break; case "Single": TranslateSingle(m, false); break; case "SingleOrDefault": TranslateSingle(m, true); break; case "Count": Translation = new CountQueryTranslated(this) { Criteria = rootCriteria }; break; case "Sum": HandleProjectionOfSingleElement(m, s => NHEX.Projections.Sum(s)); break; case "Min": HandleProjectionOfSingleElement(m, s => NHEX.Projections.Min(s)); break; case "Max": HandleProjectionOfSingleElement(m, s => NHEX.Projections.Max(s)); break; case "Average": HandleProjectionOfSingleElement(m, s => NHEX.Projections.Avg(s)); break; case "GroupBy": HandleGroupBy(m); break; default: Console.WriteLine("Unknown method " + m.Method.Name); break; } } return(base.VisitMethodCall(m)); }