diff --git a/src/NHibernate.Test/NHSpecificTest/NH3609/Entity.cs b/src/NHibernate.Test/NHSpecificTest/NH3609/Entity.cs new file mode 100644 index 00000000000..1696d203346 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/NH3609/Entity.cs @@ -0,0 +1,10 @@ +using System; + +namespace NHibernate.Test.NHSpecificTest.NH3609 +{ + class Entity + { + public virtual Guid Id { get; set; } + public virtual string Name { get; set; } + } +} \ No newline at end of file diff --git a/src/NHibernate.Test/NHSpecificTest/NH3609/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/NH3609/Fixture.cs new file mode 100644 index 00000000000..7dfbc4e6173 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/NH3609/Fixture.cs @@ -0,0 +1,82 @@ +using System.Linq; +using NHibernate.Criterion; +using NHibernate.Linq; +using NHibernate.Transform; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.NH3609 +{ + [TestFixture] + public class Fixture : BugTestCase + { + protected override void OnSetUp() + { + using (ISession session = OpenSession()) + using (ITransaction transaction = session.BeginTransaction()) + { + var e1 = new Entity {Name = "Bob"}; + session.Save(e1); + + var e2 = new Entity {Name = "Sally"}; + session.Save(e2); + + session.Flush(); + transaction.Commit(); + } + } + + protected override void OnTearDown() + { + using (ISession session = OpenSession()) + using (ITransaction transaction = session.BeginTransaction()) + { + session.Delete("from System.Object"); + + session.Flush(); + transaction.Commit(); + } + } + + [Test] + public void CountWithConditionalDoesNotThrow() + { + using (ISession session = OpenSession()) + using (session.BeginTransaction()) + { + MappingEntity mappingEntity = null; + Assert.DoesNotThrow(() => + session.QueryOver().SelectList( + builder => + builder.Select(Projections.Count(Projections.Conditional( + Restrictions.Eq(Projections.Property(x => x.Name), "FOO"), + Projections.Constant("", NHibernateUtil.String), + Projections.Constant(null, NHibernateUtil.String))).WithAlias(() => mappingEntity.Count)) + ) + .TransformUsing(Transformers.AliasToBean()).List() + ); + } + } + + [Test] + public void GroupByClauseHasParameterSet() + { + using (ISession session = OpenSession()) + using (session.BeginTransaction()) + { + MappingEntity mappingEntity = null; + Assert.DoesNotThrow(() => + session.QueryOver().SelectList( + builder => + builder.Select(Projections.GroupProperty( + Projections.Conditional( + Restrictions.Eq(Projections.Property(x => x.Name), ""), + Projections.Constant(1), Projections.Constant(2))) + .WithAlias(() => mappingEntity.Count)) + ) + .TransformUsing(Transformers.AliasToBean()).List() + ); + } + + } + } +} \ No newline at end of file diff --git a/src/NHibernate.Test/NHSpecificTest/NH3609/MappingEntity.cs b/src/NHibernate.Test/NHSpecificTest/NH3609/MappingEntity.cs new file mode 100644 index 00000000000..d852447f598 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/NH3609/MappingEntity.cs @@ -0,0 +1,9 @@ +using System; + +namespace NHibernate.Test.NHSpecificTest.NH3609 +{ + class MappingEntity + { + public virtual int Count { get; set; } + } +} \ No newline at end of file diff --git a/src/NHibernate.Test/NHSpecificTest/NH3609/Mappings.hbm.xml b/src/NHibernate.Test/NHSpecificTest/NH3609/Mappings.hbm.xml new file mode 100644 index 00000000000..0acfe996131 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/NH3609/Mappings.hbm.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/NHibernate.Test/NHibernate.Test.csproj b/src/NHibernate.Test/NHibernate.Test.csproj index 7c49aefb6ca..8605803f5c8 100644 --- a/src/NHibernate.Test/NHibernate.Test.csproj +++ b/src/NHibernate.Test/NHibernate.Test.csproj @@ -683,6 +683,9 @@ + + + @@ -3014,6 +3017,7 @@ + diff --git a/src/NHibernate/Criterion/CountProjection.cs b/src/NHibernate/Criterion/CountProjection.cs index fd4ba343e1e..bdc42a42831 100644 --- a/src/NHibernate/Criterion/CountProjection.cs +++ b/src/NHibernate/Criterion/CountProjection.cs @@ -35,19 +35,18 @@ public override SqlString ToSqlString(ICriteria criteria, int position, ICriteri { buf.Add("distinct "); } - string column; if(projection!=null) { - column = + buf.Add( SqlStringHelper.RemoveAsAliasesFromSql(projection.ToSqlString(criteria, position, criteriaQuery, - enabledFilters)).ToString(); + enabledFilters))); } else { - column = criteriaQuery.GetColumn(criteria, propertyName); + buf.Add(criteriaQuery.GetColumn(criteria, propertyName)); } - buf.Add(column).Add(") as y").Add(position.ToString()).Add("_"); + buf.Add(") as y").Add(position.ToString()).Add("_"); return buf.ToSqlString(); } diff --git a/src/NHibernate/Loader/AbstractEntityJoinWalker.cs b/src/NHibernate/Loader/AbstractEntityJoinWalker.cs index 78ddf6ad6dc..aed97765ee7 100644 --- a/src/NHibernate/Loader/AbstractEntityJoinWalker.cs +++ b/src/NHibernate/Loader/AbstractEntityJoinWalker.cs @@ -43,15 +43,15 @@ protected void InitProjection(SqlString projectionString, SqlString whereString, { WalkEntityTree(persister, Alias); Persisters = new ILoadable[0]; - InitStatementString(projectionString, whereString, orderByString, groupByString.ToString(), havingString, lockMode); + InitStatementString(projectionString, whereString, orderByString, groupByString, havingString, lockMode); } private void InitStatementString(SqlString condition, SqlString orderBy, LockMode lockMode) { - InitStatementString(null, condition, orderBy, string.Empty, null, lockMode); + InitStatementString(null, condition, orderBy, null, null, lockMode); } - private void InitStatementString(SqlString projection,SqlString condition, SqlString orderBy, string groupBy, SqlString having, LockMode lockMode) + private void InitStatementString(SqlString projection,SqlString condition, SqlString orderBy, SqlString groupBy, SqlString having, LockMode lockMode) { int joins = CountEntityPersisters(associations); Suffixes = BasicLoader.GenerateSuffixes(joins + 1); diff --git a/src/NHibernate/SqlCommand/SqlSelectBuilder.cs b/src/NHibernate/SqlCommand/SqlSelectBuilder.cs index dab0a6b076f..57d2aadf047 100644 --- a/src/NHibernate/SqlCommand/SqlSelectBuilder.cs +++ b/src/NHibernate/SqlCommand/SqlSelectBuilder.cs @@ -18,7 +18,7 @@ public class SqlSelectBuilder : SqlBaseBuilder, ISqlStringBuilder private SqlString whereClause; private SqlString outerJoinsAfterWhere; private SqlString orderByClause; - private string groupByClause; + private SqlString groupByClause; private SqlString havingClause; private LockMode lockMode; private string comment; @@ -83,7 +83,7 @@ public SqlSelectBuilder SetOrderByClause(SqlString orderByClause) /// /// The groupByClause to set /// The SqlSelectBuilder - public SqlSelectBuilder SetGroupByClause(string groupByClause) + public SqlSelectBuilder SetGroupByClause(SqlString groupByClause) { this.groupByClause = groupByClause; return this; @@ -247,7 +247,7 @@ public SqlString ToSqlString() } } - if (StringHelper.IsNotEmpty(groupByClause)) + if (SqlStringHelper.IsNotEmpty(groupByClause)) { sqlBuilder.Add(" GROUP BY ") .Add(groupByClause);