Skip to content

Commit

Permalink
Merge pull request #673 from MasoniteFramework/feature/fix-where-exists
Browse files Browse the repository at this point in the history
fixed missing query builder lambda expression for where_exists and where_not_exists methods
  • Loading branch information
josephmancuso authored May 16, 2022
2 parents 123e42b + fae270b commit eb847dc
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,15 @@ def where_exists(self, value: "str|int|QueryBuilder"):
Returns:
self
"""
if isinstance(value, QueryBuilder):
if inspect.isfunction(value):
self._wheres += (
(
QueryExpression(
None, "EXISTS", SubSelectExpression(value(self.new()))
)
),
)
elif isinstance(value, QueryBuilder):
self._wheres += (
(QueryExpression(None, "EXISTS", SubSelectExpression(value))),
)
Expand All @@ -712,7 +720,16 @@ def where_not_exists(self, value: "str|int|QueryBuilder"):
Returns:
self
"""
if isinstance(value, QueryBuilder):

if inspect.isfunction(value):
self._wheres += (
(
QueryExpression(
None, "NOT EXISTS", SubSelectExpression(value(self.new()))
)
),
)
elif isinstance(value, QueryBuilder):
self._wheres += (
(QueryExpression(None, "NOT EXISTS", SubSelectExpression(value))),
)
Expand Down
16 changes: 16 additions & 0 deletions src/masoniteorm/testing/BaseTestCaseSelectGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,22 @@ def test_where_regexp(self):
)()
self.assertEqual(to_sql, sql)

def test_where_exists_with_lambda(self):
to_sql = self.builder.where_exists(lambda q: q.where("age", 1)).to_sql()
sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
print(to_sql)
self.assertEqual(to_sql, sql)

def test_where_not_exists_with_lambda(self):
to_sql = self.builder.where_not_exists(lambda q: q.where("age", 1)).to_sql()
sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
print(to_sql)
self.assertEqual(to_sql, sql)

def test_where_not_regexp(self):
to_sql = self.builder.where("age", "not regexp", "Joe").to_sql()
sql = getattr(
Expand Down
6 changes: 6 additions & 0 deletions tests/mssql/grammar/test_mssql_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,9 @@ def can_user_where_raw_and_where(self):
builder.where_raw("`age` = '18'").where("name", "=", "James").to_sql()
"""
return "SELECT * FROM [users] WHERE age = '18' AND [users].[name] = 'James'"

def where_exists_with_lambda(self):
return """SELECT * FROM [users] WHERE EXISTS (SELECT * FROM [users] WHERE [users].[age] = '1')"""

def where_not_exists_with_lambda(self):
return """SELECT * FROM [users] WHERE NOT EXISTS (SELECT * FROM [users] WHERE [users].[age] = '1')"""
6 changes: 6 additions & 0 deletions tests/mysql/grammar/test_mysql_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,9 @@ def can_user_where_raw_and_where(self):
builder.where_raw("`age` = '18'").where("name", "=", "James").to_sql()
"""
return "SELECT * FROM `users` WHERE age = '18' AND `users`.`name` = 'James'"

def where_exists_with_lambda(self):
return """SELECT * FROM `users` WHERE EXISTS (SELECT * FROM `users` WHERE `users`.`age` = '1')"""

def where_not_exists_with_lambda(self):
return """SELECT * FROM `users` WHERE NOT EXISTS (SELECT * FROM `users` WHERE `users`.`age` = '1')"""
6 changes: 6 additions & 0 deletions tests/postgres/grammar/test_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,9 @@ def can_user_where_raw_and_where(self):
builder.where_raw("`age` = '18'").where("name", "=", "James").to_sql()
"""
return """SELECT * FROM "users" WHERE age = '18' AND "users"."name" = 'James'"""

def where_exists_with_lambda(self):
return """SELECT * FROM "users" WHERE EXISTS (SELECT * FROM "users" WHERE "users"."age" = '1')"""

def where_not_exists_with_lambda(self):
return """SELECT * FROM "users" WHERE NOT EXISTS (SELECT * FROM "users" WHERE "users"."age" = '1')"""
7 changes: 7 additions & 0 deletions tests/sqlite/grammar/test_sqlite_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class TestSQLiteGrammar(BaseTestCaseSelectGrammar, unittest.TestCase):

grammar = SQLiteGrammar
maxDiff = None

def can_compile_select(self):
"""
Expand Down Expand Up @@ -427,3 +428,9 @@ def can_user_where_raw_and_where(self):
builder.where_raw("age = '18'").where("name", "=", "James").to_sql()
"""
return """SELECT * FROM "users" WHERE age = '18' AND "users"."name" = 'James'"""

def where_exists_with_lambda(self):
return """SELECT * FROM "users" WHERE EXISTS (SELECT * FROM "users" WHERE "users"."age" = '1')"""

def where_not_exists_with_lambda(self):
return """SELECT * FROM "users" WHERE NOT EXISTS (SELECT * FROM "users" WHERE "users"."age" = '1')"""

0 comments on commit eb847dc

Please sign in to comment.