public void CanExecuteMultiplyQueriesInSingleRoundTrip() { using (ISession s = OpenSession()) { Item item = new Item(); item.Id = 1; s.Save(item); s.Flush(); } using (ISession s = OpenSession()) { ICriteria getItems = s.CreateCriteria(typeof(Item)); ICriteria countItems = s.CreateCriteria(typeof(Item)) .SetProjection(Projections.RowCount()); IMultiCriteria multiCriteria = s.CreateMultiCriteria() .Add(getItems) .Add(countItems); IList results = multiCriteria.List(); IList items = (IList)results[0]; Item fromDb = (Item)items[0]; Assert.AreEqual(1, fromDb.Id); IList counts = (IList)results[1]; int count = (int)counts[0]; Assert.AreEqual(1, count); } using (ISession s = OpenSession()) { s.Delete("from Item"); s.Flush(); } }
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); } }
public void TwoMultiQueriesWithDifferentPagingGetDifferentResultsWhenUsingCachedQueries() { CreateItems(); using (ISession s = OpenSession()) { ICriteria criteria = s.CreateCriteria(typeof(Item)) .Add(Expression.Gt("id", 50)); IMultiCriteria multiCriteria = s.CreateMultiCriteria() .Add(CriteriaTransformer.Clone(criteria).SetFirstResult(10)) .Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount())); multiCriteria.SetCacheable(true); IList results = multiCriteria.List(); IList items = (IList)results[0]; Assert.AreEqual(89, items.Count); int count = (int)((IList)results[1])[0]; Assert.AreEqual(99L, count); } using (ISession s = OpenSession()) { ICriteria criteria = s.CreateCriteria(typeof(Item)) .Add(Expression.Gt("id", 50)); IMultiCriteria multiCriteria = s.CreateMultiCriteria() .Add(CriteriaTransformer.Clone(criteria).SetFirstResult(20)) .Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount())); multiCriteria.SetCacheable(true); IList results = multiCriteria.List(); IList items = (IList)results[0]; Assert.AreEqual(79, items.Count, "Should have gotten different result here, because the paging is different"); int count = (int)((IList)results[1])[0]; Assert.AreEqual(99L, count); } RemoveAllItems(); }
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); }
public void CanGetResultInAGenericList() { using (ISession s = OpenSession()) { ICriteria getItems = s.CreateCriteria(typeof(Item)); ICriteria countItems = s.CreateCriteria(typeof(Item)) .SetProjection(Projections.RowCount()); IMultiCriteria multiCriteria = s.CreateMultiCriteria() .Add(getItems) // we expect a non-generic result from this (ArrayList) .Add <int>(countItems); // we expect a generic result from this (List<int>) IList results = multiCriteria.List(); Assert.That(results[0], Is.InstanceOf <ArrayList>()); Assert.That(results[1], Is.InstanceOf <List <int> >()); } }
private void DoMutiQueryAndAssert() { using (ISession s = OpenSession()) { ICriteria criteria = s.CreateCriteria(typeof(Item)) .Add(Expression.Gt("id", 50)); IMultiCriteria multiCriteria = s.CreateMultiCriteria() .Add(CriteriaTransformer.Clone(criteria).SetFirstResult(10)) .Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount())); multiCriteria.SetCacheable(true); IList results = multiCriteria.List(); IList items = (IList)results[0]; Assert.AreEqual(89, items.Count); int count = (int)((IList)results[1])[0]; Assert.AreEqual(99L, count); } }
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); }
public void ExecutingCriteriaThroughMultiCriteriaTransformsResults() { CreateItems(); using (ISession session = OpenSession()) { ResultTransformerStub transformer = new ResultTransformerStub(); ICriteria criteria = session.CreateCriteria(typeof(Item)) .SetResultTransformer(transformer); IMultiCriteria multiCriteria = session.CreateMultiCriteria() .Add(criteria); multiCriteria.List(); Assert.IsTrue(transformer.WasTransformTupleCalled, "Transform Tuple was not called"); Assert.IsTrue(transformer.WasTransformListCalled, "Transform List was not called"); } RemoveAllItems(); }
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); } }
/// <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); }
public void CanGetMultiQueryFromSecondLevelCache() { CreateItems(); //set the query in the cache DoMutiQueryAndAssert(); Hashtable cacheHashtable = GetHashTableUsedAsQueryCache(); IList cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0]; IList cachedQuery = (IList)cachedListEntry[1]; IList firstQueryResults = (IList)cachedQuery[0]; firstQueryResults.Clear(); firstQueryResults.Add(3); firstQueryResults.Add(4); IList secondQueryResults = (IList)cachedQuery[1]; secondQueryResults[0] = 2; using (ISession s = sessions.OpenSession()) { ICriteria criteria = s.CreateCriteria(typeof(Item)) .Add(Expression.Gt("id", 50)); IMultiCriteria multiCriteria = s.CreateMultiCriteria() .Add(CriteriaTransformer.Clone(criteria).SetFirstResult(10)) .Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount())); multiCriteria.SetCacheable(true); IList results = multiCriteria.List(); IList items = (IList)results[0]; Assert.AreEqual(2, items.Count); int count = (int)((IList)results[1])[0]; Assert.AreEqual(2L, count); } RemoveAllItems(); }