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