class Card::Query::Value
handling for CQL value clauses, eg [operator, value]
Constants
- SQL_FIELD
Attributes
Public Class Methods
Source
# File lib/card/query/value/match_value.rb, line 5 def match_prefices @match_prefices ||= %w[= ~] end
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
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
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
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
Source
# File lib/card/query/value.rb, line 88 def field_sql field "#{@query.table_alias}.#{standardize_field field}" end
Source
# File lib/card/query/value.rb, line 65 def operator? key OPERATORS.key? key.to_s end
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
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
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
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
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
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
Source
# File lib/card/query/value.rb, line 92 def standardize_field field SQL_FIELD[field.to_sym] || safe_sql(field.to_s) end