aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/where_clause_factory.rb
blob: 156088f53460f38497cf840e46ad7c8123205d1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true
module ActiveRecord
  class Relation
    class WhereClauseFactory # :nodoc:
      def initialize(klass, predicate_builder)
        @klass = klass
        @predicate_builder = predicate_builder
      end

      def build(opts, other)
        case opts
        when String, Array
          parts = [klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))]
        when Hash
          attributes = predicate_builder.resolve_column_aliases(opts)
          attributes = klass.send(:expand_hash_conditions_for_aggregates, attributes)
          attributes.stringify_keys!

          if perform_case_sensitive?(options = other.last)
            parts, binds = build_for_case_sensitive(attributes, options)
          else
            attributes, binds = predicate_builder.create_binds(attributes)
            parts = predicate_builder.build_from_hash(attributes)
          end
        when Arel::Nodes::Node
          parts = [opts]
        else
          raise ArgumentError, "Unsupported argument type: #{opts} (#{opts.class})"
        end

        WhereClause.new(parts, binds || [])
      end

      # TODO Change this to private once we've dropped Ruby 2.2 support.
      # Workaround for Ruby 2.2 "private attribute?" warning.
      protected

        attr_reader :klass, :predicate_builder

      private

        def perform_case_sensitive?(options)
          options && options.key?(:case_sensitive)
        end

        def build_for_case_sensitive(attributes, options)
          parts, binds = [], []
          table = klass.arel_table

          attributes.each do |attribute, value|
            if reflection = klass._reflect_on_association(attribute)
              attribute = reflection.foreign_key.to_s
              value = value[reflection.klass.primary_key] unless value.nil?
            end

            if value.nil?
              parts << table[attribute].eq(value)
            else
              column = klass.column_for_attribute(attribute)

              binds << predicate_builder.send(:build_bind_attribute, attribute, value)
              value = Arel::Nodes::BindParam.new

              predicate = if options[:case_sensitive]
                klass.connection.case_sensitive_comparison(table, attribute, column, value)
              else
                klass.connection.case_insensitive_comparison(table, attribute, column, value)
              end

              parts << predicate
            end
          end

          [parts, binds]
        end
    end
  end
end