4141 *
4242 * @author Greg Turnquist
4343 * @author Christoph Strobl
44+ * @author Mark Paluch
4445 */
4546class HqlQueryTransformerTests {
4647
@@ -182,6 +183,12 @@ void multipleAliasesShouldBeGathered() {
182183
183184 @ Test
184185 void createsCountQueryCorrectly () {
186+
187+ assertCountQuery ("SELECT id FROM Person" , "SELECT count(id) FROM Person" );
188+ assertCountQuery ("SELECT p.id FROM Person p" , "SELECT count(p) FROM Person p" );
189+ assertCountQuery ("SELECT id FROM Person p" , "SELECT count(id) FROM Person p" );
190+ assertCountQuery ("SELECT id, name FROM Person" , "SELECT count(*) FROM Person" );
191+ assertCountQuery ("SELECT id, name FROM Person p" , "SELECT count(p) FROM Person p" );
185192 assertCountQuery (QUERY , COUNT_QUERY );
186193 }
187194
@@ -204,6 +211,9 @@ void createsCountQueryForConstructorQueries() {
204211
205212 assertCountQuery ("select distinct new com.example.User(u.name) from User u where u.foo = ?1" ,
206213 "select count(distinct u) from User u where u.foo = ?1" );
214+
215+ assertCountQuery ("select distinct new com.example.User(name, lastname) from User where foo = ?1" ,
216+ "select count(distinct name, lastname) from User where foo = ?1" );
207217 }
208218
209219 @ Test
@@ -913,7 +923,7 @@ void queryParserPicksCorrectAliasAmidstMultipleAlises() {
913923 void countQueryShouldWorkEvenWithoutExplicitAlias () {
914924
915925 assertCountQuery ("FROM BookError WHERE portal = :portal" ,
916- "select count(__) FROM BookError AS __ WHERE portal = :portal" );
926+ "select count(__) FROM BookError WHERE portal = :portal" );
917927
918928 assertCountQuery ("FROM BookError b WHERE portal = :portal" ,
919929 "select count(b) FROM BookError b WHERE portal = :portal" );
@@ -1107,15 +1117,15 @@ void aliasesShouldNotOverlapWithSortProperties() {
11071117 @ Test // GH-3269, GH-3689
11081118 void createsCountQueryUsingAliasCorrectly () {
11091119
1110- assertCountQuery ("select distinct 1 as x from Employee" , "select count(distinct 1) from Employee AS __ " );
1111- assertCountQuery ("SELECT DISTINCT abc AS x FROM T" , "SELECT count(DISTINCT abc) FROM T AS __ " );
1112- assertCountQuery ("select distinct a as x, b as y from Employee" , "select count(distinct a, b) from Employee AS __ " );
1120+ assertCountQuery ("select distinct 1 as x from Employee" , "select count(distinct 1) from Employee" );
1121+ assertCountQuery ("SELECT DISTINCT abc AS x FROM T" , "SELECT count(DISTINCT abc) FROM T" );
1122+ assertCountQuery ("select distinct a as x, b as y from Employee" , "select count(distinct a, b) from Employee" );
11131123 assertCountQuery ("select distinct sum(amount) as x from Employee GROUP BY n" ,
1114- "select count(distinct sum(amount)) from Employee AS __ GROUP BY n" );
1124+ "select count(distinct sum(amount)) from Employee GROUP BY n" );
11151125 assertCountQuery ("select distinct a, b, sum(amount) as c, d from Employee GROUP BY n" ,
1116- "select count(distinct a, b, sum(amount), d) from Employee AS __ GROUP BY n" );
1126+ "select count(distinct a, b, sum(amount), d) from Employee GROUP BY n" );
11171127 assertCountQuery ("select distinct a, count(b) as c from Employee GROUP BY n" ,
1118- "select count(distinct a, count(b)) from Employee AS __ GROUP BY n" );
1128+ "select count(distinct a, count(b)) from Employee GROUP BY n" );
11191129 assertCountQuery ("select distinct substring(e.firstname, 1, position('a' in e.lastname)) as x from from Employee" ,
11201130 "select count(distinct substring(e.firstname, 1, position('a' in e.lastname))) from from Employee" );
11211131 }
0 commit comments