From: <ric...@us...> - 2010-04-27 16:28:16
|
Revision: 4973 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4973&view=rev Author: ricbrown Date: 2010-04-27 16:28:10 +0000 (Tue, 27 Apr 2010) Log Message: ----------- Implement NH-2186 (Allow MultiCriteria to directly add IQueryOver; improvement suggested by Jason Dentler) Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs trunk/nhibernate/src/NHibernate/IMultiCriteria.cs trunk/nhibernate/src/NHibernate/IQueryOver.cs trunk/nhibernate/src/NHibernate/Impl/MultiCriteriaImpl.cs trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs Modified: trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs 2010-04-22 22:25:21 UTC (rev 4972) +++ trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs 2010-04-27 16:28:10 UTC (rev 4973) @@ -35,6 +35,11 @@ get { return criteria; } } + public ICriteria RootCriteria + { + get { return impl; } + } + public DetachedCriteria DetachedCriteria { get { return new DetachedCriteria(impl, impl); } @@ -138,9 +143,6 @@ } - ICriteria IQueryOver<TRoot>.UnderlyingCriteria - { get { return UnderlyingCriteria; } } - IList<TRoot> IQueryOver<TRoot>.List() { return List(); } Modified: trunk/nhibernate/src/NHibernate/IMultiCriteria.cs =================================================================== --- trunk/nhibernate/src/NHibernate/IMultiCriteria.cs 2010-04-22 22:25:21 UTC (rev 4972) +++ trunk/nhibernate/src/NHibernate/IMultiCriteria.cs 2010-04-27 16:28:10 UTC (rev 4973) @@ -83,6 +83,44 @@ IMultiCriteria Add(string key, DetachedCriteria detachedCriteria); /// <summary> + /// Adds the specified IQueryOver to the query. The result will be contained in a <see cref="System.Collections.Generic.List{resultGenericListType}"/> + /// </summary> + /// <param name="resultGenericListType">Return results in a <see cref="System.Collections.Generic.List{resultGenericListType}"/></param> + /// <param name="queryOver">The IQueryOver.</param> + /// <returns></returns> + IMultiCriteria Add(System.Type resultGenericListType, IQueryOver queryOver); + + /// <summary> + /// Adds the specified IQueryOver to the query. The result will be contained in a <see cref="System.Collections.Generic.List{T}"/> + /// </summary> + /// <param name="queryOver">The IQueryOver.</param> + /// <returns></returns> + IMultiCriteria Add<T>(IQueryOver<T> queryOver); + + /// <summary> + /// Adds the specified IQueryOver to the query. The result will be contained in a <see cref="System.Collections.Generic.List{U}"/> + /// </summary> + /// <param name="queryOver">The IQueryOver.</param> + /// <returns></returns> + IMultiCriteria Add<U>(IQueryOver queryOver); + + /// <summary> + /// Adds the specified IQueryOver to the query, and associates it with the given key. The result will be contained in a <see cref="System.Collections.Generic.List{T}"/> + /// </summary> + /// <param name="key">The key</param> + /// <param name="queryOver">The IQueryOver</param> + /// <returns></returns> + IMultiCriteria Add<T>(string key, IQueryOver<T> queryOver); + + /// <summary> + /// Adds the specified IQueryOver to the query, and associates it with the given key. The result will be contained in a <see cref="System.Collections.Generic.List{U}"/> + /// </summary> + /// <param name="key">The key</param> + /// <param name="queryOver">The IQueryOver</param> + /// <returns></returns> + IMultiCriteria Add<U>(string key, IQueryOver queryOver); + + /// <summary> /// Sets whatevert this criteria is cacheable. /// </summary> /// <param name="cachable">if set to <c>true</c> [cachable].</param> Modified: trunk/nhibernate/src/NHibernate/IQueryOver.cs =================================================================== --- trunk/nhibernate/src/NHibernate/IQueryOver.cs 2010-04-22 22:25:21 UTC (rev 4972) +++ trunk/nhibernate/src/NHibernate/IQueryOver.cs 2010-04-27 16:28:10 UTC (rev 4973) @@ -10,6 +10,19 @@ namespace NHibernate { + public interface IQueryOver + { + /// <summary> + /// Access the underlying ICriteria + /// </summary> + ICriteria UnderlyingCriteria { get; } + + /// <summary> + /// Access the root underlying ICriteria + /// </summary> + ICriteria RootCriteria { get; } + } + /// <summary> /// QueryOver<TRoot> is an API for retrieving entities by composing /// <see cref="Criterion.Expression" /> objects expressed using Lambda expression syntax. @@ -22,14 +35,9 @@ /// .List(); /// </code> /// </remarks> - public interface IQueryOver<TRoot> + public interface IQueryOver<TRoot> : IQueryOver { /// <summary> - /// Access the underlying ICriteria - /// </summary> - ICriteria UnderlyingCriteria { get; } - - /// <summary> /// Get the results of the root type and fill the <see cref="IList<T>"/> /// </summary> /// <returns>The list filled with the results.</returns> Modified: trunk/nhibernate/src/NHibernate/Impl/MultiCriteriaImpl.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Impl/MultiCriteriaImpl.cs 2010-04-22 22:25:21 UTC (rev 4972) +++ trunk/nhibernate/src/NHibernate/Impl/MultiCriteriaImpl.cs 2010-04-27 16:28:10 UTC (rev 4973) @@ -424,6 +424,31 @@ return this; } + public IMultiCriteria Add(System.Type resultGenericListType, IQueryOver queryOver) + { + return Add(resultGenericListType, queryOver.RootCriteria); + } + + public IMultiCriteria Add<T>(IQueryOver<T> queryOver) + { + return Add<T>(queryOver.RootCriteria); + } + + public IMultiCriteria Add<U>(IQueryOver queryOver) + { + return Add<U>(queryOver.RootCriteria); + } + + public IMultiCriteria Add<T>(string key, IQueryOver<T> queryOver) + { + return Add<T>(key, queryOver.RootCriteria); + } + + public IMultiCriteria Add<U>(string key, IQueryOver queryOver) + { + return Add<U>(key, queryOver.RootCriteria); + } + public IMultiCriteria SetCacheable(bool cachable) { isCacheable = cachable; Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs 2010-04-22 22:25:21 UTC (rev 4972) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs 2010-04-27 16:28:10 UTC (rev 4973) @@ -265,24 +265,8 @@ [Test] public void RowCount() { - using (ISession s = OpenSession()) - using (ITransaction t = s.BeginTransaction()) - { - s.Save(new Person() { Name = "Name 1", Age = 1 } - .AddChild(new Child() { Nickname = "Name 1.1", Age = 1})); + SetupPagingData(); - s.Save(new Person() { Name = "Name 2", Age = 2 } - .AddChild(new Child() { Nickname = "Name 2.1", Age = 3})); - - s.Save(new Person() { Name = "Name 3", Age = 3 } - .AddChild(new Child() { Nickname = "Name 3.1", Age = 2})); - - s.Save(new Person() { Name = "Name 4", Age = 4 } - .AddChild(new Child() { Nickname = "Name 4.1", Age = 4})); - - t.Commit(); - } - using (ISession s = OpenSession()) { IQueryOver<Person> query = @@ -301,6 +285,79 @@ } } + [Test] + public void MultiCriteria() + { + SetupPagingData(); + + using (ISession s = OpenSession()) + { + IQueryOver<Person> query = + s.QueryOver<Person>() + .JoinQueryOver(p => p.Children) + .OrderBy(c => c.Age).Desc + .Skip(2) + .Take(1); + + var multiCriteria = + s.CreateMultiCriteria() + .Add("page", query) + .Add<int>("count", query.ToRowCountQuery()); + + var pageResults = (IList<Person>) multiCriteria.GetResult("page"); + var countResults = (IList<int>) multiCriteria.GetResult("count"); + + Assert.That(pageResults.Count, Is.EqualTo(1)); + Assert.That(pageResults[0].Name, Is.EqualTo("Name 3")); + Assert.That(countResults.Count, Is.EqualTo(1)); + Assert.That(countResults[0], Is.EqualTo(4)); + } + + using (ISession s = OpenSession()) + { + QueryOver<Person> query = + QueryOver.Of<Person>() + .JoinQueryOver(p => p.Children) + .OrderBy(c => c.Age).Desc + .Skip(2) + .Take(1); + + var multiCriteria = + s.CreateMultiCriteria() + .Add("page", query) + .Add<int>("count", query.ToRowCountQuery()); + + var pageResults = (IList<Person>) multiCriteria.GetResult("page"); + var countResults = (IList<int>) multiCriteria.GetResult("count"); + + Assert.That(pageResults.Count, Is.EqualTo(1)); + Assert.That(pageResults[0].Name, Is.EqualTo("Name 3")); + Assert.That(countResults.Count, Is.EqualTo(1)); + Assert.That(countResults[0], Is.EqualTo(4)); + } + } + + private void SetupPagingData() + { + using (ISession s = OpenSession()) + using (ITransaction t = s.BeginTransaction()) + { + s.Save(new Person() { Name = "Name 1", Age = 1 } + .AddChild(new Child() { Nickname = "Name 1.1", Age = 1})); + + s.Save(new Person() { Name = "Name 2", Age = 2 } + .AddChild(new Child() { Nickname = "Name 2.1", Age = 3})); + + s.Save(new Person() { Name = "Name 3", Age = 3 } + .AddChild(new Child() { Nickname = "Name 3.1", Age = 2})); + + s.Save(new Person() { Name = "Name 4", Age = 4 } + .AddChild(new Child() { Nickname = "Name 4.1", Age = 4})); + + t.Commit(); + } + } + } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |