From: <te...@us...> - 2009-07-09 10:00:12
|
Revision: 4593 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4593&view=rev Author: tehlike Date: 2009-07-09 10:00:00 +0000 (Thu, 09 Jul 2009) Log Message: ----------- Fix for NH-1877 Modified Paths: -------------- branches/2.1.x/nhibernate/src/NHibernate/Criterion/Projections.cs branches/2.1.x/nhibernate/src/NHibernate/NHibernate.csproj branches/2.1.x/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- branches/2.1.x/nhibernate/src/NHibernate/Criterion/GroupedProjection.cs branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/ branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/Fixture.cs branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/Mappings.hbm.xml branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/Person.cs Added: branches/2.1.x/nhibernate/src/NHibernate/Criterion/GroupedProjection.cs =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate/Criterion/GroupedProjection.cs (rev 0) +++ branches/2.1.x/nhibernate/src/NHibernate/Criterion/GroupedProjection.cs 2009-07-09 10:00:00 UTC (rev 4593) @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Text; +using NHibernate.Engine; +using NHibernate.SqlCommand; +using NHibernate.Type; +using NHibernate.Util; + +namespace NHibernate.Criterion +{ + public class GroupedProjection:IProjection + { + private readonly IProjection projection; + + public GroupedProjection(IProjection projection) + { + this.projection = projection; + } + + public virtual SqlString ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters) + { + return projection.ToSqlString(criteria, position, criteriaQuery, enabledFilters); + } + + public virtual SqlString ToGroupSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters) + { + return StringHelper.RemoveAsAliasesFromSql(this.projection.ToSqlString(criteria, 0, criteriaQuery, enabledFilters)); + } + + public virtual IType[] GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery) + { + return projection.GetTypes(criteria, criteriaQuery); + } + + public virtual IType[] GetTypes(String alias, ICriteria criteria, ICriteriaQuery criteriaQuery) + { + return this.projection.GetTypes(alias,criteria,criteriaQuery); + } + + public virtual string[] GetColumnAliases(int loc) + { + return projection.GetColumnAliases(loc); + } + + public virtual string[] GetColumnAliases(string alias, int loc) + { + return null; + } + + public virtual string[] Aliases + { + get { return new string[] { }; } + } + + public virtual bool IsGrouped + { + get { return true; } + } + + public bool IsAggregate + { + get { return projection.IsAggregate; } + } + + /// <summary> + /// Gets the typed values for parameters in this projection + /// </summary> + /// <param name="criteria">The criteria.</param> + /// <param name="criteriaQuery">The criteria query.</param> + /// <returns></returns> + public TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery) + { + return projection.GetTypedValues(criteria, criteriaQuery); + } + + public override string ToString() + { + return projection.ToString(); + } + } +} Modified: branches/2.1.x/nhibernate/src/NHibernate/Criterion/Projections.cs =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate/Criterion/Projections.cs 2009-07-08 17:07:02 UTC (rev 4592) +++ branches/2.1.x/nhibernate/src/NHibernate/Criterion/Projections.cs 2009-07-09 10:00:00 UTC (rev 4593) @@ -202,6 +202,16 @@ } /// <summary> + /// A grouping projection value + /// </summary> + /// <param name="projection"></param> + /// <returns></returns> + public static GroupedProjection GroupProperty(IProjection projection) + { + return new GroupedProjection(projection); + } + + /// <summary> /// A projected property value /// </summary> /// <param name="propertyName"></param> Modified: branches/2.1.x/nhibernate/src/NHibernate/NHibernate.csproj =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate/NHibernate.csproj 2009-07-08 17:07:02 UTC (rev 4592) +++ branches/2.1.x/nhibernate/src/NHibernate/NHibernate.csproj 2009-07-09 10:00:00 UTC (rev 4593) @@ -460,6 +460,7 @@ <Compile Include="Cfg\MappingSchema\HbmTimestamp.cs" /> <Compile Include="Cfg\MappingSchema\HbmVersion.cs" /> <Compile Include="Cfg\MappingSchema\IDecoratable.cs" /> + <Compile Include="Criterion\GroupedProjection.cs" /> <Compile Include="Criterion\IPropertyProjection.cs" /> <Compile Include="Dialect\MsSql2008Dialect.cs" /> <Compile Include="Dialect\InformixDialect0940.cs" /> Added: branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/Fixture.cs =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/Fixture.cs (rev 0) +++ branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/Fixture.cs 2009-07-09 10:00:00 UTC (rev 4593) @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Text; +using NHibernate.Criterion; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.NH1877 +{ + [TestFixture] + public class Fixture : BugTestCase + { + protected override void OnSetUp() + { + using(var session=OpenSession()) + using(var tran=session.BeginTransaction()) + { + session.Save(new Person {BirthDate = new DateTime(1988, 7, 21)}); + session.Save(new Person { BirthDate = new DateTime(1987, 7, 22) }); + session.Save(new Person { BirthDate = new DateTime(1986, 7, 23) }); + session.Save(new Person { BirthDate = new DateTime(1987, 7, 24) }); + session.Save(new Person { BirthDate = new DateTime(1988, 7, 25) }); + tran.Commit(); + } + } + + protected override void OnTearDown() + { + using (var session = OpenSession()) + using (var tran = session.BeginTransaction()) + { + session.CreateQuery("delete from Person").ExecuteUpdate(); + tran.Commit(); + } + } + + [Test] + public void CanGroupByWithPropertyName() + { + using(var session=OpenSession()) + { + var crit = session.CreateCriteria(typeof (Person)) + .SetProjection(Projections.GroupProperty("BirthDate"), + Projections.Count("Id")); + var result = crit.List(); + Assert.That(result,Has.Count.EqualTo(5)); + } + } + + [Test] + public void CanGroupByWithSqlFunctionProjection() + { + using (var session = OpenSession()) + { + var crit = session.CreateCriteria(typeof (Person)) + .SetProjection( + Projections.GroupProperty(Projections.SqlFunction("month", NHibernateUtil.Int32, Projections.Property("BirthDate")))); + + var result = crit.UniqueResult(); + Assert.That(result, Has.Count.EqualTo(1)); + Assert.That(result,Is.EqualTo(7)); + } + } + } +} \ No newline at end of file Added: branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/Mappings.hbm.xml =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/Mappings.hbm.xml (rev 0) +++ branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/Mappings.hbm.xml 2009-07-09 10:00:00 UTC (rev 4593) @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" + namespace="NHibernate.Test.NHSpecificTest.NH1877" + default-lazy="false"> + + <class name="Person"> + <id name="Id"> + <generator class="hilo"/> + </id> + <property name="BirthDate"/> + + </class> +</hibernate-mapping> \ No newline at end of file Added: branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/Person.cs =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/Person.cs (rev 0) +++ branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1877/Person.cs 2009-07-09 10:00:00 UTC (rev 4593) @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace NHibernate.Test.NHSpecificTest.NH1877 +{ + public class Person + { + public virtual long Id { get; set; } + public virtual DateTime BirthDate { get; set; } + } +} Modified: branches/2.1.x/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-07-08 17:07:02 UTC (rev 4592) +++ branches/2.1.x/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-07-09 10:00:00 UTC (rev 4593) @@ -535,6 +535,8 @@ <Compile Include="NHSpecificTest\NH1867\AddMappingTest.cs" /> <Compile Include="NHSpecificTest\NH1868\Fixture.cs" /> <Compile Include="NHSpecificTest\NH1868\Model.cs" /> + <Compile Include="NHSpecificTest\NH1877\Fixture.cs" /> + <Compile Include="NHSpecificTest\NH1877\Person.cs" /> <Compile Include="NHSpecificTest\NH473\Child.cs" /> <Compile Include="NHSpecificTest\NH473\Fixture.cs" /> <Compile Include="NHSpecificTest\NH473\Parent.cs" /> @@ -1945,6 +1947,7 @@ <EmbeddedResource Include="Bytecode\Lightweight\ProductLine.hbm.xml" /> <EmbeddedResource Include="DriverTest\MultiTypeEntity.hbm.xml" /> <Content Include="DynamicEntity\package.html" /> + <EmbeddedResource Include="NHSpecificTest\NH1877\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1868\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1849\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1857\Mappings.hbm.xml" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |