query_methods.rb 7.9 KB
Newer Older
1
require 'active_support/core_ext/array/wrap'
2 3
require 'active_support/core_ext/object/blank'

4 5
module ActiveRecord
  module QueryMethods
6 7
    extend ActiveSupport::Concern

8
    attr_accessor :includes_values, :eager_load_values, :preload_values,
9
                  :select_values, :group_values, :order_values, :reorder_flag, :joins_values, :where_values, :having_values,
10
                  :limit_value, :offset_value, :lock_value, :readonly_value, :create_with_value, :from_value
11

12
    def includes(*args)
13 14
      args.reject! {|a| a.blank? }

A
Aaron Patterson 已提交
15 16
      return clone if args.empty?

17
      relation = clone
A
Aaron Patterson 已提交
18
      relation.includes_values = (relation.includes_values + args).flatten.uniq
19
      relation
20
    end
21

22
    def eager_load(*args)
23
      relation = clone
24
      relation.eager_load_values += args unless args.blank?
25
      relation
26 27 28
    end

    def preload(*args)
29
      relation = clone
30
      relation.preload_values += args unless args.blank?
31
      relation
32
    end
33

34
    def select(value = Proc.new)
35
      if block_given?
36
        to_a.select {|*block_args| value.call(*block_args) }
37
      else
38
        relation = clone
39
        relation.select_values += [value]
40
        relation
S
Santiago Pastorino 已提交
41
      end
42
    end
S
Santiago Pastorino 已提交
43

44
    def group(*args)
45
      relation = clone
46
      relation.group_values += args.flatten unless args.blank?
47
      relation
48
    end
49

50
    def order(*args)
51
      relation = clone
52
      relation.order_values += args.flatten unless args.blank?
53
      relation
54
    end
55

56
    def reorder(*args)
57
      relation = clone
58
      unless args.blank?
59 60 61
        relation.order_values = args
        relation.reorder_flag = true
      end
62
      relation
63 64
    end

65
    def joins(*args)
66 67
      relation = clone

A
Aaron Patterson 已提交
68 69
      args.flatten!
      relation.joins_values += args unless args.blank?
70 71

      relation
P
Pratik Naik 已提交
72 73
    end

74
    def where(opts, *rest)
75
      relation = clone
76
      relation.where_values += build_where(opts, rest) unless opts.blank?
77
      relation
78
    end
P
Pratik Naik 已提交
79

80
    def having(*args)
81
      relation = clone
82
      relation.having_values += build_where(*args) unless args.blank?
83
      relation
84 85
    end

86
    def limit(value)
87 88 89
      relation = clone
      relation.limit_value = value
      relation
90 91
    end

92
    def offset(value)
93 94 95
      relation = clone
      relation.offset_value = value
      relation
96 97 98
    end

    def lock(locks = true)
99 100
      relation = clone

101
      case locks
102
      when String, TrueClass, NilClass
103
        relation.lock_value = locks || true
104
      else
105
        relation.lock_value = false
106
      end
107 108

      relation
109 110
    end

111
    def readonly(value = true)
112 113 114
      relation = clone
      relation.readonly_value = value
      relation
115 116
    end

117
    def create_with(value)
118 119 120
      relation = clone
      relation.create_with_value = value
      relation
121 122
    end

123
    def from(value)
124 125 126
      relation = clone
      relation.from_value = value
      relation
127 128
    end

129 130
    def extending(*modules, &block)
      modules << Module.new(&block) if block_given?
131 132 133 134

      relation = clone
      relation.send(:apply_modules, *modules)
      relation
135 136
    end

137
    def reverse_order
138
      order_clause = arel.order_clauses.join(', ')
139 140
      relation = except(:order)

141 142 143 144
      order = order_clause.blank? ?
        "#{@klass.table_name}.#{@klass.primary_key} DESC" :
        reverse_sql_order(order_clause)

145
      relation.order(Arel::SqlLiteral.new(order))
146 147
    end

148 149
    def arel
      @arel ||= build_arel
150 151
    end

152 153
    def custom_join_sql(*joins)
      arel = table
154

155 156 157 158 159 160 161 162 163
      joins.each do |join|
        next if join.blank?

        @implicit_readonly = true

        case join
        when Hash, Array, Symbol
          if array_of_strings?(join)
            join_string = join.join(' ')
164
            arel = arel.join(Arel::SqlLiteral.new(join_string))
165
          end
166 167
        when String
          arel = arel.join(Arel::SqlLiteral.new(join))
168 169 170 171
        else
          arel = arel.join(join)
        end
      end
172

