Esempio n. 1
0
        public void MultiCriteriaQueriesWithIntsShouldExecuteCorrectly()
        {
            var driver = Sfi.ConnectionProvider.Driver;

            if (!driver.SupportsMultipleQueries)
            {
                Assert.Ignore("Driver {0} does not support multi-queries", driver.GetType().FullName);
            }

            // Test querying IntData
            using (ISession session = this.OpenSession())
            {
                ICriteria criteriaWithPagination = session.CreateCriteria <DomainClass>();
                criteriaWithPagination.Add(Expression.Le("IntData", 2));
                ICriteria criteriaWithRowCount = CriteriaTransformer.Clone(criteriaWithPagination);
                criteriaWithPagination.SetFirstResult(0).SetMaxResults(1);
                criteriaWithRowCount.SetProjection(Projections.RowCountInt64());

                IMultiCriteria multiCriteria = session.CreateMultiCriteria();
                multiCriteria.Add(criteriaWithPagination);
                multiCriteria.Add(criteriaWithRowCount);

                IList results    = multiCriteria.List();
                long  numResults = (long)((IList)results[1])[0];
                IList list       = (IList)results[0];

                Assert.AreEqual(2, criteriaWithRowCount.UniqueResult <long>());
                Assert.AreEqual(1, list.Count);
            }
        }
Esempio n. 2
0
        public void CanNotAddDetachedCriteriaWithKeyThatAlreadyExists()
        {
            using (ISession session = OpenSession())
            {
                IMultiCriteria multiCriteria = session.CreateMultiCriteria();

                DetachedCriteria firstCriteria = DetachedCriteria.For(typeof(Item))
                                                 .Add(Expression.Lt("id", 50));

                DetachedCriteria secondCriteria = DetachedCriteria.For(typeof(Item));

                multiCriteria.Add("firstCriteria", firstCriteria);

                try
                {
                    multiCriteria.Add("firstCriteria", secondCriteria);
                    Assert.Fail("This should've thrown an InvalidOperationException");
                }
                catch (InvalidOperationException)
                {
                }
                catch (Exception)
                {
                    Assert.Fail("This should've thrown an InvalidOperationException");
                }
            }
        }
