[12.x] Fix/cross database null safe equals

Merged
PA
9mo ago
+262-33

I was the original author of whereNotMorphedTo() (#42264) and later updated it to use MySQL's <=> operator for nullable morph support (#42878). That was MySQL specific and has since caused issues on other databases (#52454, #57698).

This PR introduces a first-class NullSafeEquals where clause type that each grammar compiles using the correct syntax:

  • Base/PostgreSQL: IS NOT DISTINCT FROM (the SQL standard)
  • MySQL/MariaDB: <=> (the only one that differs)
  • SQLite: IS (SQLite's equivalent, left as-is for backward compatibility)
  • SQL Server: EXISTS (SELECT column INTERSECT SELECT ?)

[!IMPORTANT]
SQL Server added support for is distinct from in 2022+. Laravel still supports SQL Server 2017+, so this well-known single-binding trick is used instead

The <=> operator can still be passed to where() and will be automatically routed to the NullSafeEquals type, similar to how bitwise operators are handled. Existing code using where('col', '<=>', $value) will now work correctly across all databases.
Also adds whereNullSafeEquals() and orWhereNullSafeEquals() to the query builder, and removes the <=> interception that was added to SQLiteGrammar::whereBasic() in #57698.

Backward Compatible
Laravel 12 currently only supports whereNotMorphedTo on MySQL/Maria and Sqlite. All other databases error out at <=>. This change doesn't affect the compiled sql of MySQL/Maria and Sqlite, simply adds support for the other databases.