Skip to content

Commit f090edb

Browse files
committed
Restored hack in GroupedProjection and added support for multi-columns property projection
1 parent 584454e commit f090edb

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
@@ -394,6 +394,18 @@ public async Task QueryOverOrderByAndWhereWithIdProjectionDoesntThrowAsync()
394394
}
395395
}
396396

397+
[Test]
398+
public async Task CriteriaGroupProjectionAsync()
399+
{
400+
using (ISession s = OpenSession())
401+
{
402+
await (s.CreateCriteria<ClassWithCompositeId>()
403+
.SetProjection(Projections.GroupProperty(Projections.Id()))
404+
.Add(Restrictions.Eq(Projections.Id(), id))
405+
.ListAsync<Id>());
406+
}
407+
}
408+
397409
[Test]
398410
public async Task QueryOverInClauseAsync()
399411
{

src/NHibernate.Test/CompositeId/ClassWithCompositeIdFixture.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,18 @@ public void QueryOverOrderByAndWhereWithIdProjectionDoesntThrow()
383383
}
384384
}
385385

386+
[Test]
387+
public void CriteriaGroupProjection()
388+
{
389+
using (ISession s = OpenSession())
390+
{
391+
s.CreateCriteria<ClassWithCompositeId>()
392+
.SetProjection(Projections.GroupProperty(Projections.Id()))
393+
.Add(Restrictions.Eq(Projections.Id(), id))
394+
.List<Id>();
395+
}
396+
}
397+
386398
[Test]
387399
public void QueryOverInClause()
388400
{

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)