Class: Perpetuity::Postgres::QueryExpression

Inherits:
Object
  • Object
show all
Defined in:
lib/perpetuity/postgres/query_expression.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, comparator, value) ⇒ QueryExpression

Returns a new instance of QueryExpression.



9
10
11
12
13
# File 'lib/perpetuity/postgres/query_expression.rb', line 9

def initialize attribute, comparator, value
  @attribute = attribute
  @comparator = comparator
  @value = value
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



8
9
10
# File 'lib/perpetuity/postgres/query_expression.rb', line 8

def attribute
  @attribute
end

#comparatorObject

Returns the value of attribute comparator.



8
9
10
# File 'lib/perpetuity/postgres/query_expression.rb', line 8

def comparator
  @comparator
end

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/perpetuity/postgres/query_expression.rb', line 8

def value
  @value
end

Instance Method Details

#!=Object



64
65
66
# File 'lib/perpetuity/postgres/query_expression.rb', line 64

def !=
  "#{attribute} != #{sql_value}"
end

#&(other) ⇒ Object



90
91
92
# File 'lib/perpetuity/postgres/query_expression.rb', line 90

def & other
  QueryIntersection.new(self, other)
end

#<Object



48
49
50
# File 'lib/perpetuity/postgres/query_expression.rb', line 48

def <
  "#{attribute} < #{sql_value}"
end

#<=Object



52
53
54
# File 'lib/perpetuity/postgres/query_expression.rb', line 52

def <=
  "#{attribute} <= #{sql_value}"
end

#==Object



44
45
46
# File 'lib/perpetuity/postgres/query_expression.rb', line 44

def ==
  "#{attribute} = #{sql_value}"
end

#=~Object



77
78
79
80
81
82
83
84
# File 'lib/perpetuity/postgres/query_expression.rb', line 77

def =~
  regexp_comparator = if value.casefold?
                        '~*'
                      else
                        '~'
                      end
  "#{attribute} #{regexp_comparator} #{sql_value}"
end

#>Object



56
57
58
# File 'lib/perpetuity/postgres/query_expression.rb', line 56

def >
  "#{attribute} > #{sql_value}"
end

#>=Object



60
61
62
# File 'lib/perpetuity/postgres/query_expression.rb', line 60

def >=
  "#{attribute} >= #{sql_value}"
end

#inObject



68
69
70
71
72
73
74
75
# File 'lib/perpetuity/postgres/query_expression.rb', line 68

def in
  case sql_value
  when Range
    "#{attribute} BETWEEN #{SQLValue.new(sql_value.min)} AND #{SQLValue.new(sql_value.max)}"
  else
    "#{attribute} IN #{sql_value}"
  end
end

#sql_value(value = self.value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/perpetuity/postgres/query_expression.rb', line 27

def sql_value value=self.value
  if value.is_a? String or value.is_a? Symbol
    SQLValue.new(value)
  elsif value.is_a? Regexp
    "'#{value.to_s.sub(/\A\(\?i?-mi?x\:/, '').sub(/\)\z/, '')}'"
  elsif value.is_a? Time
    SQLValue.new(value)
  elsif value.is_a? Array
    value.map! do |element|
      sql_value(element)
    end
    "(#{value.join(',')})"
  else
    value
  end
end

#to_dbObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/perpetuity/postgres/query_expression.rb', line 15

def to_db
  if value.nil?
    if comparator == :==
      "#{attribute} IS NULL"
    elsif comparator == :!=
      "#{attribute} IS NOT NULL"
    end
  else
    public_send comparator
  end
end

#|(other) ⇒ Object



86
87
88
# File 'lib/perpetuity/postgres/query_expression.rb', line 86

def | other
  QueryUnion.new(self, other)
end