Module: Predicate::ToSequel::Methods

Included in:
Predicate::ToSequel
Defined in:
lib/predicate/sequel/to_sequel.rb

Instance Method Summary collapse

Instance Method Details

#on_and(sexpr) ⇒ Object



75
76
77
78
# File 'lib/predicate/sequel/to_sequel.rb', line 75

def on_and(sexpr)
  body = sexpr.sexpr_body
  body[1..-1].inject(apply(body.first)){|f,t| f & apply(t) }
end

#on_contradiction(sexpr) ⇒ Object



25
26
27
# File 'lib/predicate/sequel/to_sequel.rb', line 25

def on_contradiction(sexpr)
  ::Sequel::SQL::BooleanConstant.new(false)
end

#on_dyadic_comp(sexpr) ⇒ Object Also known as: on_lt, on_lte, on_gt, on_gte



39
40
41
42
# File 'lib/predicate/sequel/to_sequel.rb', line 39

def on_dyadic_comp(sexpr)
  left, right = apply(sexpr.left), apply(sexpr.right)
  left.send(sexpr.operator_symbol, right)
end

#on_eq(sexpr) ⇒ Object



29
30
31
32
# File 'lib/predicate/sequel/to_sequel.rb', line 29

def on_eq(sexpr)
  left, right = apply(sexpr.left), apply(sexpr.right)
  ::Sequel.expr(left => right)
end

#on_identifier(sexpr) ⇒ Object



7
8
9
# File 'lib/predicate/sequel/to_sequel.rb', line 7

def on_identifier(sexpr)
  ::Sequel.identifier(sexpr.last)
end

#on_in(sexpr) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/predicate/sequel/to_sequel.rb', line 48

def on_in(sexpr)
  left, right = apply(sexpr.identifier), sexpr.right
  if right.literal?
    values = Array(right.value).uniq
    if values.include?(nil)
      nonnil = values.compact
      if nonnil.empty?
        ::Sequel.expr(left => nil)
      elsif nonnil.size == 1
        ::Sequel.expr(left => nil) | ::Sequel.expr(left => nonnil.first)
      else
        ::Sequel.expr(left => nil) | ::Sequel.expr(left => nonnil)
      end
    else
      ::Sequel.expr(left => right.value)
    end
  elsif right.opaque?
    ::Sequel.expr(left => apply(right))
  else
    raise Error, "Unable to compile `#{right}` to sequel"
  end
end

#on_literal(sexpr) ⇒ Object



15
16
17
# File 'lib/predicate/sequel/to_sequel.rb', line 15

def on_literal(sexpr)
  sexpr.last.nil? ? nil : ::Sequel.expr(sexpr.last)
end

#on_match(sexpr) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/predicate/sequel/to_sequel.rb', line 85

def on_match(sexpr)
  left, right = sexpr.left, sexpr.right
  left  = [ left.first,  "%#{left.last}%"  ] if left.first  == :literal && !left.last.is_a?(Regexp)
  right = [ right.first, "%#{right.last}%" ] if right.first == :literal && !right.last.is_a?(Regexp)
  left, right = apply(left), apply(right)
  if sexpr.case_sentitive?
    left.like(right)
  else
    left.ilike(right)
  end
end

#on_neq(sexpr) ⇒ Object



34
35
36
37
# File 'lib/predicate/sequel/to_sequel.rb', line 34

def on_neq(sexpr)
  left, right = apply(sexpr.left), apply(sexpr.right)
  ~::Sequel.expr(left => right)
end

#on_not(sexpr) ⇒ Object



71
72
73
# File 'lib/predicate/sequel/to_sequel.rb', line 71

def on_not(sexpr)
  ~apply(sexpr.last)
end

#on_opaque(sexpr) ⇒ Object

Raises:



97
98
99
100
# File 'lib/predicate/sequel/to_sequel.rb', line 97

def on_opaque(sexpr)
  return [sexpr.last] if sexpr.last.respond_to?(:sql_literal)
  raise Error, "Unable to compile #{sexpr} to Sequel"
end

#on_or(sexpr) ⇒ Object



80
81
82
83
# File 'lib/predicate/sequel/to_sequel.rb', line 80

def on_or(sexpr)
  body = sexpr.sexpr_body
  body[1..-1].inject(apply(body.first)){|f,t| f | apply(t) }
end

#on_qualified_identifier(sexpr) ⇒ Object



11
12
13
# File 'lib/predicate/sequel/to_sequel.rb', line 11

def on_qualified_identifier(sexpr)
  ::Sequel.identifier(sexpr.name).qualify(sexpr.qualifier)
end

#on_tautology(sexpr) ⇒ Object



21
22
23
# File 'lib/predicate/sequel/to_sequel.rb', line 21

def on_tautology(sexpr)
  ::Sequel::SQL::BooleanConstant.new(true)
end

#on_unsupported(sexpr) ⇒ Object Also known as: on_native, on_intersect

Raises:



102
103
104
# File 'lib/predicate/sequel/to_sequel.rb', line 102

def on_unsupported(sexpr)
  raise NotSupportedError
end