173 174 175
      arel.joins(arel)
    end

176 177
    def build_arel
      arel = table
178

179
      arel = build_joins(arel, @joins_values) unless @joins_values.empty?
180

181
      (@where_values - ['']).uniq.each do |where|
182
        case where
183 184
        when Arel::SqlLiteral
          arel = arel.where(where)
185
        else
186 187
          sql = where.is_a?(String) ? where : where.to_sql
          arel = arel.where(Arel::SqlLiteral.new("(#{sql})"))
188
        end
189 190
      end

191
      arel = arel.having(*@having_values.uniq.reject{|h| h.blank?}) unless @having_values.empty?
192

193
      arel = arel.take(@limit_value) if @limit_value
194
      arel = arel.skip(@offset_value) if @offset_value
195

196
      arel = arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty?
197

198
      arel = arel.order(*@order_values.uniq.reject{|o| o.blank?}) unless @order_values.empty?
199

200
      arel = build_select(arel, @select_values.uniq)
201

202
      arel = arel.from(@from_value) if @from_value
203
      arel = arel.lock(@lock_value) if @lock_value
204 205

      arel
206 207
    end

208
    def build_where(opts, other = [])
A
Aaron Patterson 已提交
209 210
      case opts
      when String, Array
211
        [@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))]
A
Aaron Patterson 已提交
212 213
      when Hash
        attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts)
214
        PredicateBuilder.new(table.engine).build_from_hash(attributes, table)
215
      else
216
        [opts]
217 218 219 220 221
      end
    end

    private

222 223 224 225 226 227 228 229 230
    def build_joins(relation, joins)
      association_joins = []

      joins = @joins_values.map {|j| j.respond_to?(:strip) ? j.strip : j}.uniq

      joins.each do |join|
        association_joins << join if [Hash, Array, Symbol].include?(join.class) && !array_of_strings?(join)
      end

231
      stashed_association_joins = joins.grep(ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation)
232

S
Santiago Pastorino 已提交
233
      non_association_joins = (joins - association_joins - stashed_association_joins)
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
      custom_joins = custom_join_sql(*non_association_joins)

      join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, custom_joins)

      join_dependency.graft(*stashed_association_joins)

      @implicit_readonly = true unless association_joins.empty? && stashed_association_joins.empty?

      to_join = []

      join_dependency.join_associations.each do |association|
        if (association_relation = association.relation).is_a?(Array)
          to_join << [association_relation.first, association.join_class, association.association_join.first]
          to_join << [association_relation.last, association.join_class, association.association_join.last]
        else
          to_join << [association_relation, association.join_class, association.association_join]
        end
      end

253 254
      to_join.uniq.each do |left, join_class, right|
        relation = relation.join(left, join_class).on(*right)
255 256 257 258 259
      end

      relation.join(custom_joins)
    end

260
    def build_select(arel, selects)
261
      unless selects.empty?
262
        @implicit_readonly = false
263 264
        # TODO: fix this ugly hack, we should refactor the callers to get an ARel compatible array.
        # Before this change we were passing to ARel the last element only, and ARel is capable of handling an array
A
Aaron Patterson 已提交
265 266 267 268 269
        case select = selects.last
        when Arel::Expression, Arel::SqlLiteral
          arel.project(select)
        when /^COUNT\(/
          arel.project(Arel::SqlLiteral.new(select))
270
        else
A
Aaron Patterson 已提交
271
          arel.project(*selects)
272 273
        end
      else
274
        arel.project(Arel::SqlLiteral.new(@klass.quoted_table_name + '.*'))
275 276 277
      end
    end

P
Pratik Naik 已提交
278
    def apply_modules(modules)
N
Neeraj Singh 已提交
279 280 281 282 283
      if modules.present?
        values = Array.wrap(modules)
        @extensions += values
        values.each {|extension| extend(extension) }
      end
P
Pratik Naik 已提交
284 285
    end

286
    def reverse_sql_order(order_query)
287
      order_query.split(',').each { |s|
288 289 290 291 292 293 294 295 296 297
        if s.match(/\s(asc|ASC)$/)
          s.gsub!(/\s(asc|ASC)$/, ' DESC')
        elsif s.match(/\s(desc|DESC)$/)
          s.gsub!(/\s(desc|DESC)$/, ' ASC')
        else
          s.concat(' DESC')
        end
      }.join(',')
    end

P
Pratik Naik 已提交
298 299 300 301
    def array_of_strings?(o)
      o.is_a?(Array) && o.all?{|obj| obj.is_a?(String)}
    end

302 303
  end
end