1 # Mixin module providing a method to convert filters into a list of SQL
2 # fragments suitable to be fed to ActiveRecord #where.
11 # +filters+ array of conditions, each being [column, operator, operand]
12 # +model_class+ subclass of ActiveRecord being filtered
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
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
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")
40 if operator == '@@' # full-text-search
42 raise ArgumentError.new("Full text search on individual columns is not supported")
44 attrs = [] # skip the generic per-column operator loop below
45 # Use to_tsquery since plainto_tsquery does not support prefix search.
46 # Instead split operand and join the words with ' & ' and add ':*' to the last word
47 # Thus when searched for "some str", objects containing "some" and "str:*" are found.
48 cond_out << model_class.full_text_tsvector+" @@ to_tsquery(?)"
50 param_out << operand.split.join(' & ')
53 if !model_class.searchable_columns(operator).index attr.to_s
54 raise ArgumentError.new("Invalid attribute '#{attr}' in filter")
56 case operator.downcase
57 when '=', '<', '<=', '>', '>=', '!=', 'like', 'ilike'
58 attr_type = model_class.attribute_column(attr).type
59 operator = '<>' if operator == '!='
60 if operand.is_a? String
61 if attr_type == :boolean
62 if not ['=', '<>'].include?(operator)
63 raise ArgumentError.new("Invalid operator '#{operator}' for " \
64 "boolean attribute '#{attr}'")
67 when '1', 't', 'true', 'y', 'yes'
69 when '0', 'f', 'false', 'n', 'no'
72 raise ArgumentError("Invalid operand '#{operand}' for " \
73 "boolean attribute '#{attr}'")
76 cond_out << "#{ar_table_name}.#{attr} #{operator} ?"
77 if (# any operator that operates on value rather than
79 operator.match(/[<=>]/) and (attr_type == :datetime))
80 operand = Time.parse operand
83 elsif operand.nil? and operator == '='
84 cond_out << "#{ar_table_name}.#{attr} is null"
85 elsif operand.nil? and operator == '<>'
86 cond_out << "#{ar_table_name}.#{attr} is not null"
87 elsif (attr_type == :boolean) and ['=', '<>'].include?(operator) and
88 [true, false].include?(operand)
89 cond_out << "#{ar_table_name}.#{attr} #{operator} ?"
92 raise ArgumentError.new("Invalid operand type '#{operand.class}' "\
93 "for '#{operator}' operator in filters")
96 if operand.is_a? Array
97 cond_out << "#{ar_table_name}.#{attr} #{operator} (?)"
99 if operator == 'not in' and not operand.include?(nil)
100 # explicitly allow NULL
101 cond_out[-1] = "(#{cond_out[-1]} OR #{ar_table_name}.#{attr} IS NULL)"
104 raise ArgumentError.new("Invalid operand type '#{operand.class}' "\
105 "for '#{operator}' operator in filters")
108 operand = [operand] unless operand.is_a? Array
111 cl = ArvadosModel::kind_class op
113 cond << "#{ar_table_name}.#{attr} like ?"
114 param_out << cl.uuid_like_pattern
119 cond_out << cond.join(' OR ')
123 conds_out << cond_out.join(' OR ') if cond_out.any?
126 {:cond_out => conds_out, :param_out => param_out}