Example #1
0
        public void Test_PropertyEqAll()
        {
            ICriteria expected = CreateSession()
                                 .CreateCriteria(typeof(Person))
                                 .Add(Subqueries.PropertyEqAll("Name", DetachedCriteriaSubquery));

            ICriteria actual = CreateSession()
                               .CreateCriteria(typeof(Person))
                               .Add(LambdaSubquery.WhereAll <Person>(p => p.Name == DetachedCriteriaSubquery.As <string>()));

            AssertCriteriaAreEqual(expected, actual);
        }
Example #2
0
        public void TestAllBuilderOverloads()
        {
            ICriteria expected = CreateSession()
                                 .CreateCriteria(typeof(Person))
                                 .Add(Subqueries.PropertyIn("Name", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyNotIn("Name", DetachedCriteriaSubquery))

                                 .Add(Subqueries.PropertyEq("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyNe("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGt("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGe("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLt("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLe("Age", DetachedCriteriaSubquery))

                                 .Add(Subqueries.PropertyEqAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGtAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGeAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLtAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLeAll("Age", DetachedCriteriaSubquery))

                                 .Add(Subqueries.PropertyGtSome("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGeSome("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLtSome("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLeSome("Age", DetachedCriteriaSubquery));

            ICriteria actual = CreateSession()
                               .CreateCriteria(typeof(Person))
                               .Add(LambdaSubquery.Property <Person>(p => p.Name).In(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Name).NotIn(DetachedCriteriaSubquery))

                               .Add(LambdaSubquery.Property <Person>(p => p.Age).Eq(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).Ne(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).Gt(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).Ge(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).Lt(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).Le(DetachedCriteriaSubquery))

                               .Add(LambdaSubquery.Property <Person>(p => p.Age).EqAll(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).GtAll(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).GeAll(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).LtAll(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).LeAll(DetachedCriteriaSubquery))

                               .Add(LambdaSubquery.Property <Person>(p => p.Age).GtSome(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).GeSome(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).LtSome(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).LeSome(DetachedCriteriaSubquery));

            AssertCriteriaAreEqual(expected, actual);
        }
Example #3
0
        public void TestAllExpressionCombinations()
        {
            ICriteria expected = CreateSession()
                                 .CreateCriteria(typeof(Person), "personAlias")
                                 .Add(Subqueries.PropertyEq("personAlias.Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyNe("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGt("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGe("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLt("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLe("Age", DetachedCriteriaSubquery))

                                 .Add(Subqueries.PropertyEqAll("personAlias.Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGtAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGeAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLtAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLeAll("Age", DetachedCriteriaSubquery))

                                 .Add(Subqueries.PropertyGtSome("personAlias.Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGeSome("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLtSome("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLeSome("Age", DetachedCriteriaSubquery));

            Person    personAlias = null;
            ICriteria actual      = CreateSession()
                                    .CreateCriteria(typeof(Person), () => personAlias)
                                    .Add(LambdaSubquery.Where(() => personAlias.Age == DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.Where <Person>(p => p.Age != DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.Where <Person>(p => p.Age > DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.Where <Person>(p => p.Age >= DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.Where <Person>(p => p.Age < DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.Where <Person>(p => p.Age <= DetachedCriteriaSubquery.As <int>()))

                                    .Add(LambdaSubquery.WhereAll(() => personAlias.Age == DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereAll <Person>(p => p.Age > DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereAll <Person>(p => p.Age >= DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereAll <Person>(p => p.Age < DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereAll <Person>(p => p.Age <= DetachedCriteriaSubquery.As <int>()))

                                    .Add(LambdaSubquery.WhereSome(() => personAlias.Age > DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereSome <Person>(p => p.Age >= DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereSome <Person>(p => p.Age < DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereSome <Person>(p => p.Age <= DetachedCriteriaSubquery.As <int>()));

            AssertCriteriaAreEqual(expected, actual);
        }
Example #4
0
        public void PropertyAsAllSyntaxCriterion()
        {
            ICriteria expected =
                CreateTestCriteria(typeof(Person))
                .Add(Subqueries.PropertyEqAll("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyGeAll("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyGtAll("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyLeAll("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyLtAll("Age", DetachedCriteriaAge));

            var actual =
                CreateTestQueryOver <Person>()
                .And(Subqueries.WhereAll <Person>(p => p.Name == DetachedQueryOverName.As <string>()))
                .And(Subqueries.WhereAll <Person>(p => p.Age >= DetachedQueryOverAge.As <int>()))
                .And(Subqueries.WhereAll <Person>(p => p.Age > DetachedQueryOverAge.As <int>()))
                .And(Subqueries.WhereAll <Person>(p => p.Age <= DetachedQueryOverAge.As <int>()))
                .And(Subqueries.WhereAll <Person>(p => p.Age < DetachedQueryOverAge.As <int>()));

            AssertCriteriaAreEqual(expected, actual);
        }
Example #5
0
        public void PropertyAllCriterion()
        {
            ICriteria expected =
                CreateTestCriteria(typeof(Person))
                .Add(Subqueries.PropertyEqAll("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyGeAll("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyGtAll("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyLeAll("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyLtAll("Age", DetachedCriteriaAge));

            var actual =
                CreateTestQueryOver <Person>()
                .And(Subqueries.WhereProperty <Person>(p => p.Name).EqAll(DetachedQueryOverName))
                .And(Subqueries.WhereProperty <Person>(p => p.Age).GeAll(DetachedQueryOverAge))
                .And(Subqueries.WhereProperty <Person>(p => p.Age).GtAll(DetachedQueryOverAge))
                .And(Subqueries.WhereProperty <Person>(p => p.Age).LeAll(DetachedQueryOverAge))
                .And(Subqueries.WhereProperty <Person>(p => p.Age).LtAll(DetachedQueryOverAge));

            AssertCriteriaAreEqual(expected, actual);
        }
        public async Task ExecutableCriteriaAsync()
        {
            // All query below don't have sense, are only to test if all needed classes are serializable

            // Basic criterion
            DetachedCriteria dc = DetachedCriteria.For(typeof(Student))
                                  .Add(Expression.Between("Name", "aaaaa", "zzzzz"))
                                  .Add(Expression.EqProperty("Name", "Name"))
                                  .Add(Expression.Ge("Name", "a"))
                                  .Add(Expression.GeProperty("Name", "Name"))
                                  .Add(Expression.Gt("Name", "z"))
                                  .Add(Expression.GtProperty("Name", "Name"))
                                  .Add(Expression.IdEq(1))
                                  .Add(Expression.In("Name", new string[] { "Gavin", "Ralph" }))
                                  .Add(Expression.InsensitiveLike("Name", "GAVIN"))
                                  .Add(Expression.IsEmpty("Enrolments"))
                                  .Add(Expression.IsNotEmpty("Enrolments"))
                                  .Add(Expression.IsNotNull("PreferredCourse"))
                                  .Add(Expression.IsNull("PreferredCourse"))
                                  .Add(Expression.Le("Name", "a"))
                                  .Add(Expression.LeProperty("Name", "Name"))
                                  .Add(Expression.Lt("Name", "z"))
                                  .Add(Expression.LtProperty("Name", "Name"))
                                  .Add(Expression.Like("Name", "G%"))
                                  .Add(Expression.Not(Expression.Eq("Name", "Ralph")))
                                  .Add(Expression.NotEqProperty("Name", "Name"))
                                  .AddOrder(Order.Asc("StudentNumber"))
                                  .SetProjection(Property.ForName("StudentNumber"));

            await(SerializeAndListAsync(dc));

            // Like match modes
            dc = DetachedCriteria.For(typeof(Student))
                 .Add(Expression.Like("Name", "Gavin", MatchMode.Anywhere))
                 .Add(Expression.Like("Name", "Gavin", MatchMode.End))
                 .Add(Expression.Like("Name", "Gavin", MatchMode.Exact))
                 .Add(Expression.Like("Name", "Gavin", MatchMode.Start));

            await(SerializeAndListAsync(dc));

            // Logical Expression
            dc = DetachedCriteria.For(typeof(Student))
                 .Add(Expression.Or(Expression.Eq("Name", "Ralph"), Expression.Eq("Name", "Gavin")))
                 .Add(
                Expression.And(Expression.Gt("StudentNumber", 1L),
                               Expression.Lt("StudentNumber", 10L)));

            await(SerializeAndListAsync(dc));

            // Projections
            dc = DetachedCriteria.For(typeof(Enrolment))
                 .SetProjection(Projections.Distinct(Projections.ProjectionList()
                                                     .Add(Projections.Property("StudentNumber"), "stNumber")
                                                     .Add(Projections.Property("CourseCode"), "cCode")))
                 .Add(Expression.Lt("StudentNumber", 668L));
            await(SerializeAndListAsync(dc));

            if (TestDialect.SupportsCountDistinct)
            {
                dc = DetachedCriteria.For(typeof(Enrolment))
                     .SetProjection(Projections.Count("StudentNumber").SetDistinct());
                await(SerializeAndListAsync(dc));
            }

            dc = DetachedCriteria.For(typeof(Enrolment))
                 .SetProjection(Projections.ProjectionList()
                                .Add(Projections.Count("StudentNumber"))
                                .Add(Projections.Max("StudentNumber"))
                                .Add(Projections.Min("StudentNumber"))
                                .Add(Projections.Avg("StudentNumber")));
            await(SerializeAndListAsync(dc));

            // Junctions
            dc = DetachedCriteria.For(typeof(Student))
                 .Add(Expression.Conjunction()
                      .Add(Expression.Eq("Name", "Ralph"))
                      .Add(Expression.Eq("StudentNumber", 1L)))
                 .Add(Expression.Disjunction()
                      .Add(Expression.Eq("Name", "Ralph"))
                      .Add(Expression.Eq("Name", "Gavin")));
            await(SerializeAndListAsync(dc));

            // Subquery
            dc = DetachedCriteria.For(typeof(Student))
                 .Add(Property.ForName("StudentNumber").Eq(232L))
                 .SetProjection(Property.ForName("Name"));

            DetachedCriteria dcs = DetachedCriteria.For(typeof(Student))
                                   .Add(Subqueries.PropertyEqAll("Name", dc));

            await(SerializeAndListAsync(dc));

            // SQLCriterion
            dc = DetachedCriteria.For(typeof(Student))
                 .Add(Expression.Sql("{alias}.Name = 'Gavin'"));
            await(SerializeAndListAsync(dc));

            // SQLProjection
            dc = DetachedCriteria.For(typeof(Enrolment))
                 .SetProjection(Projections.SqlProjection("1 as constOne, count(*) as countStar",
                                                          new String[] { "constOne", "countStar" },
                                                          new IType[] { NHibernateUtil.Int32, NHibernateUtil.Int32 }));
            await(SerializeAndListAsync(dc));

            dc = DetachedCriteria.For(typeof(Student))
                 .SetProjection(
                Projections.SqlGroupProjection("COUNT({alias}.studentId), {alias}.preferredCourseCode",
                                               "{alias}.preferredCourseCode",
                                               new string[] { "studentsOfCourse", "CourseCode" },
                                               new IType[] { NHibernateUtil.Int32, NHibernateUtil.Int32 }));
            await(SerializeAndListAsync(dc));

            // Result transformers
            dc = DetachedCriteria.For(typeof(Enrolment))
                 .CreateAlias("Student", "st")
                 .CreateAlias("Course", "co")
                 .SetProjection(Projections.ProjectionList()
                                .Add(Projections.Property("st.Name"), "studentName")
                                .Add(Projections.Property("co.Description"), "courseDescription")
                                )
                 .AddOrder(Order.Desc("studentName"))
                 .SetResultTransformer(Transformers.AliasToBean(typeof(StudentDTO)));
            await(SerializeAndListAsync(dc));
        }
Example #7
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);
        }