Skip to content

NH-3609 - Fix using Projections.Conditional inside of Projections.Count #261

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

Closed
wants to merge 4 commits into from
Closed
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
10 changes: 10 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3609/Entity.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
82 changes: 82 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3609/Fixture.cs
Original file line number Diff line number Diff line change
@@ -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<Entity>().SelectList(
builder =>
builder.Select(Projections.Count(Projections.Conditional(
Restrictions.Eq(Projections.Property<Entity>(x => x.Name), "FOO"),
Projections.Constant("", NHibernateUtil.String),
Projections.Constant(null, NHibernateUtil.String))).WithAlias(() => mappingEntity.Count))
)
.TransformUsing(Transformers.AliasToBean<MappingEntity>()).List<MappingEntity>()
Copy link
Member

Choose a reason for hiding this comment

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

Can you check something here?

);
}
}

[Test]
public void GroupByClauseHasParameterSet()
{
using (ISession session = OpenSession())
using (session.BeginTransaction())
{
MappingEntity mappingEntity = null;
Assert.DoesNotThrow(() =>
session.QueryOver<Entity>().SelectList(
builder =>
builder.Select(Projections.GroupProperty(
Projections.Conditional(
Restrictions.Eq(Projections.Property<Entity>(x => x.Name), ""),
Projections.Constant(1), Projections.Constant(2)))
.WithAlias(() => mappingEntity.Count))
)
.TransformUsing(Transformers.AliasToBean<MappingEntity>()).List<MappingEntity>()
Copy link
Member

Choose a reason for hiding this comment

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

Can you check something here?

);
}

}
}
}
9 changes: 9 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3609/MappingEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace NHibernate.Test.NHSpecificTest.NH3609
{
class MappingEntity
{
public virtual int Count { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3609/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH3609">

<class name="Entity">
<id name="Id" generator="guid.comb" />
<property name="Name" />
</class>

</hibernate-mapping>
4 changes: 4 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,9 @@
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Domain.cs" />
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Fixture.cs" />
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
<Compile Include="NHSpecificTest\NH3609\MappingEntity.cs" />
<Compile Include="NHSpecificTest\NH3609\Entity.cs" />
<Compile Include="NHSpecificTest\NH3609\Fixture.cs" />
<Compile Include="NHSpecificTest\NH1082\SynchronizationThatThrowsExceptionAtBeforeTransactionCompletion.cs" />
<Compile Include="NHSpecificTest\NH2756\Fixture.cs" />
<Compile Include="NHSpecificTest\NH2756\Model.cs" />
Expand Down Expand Up @@ -3014,6 +3017,7 @@
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="NHSpecificTest\NH3609\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3590\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3377\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2865\Mappings.hbm.xml" />
Expand Down
9 changes: 4 additions & 5 deletions src/NHibernate/Criterion/CountProjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
6 changes: 3 additions & 3 deletions src/NHibernate/Loader/AbstractEntityJoinWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/NHibernate/SqlCommand/SqlSelectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,7 +83,7 @@ public SqlSelectBuilder SetOrderByClause(SqlString orderByClause)
/// </summary>
/// <param name="groupByClause">The groupByClause to set</param>
/// <returns>The SqlSelectBuilder</returns>
public SqlSelectBuilder SetGroupByClause(string groupByClause)
public SqlSelectBuilder SetGroupByClause(SqlString groupByClause)
{
this.groupByClause = groupByClause;
return this;
Expand Down Expand Up @@ -247,7 +247,7 @@ public SqlString ToSqlString()
}
}

if (StringHelper.IsNotEmpty(groupByClause))
if (SqlStringHelper.IsNotEmpty(groupByClause))
{
sqlBuilder.Add(" GROUP BY ")
.Add(groupByClause);
Expand Down