public static string GetMemberName(ICriteria rootCriteria, Expression expr) { MemberNameVisitor visitor = new MemberNameVisitor(rootCriteria); visitor.Visit(expr); return(visitor.MemberName); }
protected override Expression VisitTypeIs(TypeBinaryExpression expr) { var visitor = new MemberNameVisitor(rootCriteria); visitor.Visit(expr); string memberName = visitor.MemberName + ".class"; var metaData = session.SessionFactory.GetClassMetadata(expr.TypeOperand); if (metaData.HasSubclasses) { //make sure to include any subtypes var disjunction = new Disjunction(); foreach (string entityName in ((IEntityPersister)metaData).EntityMetamodel.SubclassEntityNames) { var metadata = session.SessionFactory.GetClassMetadata(entityName); disjunction.Add(Property.ForName(memberName).Eq(metadata.GetMappedClass(EntityMode.Poco))); } visitor.CurrentCriteria.Add(disjunction); } else { visitor.CurrentCriteria.Add(Property.ForName(memberName).Eq(expr.TypeOperand)); } return(expr); }
protected override Expression VisitPropertyAccess(PropertyAccessExpression expr) { string memberName = MemberNameVisitor.GetMemberName(_rootCriteria, expr); _projections.Add(NHProjections.Property(memberName)); return(expr); }
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)); }
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; }
private void HandleOrderByDescendingCall(MethodCallExpression call) { LinqExpression expr = ((UnaryExpression)call.Arguments[1]).Operand; string name = MemberNameVisitor.GetMemberName(rootCriteria, expr); rootCriteria.AddOrder(Order.Desc(name)); }
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 override Expression VisitPropertyAccess(PropertyAccessExpression expr) { if (expr.Type == typeof(bool)) { string name = MemberNameVisitor.GetMemberName(rootCriteria, expr); CurrentCriterions.Add(Restrictions.Eq(name, true)); } return(expr); }
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); }
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)); }
public static string GetMemberName(ICriteria rootCriteria, Expression expr) { MemberNameVisitor visitor = new MemberNameVisitor(rootCriteria); visitor.Visit(expr); return visitor.MemberName; }
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); }
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); }
protected override Expression VisitTypeIs(TypeBinaryExpression expr) { var visitor = new MemberNameVisitor(rootCriteria); visitor.Visit(expr); string memberName = visitor.MemberName + ".class"; var metaData = session.SessionFactory.GetClassMetadata(expr.TypeOperand); if (metaData.HasSubclasses) { //make sure to include any subtypes var disjunction = new Disjunction(); foreach (string entityName in ((IEntityPersister)metaData).EntityMetamodel.SubclassEntityNames) { var metadata = session.SessionFactory.GetClassMetadata(entityName); disjunction.Add(Property.ForName(memberName).Eq(metadata.GetMappedClass(EntityMode.Poco))); } visitor.CurrentCriteria.Add(disjunction); } else { visitor.CurrentCriteria.Add(Property.ForName(memberName).Eq(expr.TypeOperand)); } return expr; }
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 ICriterion GetLikeCriteria(MethodCallExpression expr, MatchMode matchMode) { return(Restrictions.Like(MemberNameVisitor.GetMemberName(rootCriteria, expr.Object), String.Format("{0}", QueryUtil.GetExpressionValue(expr.Arguments[0])), matchMode)); }