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 124
      return self if modules.empty?

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

130
    def reverse_order
N
Neeraj Singh 已提交
131
      order_clause = arel.order_clauses
132

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

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

140 141
    def arel
      @arel ||= build_arel
142 143
    end

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

147 148 149 150 151 152
      joins.each do |join|
        next if join.blank?

        @implicit_readonly = true

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

        arel.join(join)
160
      end
161

162 163 164
      arel.joins(arel)
    end

165 166
    def build_arel
      arel = table
167

168
      arel = build_joins(arel, @joins_values) unless @joins_values.empty?
169

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

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

177
      arel = arel.take(@limit_value) if @limit_value
178
      arel = arel.skip(@offset_value) if @offset_value
179

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

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

184
      arel = build_select(arel, @select_values.uniq)
185

186
      arel = arel.from(@from_value) if @from_value
187
      arel = arel.lock(@lock_value) if @lock_value
188 189

      arel
190 191
    end

A
Aaron Patterson 已提交
192 193
    private

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

206 207 208
    def build_joins(relation, joins)
      association_joins = []

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

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

215
      stashed_association_joins = joins.grep(ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation)
216

S
Santiago Pastorino 已提交
217
      non_association_joins = (joins - association_joins - stashed_association_joins)
218 219 220 221 222 223 224 225 226
      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|
227
        relation = association.join_to(relation)
228 229 230 231 232
      end

      relation.join(custom_joins)
    end

233
    def build_select(arel, selects)
234
      unless selects.empty?
235
        @implicit_readonly = false
236
        arel.project(*selects)
237
      else
238
        arel.project(Arel.sql(@klass.quoted_table_name + '.*'))
239 240 241
      end
    end

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

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

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

259 260
  end
end