Class: Card::Query::Value
- Inherits:
-
Object
- Object
- Card::Query::Value
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
#operator ⇒ Object
Returns the value of attribute operator.
7
8
9
|
# File 'lib/card/query/value.rb', line 7
def operator
@operator
end
|
#query ⇒ Object
Returns the value of attribute query.
7
8
9
|
# File 'lib/card/query/value.rb', line 7
def query
@query
end
|
#value ⇒ Object
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_operator ⇒ Object
28
29
30
31
32
33
|
# File 'lib/card/query/value.rb', line 28
def canonicalize_operator
unless (target = OPERATORS[@operator.to_s])
raise Card::Error::BadQuery, "Invalid Operator #{@operator}"
end
@operator = target
end
|
#field_sql(field) ⇒ Object
60
61
62
63
|
# File 'lib/card/query/value.rb', line 60
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
52
53
54
55
56
57
58
|
# File 'lib/card/query/value.rb', line 52
def operational_sql value
if @operator == "~"
connection.match value
else
"#{@operator} #{value}"
end
end
|
#operator?(key) ⇒ Boolean
35
36
37
|
# File 'lib/card/query/value.rb', line 35
def operator? key
OPERATORS.key? key.to_s
end
|
#parse_array_value(array) ⇒ Object
23
24
25
26
|
# File 'lib/card/query/value.rb', line 23
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
|
# File 'lib/card/query/value.rb', line 15
def parse_value rawvalue
case rawvalue
when String, Integer then ["=", rawvalue]
when Array then parse_array_value rawvalue
else raise("Invalid Condition Clause #{rawvalue}.inspect}")
end
end
|
#sqlize(v) ⇒ Object
39
40
41
42
43
44
45
|
# File 'lib/card/query/value.rb', line 39
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
47
48
49
50
|
# File 'lib/card/query/value.rb', line 47
def to_sql field
value = value_sql field, @value
"#{field_sql field} #{operational_sql value}"
end
|
#value_sql(field, value) ⇒ Object
65
66
67
68
|
# File 'lib/card/query/value.rb', line 65
def value_sql field, value
value = [value].flatten.map(&:to_name).map(&:key) if field.to_sym == :name
sqlize value
end
|