class Perpetuity::Postgres::QueryExpression

Attributes

attribute[RW]
comparator[RW]
value[RW]

Public Class Methods

new(attribute, comparator, value) click to toggle source
# File lib/perpetuity/postgres/query_expression.rb, line 9
def initialize attribute, comparator, value
  @attribute = attribute
  @comparator = comparator
  @value = value
end

Public Instance Methods

!=() click to toggle source
# File lib/perpetuity/postgres/query_expression.rb, line 64
def !=
  "#{attribute} != #{sql_value}"
end
&(other) click to toggle source
# File lib/perpetuity/postgres/query_expression.rb, line 90
def & other
  QueryIntersection.new(self, other)
end
<() click to toggle source
# File lib/perpetuity/postgres/query_expression.rb, line 48
def <
  "#{attribute} < #{sql_value}"
end
<=() click to toggle source
# File lib/perpetuity/postgres/query_expression.rb, line 52
def <=
  "#{attribute} <= #{sql_value}"
end
==() click to toggle source
# File lib/perpetuity/postgres/query_expression.rb, line 44
def ==
  "#{attribute} = #{sql_value}"
end
=~() click to toggle source
# File lib/perpetuity/postgres/query_expression.rb, line 77
def =~
  regexp_comparator = if value.casefold?
                        '~*'
                      else
                        '~'
                      end
  "#{attribute} #{regexp_comparator} #{sql_value}"
end
>() click to toggle source
# File lib/perpetuity/postgres/query_expression.rb, line 56
def >
  "#{attribute} > #{sql_value}"
end
>=() click to toggle source
# File lib/perpetuity/postgres/query_expression.rb, line 60
def >=
  "#{attribute} >= #{sql_value}"
end
in() click to toggle source
# 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) click to toggle source
# 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_db() click to toggle source
# 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) click to toggle source
# File lib/perpetuity/postgres/query_expression.rb, line 86
def | other
  QueryUnion.new(self, other)
end