Пример #1
0
        public void ValueCriterion()
        {
            ICriteria expected =
                CreateTestCriteria(typeof(Person))
                .Add(Subqueries.Eq("Name", DetachedCriteriaName))
                .Add(Subqueries.Ge("Age", DetachedCriteriaAge))
                .Add(Subqueries.Gt("Age", DetachedCriteriaAge))
                .Add(Subqueries.In("Name", DetachedCriteriaName))
                .Add(Subqueries.Le("Age", DetachedCriteriaAge))
                .Add(Subqueries.Lt("Age", DetachedCriteriaAge))
                .Add(Subqueries.Ne("Name", DetachedCriteriaName))
                .Add(Subqueries.NotIn("Name", DetachedCriteriaName));

            var actual =
                CreateTestQueryOver <Person>()
                .And(Subqueries.WhereValue("Name").Eq(DetachedQueryOverName))
                .And(Subqueries.WhereValue("Age").Ge(DetachedQueryOverAge))
                .And(Subqueries.WhereValue("Age").Gt(DetachedQueryOverAge))
                .And(Subqueries.WhereValue("Name").In(DetachedQueryOverName))
                .And(Subqueries.WhereValue("Age").Le(DetachedQueryOverAge))
                .And(Subqueries.WhereValue("Age").Lt(DetachedQueryOverAge))
                .And(Subqueries.WhereValue("Name").Ne(DetachedQueryOverName))
                .And(Subqueries.WhereValue("Name").NotIn(DetachedQueryOverName));

            AssertCriteriaAreEqual(expected, actual);
        }
        private static IQueryOver <SupplementCycleDefinition, SupplementCycleDefinition> getSupplementsCycleDefinitionsCriterias(GetSupplementsCycleDefinitionsParam param, Profile loggedProfile, List <Guid> ids, Profile myProfile,
                                                                                                                                 IQueryOver <SupplementCycleDefinition, SupplementCycleDefinition> queryCustomer)
        {
            SupplementCycleDosage dosage = null;
            SupplementCycleWeek   week   = null;

            queryCustomer = queryCustomer.JoinAlias(x => x.Weeks, () => week)
                            .JoinAlias(x => x.Weeks.First().Dosages, () => dosage);

            if (param.LegalCriteria == CanBeIllegalCriteria.OnlyLegal)
            {
                queryCustomer = queryCustomer.Where(x => !x.CanBeIllegal);
            }
            else if (param.LegalCriteria == CanBeIllegalCriteria.OnlyIllegal)
            {
                queryCustomer = queryCustomer.Where(x => x.CanBeIllegal);
            }

            if (param.PlanId.HasValue)
            {
                queryCustomer = queryCustomer.Where(x => x.GlobalId == param.PlanId.Value);
            }
            if (param.Languages.Count > 0)
            {
                var langOr = Restrictions.Disjunction();
                foreach (var lang in param.Languages)
                {
                    langOr.Add <SupplementCycleDefinition>(x => x.Language == lang);
                }
                queryCustomer = queryCustomer.And(langOr);
            }
            if (param.Purposes.Count > 0)
            {
                var purposeOr = Restrictions.Disjunction();
                foreach (var purpose in param.Purposes)
                {
                    purposeOr.Add <SupplementCycleDefinition>(x => x.Purpose == (WorkoutPlanPurpose)purpose);
                }
                queryCustomer = queryCustomer.And(purposeOr);
            }
            if (param.Difficults.Count > 0)
            {
                var mainOr = Restrictions.Disjunction();
                foreach (TrainingPlanDifficult diff in param.Difficults)
                {
                    var tt = (BodyArchitect.Model.TrainingPlanDifficult)diff;
                    mainOr.Add <SupplementCycleDefinition>(x => x.Difficult == tt);
                }
                queryCustomer = queryCustomer.And(mainOr);
            }

            if (param.Supplements.Count > 0)
            {
                Junction supplementsOperations = null;
                if (param.SupplementsListOperator == CriteriaOperator.Or)
                {
                    supplementsOperations = Restrictions.Disjunction();
                    foreach (var supplementId in param.Supplements)
                    {
                        supplementsOperations.Add <SupplementCycleDefinition>(x => dosage.Supplement.GlobalId == supplementId);
                    }
                }
                else
                {
                    supplementsOperations = Restrictions.Conjunction();
                    foreach (var supplementId in param.Supplements)
                    {
                        var orderIdsCriteria = DetachedCriteria.For <SupplementCycleDosage>();
                        orderIdsCriteria.SetProjection(Projections.CountDistinct("GlobalId"))
                        .Add(Restrictions.Where <SupplementCycleDosage>(x => x.Supplement.GlobalId == supplementId))
                        .Add(Restrictions.Where <SupplementCycleDosage>(x => x.Week.GlobalId == week.GlobalId));

                        supplementsOperations.Add(Subqueries.Lt(0, orderIdsCriteria));
                        //supplementsOperations.Add<SupplementCycleDosage>(x => dosage.Supplement.GlobalId == supplementId);
                    }
                }
                queryCustomer = queryCustomer.And(supplementsOperations);
            }

            queryCustomer = queryCustomer.Where(x => x.Profile == loggedProfile || (x.Profile != loggedProfile && x.Status == PublishStatus.Published));

            var groupOr = new Disjunction();

            if (param.SearchGroups.Count > 0)
            {
                if (param.SearchGroups.IndexOf(WorkoutPlanSearchCriteriaGroup.Mine) > -1)
                {
                    groupOr.Add <BodyArchitect.Model.SupplementCycleDefinition>(x => x.Profile == myProfile);
                }
                if (param.SearchGroups.IndexOf(WorkoutPlanSearchCriteriaGroup.Favorites) > -1)
                {
                    if (myProfile.FavoriteSupplementCycleDefinitions.Count > 0)
                    {
                        groupOr.Add <BodyArchitect.Model.SupplementCycleDefinition>(x => x.GlobalId.IsIn((ICollection)ids));
                    }
                }
                if (param.SearchGroups.IndexOf(WorkoutPlanSearchCriteriaGroup.Other) > -1)
                {
                    var tmpAnd = Restrictions.Conjunction();
                    tmpAnd.Add <BodyArchitect.Model.SupplementCycleDefinition>(
                        dto => dto.Profile != null && dto.Profile != myProfile);

                    if (ids.Count > 0)
                    {
                        tmpAnd.Add(Restrictions.On <BodyArchitect.Model.SupplementCycleDefinition>(x => x.GlobalId).Not.IsIn((ICollection)ids));
                    }

                    groupOr.Add(tmpAnd);
                }
                queryCustomer = queryCustomer.Where(groupOr);
            }
            return(queryCustomer);
        }
Пример #3
0
        public void SubqueriesExpressions()
        {
            DetachedCriteria dc = DetachedCriteria.For(typeof(Student))
                                  .Add(Expression.Eq("Name", "Gavin King"));
            ICriterion c = Subqueries.Eq("Gavin King", dc);

            NHAssert.IsSerializable(c);
            c = Subqueries.EqAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Exists(dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Ge("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.GeAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.GeSome("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Gt("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.GtAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.GtSome("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.In("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Le("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.LeAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.LeSome("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Lt("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.LtAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.LtSome("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Ne("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.NotExists(dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.NotIn("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyEq("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyEqAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGe("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGeAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGeSome("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGt("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGtAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGtSome("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyIn("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLe("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLeAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLeSome("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLt("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLtAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLtSome("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyNe("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyNotIn("Name", dc);
            NHAssert.IsSerializable(c);
        }