Merge branch 'master' into 6859-fix-invalid-manifests
[arvados.git] / services / api / lib / record_filters.rb
index 256997e4bae4212b858b8f0c23c779f0426f6f10..350c3802fc60e606fd0dba703429ca7dc98f847e 100644 (file)
@@ -34,14 +34,26 @@ module RecordFilters
       elsif !operator.is_a? String
         raise ArgumentError.new("Invalid operator '#{operator}' (#{operator.class}) in filter")
       end
+
       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
+        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(?)"
-        operand = '-' if (!operand or operand.empty?)
-        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
@@ -65,7 +77,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))
@@ -110,7 +127,6 @@ module RecordFilters
           end
           cond_out << cond.join(' OR ')
         end
-       end
       end
       conds_out << cond_out.join(' OR ') if cond_out.any?
     end