Skip to content

Commit abaf3d6

Browse files
committed
Restored hack in GroupedProjection and added support for multi-columns property projection
1 parent bbcb467 commit abaf3d6

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
@@ -397,5 +397,17 @@ public async Task QueryOverOrderByAndWhereWithIdProjectionDoesntThrowAsync()
397397
Assert.That(results.Count, Is.EqualTo(1));
398398
}
399399
}
400+
401+
[Test]
402+
public async Task CriteriaGroupProjectionAsync()
403+
{
404+
using (ISession s = OpenSession())
405+
{
406+
await (s.CreateCriteria<ClassWithCompositeId>()
407+
.SetProjection(Projections.GroupProperty(Projections.Id()))
408+
.Add(Restrictions.Eq(Projections.Id(), id))
409+
.ListAsync<Id>());
410+
}
411+
}
400412
}
401413
}

src/NHibernate.Test/CompositeId/ClassWithCompositeIdFixture.cs

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

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)