Skip to content

Commit e258abe

Browse files
authored
Fix Count Distinct on path with hql reserved words (nhibernate#3217)
Fixes nhibernate#3215
1 parent 58993cf commit e258abe

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using NHibernate.Cfg;
12+
using NHibernate.Cfg.MappingSchema;
13+
using NHibernate.Mapping.ByCode;
14+
using NUnit.Framework;
15+
16+
namespace NHibernate.Test.NHSpecificTest.GH3215
17+
{
18+
using System.Threading.Tasks;
19+
[TestFixture]
20+
public class ByCodeFixtureAsync : TestCaseMappingByCode
21+
{
22+
protected override HbmMapping GetMappings()
23+
{
24+
var mapper = new ModelMapper();
25+
26+
mapper.Class<Member>(rc =>
27+
{
28+
rc.Id(x => x.Code);
29+
rc.Property(x => x.Name);
30+
rc.Property(x => x.Date);
31+
});
32+
33+
mapper.Class<Request>(rc =>
34+
{
35+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
36+
rc.ManyToOne(x => x.Member);
37+
});
38+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
39+
}
40+
41+
protected override bool AppliesTo(Dialect.Dialect dialect)
42+
{
43+
return TestDialect.SupportsCountDistinct;
44+
}
45+
46+
protected override void Configure(Configuration configuration)
47+
{
48+
base.Configure(configuration);
49+
configuration.SetProperty(Environment.Hbm2ddlKeyWords, "auto-quote");
50+
}
51+
52+
[Test]
53+
public async Task CountDistinctWithReservedWordsAsync()
54+
{
55+
using (var session = OpenSession())
56+
{
57+
var hql = "select Count(DISTINCT r.Member.id), Count(DISTINCT Date(r.Member.Date)) from Request r";
58+
var result = await (session.CreateQuery(hql).ListAsync());
59+
60+
Assert.That(result, Has.Count.EqualTo(1));
61+
}
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH3215
4+
{
5+
class Member
6+
{
7+
public virtual int Code { get; set; }
8+
public virtual string Name { get; set; }
9+
public virtual DateTime Date { get; set; }
10+
}
11+
12+
class Request
13+
{
14+
public virtual Guid Id { get; set; }
15+
public virtual Member Member { get; set; }
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using NHibernate.Cfg;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Mapping.ByCode;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.NHSpecificTest.GH3215
7+
{
8+
[TestFixture]
9+
public class ByCodeFixture : TestCaseMappingByCode
10+
{
11+
protected override HbmMapping GetMappings()
12+
{
13+
var mapper = new ModelMapper();
14+
15+
mapper.Class<Member>(rc =>
16+
{
17+
rc.Id(x => x.Code);
18+
rc.Property(x => x.Name);
19+
rc.Property(x => x.Date);
20+
});
21+
22+
mapper.Class<Request>(rc =>
23+
{
24+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
25+
rc.ManyToOne(x => x.Member);
26+
});
27+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
28+
}
29+
30+
protected override bool AppliesTo(Dialect.Dialect dialect)
31+
{
32+
return TestDialect.SupportsCountDistinct;
33+
}
34+
35+
protected override void Configure(Configuration configuration)
36+
{
37+
base.Configure(configuration);
38+
configuration.SetProperty(Environment.Hbm2ddlKeyWords, "auto-quote");
39+
}
40+
41+
[Test]
42+
public void CountDistinctWithReservedWords()
43+
{
44+
using (var session = OpenSession())
45+
{
46+
var hql = "select Count(DISTINCT r.Member.id), Count(DISTINCT Date(r.Member.Date)) from Request r";
47+
var result = session.CreateQuery(hql).List();
48+
49+
Assert.That(result, Has.Count.EqualTo(1));
50+
}
51+
}
52+
}
53+
}

src/NHibernate/Hql/Ast/ANTLR/Hql.g

+1-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ vectorExpr
600600
// NOTE: handleDotIdent() is called immediately after the first IDENT is recognized because
601601
// the method looks a head to find keywords after DOT and turns them into identifiers.
602602
identPrimary
603-
: identifier { HandleDotIdent(); }
603+
: identifier {{ HandleDotIdent(); }}
604604
( options {greedy=true;} : DOT^ ( identifier | o=OBJECT { $o.Type = IDENT; } ) )*
605605
( ( op=OPEN^ { $op.Type = METHOD_CALL;} exprList CLOSE! )
606606
)?

0 commit comments

Comments
 (0)