empty_array_consider_nulls.rb

lib/sequel/extensions/empty_array_consider_nulls.rb
Last Update: 2017-08-01 08:12:00 -0700

This changes Sequel’s literalization of IN/NOT IN with an empty array value to consider NULL values if one of the referenced columns is NULL:

DB[:test].where(name: [])
# SELECT * FROM test WHERE (name != name)
DB[:test].exclude(name: [])
# SELECT * FROM test WHERE (name = name)

The default Sequel behavior is to ignore NULLs, as the above query is not generally optimized well by databases.

You can load this extension into specific datasets:

ds = DB[:table]
ds = ds.extension(:empty_array_consider_nulls)

Or you can load it into all of a database’s datasets, which is probably the desired behavior if you are using this extension:

DB.extension(:empty_array_consider_nulls)

Related module: Sequel::EmptyArrayConsiderNulls