query_methods.rb 6.4 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, :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 += Array.wrap(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 joins(*args)
57 58
      relation = clone

A
Aaron Patterson 已提交
59 60
      args.flatten!
      relation.joins_values += args unless args.blank?
61 62

      relation
P
Pratik Naik 已提交
63 64
    end

65
    def where(opts, *rest)
66
      relation = clone
67
      relation.where_values += build_where(opts, rest) unless opts.blank?
68
      relation
69
    end
P
Pratik Naik 已提交
70

71
    def having(*args)
72
      relation = clone
73
      relation.having_values += build_where(*args) unless args.blank?
74
      relation
75 76
    end

77
    def limit(value)
78 79 80
      relation = clone
      relation.limit_value = value
      relation
81 82
    end

83
    def offset(value)
84 85 86
      relation = clone
      relation.offset_value = value
      relation
87 88 89
    end

    def lock(locks = true)
90 91
      relation = clone

92
      case locks
93
      when String, TrueClass, NilClass
94
        relation.lock_value = locks || true
95
      else
96
        relation.lock_value = false
97
      end
98 99

      relation
100 101
    end

102
    def readonly(value = true)
103 104 105
      relation = clone
      relation.readonly_value = value
      relation
106 107
    end

108
    def create_with(value)
109 110 111
      relation = clone
      relation.create_with_value = value
      relation
112 113
    end

114
    def from(value)
115 116 117
      relation = clone
      relation.from_value = value
      relation
118 119
    end

120 121
    def extending(*modules)
      modules << Module.new(&Proc.new) if block_given?
122 123

      relation = clone
124
      relation.send(:apply_modules, modules.flatten)
125
      relation
126 127
    end

128
    def reverse_order
N
Neeraj Singh 已提交
129
      order_clause = arel.order_clauses
130

N
Neeraj Singh 已提交
131
      order = order_clause.empty? ?
132
        "#{@klass.table_name}.#{@klass.primary_key} DESC" :
N
Neeraj Singh 已提交
133
        reverse_sql_order(order_clause).join(', ')
134

N
Neeraj Singh 已提交
135
      except(:order).order(Arel::SqlLiteral.new(order))
136 137
    end

138 139
    def arel
      @arel ||= build_arel
140 141
    end

142
    def custom_join_sql(*joins)
A
Aaron Patterson 已提交
143
      arel = table.select_manager
144

145 146 147 148 149 150
      joins.each do |join|
        next if join.blank?

        @implicit_readonly = true

        case join
151
        when Array
152
          join = Arel.sql(join.join(' ')) if array_of_strings?(join)
153
        when String
154
          join = Arel.sql(join)
155
        end
A
Aaron Patterson 已提交
156 157

        arel.join(join)
158
      end
159

160 161 162
      arel.joins(arel)
    end

163 164
    def build_arel
      arel = table
165

166
      arel = build_joins(arel, @joins_values) unless @joins_values.empty?
167

168
      (@where_values - ['']).uniq.each do |where|
169 170
        where = Arel.sql(where) if String === where
        arel = arel.where(Arel::Nodes::Grouping.new(where))
171 172
      end

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

175
      arel = arel.take(@limit_value) if @limit_value
176
      arel = arel.skip(@offset_value) if @offset_value
177

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

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

182
      arel = build_select(arel, @select_values.uniq)
183

184
      arel = arel.from(@from_value) if @from_value
185
      arel = arel.lock(@lock_value) if @lock_value
186 187

      arel
188 189
    end

A
Aaron Patterson 已提交
190 191
    private

192
    def build_where(opts, other = [])
A
Aaron Patterson 已提交
193 194
      case opts
      when String, Array
195
        [@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))]
A
Aaron Patterson 已提交
196 197
      when Hash
        attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts)
198
        PredicateBuilder.build_from_hash(table.engine, attributes, table)
199
      else
200
        [opts]
201 202 203
      end
    end

204 205 206
    def build_joins(relation, joins)
      association_joins = []

207
      joins = joins.map {|j| j.respond_to?(:strip) ? j.strip : j}.uniq
208 209 210 211 212

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

213
      stashed_association_joins = joins.grep(ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation)
214

S
Santiago Pastorino 已提交
215
      non_association_joins = (joins - association_joins - stashed_association_joins)
216 217 218 219 220 221 222 223 224
      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?

      join_dependency.join_associations.each do |association|
225
        relation = association.join_to(relation)
226 227 228 229 230
      end

      relation.join(custom_joins)
    end

231
    def build_select(arel, selects)
232
      unless selects.empty?
233
        @implicit_readonly = false
234
        arel.project(*selects)
235
      else
236
        arel.project(Arel::SqlLiteral.new(@klass.quoted_table_name + '.*'))
237 238 239
      end
    end

P
Pratik Naik 已提交
240
    def apply_modules(modules)
241
      unless modules.empty?
242
        @extensions += modules
243
        modules.each {|extension| extend(extension) }
N
Neeraj Singh 已提交
244
      end
P
Pratik Naik 已提交
245 246
    end

247
    def reverse_sql_order(order_query)
248 249 250
      order_query.join(', ').split(',').collect do |s|
        s.gsub!(/\sasc\Z/i, ' DESC') || s.gsub!(/\sdesc\Z/i, ' ASC') || s.concat(' DESC')
      end
251 252
    end

P
Pratik Naik 已提交
253 254 255 256
    def array_of_strings?(o)
      o.is_a?(Array) && o.all?{|obj| obj.is_a?(String)}
    end

257 258
  end
end