8784: Fix test for latest firefox.
[arvados.git] / services / api / lib / record_filters.rb
1 # Mixin module providing a method to convert filters into a list of SQL
2 # fragments suitable to be fed to ActiveRecord #where.
3 #
4 # Expects:
5 #   model_class
6 # Operates on:
7 #   @objects
8 module RecordFilters
9
10   # Input:
11   # +filters+        array of conditions, each being [column, operator, operand]
12   # +model_class+    subclass of ActiveRecord being filtered
13   #
14   # Output:
15   # Hash with two keys:
16   # :cond_out  array of SQL fragments for each filter expression
17   # :param_out  array of values for parameter substitution in cond_out
18   def record_filters filters, model_class
19     conds_out = []
20     param_out = []
21
22     ar_table_name = model_class.table_name
23     filters.each do |filter|
24       attrs_in, operator, operand = filter
25       if attrs_in == 'any' && operator != '@@'
26         attrs = model_class.searchable_columns(operator)
27       elsif attrs_in.is_a? Array
28         attrs = attrs_in
29       else
30         attrs = [attrs_in]
31       end
32       if !filter.is_a? Array
33         raise ArgumentError.new("Invalid element in filters array: #{filter.inspect} is not an array")
34       elsif !operator.is_a? String
35         raise ArgumentError.new("Invalid operator '#{operator}' (#{operator.class}) in filter")
36       end
37
38       cond_out = []
39
40       if operator == '@@'
41         # Full-text search
42         if attrs_in != 'any'
43           raise ArgumentError.new("Full text search on individual columns is not supported")
44         end
45         if operand.is_a? Array
46           raise ArgumentError.new("Full text search not supported for array operands")
47         end
48
49         # Skip the generic per-column operator loop below
50         attrs = []
51         # Use to_tsquery since plainto_tsquery does not support prefix
52         # search. And, split operand and join the words with ' & '
53         cond_out << model_class.full_text_tsvector+" @@ to_tsquery(?)"
54         param_out << operand.split.join(' & ')
55       end
56       attrs.each do |attr|
57         if !model_class.searchable_columns(operator).index attr.to_s
58           raise ArgumentError.new("Invalid attribute '#{attr}' in filter")
59         end
60         case operator.downcase
61         when '=', '<', '<=', '>', '>=', '!=', 'like', 'ilike'
62           attr_type = model_class.attribute_column(attr).type
63           operator = '<>' if operator == '!='
64           if operand.is_a? String
65             if attr_type == :boolean
66               if not ['=', '<>'].include?(operator)
67                 raise ArgumentError.new("Invalid operator '#{operator}' for " \
68                                         "boolean attribute '#{attr}'")
69               end
70               case operand.downcase
71               when '1', 't', 'true', 'y', 'yes'
72                 operand = true
73               when '0', 'f', 'false', 'n', 'no'
74                 operand = false
75               else
76                 raise ArgumentError("Invalid operand '#{operand}' for " \
77                                     "boolean attribute '#{attr}'")
78               end
79             end
80             if operator == '<>'
81               # explicitly allow NULL
82               cond_out << "#{ar_table_name}.#{attr} #{operator} ? OR #{ar_table_name}.#{attr} IS NULL"
83             else
84               cond_out << "#{ar_table_name}.#{attr} #{operator} ?"
85             end
86             if (# any operator that operates on value rather than
87                 # representation:
88                 operator.match(/[<=>]/) and (attr_type == :datetime))
89               operand = Time.parse operand
90             end
91             param_out << operand
92           elsif operand.nil? and operator == '='
93             cond_out << "#{ar_table_name}.#{attr} is null"
94           elsif operand.nil? and operator == '<>'
95             cond_out << "#{ar_table_name}.#{attr} is not null"
96           elsif (attr_type == :boolean) and ['=', '<>'].include?(operator) and
97               [true, false].include?(operand)
98             cond_out << "#{ar_table_name}.#{attr} #{operator} ?"
99             param_out << operand
100           else
101             raise ArgumentError.new("Invalid operand type '#{operand.class}' "\
102                                     "for '#{operator}' operator in filters")
103           end
104         when 'in', 'not in'
105           if operand.is_a? Array
106             cond_out << "#{ar_table_name}.#{attr} #{operator} (?)"
107             param_out << operand
108             if operator == 'not in' and not operand.include?(nil)
109               # explicitly allow NULL
110               cond_out[-1] = "(#{cond_out[-1]} OR #{ar_table_name}.#{attr} IS NULL)"
111             end
112           else
113             raise ArgumentError.new("Invalid operand type '#{operand.class}' "\
114                                     "for '#{operator}' operator in filters")
115           end
116         when 'is_a'
117           operand = [operand] unless operand.is_a? Array
118           cond = []
119           operand.each do |op|
120             cl = ArvadosModel::kind_class op
121             if cl
122               cond << "#{ar_table_name}.#{attr} like ?"
123               param_out << cl.uuid_like_pattern
124             else
125               cond << "1=0"
126             end
127           end
128           cond_out << cond.join(' OR ')
129         else
130           raise ArgumentError.new("Invalid operator '#{operator}'")
131         end
132       end
133       conds_out << cond_out.join(' OR ') if cond_out.any?
134     end
135
136     {:cond_out => conds_out, :param_out => param_out}
137   end
138
139 end