Skip to content

Instantly share code, notes, and snippets.

@jamesgolick
Created December 15, 2009 17:00
Show Gist options
  • Select an option

  • Save jamesgolick/257087 to your computer and use it in GitHub Desktop.

Select an option

Save jamesgolick/257087 to your computer and use it in GitHub Desktop.
require 'sequel'
# Out of the box, Sequel uses IS TRUE/FALSE for boolean parameters
# This prevents MySQL from using indexes.
#
# This patch fixes that.
module Sequel
module SQL
class BooleanExpression
def self.from_value_pairs(pairs, op=:AND, negate=false)
pairs = pairs.collect do |l,r|
ce = case r
when Range
new(:AND, new(:>=, l, r.begin), new(r.exclude_end? ? :< : :<=, l, r.end))
when Array, ::Sequel::Dataset, SQLArray
new(:IN, l, r)
when NegativeBooleanConstant
new(:"IS NOT", l, r.constant)
when BooleanConstant
new(:IS, l, r.constant)
when NilClass
new(:IS, l, r)
when Regexp
StringExpression.like(l, r)
else
new(:'=', l, r)
end
negate ? invert(ce) : ce
end
pairs.length == 1 ? pairs.at(0) : new(op, *pairs)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment