Skip to content

Commit a117942

Browse files
Merge pull request #774 from felipehertzer/2.0
Fixed wrong select structure when using having and order by
2 parents 3cbb4f3 + a879d89 commit a117942

File tree

8 files changed

+41
-8
lines changed

8 files changed

+41
-8
lines changed

src/masoniteorm/query/grammars/MSSQLGrammar.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class MSSQLGrammar(BaseGrammar):
1010
"MIN": "MIN",
1111
"AVG": "AVG",
1212
"COUNT": "COUNT",
13-
"AVG": "AVG",
1413
}
1514

1615
join_keywords = {
@@ -37,7 +36,7 @@ def select_no_table(self):
3736
return "SELECT {columns}"
3837

3938
def select_format(self):
40-
return "SELECT {keyword} {limit} {columns} FROM {table} {lock} {joins} {wheres} {group_by} {order_by} {offset} {having}"
39+
return "SELECT {keyword} {limit} {columns} FROM {table} {lock} {joins} {wheres} {group_by} {having} {order_by} {offset}"
4140

4241
def update_format(self):
4342
return "UPDATE {table} SET {key_equals} {wheres}"

src/masoniteorm/query/grammars/MySQLGrammar.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class MySQLGrammar(BaseGrammar):
1010
"MIN": "MIN",
1111
"AVG": "AVG",
1212
"COUNT": "COUNT",
13-
"AVG": "AVG",
1413
}
1514

1615
join_keywords = {
@@ -50,7 +49,7 @@ class MySQLGrammar(BaseGrammar):
5049
locks = {"share": "LOCK IN SHARE MODE", "update": "FOR UPDATE"}
5150

5251
def select_format(self):
53-
return "SELECT {keyword} {columns} FROM {table} {joins} {wheres} {group_by} {order_by} {limit} {offset} {having} {lock}"
52+
return "SELECT {keyword} {columns} FROM {table} {joins} {wheres} {group_by} {having} {order_by} {limit} {offset} {lock}"
5453

5554
def select_no_table(self):
5655
return "SELECT {columns} {lock}"

src/masoniteorm/query/grammars/PostgresGrammar.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class PostgresGrammar(BaseGrammar):
1111
"MIN": "MIN",
1212
"AVG": "AVG",
1313
"COUNT": "COUNT",
14-
"AVG": "AVG",
1514
}
1615

1716
join_keywords = {
@@ -38,7 +37,7 @@ def select_no_table(self):
3837
return "SELECT {columns} {lock}"
3938

4039
def select_format(self):
41-
return "SELECT {keyword} {columns} FROM {table} {joins} {wheres} {group_by} {order_by} {limit} {offset} {having} {lock}"
40+
return "SELECT {keyword} {columns} FROM {table} {joins} {wheres} {group_by} {having} {order_by} {limit} {offset} {lock}"
4241

4342
def update_format(self):
4443
return "UPDATE {table} SET {key_equals} {wheres}"

src/masoniteorm/query/grammars/SQLiteGrammar.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class SQLiteGrammar(BaseGrammar):
1111
"MIN": "MIN",
1212
"AVG": "AVG",
1313
"COUNT": "COUNT",
14-
"AVG": "AVG",
1514
}
1615

1716
join_keywords = {
@@ -35,7 +34,7 @@ class SQLiteGrammar(BaseGrammar):
3534
locks = {"share": "", "update": ""}
3635

3736
def select_format(self):
38-
return "SELECT {keyword} {columns} FROM {table} {joins} {wheres} {group_by} {order_by} {limit} {offset} {having} {lock}"
37+
return "SELECT {keyword} {columns} FROM {table} {joins} {wheres} {group_by} {having} {order_by} {limit} {offset} {lock}"
3938

4039
def select_no_table(self):
4140
return "SELECT {columns} {lock}"

tests/mssql/grammar/test_mssql_select_grammar.py

+11
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ def can_compile_having(self):
246246
"""
247247
return "SELECT SUM([users].[age]) AS age FROM [users] GROUP BY [users].[age] HAVING [users].[age]"
248248

249+
def can_compile_having_order(self):
250+
"""
251+
builder.sum('age').group_by('age').having('age').order_by('age', 'desc').to_sql()
252+
"""
253+
return "SELECT SUM([users].[age]) AS age FROM [users] GROUP BY [users].[age] HAVING [users].[age] ORDER [users].[age] DESC"
254+
249255
def can_compile_between(self):
250256
"""
251257
builder.between('age', 18, 21).to_sql()
@@ -308,6 +314,11 @@ def test_can_compile_having_raw(self):
308314
to_sql, "SELECT COUNT(*) as counts FROM [users] HAVING counts > 10"
309315
)
310316

