|
From: <ric...@us...> - 2010-03-07 09:47:51
|
Revision: 4954
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4954&view=rev
Author: ricbrown
Date: 2010-03-07 09:47:45 +0000 (Sun, 07 Mar 2010)
Log Message:
-----------
Moved QueryOverTransformer methods into the IQueryOver interface, and dropped ICloneable.
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs
trunk/nhibernate/src/NHibernate/IQueryOver.cs
trunk/nhibernate/src/NHibernate/NHibernate.csproj
trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs
trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs
Removed Paths:
-------------
trunk/nhibernate/src/NHibernate/QueryOverTransformer.cs
Modified: trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs 2010-03-06 22:49:21 UTC (rev 4953)
+++ trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs 2010-03-07 09:47:45 UTC (rev 4954)
@@ -40,8 +40,6 @@
get { return new DetachedCriteria(impl, impl); }
}
- public abstract object Clone();
-
}
[Serializable]
@@ -107,6 +105,28 @@
}
/// <summary>
+ /// Clones the QueryOver, clears the orders and paging, and projects the RowCount
+ /// </summary>
+ /// <returns></returns>
+ public QueryOver<TRoot,TRoot> ToRowCountQuery()
+ {
+ return
+ Clone()
+ .ClearOrders()
+ .Skip(0)
+ .Take(RowSelection.NoValue)
+ .Select(Projections.RowCount());
+ }
+
+ /// <summary>
+ /// Creates an exact clone of the QueryOver
+ /// </summary>
+ public QueryOver<TRoot,TRoot> Clone()
+ {
+ return new QueryOver<TRoot,TRoot>((CriteriaImpl)criteria.Clone());
+ }
+
+ /// <summary>
/// Method to allow comparison of detached query in Lambda expression
/// e.g., p => p.Name == myQuery.As<string>
/// </summary>
@@ -127,6 +147,9 @@
IList<U> IQueryOver<TRoot>.List<U>()
{ return List<U>(); }
+ int IQueryOver<TRoot>.RowCount()
+ { return ToRowCountQuery().SingleOrDefault<int>(); }
+
TRoot IQueryOver<TRoot>.SingleOrDefault()
{ return SingleOrDefault(); }
@@ -145,6 +168,9 @@
IFutureValue<U> IQueryOver<TRoot>.FutureValue<U>()
{ return FutureValue<U>(); }
+ IQueryOver<TRoot,TRoot> IQueryOver<TRoot>.Clone()
+ { return Clone(); }
+
}
/// <summary>
@@ -179,11 +205,6 @@
this.criteria = criteria;
}
- public override object Clone()
- {
- return new QueryOver<TRoot,TRoot>((CriteriaImpl)criteria.Clone());
- }
-
public QueryOver<TRoot,TSubType> And(Expression<Func<TSubType, bool>> expression)
{
return Add(expression);
Modified: trunk/nhibernate/src/NHibernate/IQueryOver.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/IQueryOver.cs 2010-03-06 22:49:21 UTC (rev 4953)
+++ trunk/nhibernate/src/NHibernate/IQueryOver.cs 2010-03-07 09:47:45 UTC (rev 4954)
@@ -22,7 +22,7 @@
/// .List();
/// </code>
/// </remarks>
- public interface IQueryOver<TRoot> : ICloneable
+ public interface IQueryOver<TRoot>
{
/// <summary>
/// Access the underlying ICriteria
@@ -42,6 +42,12 @@
IList<U> List<U>();
/// <summary>
+ /// Clones the QueryOver, removes orders and paging, projects the row-count
+ /// for the query and returns the Single() result
+ /// </summary>
+ int RowCount();
+
+ /// <summary>
/// Convenience method to return a single instance that matches
/// the query, or null if the query returns no results.
/// </summary>
@@ -84,6 +90,11 @@
/// </summary>
IFutureValue<U> FutureValue<U>();
+ /// <summary>
+ /// Creates an exact clone of the IQueryOver
+ /// </summary>
+ IQueryOver<TRoot,TRoot> Clone();
+
}
/// <summary>
Modified: trunk/nhibernate/src/NHibernate/NHibernate.csproj
===================================================================
--- trunk/nhibernate/src/NHibernate/NHibernate.csproj 2010-03-06 22:49:21 UTC (rev 4953)
+++ trunk/nhibernate/src/NHibernate/NHibernate.csproj 2010-03-07 09:47:45 UTC (rev 4954)
@@ -830,7 +830,6 @@
<Compile Include="Hql\Ast\ANTLR\Util\NodeTraverser.cs" />
<Compile Include="Param\VersionTypeSeedParameterSpecification.cs" />
<Compile Include="Proxy\AbstractProxyFactory.cs" />
- <Compile Include="QueryOverTransformer.cs" />
<Compile Include="SqlCommand\InsertSelect.cs" />
<Compile Include="Tool\hbm2ddl\SchemaMetadataUpdater.cs" />
<Compile Include="Tool\hbm2ddl\ScriptSplitter.cs" />
Deleted: trunk/nhibernate/src/NHibernate/QueryOverTransformer.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/QueryOverTransformer.cs 2010-03-06 22:49:21 UTC (rev 4953)
+++ trunk/nhibernate/src/NHibernate/QueryOverTransformer.cs 2010-03-07 09:47:45 UTC (rev 4954)
@@ -1,54 +0,0 @@
-using NHibernate.Criterion;
-using NHibernate.Engine;
-using NHibernate.Impl;
-
-namespace NHibernate
-{
- /// <summary>
- /// Transforms QueryOver queries
- /// </summary>
- public static class QueryOverTransformer
- {
- ///<summary>
- /// Returns a clone of the original QueryOver, which will return the count
- /// of rows that are returned by the original QueryOver query.
- ///</summary>
- public static QueryOver<T,T> TransformToRowCount<T>(QueryOver<T> query)
- {
- QueryOver<T,T> clonedQuery = (QueryOver<T,T>)query.Clone();
- return (QueryOver<T,T>)TransformToRowCount((IQueryOver<T>)clonedQuery);
- }
-
- ///<summary>
- /// Returns a clone of the original IQueryOver, which will return the count
- /// of rows that are returned by the original IQueryOver query.
- ///</summary>
- public static IQueryOver<T,T> TransformToRowCount<T>(IQueryOver<T> query)
- {
- IQueryOver<T,T> clonedQuery = (IQueryOver<T,T>)query.Clone();
-
- return
- clonedQuery
- .ClearOrders()
- .Skip(0)
- .Take(RowSelection.NoValue)
- .Select(Projections.RowCount());
- }
-
- /// <summary>
- /// Creates an exact clone of the IQueryOver
- /// </summary>
- public static IQueryOver<T,T> Clone<T>(IQueryOver<T> query)
- {
- return (IQueryOver<T,T>)query.Clone();
- }
-
- /// <summary>
- /// Creates an exact clone of the QueryOver
- /// </summary>
- public static QueryOver<T,T> Clone<T>(QueryOver<T> query)
- {
- return (QueryOver<T,T>)query.Clone();
- }
- }
-}
Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs 2010-03-06 22:49:21 UTC (rev 4953)
+++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs 2010-03-07 09:47:45 UTC (rev 4954)
@@ -262,6 +262,45 @@
}
}
+ [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}));
+
+ 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 =
+ s.QueryOver<Person>()
+ .JoinQueryOver(p => p.Children)
+ .OrderBy(c => c.Age).Desc
+ .Skip(2)
+ .Take(1);
+
+ IList<Person> results = query.List();
+ int rowCount = query.RowCount();
+
+ Assert.That(results.Count, Is.EqualTo(1));
+ Assert.That(results[0].Name, Is.EqualTo("Name 3"));
+ Assert.That(rowCount, Is.EqualTo(4));
+ }
+ }
+
}
}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs 2010-03-06 22:49:21 UTC (rev 4953)
+++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs 2010-03-07 09:47:45 UTC (rev 4954)
@@ -491,7 +491,7 @@
.Where(p => p.Name == "test")
.Select(p => p.Name);
- IQueryOver<Person> actual = QueryOverTransformer.Clone(expected);
+ IQueryOver<Person> actual = expected.Clone();
Assert.That(actual, Is.Not.SameAs(expected));
Assert.That(actual.UnderlyingCriteria, Is.Not.SameAs(expected.UnderlyingCriteria));
@@ -505,7 +505,7 @@
CreateTestQueryOver<Person>()
.JoinQueryOver(p => p.Children);
- IQueryOver<Person,Person> actual = QueryOverTransformer.Clone(expected);
+ IQueryOver<Person,Person> actual = expected.Clone();
ICriteria expectedCriteria = expected.UnderlyingCriteria.GetCriteriaByAlias("this");
@@ -520,38 +520,13 @@
.Where(p => p.Name == "test")
.Select(p => p.Name);
- QueryOver<Person> actual = QueryOverTransformer.Clone(expected);
+ QueryOver<Person> actual = expected.Clone();
Assert.That(actual, Is.Not.SameAs(expected));
Assert.That(actual.UnderlyingCriteria, Is.Not.SameAs(expected.UnderlyingCriteria));
AssertCriteriaAreEqual(expected.UnderlyingCriteria, actual.UnderlyingCriteria);
}
- [Test]
- public void TransformQueryOverToRowCount()
- {
- QueryOver<Person> expected =
- QueryOver.Of<Person>()
- .Where(p => p.Name == "test")
- .JoinQueryOver(p => p.Children)
- .Where((Child c) => c.Age == 5)
- .Select(Projections.RowCount());
-
- QueryOver<Person> actual =
- QueryOver.Of<Person>()
- .Where(p => p.Name == "test")
- .JoinQueryOver(p => p.Children)
- .Where((Child c) => c.Age == 5)
- .OrderBy(c => c.Age).Asc
- .Skip(20)
- .Take(10);
-
- expected = QueryOverTransformer.Clone(expected);
- actual = QueryOverTransformer.TransformToRowCount(actual);
-
- AssertCriteriaAreEqual(expected.UnderlyingCriteria, actual);
- }
-
}
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|