protected override Expression VisitPropertyAccess(PropertyAccessExpression expr)
        {
            string memberName = MemberNameVisitor.GetMemberName(_rootCriteria, expr);

            _projections.Add(NHProjections.Property(memberName));
            return(expr);
        }
예제 #2
0
        private void HandleOrderByDescendingCall(MethodCallExpression call)
        {
            LinqExpression expr = ((UnaryExpression)call.Arguments[1]).Operand;

            string name = MemberNameVisitor.GetMemberName(rootCriteria, expr);

            rootCriteria.AddOrder(Order.Desc(name));
        }
예제 #3
0
        protected override Expression VisitPropertyAccess(PropertyAccessExpression expr)
        {
            if (expr.Type == typeof(bool))
            {
                string name = MemberNameVisitor.GetMemberName(rootCriteria, expr);
                CurrentCriterions.Add(Restrictions.Eq(name, true));
            }

            return(expr);
        }
예제 #4
0
        private ICriterion GetCollectionContainsCriteria(Expression list, Expression containedExpr)
        {
            var values = QueryUtil.GetExpressionValue(list) as ICollection;

            if (values == null)
            {
                throw new InvalidOperationException("Expression argument must be of type ICollection.");
            }

            return(Restrictions.In(MemberNameVisitor.GetMemberName(rootCriteria, containedExpr),
                                   values));
        }
        protected override NewExpression VisitNew(NewExpression expr)
        {
            NewExpression newExpr = base.VisitNew(expr);

            _transformer = new TypeSafeConstructorMemberInitResultTransformer(expr);

            var aggregators = expr.Arguments.Where(arg => arg is MethodCallExpression && SupportsMethod(((MethodCallExpression)arg).Method.Name));

            if (aggregators.Any())
            {
                foreach (var exp in expr.Arguments.Except(aggregators))
                {
                    string propertyName = MemberNameVisitor.GetMemberName(_rootCriteria, exp);
                    if (!String.IsNullOrEmpty(propertyName))
                    {
                        _projections.Add(NHProjections.GroupProperty(propertyName));
                    }
                }
            }

            return(newExpr);
        }
예제 #6
0
        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);
        }
 protected override Expression VisitPropertyAccess(PropertyAccessExpression expr)
 {
     Type = BinaryCriterionType.Property;
     Name = MemberNameVisitor.GetMemberName(rootCriteria, expr);
     return(expr);
 }
예제 #9
0
 private ICriterion GetLikeCriteria(MethodCallExpression expr, MatchMode matchMode)
 {
     return(Restrictions.Like(MemberNameVisitor.GetMemberName(rootCriteria, expr.Object),
                              String.Format("{0}", QueryUtil.GetExpressionValue(expr.Arguments[0])),
                              matchMode));
 }