Method: InterMine::PathQuery::Query#where

Defined in:
lib/intermine/query.rb

#where(*wheres) ⇒ Object

Add a constraint clause to the query.

query.where(:symbol => "eve")
query.where(:symbol => %{eve h bib zen})
query.where(:length => {:le => 100}, :symbol => "eve*")

Interprets the arguments in a style similar to that of ActiveRecord constraints, and adds them to the query. If multiple constraints are supplied in a single hash (as in the third example), then the order in which they are applied to the query (and thus the codes they will receive) is not predictable. To determine the order use chained where clauses or use multiple hashes:

query.where({:length => {:le => 100}}, {:symbol => "eve*"})

Returns self to support method chaining



788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
# File 'lib/intermine/query.rb', line 788

def where(*wheres)
   if @views.empty?
       self.select('*')
   end
   wheres.each do |w|
     w.each do |k,v|
        if v.is_a?(Hash)
            parameters = {:path => k}
            v.each do |subk, subv|
                normalised_k = subk.to_s.upcase.gsub(/_/, " ")
                if subk == :with
                    parameters[:extra_value] = subv
                elsif subk == :sub_class
                    parameters[subk] = subv
                elsif subk == :code
                    parameters[:code] = subv
                elsif LoopConstraint.valid_ops.include?(normalised_k)
                    parameters[:op] = normalised_k
                    parameters[:loopPath] = subv
                else
                    if subv.nil?
                        if subk == "="
                            parameters[:op] = "IS NULL"
                        elsif subk == "!="
                            parameters[:op] = "IS NOT NULL"
                        else
                            parameters[:op] = normalised_k
                        end
                    elsif subv.is_a?(Range) or subv.is_a?(Array)
                        if subk == "="
                            parameters[:op] = "ONE OF"
                        elsif subk == "!="
                            parameters[:op] = "NONE OF"
                        else
                            parameters[:op] = normalised_k
                        end
                        parameters[:values] = subv.to_a
                    elsif subv.is_a?(Lists::List)
                        if subk == "="
                            parameters[:op] = "IN"
                        elsif subk == "!="
                            parameters[:op] = "NOT IN"
                        else
                            parameters[:op] = normalised_k
                        end
                        parameters[:value] = subv.name
                    else
                        parameters[:op] = normalised_k
                        parameters[:value] = subv
                    end
                end
            end
            add_constraint(parameters)
        elsif v.is_a?(Range) or v.is_a?(Array)
            add_constraint(k.to_s, 'ONE OF', v.to_a)
        elsif v.is_a?(InterMine::Metadata::ClassDescriptor)
            add_constraint(:path => k.to_s, :sub_class => v.name)
        elsif v.is_a?(InterMine::Lists::List)
            add_constraint(k.to_s, 'IN', v.name)
        elsif v.nil?
            add_constraint(k.to_s, "IS NULL")
        else
            if path(k.to_s).is_attribute?
                add_constraint(k.to_s, '=', v)
            else
                add_constraint(k.to_s, 'LOOKUP', v)
            end
        end
     end
   end
   return self
end