X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/37ab0eedec5eaf99c27b6b64fd04cc9248081713..cd675c02301fa53b5fd6e173ce75c58b202aaaa5:/services/api/lib/record_filters.rb diff --git a/services/api/lib/record_filters.rb b/services/api/lib/record_filters.rb index 5688ca6140..517a7fe284 100644 --- a/services/api/lib/record_filters.rb +++ b/services/api/lib/record_filters.rb @@ -31,7 +31,10 @@ module RecordFilters model_table_name = model_class.table_name filters.each do |filter| attrs_in, operator, operand = filter - if attrs_in == 'any' && operator != '@@' + if operator == '@@' + raise ArgumentError.new("Full text search operator is no longer supported") + end + if attrs_in == 'any' attrs = model_class.searchable_columns(operator) elsif attrs_in.is_a? Array attrs = attrs_in @@ -54,22 +57,6 @@ module RecordFilters attrs = [] end - if operator == '@@' - # Full-text search - if attrs_in != 'any' - raise ArgumentError.new("Full text search on individual columns is not supported") - end - 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.join(' & ') - end attrs.each do |attr| subproperty = attr.split(".", 2) @@ -154,6 +141,23 @@ module RecordFilters cond_out << "jsonb_exists(#{attr_table_name}.#{attr}, ?)" param_out << operand + elsif expr = /^ *\( *(\w+) *(<=?|>=?|=) *(\w+) *\) *$/.match(attr) + if operator != '=' || ![true,"true"].index(operand) + raise ArgumentError.new("Invalid expression filter '#{attr}': subsequent elements must be [\"=\", true]") + end + operator = expr[2] + attr1, attr2 = expr[1], expr[3] + allowed = attr_model_class.searchable_columns(operator) + [attr1, attr2].each do |tok| + if !allowed.index(tok) + raise ArgumentError.new("Invalid attribute in expression: '#{tok}'") + end + col = attr_model_class.columns.select { |c| c.name == tok }.first + if col.type != :integer + raise ArgumentError.new("Non-numeric attribute in expression: '#{tok}'") + end + end + cond_out << "#{attr1} #{operator} #{attr2}" else if !attr_model_class.searchable_columns(operator).index attr raise ArgumentError.new("Invalid attribute '#{attr}' in filter")