class Card::Query::Value

handling for CQL value clauses, eg [operator, value]

Constants

SQL_FIELD

Attributes

operator[R]
query[R]
value[R]

Public Class Methods

match_prefices() click to toggle source
# File lib/card/query/value/match_value.rb, line 5
def match_prefices
  @match_prefices ||= %w[= ~]
end
match_term_and_prefix_re() click to toggle source
# File lib/card/query/value/match_value.rb, line 9
def match_term_and_prefix_re
  @match_term_and_prefix_re ||=
    /^(?<prefix>[#{Regexp.escape match_prefices.join}]*)\s*(?<term>.*)$/
end
new(rawvalue, query) click to toggle source
# File lib/card/query/value.rb, line 12
def initialize rawvalue, query
  @query = query
  @operator, @value = parse_value rawvalue
  canonicalize_operator
end

Public Instance Methods

to_sql(field) click to toggle source
# File lib/card/query/value.rb, line 18
def to_sql field
  if @operator == "~"
    match_sql field
  else
    standard_sql field
  end
end

Private Instance Methods

canonicalize_operator() click to toggle source
# File lib/card/query/value.rb, line 57
def canonicalize_operator
  unless (target = OPERATORS[@operator.to_s])
    raise Error::BadQuery, "Invalid operator: #{@operator}"
  end

  @operator = target
end
field_sql(field) click to toggle source
# File lib/card/query/value.rb, line 88
def field_sql field
  "#{@query.table_alias}.#{standardize_field field}"
end
operator?(key) click to toggle source
# File lib/card/query/value.rb, line 65
def operator? key
  OPERATORS.key? key.to_s
end
parse_array_value(array) click to toggle source
# File lib/card/query/value.rb, line 42
def parse_array_value array
  operator = operator?(array.first) ? array.shift : :in
  [operator, array.flatten.map { |i| parse_simple_value i }]
end
parse_simple_value(value) click to toggle source
# File lib/card/query/value.rb, line 47
def parse_simple_value value
  case value
  when String, Integer then value
  when Symbol          then value.to_s
  when nil             then nil
  else
    raise Error::BadQuery, "Invalid property value: #{value.inspect}"
  end
end
parse_value(value) click to toggle source
# File lib/card/query/value.rb, line 33
def parse_value value
  case value
  when Array                  then parse_array_value value.clone
  when ActiveRecord::Relation then ["in", value]
  when nil                    then ["is", nil]
  else                             ["=", parse_simple_value(value)]
  end
end
sqlize(v) click to toggle source
# File lib/card/query/value.rb, line 69
def sqlize v
  case v
  when Query                  then v.to_sql
  when ActiveRecord::Relation then "(#{v.to_sql})"
  when Array                  then sqlize_array v
  when nil                    then "NULL"
  else quote(v.to_s)
  end
end
sqlize_array(array) click to toggle source
# File lib/card/query/value.rb, line 79
def sqlize_array array
  array.flatten!
  if array.size == 1 && !@operator.in?(["in", "not in"])
    sqlize array.first
  else
    "(#{array.map { |x| sqlize(x) }.join(',')})"
  end
end
standard_sql(field) click to toggle source
# File lib/card/query/value.rb, line 28
def standard_sql field
  @value = Array.wrap(@value).map { |v| v.to_name.key } if field.to_sym == :name
  "#{field_sql field} #{@operator} #{sqlize @value}"
end
standardize_field(field) click to toggle source
# File lib/card/query/value.rb, line 92
def standardize_field field
  SQL_FIELD[field.to_sym] || safe_sql(field.to_s)
end