6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/ahoy/properties.rb', line 6
def where_properties(properties)
relation = self
column_type = columns_hash["properties"].type
adapter_name = connection.adapter_name.downcase
case adapter_name
when /mysql/
if column_type == :json
properties.each do |k, v|
if v.nil?
v = "null"
elsif v == true
v = "true"
end
relation = relation.where("JSON_UNQUOTE(properties -> ?) = ?", "$.#{k.to_s}", v.as_json)
end
else
properties.each do |k, v|
relation = relation.where("properties REGEXP ?", "[{,]#{{k.to_s => v}.to_json.sub(/\A\{/, "").sub(/\}\z/, "").gsub("+", "\\\\+")}[,}]")
end
end
when /postgres|postgis/
if column_type == :jsonb || column_type == :json
properties.each do |k, v|
relation =
if v.nil?
relation.where("properties ->> ? IS NULL", k.to_s)
else
relation.where("properties ->> ? = ?", k.to_s, v.as_json.to_s)
end
end
elsif column_type == :hstore
properties.each do |k, v|
relation =
if v.nil?
relation.where("properties -> ? IS NULL", k.to_s)
else
relation.where("properties -> ? = ?", k.to_s, v.to_s)
end
end
else
properties.each do |k, v|
relation = relation.where("properties SIMILAR TO ?", "%[{,]#{{k.to_s => v}.to_json.sub(/\A\{/, "").sub(/\}\z/, "").gsub("+", "\\\\+")}[,}]%")
end
end
else
raise "Adapter not supported: #{adapter_name}"
end
relation
end
|