Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[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'
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       cond_out = []
38       attrs.each do |attr|
39         if !model_class.searchable_columns(operator).index attr.to_s
40           raise ArgumentError.new("Invalid attribute '#{attr}' in filter")
41         end
42         case operator.downcase
43         when '=', '<', '<=', '>', '>=', '!=', 'like', 'ilike'
44           attr_type = model_class.attribute_column(attr).type
45           operator = '<>' if operator == '!='
46           if operand.is_a? String
47             if attr_type == :boolean
48               if not ['=', '<>'].include?(operator)
49                 raise ArgumentError.new("Invalid operator '#{operator}' for " \
50                                         "boolean attribute '#{attr}'")
51               end
52               case operand.downcase
53               when '1', 't', 'true', 'y', 'yes'
54                 operand = true
55               when '0', 'f', 'false', 'n', 'no'
56                 operand = false
57               else
58                 raise ArgumentError("Invalid operand '#{operand}' for " \
59                                     "boolean attribute '#{attr}'")
60               end
61             end
62             cond_out << "#{ar_table_name}.#{attr} #{operator} ?"
63             if (# any operator that operates on value rather than
64                 # representation:
65                 operator.match(/[<=>]/) and (attr_type == :datetime))
66               operand = Time.parse operand
67             end
68             param_out << operand
69           elsif operand.nil? and operator == '='
70             cond_out << "#{ar_table_name}.#{attr} is null"
71           elsif operand.nil? and operator == '<>'
72             cond_out << "#{ar_table_name}.#{attr} is not null"
73           elsif (attr_type == :boolean) and ['=', '<>'].include?(operator) and
74               [true, false].include?(operand)
75             cond_out << "#{ar_table_name}.#{attr} #{operator} ?"
76             param_out << operand
77           else
78             raise ArgumentError.new("Invalid operand type '#{operand.class}' "\
79                                     "for '#{operator}' operator in filters")
80           end
81         when 'in', 'not in'
82           if operand.is_a? Array
83             cond_out << "#{ar_table_name}.#{attr} #{operator} (?)"
84             param_out << operand
85             if operator == 'not in' and not operand.include?(nil)
86               # explicitly allow NULL
87               cond_out[-1] = "(#{cond_out[-1]} OR #{ar_table_name}.#{attr} IS NULL)"
88             end
89           else
90             raise ArgumentError.new("Invalid operand type '#{operand.class}' "\
91                                     "for '#{operator}' operator in filters")
92           end
93         when 'is_a'
94           operand = [operand] unless operand.is_a? Array
95           cond = []
96           operand.each do |op|
97             cl = ArvadosModel::kind_class op
98             if cl
99               cond << "#{ar_table_name}.#{attr} like ?"
100               param_out << cl.uuid_like_pattern
101             else
102               cond << "1=0"
103             end
104           end
105           cond_out << cond.join(' OR ')
106         end
107       end
108       conds_out << cond_out.join(' OR ') if cond_out.any?
109     end
110
111     {:cond_out => conds_out, :param_out => param_out}
112   end
113
114 end