Skip to content

Commit e8e7896

Browse files
committed
Restored hack in GroupedProjection and added support for multi-columns property projection
1 parent 82c514e commit e8e7896

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/NHibernate.Test/Async/CompositeId/ClassWithCompositeIdFixture.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,5 +396,17 @@ public async Task QueryOverOrderByAndWhereWithIdProjectionDoesntThrowAsync()
396396
Assert.That(results.Count, Is.EqualTo(1));
397397
}
398398
}
399+
400+
[Test]
401+
public async Task CriteriaGroupProjectionAsync()
402+
{
403+
using (ISession s = OpenSession())
404+
{
405+
await (s.CreateCriteria<ClassWithCompositeId>()
406+
.SetProjection(Projections.GroupProperty(Projections.Id()))
407+
.Add(Restrictions.Eq(Projections.Id(), id))
408+
.ListAsync<Id>());
409+
}
410+
}
399411
}
400412
}

src/NHibernate.Test/CompositeId/ClassWithCompositeIdFixture.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,5 +385,17 @@ public void QueryOverOrderByAndWhereWithIdProjectionDoesntThrow()
385385
Assert.That(results.Count, Is.EqualTo(1));
386386
}
387387
}
388+
389+
[Test]
390+
public void CriteriaGroupProjection()
391+
{
392+
using (ISession s = OpenSession())
393+
{
394+
s.CreateCriteria<ClassWithCompositeId>()
395+
.SetProjection(Projections.GroupProperty(Projections.Id()))
396+
.Add(Restrictions.Eq(Projections.Id(), id))
397+
.List<Id>();
398+
}
399+
}
388400
}
389401
}

src/NHibernate/Criterion/GroupedProjection.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace NHibernate.Criterion
99
public class GroupedProjection : IProjection
1010
{
1111
private readonly IProjection projection;
12+
private SqlString renderedProjection;
1213

1314
public GroupedProjection(IProjection projection)
1415
{
@@ -17,12 +18,16 @@ public GroupedProjection(IProjection projection)
1718

1819
public virtual SqlString ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery)
1920
{
20-
return projection.ToSqlString(criteria, position, criteriaQuery);
21+
return renderedProjection = projection.ToSqlString(criteria, position, criteriaQuery);
2122
}
2223

2324
public virtual SqlString ToGroupSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
2425
{
25-
return SqlStringHelper.Join(new SqlString(","), CriterionUtil.GetColumnNamesAsSqlStringParts(null, projection, criteriaQuery, criteria));
26+
if (projection is IPropertyProjection propertyProjection)
27+
return new SqlString(string.Join(", ", criteriaQuery.GetColumns(criteria, propertyProjection.PropertyName)));
28+
29+
//This is kind of a hack. The hack is based on the fact that ToGroupSqlString always called after ToSqlString.
30+
return SqlStringHelper.RemoveAsAliasesFromSql(renderedProjection);
2631
}
2732

2833
public virtual IType[] GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery)

0 commit comments

Comments
 (0)