Skip to content

Commit

Permalink
Merge pull request #901 from circulon/fix/on_null_always_last_criteria
Browse files Browse the repository at this point in the history
fixed .on_null and .on_not_null had to be last criteria
  • Loading branch information
josephmancuso authored Oct 26, 2024
2 parents 8a66a07 + afb140b commit 1efddf6
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/masoniteorm/query/grammars/BaseGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,13 @@ def process_joins(self, qmark=False):
on_string += f"{keyword} {self._table_column_string(clause.column1)} {clause.equality} {self._table_column_string(clause.column2)} "
else:
if clause.value_type == "NULL":
sql_string = self.where_null_string()
sql_string = f"{self.where_null_string()} "
on_string += sql_string.format(
keyword=keyword,
column=self.process_column(clause.column),
)
elif clause.value_type == "NOT NULL":
sql_string = self.where_not_null_string()
sql_string = f"{self.where_not_null_string()} "
on_string += sql_string.format(
keyword=keyword,
column=self.process_column(clause.column),
Expand Down
15 changes: 15 additions & 0 deletions src/masoniteorm/testing/BaseTestCaseSelectGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,22 @@ def test_can_compile_join_clause_with_null(self):
clause = (
JoinClause("report_groups as rg")
.on_null("bgt.acct")
.or_on_null("bgt.dept")
.on_value("rg.abc", 10)
)
to_sql = self.builder.join(clause).to_sql()

sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
self.assertEqual(to_sql, sql)

def test_can_compile_join_clause_with_not_null(self):
clause = (
JoinClause("report_groups as rg")
.on_not_null("bgt.acct")
.or_on_not_null("bgt.dept")
.on_value("rg.abc", 10)
)
to_sql = self.builder.join(clause).to_sql()

Expand Down
16 changes: 15 additions & 1 deletion tests/mssql/grammar/test_mssql_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,25 @@ def can_compile_join_clause_with_null(self):
clause = (
JoinClause("report_groups as rg")
.on_null("bgt.acct")
.or_on_null("bgt.dept")
.on_value("rg.abc", 10)
)
builder.join(clause).to_sql()
"""
return "SELECT * FROM [users] INNER JOIN [report_groups] AS [rg] ON [acct] IS NULL OR [dept] IS NULL AND [rg].[abc] = '10'"

def can_compile_join_clause_with_not_null(self):
"""
builder = self.get_builder()
clause = (
JoinClause("report_groups as rg")
.on_not_null("bgt.acct")
.or_on_not_null("bgt.dept")
.on_value("rg.abc", 10)
)
builder.join(clause).to_sql()
"""
return "SELECT * FROM [users] INNER JOIN [report_groups] AS [rg] ON [acct] IS NULL OR [dept] IS NOT NULL"
return "SELECT * FROM [users] INNER JOIN [report_groups] AS [rg] ON [acct] IS NOT NULL OR [dept] IS NOT NULL AND [rg].[abc] = '10'"

def can_compile_join_clause_with_lambda(self):
"""
Expand Down
16 changes: 15 additions & 1 deletion tests/mysql/grammar/test_mysql_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,25 @@ def can_compile_join_clause_with_null(self):
clause = (
JoinClause("report_groups as rg")
.on_null("bgt.acct")
.or_on_null("bgt.dept")
.on_value("rg.abc", 10)
)
builder.join(clause).to_sql()
"""
return "SELECT * FROM `users` INNER JOIN `report_groups` AS `rg` ON `acct` IS NULL OR `dept` IS NULL AND `rg`.`abc` = '10'"

def can_compile_join_clause_with_not_null(self):
"""
builder = self.get_builder()
clause = (
JoinClause("report_groups as rg")
.on_not_null("bgt.acct")
.or_on_not_null("bgt.dept")
.on_value("rg.abc", 10)
)
builder.join(clause).to_sql()
"""
return "SELECT * FROM `users` INNER JOIN `report_groups` AS `rg` ON `acct` IS NULL OR `dept` IS NOT NULL"
return "SELECT * FROM `users` INNER JOIN `report_groups` AS `rg` ON `acct` IS NOT NULL OR `dept` IS NOT NULL AND `rg`.`abc` = '10'"

def can_compile_join_clause_with_lambda(self):
"""
Expand Down
16 changes: 15 additions & 1 deletion tests/postgres/grammar/test_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,25 @@ def can_compile_join_clause_with_null(self):
clause = (
JoinClause("report_groups as rg")
.on_null("bgt.acct")
.or_on_null("bgt.dept")
.on_value("rg.abc", 10)
)
builder.join(clause).to_sql()
"""
return """SELECT * FROM "users" INNER JOIN "report_groups" AS "rg" ON "acct" IS NULL OR "dept" IS NULL AND "rg"."abc" = '10'"""

def can_compile_join_clause_with_not_null(self):
"""
builder = self.get_builder()
clause = (
JoinClause("report_groups as rg")
.on_not_null("bgt.acct")
.or_on_not_null("bgt.dept")
.on_value("rg.abc", 10)
)
builder.join(clause).to_sql()
"""
return """SELECT * FROM "users" INNER JOIN "report_groups" AS "rg" ON "acct" IS NULL OR "dept" IS NOT NULL"""
return """SELECT * FROM "users" INNER JOIN "report_groups" AS "rg" ON "acct" IS NOT NULL OR "dept" IS NOT NULL AND "rg"."abc" = '10'"""

def can_compile_join_clause_with_lambda(self):
"""
Expand Down
16 changes: 15 additions & 1 deletion tests/sqlite/grammar/test_sqlite_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,25 @@ def can_compile_join_clause_with_null(self):
clause = (
JoinClause("report_groups as rg")
.on_null("bgt.acct")
.or_on_null("bgt.dept")
.on_value("rg.abc", 10)
)
builder.join(clause).to_sql()
"""
return """SELECT * FROM "users" INNER JOIN "report_groups" AS "rg" ON "acct" IS NULL OR "dept" IS NULL AND "rg"."abc" = '10'"""

def can_compile_join_clause_with_not_null(self):
"""
builder = self.get_builder()
clause = (
JoinClause("report_groups as rg")
.on_not_null("bgt.acct")
.or_on_not_null("bgt.dept")
.on_value("rg.abc", 10)
)
builder.join(clause).to_sql()
"""
return """SELECT * FROM "users" INNER JOIN "report_groups" AS "rg" ON "acct" IS NULL OR "dept" IS NOT NULL"""
return """SELECT * FROM "users" INNER JOIN "report_groups" AS "rg" ON "acct" IS NOT NULL OR "dept" IS NOT NULL AND "rg"."abc" = '10'"""

def can_compile_join_clause_with_lambda(self):
"""
Expand Down

0 comments on commit 1efddf6

Please sign in to comment.