diff options
author | Dmytro Shteflyuk <kpumuk@kpumuk.info> | 2018-11-15 14:49:55 -0500 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2018-11-15 14:49:55 -0500 |
commit | b5302d5a820b078b6488104dd695a679e5a49623 (patch) | |
tree | a8f393036fd1b72c33d9ed448f6ada379b2dd429 /activerecord/lib/arel/visitors/to_sql.rb | |
parent | d1c76dd4b0d308db432afb56c7dafcbefbf4ee3a (diff) | |
download | rails-b5302d5a820b078b6488104dd695a679e5a49623.tar.gz rails-b5302d5a820b078b6488104dd695a679e5a49623.tar.bz2 rails-b5302d5a820b078b6488104dd695a679e5a49623.zip |
Arel: Implemented DB-aware NULL-safe comparison (#34451)
* Arel: Implemented DB-aware NULL-safe comparison
* Fixed where clause inversion for NULL-safe comparison
* Renaming "null_safe_eq" to "is_not_distinct_from", "null_safe_not_eq" to "is_distinct_from"
[Dmytro Shteflyuk + Rafael Mendonça França]
Diffstat (limited to 'activerecord/lib/arel/visitors/to_sql.rb')
-rw-r--r-- | activerecord/lib/arel/visitors/to_sql.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/activerecord/lib/arel/visitors/to_sql.rb b/activerecord/lib/arel/visitors/to_sql.rb index 7efd74dbc9..f9fe4404eb 100644 --- a/activerecord/lib/arel/visitors/to_sql.rb +++ b/activerecord/lib/arel/visitors/to_sql.rb @@ -641,6 +641,26 @@ module Arel # :nodoc: all end end + def visit_Arel_Nodes_IsNotDistinctFrom(o, collector) + if o.right.nil? + collector = visit o.left, collector + collector << " IS NULL" + else + collector = is_distinct_from(o, collector) + collector << " = 0" + end + end + + def visit_Arel_Nodes_IsDistinctFrom(o, collector) + if o.right.nil? + collector = visit o.left, collector + collector << " IS NOT NULL" + else + collector = is_distinct_from(o, collector) + collector << " = 1" + end + end + def visit_Arel_Nodes_NotEqual(o, collector) right = o.right @@ -873,6 +893,19 @@ module Arel # :nodoc: all collector end end + + def is_distinct_from(o, collector) + collector << "CASE WHEN " + collector = visit o.left, collector + collector << " = " + collector = visit o.right, collector + collector << " OR (" + collector = visit o.left, collector + collector << " IS NULL AND " + collector = visit o.right, collector + collector << " IS NULL)" + collector << " THEN 0 ELSE 1 END" + end end end end |