X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/cda964acdb8132d90b881e62db008c574fdd5cc4..d5eb6a70f6f201ad915bf0ae02ddc4dbb9de526b:/services/api/lib/record_filters.rb diff --git a/services/api/lib/record_filters.rb b/services/api/lib/record_filters.rb index 0b65108796..eb8d09b74c 100644 --- a/services/api/lib/record_filters.rb +++ b/services/api/lib/record_filters.rb @@ -1,3 +1,7 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: AGPL-3.0 + # Mixin module providing a method to convert filters into a list of SQL # fragments suitable to be fed to ActiveRecord #where. # @@ -37,17 +41,23 @@ module RecordFilters cond_out = [] - if operator == '@@' # full-text-search + if operator == '@@' + # Full-text search if attrs_in != 'any' raise ArgumentError.new("Full text search on individual columns is not supported") end - attrs = [] # skip the generic per-column operator loop below - # Use to_tsquery since plainto_tsquery does not support prefix search. - # Instead split operand, add ':*' to each word and join the words with ' & ' + if operand.is_a? Array + raise ArgumentError.new("Full text search not supported for array operands") + end + + # Skip the generic per-column operator loop below + attrs = [] + # Use to_tsquery since plainto_tsquery does not support prefix + # search. And, split operand and join the words with ' & ' cond_out << model_class.full_text_tsvector+" @@ to_tsquery(?)" - param_out << operand.split.each {|s| s.concat(':*')}.join(' & ') - else - attrs.each do |attr| + param_out << operand.split.join(' & ') + end + attrs.each do |attr| if !model_class.searchable_columns(operator).index attr.to_s raise ArgumentError.new("Invalid attribute '#{attr}' in filter") end @@ -71,7 +81,12 @@ module RecordFilters "boolean attribute '#{attr}'") end end - cond_out << "#{ar_table_name}.#{attr} #{operator} ?" + if operator == '<>' + # explicitly allow NULL + cond_out << "#{ar_table_name}.#{attr} #{operator} ? OR #{ar_table_name}.#{attr} IS NULL" + else + cond_out << "#{ar_table_name}.#{attr} #{operator} ?" + end if (# any operator that operates on value rather than # representation: operator.match(/[<=>]/) and (attr_type == :datetime)) @@ -115,8 +130,9 @@ module RecordFilters end end cond_out << cond.join(' OR ') + else + raise ArgumentError.new("Invalid operator '#{operator}'") end - end end conds_out << cond_out.join(' OR ') if cond_out.any? end