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



85
86
87
88
# File 'lib/predicate/sequel/to_sequel.rb', line 85

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



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

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



45
46
47
48
# File 'lib/predicate/sequel/to_sequel.rb', line 45

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

#on_eq(sexpr) ⇒ Object



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

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/predicate/sequel/to_sequel.rb', line 54

def on_in(sexpr)
  left, right = apply(sexpr.identifier), sexpr.right
  if right.literal?
    if right.has_placeholder?
      ::Sequel.expr(left => [on_literal(right)])
    else
      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
    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
18
19
20
21
22
23
# File 'lib/predicate/sequel/to_sequel.rb', line 15

def on_literal(sexpr)
  if sexpr.last.nil?
    nil
  elsif sexpr.last.is_a?(Predicate::Placeholder)
    ::Sequel.lit("?", :"$#{sexpr.last.object_id}")
  else
    ::Sequel.expr(sexpr.last)
  end
end

#on_match(sexpr) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/predicate/sequel/to_sequel.rb', line 95

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



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

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

#on_not(sexpr) ⇒ Object



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

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

#on_opaque(sexpr) ⇒ Object

Raises:



107
108
109
110
# File 'lib/predicate/sequel/to_sequel.rb', line 107

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



90
91
92
93
# File 'lib/predicate/sequel/to_sequel.rb', line 90

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



27
28
29
# File 'lib/predicate/sequel/to_sequel.rb', line 27

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

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

Raises:



112
113
114
# File 'lib/predicate/sequel/to_sequel.rb', line 112

def on_unsupported(sexpr)
  raise NotSupportedError, "Unsupported predicate #{sexpr}"
end