module Sequel::ExcludeOrNull

Public Instance Methods

exclude_or_null(*cond, &block) click to toggle source

Performs the inverse of Sequel::Dataset#where, but also excludes rows where the given condition IS NULL.

DB[:items].exclude_or_null(category: 'software')
# SELECT * FROM items WHERE NOT coalesce((category = 'software'), false)

DB[:items].exclude_or_null(category: 'software', id: 3)
# SELECT * FROM items WHERE NOT coalesce(((category = 'software') AND (id = 3)), false)
# File lib/sequel/extensions/exclude_or_null.rb, line 40
def exclude_or_null(*cond, &block)
  add_filter(:where, cond, :or_null, &block)
end
exclude_or_null_having(*cond, &block) click to toggle source

The same as #exclude_or_null, but affecting the HAVING clause instead of the WHERE clause.

DB[:items].select_group(:name).exclude_or_null_having{count(name) < 2}
# SELECT name FROM items GROUP BY name HAVING NOT coalesce((count(name) < 2), true)
# File lib/sequel/extensions/exclude_or_null.rb, line 49
def exclude_or_null_having(*cond, &block)
  add_filter(:having, cond, :or_null, &block)
end

Private Instance Methods

_invert_filter(cond, invert) click to toggle source

Recognize :or_null value for invert, returning an expression for the invert of the condition or the condition being null.

Calls superclass method
# File lib/sequel/extensions/exclude_or_null.rb, line 57
def _invert_filter(cond, invert)
  if invert == :or_null
    ~SQL::Function.new(:coalesce, cond, SQL::Constants::SQLFALSE)
  else
    super
  end
end