Esempio n. 3
0
        public IPagedList <Album> GetAlbums(ILoadOptions options)
        {
            IPagedList <Album> result = new PagedList <Album>();

            if (options == null)
            {
                return(result);
            }

            if (options.MaxResults <= 0)
            {
                return(result);
            }

            ISession session = SessionFactory.GetSession();

            try
            {
                DetachedCriteria countCriteria = GetAlbumsImpl(options);
                DetachedCriteria listCriteria  = GetAlbumsImpl(options);

                countCriteria.SetProjection(Projections.RowCount());
                countCriteria.ClearOrders();

                listCriteria.
                SetFirstResult(options.FirstResult).
                SetMaxResults(options.MaxResults);

                IMultiCriteria multiCriteria = session.CreateMultiCriteria();
                multiCriteria.Add(countCriteria);
                multiCriteria.Add(listCriteria);


                IList queryResult = multiCriteria.List();

                result.TotalItems = (int)((IList)queryResult[0])[0];

                IList recordsList = (IList)queryResult[1];

                EntityConverter entityConverter = new EntityConverter();

                foreach (var e in recordsList)
                {
                    AlbumEntity dataEntity     = e as AlbumEntity;
                    Album       businessEntity = entityConverter.FromDataEntity(dataEntity, AlbumConvertOptions.Small);
                    result.Add(businessEntity);
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
            }
            finally
            {
                session.Close();
            }

            return(result);
        }
Esempio n. 4
0
        public void CanNotRetrieveDetachedCriteriaResultWithUnknownKey()
        {
            CreateItems();

            using (ISession session = OpenSession())
            {
                IMultiCriteria multiCriteria = session.CreateMultiCriteria();

                DetachedCriteria firstCriteria = DetachedCriteria.For(typeof(Item))
                                                 .Add(Expression.Lt("id", 50));

                multiCriteria.Add("firstCriteria", firstCriteria);

                try
                {
                    IList firstResult = (IList)multiCriteria.GetResult("unknownKey");
                    Assert.Fail("This should've thrown an InvalidOperationException");
                }
                catch (InvalidOperationException)
                {
                }
                catch (Exception)
                {
                    Assert.Fail("This should've thrown an InvalidOperationException");
                }
            }

            RemoveAllItems();
        }
Esempio n. 5
0
        public void Test()
        {
            IDriver driver = sessions.ConnectionProvider.Driver;

            if (!driver.SupportsMultipleQueries)
            {
                Assert.Ignore("Driver {0} does not support multi-queries", driver.GetType().FullName);
            }
            using (ISession session = sessions.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    EntityA a1 = CreateEntityA(session);
                    EntityA a2 = CreateEntityA(session);
                    EntityC c  = CreateEntityC(session);
                    EntityB b  = CreateEntityB(session, a1, c);

                    // make sure the created entities are no longer in the session
                    session.Clear();

                    IMultiCriteria multi = session.CreateMultiCriteria();

                    // the first query is a simple select by id on EntityA
                    multi.Add(session.CreateCriteria(typeof(EntityA)).Add(Restrictions.Eq("Id", a1.Id)));
                    // the second query is also a simple select by id on EntityB
                    multi.Add(session.CreateCriteria(typeof(EntityA)).Add(Restrictions.Eq("Id", a2.Id)));
                    // the final query selects the first element (using SetFirstResult and SetMaxResults) for each EntityB where B.A.Id = a1.Id and B.C.Id = c.Id
                    // the problem is that the paged query uses parameters @p0 and @p1 instead of @p2 and @p3
                    multi.Add(
                        session.CreateCriteria(typeof(EntityB)).Add(Restrictions.Eq("A.Id", a1.Id)).Add(Restrictions.Eq("C.Id", c.Id)).
                        SetFirstResult(0).SetMaxResults(1));

                    IList results = multi.List();

                    Assert.AreEqual(1, ((IList)results[0]).Count);
                    Assert.AreEqual(1, ((IList)results[1]).Count);
                    Assert.AreEqual(1, ((IList)results[2]).Count);
                }
            }
        }
        public void MultiCriteriaQueriesWithIntsShouldExecuteCorrectly()
        {
            // Test querying IntData
            using (ISession session = this.OpenSession())
            {
                ICriteria criteriaWithPagination = session.CreateCriteria <DomainClass>();
                criteriaWithPagination.Add(Expression.Le("IntData", 2));
                ICriteria criteriaWithRowCount = CriteriaTransformer.Clone(criteriaWithPagination);
                criteriaWithPagination.SetFirstResult(0).SetMaxResults(1);
                criteriaWithRowCount.SetProjection(Projections.RowCountInt64());

                IMultiCriteria multiCriteria = session.CreateMultiCriteria();
                multiCriteria.Add(criteriaWithPagination);
                multiCriteria.Add(criteriaWithRowCount);

                IList results    = multiCriteria.List();
                long  numResults = (long)((IList)results[1])[0];
                IList list       = (IList)results[0];

                Assert.AreEqual(2, criteriaWithRowCount.UniqueResult <long>());
                Assert.AreEqual(1, list.Count);
            }
        }
Esempio n. 7
0
        public void CanAddDetachedCriteriaWithKeyAndRetrieveResultsWithKey()
        {
            CreateItems();

            using (ISession session = OpenSession())
            {
                IMultiCriteria multiCriteria = session.CreateMultiCriteria();

                DetachedCriteria firstCriteria = DetachedCriteria.For(typeof(Item))
                                                 .Add(Expression.Lt("id", 50));

                DetachedCriteria secondCriteria = DetachedCriteria.For(typeof(Item));

                multiCriteria.Add("firstCriteria", firstCriteria);
                multiCriteria.Add("secondCriteria", secondCriteria);

                IList secondResult = (IList)multiCriteria.GetResult("secondCriteria");
                IList firstResult  = (IList)multiCriteria.GetResult("firstCriteria");

                Assert.Greater(secondResult.Count, firstResult.Count);
            }

            RemoveAllItems();
        }
