Ejemplo n.º 1
0
        public void MultipleSubqueriesShouldStayInOrder()
        {
            if (!Dialect.SupportsScalarSubSelects)
            {
                Assert.Ignore("Dialect does not support scalar sub-select");
            }

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    DetachedCriteria dc1 = DetachedCriteria.For(typeof(Person))
                                           .Add(Property.ForName("IQ").Eq(10))
                                           .SetProjection(Property.ForName("Name"));

                    DetachedCriteria dc2 = DetachedCriteria.For(typeof(Person))
                                           .Add(Property.ForName("ShoeSize").Eq(7))
                                           .SetProjection(Projections.Property("Name"));

                    IList list = s.CreateCriteria(typeof(Person), "p")
                                 .Add(Subqueries.PropertyEq("Name", dc1))
                                 .Add(Restrictions.Not(Subqueries.Eq("Sally", dc2)))
                                 .List();

                    Assert.AreEqual(1, list.Count);
                    Assert.AreEqual("Joe", ((Person)list[0]).Name);
                    tx.Commit();
                }
        }
Ejemplo n.º 2
0
        public void Property()
        {
            ICriteria expected =
                CreateTestCriteria(typeof(Person))
                .Add(Subqueries.PropertyEq("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyGe("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyGt("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyIn("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyLe("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyLt("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyNe("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyNotIn("Name", DetachedCriteriaName));

            var actual =
                CreateTestQueryOver <Person>()
                .WithSubquery.WhereProperty(p => p.Name).Eq(DetachedQueryOverName)
                .WithSubquery.WhereProperty(p => p.Age).Ge(DetachedQueryOverAge)
                .WithSubquery.WhereProperty(p => p.Age).Gt(DetachedQueryOverAge)
                .WithSubquery.WhereProperty(p => p.Name).In(DetachedQueryOverName)
                .WithSubquery.WhereProperty(p => p.Age).Le(DetachedQueryOverAge)
                .WithSubquery.WhereProperty(p => p.Age).Lt(DetachedQueryOverAge)
                .WithSubquery.WhereProperty(p => p.Name).Ne(DetachedQueryOverName)
                .WithSubquery.WhereProperty(p => p.Name).NotIn(DetachedQueryOverName);

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 3
0
        public void NestedSubqueriesShouldStayInOrder()
        {
            if (!Dialect.SupportsScalarSubSelects)
            {
                Assert.Ignore("Dialect does not support scalar sub-select");
            }

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    DetachedCriteria dc1 = DetachedCriteria.For(typeof(Person))
                                           .Add(Property.ForName("IQ").Eq(40))
                                           .SetProjection(Property.ForName("IQ"));

                    DetachedCriteria dc2 = DetachedCriteria.For(typeof(Person))
                                           .Add(Subqueries.PropertyEq("ShoeSize", dc1))
                                           .SetProjection(
                        new SqlFunctionProjection(
                            "concat",
                            NHibernateUtil.String,
                            Projections.GroupProperty("Name"),
                            new ConstantProjection(" "),
                            Projections.GroupProperty("Name")));

                    IList list = s.CreateCriteria(typeof(Person))
                                 .Add(Subqueries.Eq("Fred Fred", dc2))
                                 .List();

                    Assert.AreEqual(5, list.Count);             //yeah, it returns all five results. The key is that it didn't crash
                    tx.Commit();
                }
        }
        /// <summary>
        /// 锁定一个给定的聚合根,数据库级别的锁;
        /// 该方法内部不需要考虑事务,因为框架在调用该方法之前已经确保当前事务是开启的;
        /// 默认实现是通过ROWLOCK+UPDLOCK的方式实现行级锁;
        /// </summary>
        protected virtual long LockAggregateRootAfterTransactionStarted(string aggregateRootId)
        {
            var aggregateRootEventType = _aggregateRootEventTypeProvider.GetAggregateRootEventType(typeof(T));
            var aggregateRootName      = GetAggregateRootName();

            DetachedCriteria detachedCriteria = DetachedCriteria.For(aggregateRootEventType)
                                                .Add(Restrictions.Eq("AggregateRootName", aggregateRootName))
                                                .Add(Restrictions.Eq("AggregateRootId", aggregateRootId))
                                                .Add(Subqueries.PropertyEq("Version",
                                                                           DetachedCriteria.For(aggregateRootEventType)
                                                                           .SetProjection(Projections.Max("Version"))
                                                                           .Add(Restrictions.Eq("AggregateRootName", aggregateRootName))
                                                                           .Add(Restrictions.Eq("AggregateRootId", aggregateRootId))));

            detachedCriteria.SetLockMode(LockMode.Upgrade);
            var criteria = detachedCriteria.GetExecutableCriteria(OpenSession());

            var eventList = typeof(EventQueryHelper).GetMethod("GetList").MakeGenericMethod(aggregateRootEventType).Invoke(new EventQueryHelper(), new object[] { criteria }) as IList;

            if (eventList != null && eventList.Count > 0)
            {
                return((long)((AggregateRootEvent)eventList[0]).Version);
            }
            return(0);
        }
Ejemplo n.º 5
0
        public void Test_PropertyEqAlternativeSyntax()
        {
            ICriteria expected = CreateSession()
                                 .CreateCriteria(typeof(Person))
                                 .Add(Subqueries.PropertyEq("Name", DetachedCriteriaSubquery));

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

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 6
0
        public void PropertyAsSyntaxCriterion()
        {
            ICriteria expected =
                CreateTestCriteria(typeof(Person))
                .Add(Subqueries.PropertyEq("Name", DetachedCriteriaName));

            var actual =
                CreateTestQueryOver <Person>()
                .And(Subqueries.Where <Person>(p => p.Name == DetachedQueryOverName.As <string>()));

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 7
0
        public void Test_PropertyEq()
        {
            ICriteria expected = CreateSession()
                                 .CreateCriteria(typeof(Person))
                                 .Add(Subqueries.PropertyEq("Name", DetachedCriteriaSubquery));

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

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 8
0
        public static Translation FindByKey(int id, string language)
        {
            var crit = DetachedCriteria.For <Language>()
                       .Add(Restrictions.Eq("IsoCode", language))
                       .SetProjection(Projections.Id());

            return(Global.CurrentSession.CreateCriteria <Translation>()
                   .Add(Subqueries.PropertyEq("Language", crit))
                   .Add(Restrictions.IsNull("Translator"))
                   .CreateAlias("Key", "k")
                   .Add(Restrictions.Eq("k.Id", id))
                   .UniqueResult <Translation>());
        }
Ejemplo n.º 9
0
        public void PropertyAliasCriterion()
        {
            ICriteria expected =
                CreateTestCriteria(typeof(Person), "personAlias")
                .Add(Subqueries.PropertyEq("personAlias.Name", DetachedCriteriaName));

            Person personAlias = null;
            var    actual      =
                CreateTestQueryOver <Person>(() => personAlias)
                .And(Subqueries.WhereProperty(() => personAlias.Name).Eq(DetachedQueryOverName));

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 10
0
        public RequestEstimate GetLatestByRequest(int requestId)
        {
            var criteria = Session.CreateCriteria(typeof(RequestEstimate));

            criteria.Add(Restrictions.Eq("RequestId", requestId));

            var subquery = DetachedCriteria.For(typeof(RequestEstimate));

            subquery.Add(Restrictions.Eq("RequestId", requestId));
            subquery.SetProjection(Projections.Max("Id"));
            criteria.Add(Subqueries.PropertyEq("Id", subquery));
            return(criteria.UniqueResult <RequestEstimate>());
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
        public void DetachedSubquery()
        {
            DetachedCriteria expected =
                DetachedCriteria.For <Person>("personAlias")
                .Add(Subqueries.PropertyEq("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyEq("personAlias.Name", DetachedCriteriaName));

            Person             personAlias = null;
            QueryOver <Person> actual      =
                QueryOver.Of <Person>(() => personAlias)
                .WithSubquery.WhereProperty(p => p.Name).Eq(DetachedQueryOverName)
                .WithSubquery.WhereProperty(() => personAlias.Name).Eq(DetachedQueryOverName);

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 13
0
        public void PropertyAsSyntaxAliasCriterion()
        {
            ICriteria expected =
                CreateTestCriteria(typeof(Person), "personAlias")
                .Add(Subqueries.PropertyEq("personAlias.Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyGtSome("personAlias.Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyLtAll("personAlias.Age", DetachedCriteriaAge));

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

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
0
        public void PropertyAsSyntax()
        {
            ICriteria expected =
                CreateTestCriteria(typeof(Person))
                .Add(Subqueries.PropertyEq("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyNe("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyGe("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyGt("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyLe("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyLt("Age", DetachedCriteriaAge));

            var actual =
                CreateTestQueryOver <Person>()
                .WithSubquery.Where(p => p.Name == DetachedQueryOverName.As <string>())
                .WithSubquery.Where(p => p.Name != DetachedQueryOverName.As <string>())
                .WithSubquery.Where(p => p.Age >= DetachedQueryOverAge.As <int>())
                .WithSubquery.Where(p => p.Age > DetachedQueryOverAge.As <int>())
                .WithSubquery.Where(p => p.Age <= DetachedQueryOverAge.As <int>())
                .WithSubquery.Where(p => p.Age < DetachedQueryOverAge.As <int>());

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 16
0
        private string GetSkillDisplayName(ISession session, string skillRecord)
        {
            var subquery = Subqueries.PropertyEq("P.Id", DetachedCriteria.For <DatabaseItem>()
                                                 .Add(Restrictions.Eq("Record", skillRecord))
                                                 .SetProjection(Projections.Property("Id"))
                                                 );

            IList <DatabaseItemStat> skill = session.CreateCriteria <DatabaseItemStat>()
                                             .Add(subquery)
                                             .CreateAlias("Parent", "P")
                                             .List <DatabaseItemStat>();

            // Get the tag-name from the skill
            var skillNameEntry = skill.FirstOrDefault(m => m.Stat.Equals("skillDisplayName"));

            if (skillNameEntry?.TextValue != null)
            {
                return(skillNameEntry.TextValue);
            }

            // It may be without a name, but reference a pet skill
            var petSkillName = skill.Where(m => m.Stat.Equals("petSkillName"));

            if (petSkillName.Any())
            {
                return(GetSkillDisplayName(session, petSkillName.FirstOrDefault().TextValue));
            }

            // The pet skill may in turn reference a buff skill, or the original skill might.
            var buffSkillName = skill.Where(m => m.Stat.Equals("buffSkillName"));

            if (buffSkillName.Any())
            {
                return(GetSkillDisplayName(session, buffSkillName.FirstOrDefault().TextValue));
            }

            return(null);
        }
Ejemplo n.º 17
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);
        }