Method: Sequel::Dataset#invert

Defined in:
lib/sequel/dataset/query.rb

#invertObject

Inverts the current WHERE and HAVING clauses. If there is neither a WHERE or HAVING clause, adds a WHERE clause that is always false.

DB[:items].where(category: 'software').invert
# SELECT * FROM items WHERE (category != 'software')

DB[:items].where(category: 'software', id: 3).invert
# SELECT * FROM items WHERE ((category != 'software') OR (id != 3))

See documentation for exclude for how inversion is handled in regards to SQL 3-valued boolean logic.



471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/sequel/dataset/query.rb', line 471

def invert
  cached_dataset(:_invert_ds) do
    having, where = @opts.values_at(:having, :where)
    if having.nil? && where.nil?
      where(false)
    else
      o = {}
      o[:having] = SQL::BooleanExpression.invert(having) if having
      o[:where] = SQL::BooleanExpression.invert(where) if where
      clone(o)
    end
  end
end