Esempio n. 8
0
        /// <summary>
        /// To use multi criteria to eagerly load multiple child collection, if the first detached criteria has child collection to query,
        /// then must use .SetResultTransformer(new DistinctRootEntityResultTransformer() for the first detached criteria
        /// </summary>
        /// <param name="multiCriteria">The multi criteria.</param>
        /// <param name="detachedCriteria">The detached criteria.</param>
        /// <param name="orders">The orders.</param>
        /// <param name="returnDistinctRoot">If set to <c>true</c> [return distinct root].</param>
        /// <returns>A <see cref="IMultiCriteria"/></returns>
        public static IMultiCriteria AddDetachedCriteria(
            this IMultiCriteria multiCriteria, DetachedCriteria detachedCriteria, IEnumerable <Order> orders = null, bool returnDistinctRoot = false)
        {
            if (orders != null && orders.Count() > 0)
            {
                detachedCriteria.AddOrders(orders);
            }

            if (returnDistinctRoot)
            {
                detachedCriteria = detachedCriteria.SetResultTransformer(new DistinctRootEntityResultTransformer());
            }

            multiCriteria.Add(detachedCriteria);
            return(multiCriteria);
        }
Esempio n. 9
0
        public IList <TD> GetPageEntites(IList <ICriterion> expressions, IList <Order> orders, int pageSize, int page,
                                         out long count)
        {
            using (var session = GetSession())
            {
                using (var trans = session.BeginTransaction())
                {
                    IMultiCriteria mulicrit = session.CreateMultiCriteria();

                    ICriteria queryCrit = session.CreateCriteria(typeof(TD));
                    ICriteria countCrit = session.CreateCriteria(typeof(TD));

                    foreach (var exp in expressions)
                    {
                        queryCrit.Add(exp);
                        countCrit.Add(exp);
                    }

                    foreach (var order in orders)
                    {
                        queryCrit.AddOrder(order);
                    }

                    IList results = mulicrit
                                    .Add(queryCrit.SetFirstResult(page * pageSize).SetMaxResults(pageSize))
                                    .Add(countCrit.SetProjection(Projections.RowCountInt64()))

                                    .List();

                    trans.Commit();

                    IList <TD> _list = new List <TD>();

                    foreach (var o in (IList)results[0])
                    {
                        _list.Add((TD)o);
                    }

                    count = (long)((IList)results[1])[0];

                    return(_list);
                }
            }
        }
Esempio n. 10
0
        public virtual IList Execute(ISession theSession)
        {
            // a sub class may need this at a later date.
            session = theSession;

            if (criteriaList.Count == 0)
            {
                return(new ArrayList());
            }

            IMultiCriteria multiCriteria = theSession.CreateMultiCriteria();

            foreach (DetachedCriteria detachedCriteria in criteriaList)
            {
                multiCriteria.Add(CreateCriteria(theSession, detachedCriteria));
            }

            IList list    = multiCriteria.List();
            int   results = list.Count;

            for (int i = 0; i < results; i++)
            {
                if (collectionDelegates[i] != null)
                {
                    collectionDelegates[i]((IList)list[i]);
                }
                if (uniqueResultDelegate[i] != null)
                {
                    object single = Collection.Single((ICollection)list[i]);
                    uniqueResultDelegate[i](single);
                }
                if (collectionAndCountDelegate[i] != null)
                {
                    IList queryList = (IList)list[i];
                    int   count     = Convert.ToInt32(
                        Collection.Single((ICollection)list[i + 1])
                        );
                    collectionAndCountDelegate[i](queryList, count);
                    i += 1;//not a best practice, I will admit
                }
            }

            return(list);
        }
Esempio n. 11
0
        /// <summary>
        /// Finds all by multi criteria.
        /// </summary>
        /// <param name="detachedCriteriaDic">The detached criteria dic.</param>
        /// <returns></returns>
        public Dictionary <string, IList> FindAllByMultiCriteria(IDictionary <string, DetachedCriteria> detachedCriteriaDic)
        {
            Dictionary <string, IList> resultDict = new Dictionary <string, IList>();

            if (detachedCriteriaDic != null && detachedCriteriaDic.Keys != null && detachedCriteriaDic.Keys.Count > 0)
            {
                IMultiCriteria multicriteria = Session.CreateMultiCriteria();

                foreach (string key in detachedCriteriaDic.Keys)
                {
                    multicriteria.Add(detachedCriteriaDic[key]);
                }
                IList list = multicriteria.List();

                int count = 0;
                foreach (string key in detachedCriteriaDic.Keys)
                {
                    resultDict.Add(key, (IList)list[count]);
                    count++;
                }
            }
            return(resultDict);
        }