Пример #1
0
        private ICriterion GetCollectionContainsCriteria(CollectionAccessExpression arg, Expression containsExpression)
        {
            EntityExpression rootEntity = EntityExpressionVisitor.FirstEntity(arg);

            var rootEntityType = rootEntity.Type;

            if (rootEntity.MetaData.HasProxy)
            {
                rootEntityType = rootEntity.MetaData.GetMappedClass(EntityMode.Poco);
            }

            DetachedCriteria query = DetachedCriteria.For(rootEntityType)
                                     .SetProjection(Projections.Id());

            var visitor = new MemberNameVisitor(query.Adapt(session), true);

            visitor.Visit(arg);

            //TODO: this won't work for collections of values
            var containedEntity          = QueryUtil.GetExpressionValue(containsExpression);
            var collectionIdPropertyName = visitor.MemberName + "." + arg.ElementExpression.MetaData.IdentifierPropertyName;
            var idValue = arg.ElementExpression.MetaData.GetIdentifier(containedEntity, EntityMode.Poco);

            query.Add(Restrictions.Eq(collectionIdPropertyName, idValue));

            string identifierName = rootEntity.MetaData.IdentifierPropertyName;

            return(Subqueries.PropertyIn(identifierName, query));
        }
Пример #2
0
        private EntityExpression GetParentExpression(MemberExpression expr, out string memberName, out IType nhibernateType)
        {
            memberName     = null;
            nhibernateType = null;

            CollectionAccessExpression collectionExpr = expr.Expression as CollectionAccessExpression;

            if (collectionExpr != null)
            {
                return(null);
            }

            PropertyAccessExpression propExpr = expr.Expression as PropertyAccessExpression;

            if (propExpr != null)
            {
                memberName     = propExpr.Name + "." + expr.Member.Name;
                nhibernateType = propExpr.Expression.MetaData.GetPropertyType(memberName);
                return(propExpr.Expression);
            }

            EntityExpression entityExpr = expr.Expression as EntityExpression;

            if (entityExpr != null)
            {
                memberName     = expr.Member.Name;
                nhibernateType = entityExpr.MetaData.GetPropertyType(memberName);
                return(entityExpr);
            }

            return(null);
        }
Пример #3
0
        protected override Expression VisitCollectionAccess(CollectionAccessExpression expr)
        {
            MemberNameVisitor visitor = new MemberNameVisitor(_rootCriteria, false);

            visitor.Visit(expr.Expression);

            visitor.CurrentCriteria.CreateCriteria(expr.Name, _alias, JoinType.LeftOuterJoin);
            return(expr);
        }
        protected virtual Expression VisitCollectionAccess(CollectionAccessExpression expr)
        {
            EntityExpression e = (EntityExpression)Visit(expr.Expression);

            if (e != expr.Expression)
            {
                return(new CollectionAccessExpression(expr.Name, expr.Type, expr.NHibernateType, e, expr.ElementExpression));
            }
            return(expr);
        }
Пример #5
0
        protected override Expression VisitMemberAccess(MemberExpression m)
        {
            CollectionAccessExpression parent = m.Expression as CollectionAccessExpression;

            if (parent != null && m.Member.Name == "Count")
            {
                MethodInfo method = GetCountMethod(parent.ElementExpression.Type);
                return(Expression.Call(method, CastIfNecessary(parent)));
            }
            return(base.VisitMemberAccess(m));
        }
Пример #6
0
        private Expression CastIfNecessary(CollectionAccessExpression expr)
        {
            if (expr.Type.IsGenericType)
            {
                return(expr);
            }

            MethodInfo method = typeof(Enumerable).GetMethod("Cast")
                                .MakeGenericMethod(expr.ElementExpression.Type);

            return(Expression.Call(method, expr));
        }
Пример #7
0
        protected override Expression VisitCollectionAccess(CollectionAccessExpression expr)
        {
            expr = (CollectionAccessExpression)base.VisitCollectionAccess(expr);
            //memberNameBuilder.Append(expr.Name + ".");
            ResetMemberName(expr.Name + ".");
            currentExpression = expr;

            if (createCriteriaForCollections)
            {
                if (expr.ElementExpression != null)
                {
                    currentCriteria = EnsureCriteria(expr.Name, expr.ElementExpression.Alias);
                }
            }

            isQueringEntity = false;

            return(expr);
        }
        protected override Expression VisitCollectionAccess(CollectionAccessExpression expr)
        {
            EntityExpression elementExpression = expr.ElementExpression;

            if (elementExpression == null)
            {
                return(expr);
            }

            string alias = FindCollectionAlias(elementExpression.Type);

            if (String.IsNullOrEmpty(alias))
            {
                return(expr);
            }

            elementExpression = new EntityExpression(elementExpression.AssociationPath, alias, elementExpression.Type, elementExpression.MetaData, elementExpression.Expression);
            return(new CollectionAccessExpression(expr.Name, expr.Type, expr.NHibernateType, expr.Expression, elementExpression));
        }
        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 VisitCollectionAccess(CollectionAccessExpression expr)
 {
     return(VisitPropertyAccess(expr));
 }
 protected override Expression VisitCollectionAccess(CollectionAccessExpression expr)
 {
     Type = BinaryCriterionType.Property;
     return(expr);
 }