41
41
*
42
42
* @author Greg Turnquist
43
43
* @author Christoph Strobl
44
+ * @author Mark Paluch
44
45
*/
45
46
class HqlQueryTransformerTests {
46
47
@@ -182,6 +183,12 @@ void multipleAliasesShouldBeGathered() {
182
183
183
184
@ Test
184
185
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" );
185
192
assertCountQuery (QUERY , COUNT_QUERY );
186
193
}
187
194
@@ -204,6 +211,9 @@ void createsCountQueryForConstructorQueries() {
204
211
205
212
assertCountQuery ("select distinct new com.example.User(u.name) from User u where u.foo = ?1" ,
206
213
"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" );
207
217
}
208
218
209
219
@ Test
@@ -913,7 +923,7 @@ void queryParserPicksCorrectAliasAmidstMultipleAlises() {
913
923
void countQueryShouldWorkEvenWithoutExplicitAlias () {
914
924
915
925
assertCountQuery ("FROM BookError WHERE portal = :portal" ,
916
- "select count(__) FROM BookError AS __ WHERE portal = :portal" );
926
+ "select count(__) FROM BookError WHERE portal = :portal" );
917
927
918
928
assertCountQuery ("FROM BookError b WHERE portal = :portal" ,
919
929
"select count(b) FROM BookError b WHERE portal = :portal" );
@@ -1107,15 +1117,15 @@ void aliasesShouldNotOverlapWithSortProperties() {
1107
1117
@ Test // GH-3269, GH-3689
1108
1118
void createsCountQueryUsingAliasCorrectly () {
1109
1119
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" );
1113
1123
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" );
1115
1125
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" );
1117
1127
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" );
1119
1129
assertCountQuery ("select distinct substring(e.firstname, 1, position('a' in e.lastname)) as x from from Employee" ,
1120
1130
"select count(distinct substring(e.firstname, 1, position('a' in e.lastname))) from from Employee" );
1121
1131
}
0 commit comments