From e033bfdf3db007d2b4cd24cc804445729be0cd73 Mon Sep 17 00:00:00 2001 From: Kieren Eaton <499977+circulon@users.noreply.github.com> Date: Thu, 24 Oct 2024 13:54:43 +0800 Subject: [PATCH 1/2] fixed .on_null and .on_not_null had to be last criteria --- src/masoniteorm/query/grammars/BaseGrammar.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/masoniteorm/query/grammars/BaseGrammar.py b/src/masoniteorm/query/grammars/BaseGrammar.py index 5a053576..f08cf660 100644 --- a/src/masoniteorm/query/grammars/BaseGrammar.py +++ b/src/masoniteorm/query/grammars/BaseGrammar.py @@ -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), From afb140bf877d921c8ee9a775f717053d74fecf9b Mon Sep 17 00:00:00 2001 From: Kieren Eaton <499977+circulon@users.noreply.github.com> Date: Sat, 26 Oct 2024 07:58:50 +0800 Subject: [PATCH 2/2] updated tests fo .on_null and .on_not_null --- .../testing/BaseTestCaseSelectGrammar.py | 15 +++++++++++++++ tests/mssql/grammar/test_mssql_select_grammar.py | 16 +++++++++++++++- tests/mysql/grammar/test_mysql_select_grammar.py | 16 +++++++++++++++- tests/postgres/grammar/test_select_grammar.py | 16 +++++++++++++++- .../sqlite/grammar/test_sqlite_select_grammar.py | 16 +++++++++++++++- 5 files changed, 75 insertions(+), 4 deletions(-) diff --git a/src/masoniteorm/testing/BaseTestCaseSelectGrammar.py b/src/masoniteorm/testing/BaseTestCaseSelectGrammar.py index 0182e3c0..f1aaa119 100644 --- a/src/masoniteorm/testing/BaseTestCaseSelectGrammar.py +++ b/src/masoniteorm/testing/BaseTestCaseSelectGrammar.py @@ -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() diff --git a/tests/mssql/grammar/test_mssql_select_grammar.py b/tests/mssql/grammar/test_mssql_select_grammar.py index 28615cdf..383a0f43 100644 --- a/tests/mssql/grammar/test_mssql_select_grammar.py +++ b/tests/mssql/grammar/test_mssql_select_grammar.py @@ -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): """ diff --git a/tests/mysql/grammar/test_mysql_select_grammar.py b/tests/mysql/grammar/test_mysql_select_grammar.py index 169b6a89..8d2d80e2 100644 --- a/tests/mysql/grammar/test_mysql_select_grammar.py +++ b/tests/mysql/grammar/test_mysql_select_grammar.py @@ -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): """ diff --git a/tests/postgres/grammar/test_select_grammar.py b/tests/postgres/grammar/test_select_grammar.py index 4754114e..d4a7915e 100644 --- a/tests/postgres/grammar/test_select_grammar.py +++ b/tests/postgres/grammar/test_select_grammar.py @@ -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): """ diff --git a/tests/sqlite/grammar/test_sqlite_select_grammar.py b/tests/sqlite/grammar/test_sqlite_select_grammar.py index 015f3967..cc0bc6e5 100644 --- a/tests/sqlite/grammar/test_sqlite_select_grammar.py +++ b/tests/sqlite/grammar/test_sqlite_select_grammar.py @@ -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): """