Class: Card::Query::Value

Inherits:
Object
  • Object
show all
Includes:
Clause
Defined in:
lib/card/query/value.rb

Constant Summary collapse

SQL_FIELD =
{ name: "key", content: "db_content" }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Clause

#connection, #quote, #safe_sql

Constructor Details

#initialize(rawvalue, query) ⇒ Value

Returns a new instance of Value.



9
10
11
12
13
# File 'lib/card/query/value.rb', line 9

def initialize rawvalue, query
  @query = query
  @operator, @value = parse_value rawvalue
  canonicalize_operator
end

Instance Attribute Details

#operatorObject (readonly)

Returns the value of attribute operator.



7
8
9
# File 'lib/card/query/value.rb', line 7

def operator
  @operator
end

#queryObject (readonly)

Returns the value of attribute query.



7
8
9
# File 'lib/card/query/value.rb', line 7

def query
  @query
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/card/query/value.rb', line 7

def value
  @value
end

Instance Method Details

#canonicalize_operatorObject



29
30
31
32
33
34
35
# File 'lib/card/query/value.rb', line 29

def canonicalize_operator
  unless (target = OPERATORS[@operator.to_s])
    raise Error::BadQuery, "Invalid operator: #{@operator}"
  end

  @operator = target
end

#field_sql(field) ⇒ Object



62
63
64
65
# File 'lib/card/query/value.rb', line 62

def field_sql field
  db_field = SQL_FIELD[field.to_sym] || safe_sql(field.to_s)
  "#{@query.table_alias}.#{db_field}"
end

#operational_sql(value) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/card/query/value.rb', line 54

def operational_sql value
  if @operator == "~"
    connection.match value
  else
    "#{@operator} #{value}"
  end
end

#operator?(key) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/card/query/value.rb', line 37

def operator? key
  OPERATORS.key? key.to_s
end

#parse_array_value(array) ⇒ Object



24
25
26
27
# File 'lib/card/query/value.rb', line 24

def parse_array_value array
  operator = operator?(array.first) ? array.shift : :in
  [operator, array]
end

#parse_value(rawvalue) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/card/query/value.rb', line 15

def parse_value rawvalue
  case rawvalue
  when String, Integer then ["=", rawvalue]
  when Symbol          then ["=", rawvalue.to_s]
  when Array           then parse_array_value rawvalue
  else raise Error::BadQuery, "Invalid property value: #{rawvalue.inspect}"
  end
end

#sqlize(v) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/card/query/value.rb', line 41

def sqlize v
  case v
  when Query then  v.to_sql
  when Array then  "(" + v.flatten.map { |x| sqlize(x) }.join(",") + ")"
  else quote(v.to_s)
  end
end

#to_sql(field) ⇒ Object



49
50
51
52
# File 'lib/card/query/value.rb', line 49

def to_sql field
  value = value_sql field, @value
  "#{field_sql field} #{operational_sql value}"
end

#value_sql(field, value) ⇒ Object



67
68
69
70
# File 'lib/card/query/value.rb', line 67

def value_sql field, value
  value = [value].flatten.map(&:to_name).map(&:key) if field.to_sym == :name
  sqlize value
end