317+
def test_can_compile_having_raw_order(self):
318+
to_sql = self.builder.select_raw("COUNT(*) as counts").having_raw("counts > 10").order_by_raw(
319+
'counts DESC').to_sql()
320+
self.assertEqual(to_sql, "SELECT COUNT(*) as counts FROM [users] HAVING counts > 10 ORDER BY counts DESC")
321+
311322
def test_can_compile_select_raw(self):
312323
to_sql = self.builder.select_raw("COUNT(*)").to_sql()
313324
sql = getattr(

tests/mysql/grammar/test_mysql_select_grammar.py

+10
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ def can_compile_having(self):
248248
"""
249249
return "SELECT SUM(`users`.`age`) AS age FROM `users` GROUP BY `users`.`age` HAVING `users`.`age`"
250250

251+
def can_compile_having_order(self):
252+
"""
253+
builder.sum('age').group_by('age').having('age').order_by('age', 'desc').to_sql()
254+
"""
255+
return "SELECT SUM(`users`.`age`) AS age FROM `users` GROUP BY `users`.`age` HAVING `users`.`age` ORDER `users`.`age` DESC"
256+
251257
def can_compile_having_with_expression(self):
252258
"""
253259
builder.sum('age').group_by('age').having('age', 10).to_sql()
@@ -304,6 +310,10 @@ def test_can_compile_having_raw(self):
304310
to_sql, "SELECT COUNT(*) as counts FROM `users` HAVING counts > 10"
305311
)
306312

313+
def test_can_compile_having_raw_order(self):
314+
to_sql = self.builder.select_raw("COUNT(*) as counts").having_raw("counts > 10").order_by_raw('counts DESC').to_sql()
315+
self.assertEqual(to_sql, "SELECT COUNT(*) as counts FROM `users` HAVING counts > 10 ORDER BY counts DESC")
316+
307317
def test_can_compile_select_raw(self):
308318
to_sql = self.builder.select_raw("COUNT(*)").to_sql()
309319
self.assertEqual(to_sql, "SELECT COUNT(*) FROM `users`")

tests/postgres/grammar/test_select_grammar.py

+10
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ def can_compile_having(self):
247247
"""
248248
return """SELECT SUM("users"."age") AS age FROM "users" GROUP BY "users"."age" HAVING "users"."age\""""
249249

250+
def can_compile_having_order(self):
251+
"""
252+
builder.sum('age').group_by('age').having('age').order_by('age', 'desc').to_sql()
253+
"""
254+
return """SELECT SUM("users"."age") AS age FROM "users" GROUP BY "users"."age" HAVING "users"."age" ORDER "users"."age" DESC"""
255+
250256
def can_compile_having_with_expression(self):
251257
"""
252258
builder.sum('age').group_by('age').having('age', 10).to_sql()
@@ -309,6 +315,10 @@ def test_can_compile_having_raw(self):
309315
to_sql, """SELECT COUNT(*) as counts FROM "users" HAVING counts > 10"""
310316
)
311317

318+
def test_can_compile_having_raw_order(self):
319+
to_sql = self.builder.select_raw("COUNT(*) as counts").having_raw("counts > 10").order_by_raw('counts DESC').to_sql()
320+
self.assertEqual(to_sql, """SELECT COUNT(*) as counts FROM "users" HAVING counts > 10 ORDER BY counts DESC""")
321+
312322
def test_can_compile_where_raw_and_where_with_multiple_bindings(self):
313323
query = self.builder.where_raw(
314324
""" "age" = '?' AND "is_admin" = '?'""", [18, True]

tests/sqlite/grammar/test_sqlite_select_grammar.py

+6
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ def can_compile_having(self):
239239
"""
240240
return """SELECT SUM("users"."age") AS age FROM "users" GROUP BY "users"."age" HAVING "users"."age\""""
241241

242+
def can_compile_having_order(self):
243+
"""
244+
builder.sum('age').group_by('age').having('age').order_by('age', 'desc').to_sql()
245+
"""
246+
return """SELECT SUM("users"."age") AS age FROM "users" GROUP BY "users"."age" HAVING "users"."age\" ORDER "users"."age" DESC"""
247+
242248
def can_compile_having_raw(self):
243249
"""
244250
builder.select_raw("COUNT(*) as counts").having_raw("counts > 18").to_sql()

0 commit comments

Comments
 (0)