Skip to content

Fix Order By for composite property projection in Criteria #2160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,5 +367,35 @@ public async Task HqlInClauseSubquery_ForEntityAsync()
Assert.That(results.Count, Is.EqualTo(2));
}
}

//NH-2926 (GH-1103)
[Test]
public async Task QueryOverOrderByAndWhereWithIdProjectionDoesntThrowAsync()
{
// insert the new objects
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
ClassWithCompositeId theClass = new ClassWithCompositeId(id);
theClass.OneProperty = 5;

ClassWithCompositeId theSecondClass = new ClassWithCompositeId(secondId);
theSecondClass.OneProperty = 10;

await (s.SaveAsync(theClass));
await (s.SaveAsync(theSecondClass));

await (t.CommitAsync());
}

using (ISession s = OpenSession())
{
var results = await (s.QueryOver<ClassWithCompositeId>()
.Select(Projections.Id())
.Where(Restrictions.Eq(Projections.Id(), id))
.OrderBy(Projections.Id()).Desc.ListAsync<Id>());
Assert.That(results.Count, Is.EqualTo(1));
}
}
}
}
30 changes: 30 additions & 0 deletions src/NHibernate.Test/CompositeId/ClassWithCompositeIdFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,5 +356,35 @@ public void HqlInClauseSubquery_ForEntity()
Assert.That(results.Count, Is.EqualTo(2));
}
}

//NH-2926 (GH-1103)
[Test]
public void QueryOverOrderByAndWhereWithIdProjectionDoesntThrow()
{
// insert the new objects
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
ClassWithCompositeId theClass = new ClassWithCompositeId(id);
theClass.OneProperty = 5;

ClassWithCompositeId theSecondClass = new ClassWithCompositeId(secondId);
theSecondClass.OneProperty = 10;

s.Save(theClass);
s.Save(theSecondClass);

t.Commit();
}

using (ISession s = OpenSession())
{
var results = s.QueryOver<ClassWithCompositeId>()
.Select(Projections.Id())
.Where(Restrictions.Eq(Projections.Id(), id))
.OrderBy(Projections.Id()).Desc.List<Id>();
Assert.That(results.Count, Is.EqualTo(1));
}
}
}
}
5 changes: 5 additions & 0 deletions src/NHibernate/Criterion/CriterionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public static SqlString[] GetColumnNamesForSimpleExpression(

internal static SqlString[] GetColumnNamesUsingProjection(IProjection projection, ICriteriaQuery criteriaQuery, ICriteria criteria)
{
if (projection is IPropertyProjection propertyProjection)
Copy link
Member Author

@bahusoid bahusoid Sep 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is actually unrelated to OrderBy fix. But it fixes cases when GetColumnNamesUsingProjection is called for composite property projections (in tests it's covered by .Where(Restrictions.Eq(Projections.Id(), id)))

{
return GetColumnNamesUsingPropertyName(criteriaQuery, criteria, propertyProjection.PropertyName);
}

SqlString sqlString = projection.ToSqlString(criteria,
criteriaQuery.GetIndexForAlias(),
criteriaQuery);
Expand Down
8 changes: 5 additions & 3 deletions src/NHibernate/Criterion/IdentifierProjection.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using NHibernate.Persister.Entity;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;

namespace NHibernate.Criterion
{
[Serializable]
public class IdentifierProjection : SimpleProjection
public class IdentifierProjection : SimpleProjection, IPropertyProjection
{
private bool grouped;

Expand All @@ -21,7 +21,7 @@ protected internal IdentifierProjection() : this(false)

public override string ToString()
{
return "id";
return PropertyName;
}

public override IType[] GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery)
Expand Down Expand Up @@ -66,5 +66,7 @@ public override SqlString ToGroupSqlString(ICriteria criteria, ICriteriaQuery cr
}
return new SqlString(string.Join(",", criteriaQuery.GetIdentifierColumns(criteria)));
}

public string PropertyName => EntityPersister.EntityID;
}
}
58 changes: 34 additions & 24 deletions src/NHibernate/Criterion/Order.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Text;
using NHibernate.Criterion;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.SqlTypes;

namespace NHibernate.Criterion
{
Expand Down Expand Up @@ -38,43 +37,54 @@ public Order(string propertyName, bool ascending)
/// </summary>
public virtual SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
if (projection != null)
{
SqlString produced = projection.ToSqlString(criteria, 0, criteriaQuery);
SqlString truncated = SqlStringHelper.RemoveAsAliasesFromSql(produced);
return new SqlString(truncated, ascending ? " asc" : " desc");
}

string[] columns = criteriaQuery.GetColumnAliasesUsingProjection(criteria, propertyName);
Type.IType type = criteriaQuery.GetTypeUsingProjection(criteria, propertyName);
var columnsOrAliases = GetColumnsOrAliases(criteria, criteriaQuery);
var sqlTypes = ignoreCase ? SqlTypes(criteria, criteriaQuery) : null;

StringBuilder fragment = new StringBuilder();
ISessionFactoryImplementor factory = criteriaQuery.Factory;
for (int i = 0; i < columns.Length; i++)
var fragment = new SqlStringBuilder();
var factory = criteriaQuery.Factory;
for (var i = 0; i < columnsOrAliases.Length; i++)
{
bool lower = ignoreCase && IsStringType(type.SqlTypes(factory)[i]);

var lower = sqlTypes != null && IsStringType(sqlTypes[i]);
if (lower)
{
fragment.Append(factory.Dialect.LowercaseFunction)
.Append("(");
fragment
.Add(factory.Dialect.LowercaseFunction)
.Add("(");
}
fragment.Append(columns[i]);

fragment.AddObject(columnsOrAliases[i]);

if (lower)
{
fragment.Append(")");
fragment.Add(")");
}

fragment.Append(ascending ? " asc" : " desc");
fragment.Add(ascending ? " asc" : " desc");

if (i < columns.Length - 1)
if (i < columnsOrAliases.Length - 1)
{
fragment.Append(", ");
fragment.Add(", ");
}
}

return new SqlString(fragment.ToString());
return fragment.ToSqlString();
}

private object[] GetColumnsOrAliases(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var propName = propertyName ?? (projection as IPropertyProjection)?.PropertyName;
return propName != null
? criteriaQuery.GetColumnAliasesUsingProjection(criteria, propName)
: (object[]) CriterionUtil.GetColumnNamesUsingProjection(projection, criteriaQuery, criteria);
}

private SqlType[] SqlTypes(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var type = projection == null
? criteriaQuery.GetTypeUsingProjection(criteria, propertyName)
: projection.GetTypes(criteria, criteriaQuery)[0];

return type.SqlTypes(criteriaQuery.Factory);
}

public override string ToString()
Expand Down