Sha256: 3246e8274aa6cb2ee4c1054d581bf95a5a124091c7069058d789e79ced35acc9

Contents?: true

Size: 1.15 KB

Versions: 127

Compression:

Stored size: 1.15 KB

Contents

# frozen-string-literal: true
#
# 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

#
module Sequel
  module EmptyArrayConsiderNulls
    # Use an expression that returns NULL if the column value is NULL.
    def empty_array_value(op, cols)
      c = Array(cols)
      SQL::BooleanExpression.from_value_pairs(c.zip(c), :AND, op == :IN)
    end
    
  end

  Dataset.register_extension(:empty_array_consider_nulls, EmptyArrayConsiderNulls)
end

Version data entries

127 entries across 110 versions & 2 rubygems

Version Path
sequel-5.52.0 lib/sequel/extensions/empty_array_consider_nulls.rb
sequel-5.51.0 lib/sequel/extensions/empty_array_consider_nulls.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/sequel-5.44.0/lib/sequel/extensions/empty_array_consider_nulls.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/sequel-5.50.0/lib/sequel/extensions/empty_array_consider_nulls.rb
sequel-5.50.0 lib/sequel/extensions/empty_array_consider_nulls.rb
sequel-5.49.0 lib/sequel/extensions/empty_array_consider_nulls.rb
sequel-5.48.0 lib/sequel/extensions/empty_array_consider_nulls.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/sequel-5.47.0/lib/sequel/extensions/empty_array_consider_nulls.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/sequel-5.44.0/lib/sequel/extensions/empty_array_consider_nulls.rb
sequel-5.47.0 lib/sequel/extensions/empty_array_consider_nulls.rb
sequel-5.46.0 lib/sequel/extensions/empty_array_consider_nulls.rb
sequel-5.45.0 lib/sequel/extensions/empty_array_consider_nulls.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/sequel-5.41.0/lib/sequel/extensions/empty_array_consider_nulls.rb
tdiary-5.1.6 vendor/bundle/ruby/3.0.0/gems/sequel-5.44.0/lib/sequel/extensions/empty_array_consider_nulls.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/sequel-5.41.0/lib/sequel/extensions/empty_array_consider_nulls.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/sequel-5.39.0/lib/sequel/extensions/empty_array_consider_nulls.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/sequel-5.44.0/lib/sequel/extensions/empty_array_consider_nulls.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/sequel-5.38.0/lib/sequel/extensions/empty_array_consider_nulls.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/sequel-5.43.0/lib/sequel/extensions/empty_array_consider_nulls.rb
sequel-5.44.0 lib/sequel/extensions/empty_array_consider_nulls.rb