calculations.rb 15.3 KB
Newer Older
1
require 'active_support/core_ext/object/blank'
2
require 'active_support/core_ext/object/try'
3

4
module ActiveRecord
5
  module Calculations
P
Pratik Naik 已提交
6 7 8
    # Count operates using three different approaches.
    #
    # * Count all: By not passing any parameters to count, it will return a count of all the rows for the model.
9
    # * Count using column: By passing a column name to count, it will return a count of all the
N
Neeraj Singh 已提交
10
    #   rows for the model with supplied column present.
P
Pratik Naik 已提交
11 12 13 14
    # * Count using options will find the row count matched by the options used.
    #
    # The third approach, count using options, accepts an option hash as the only parameter. The options are:
    #
15
    # * <tt>:conditions</tt>: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ].
16
    #   See conditions in the intro to ActiveRecord::Base.
E
Emilio Tagua 已提交
17
    # * <tt>:joins</tt>: Either an SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id"
N
Neeraj Singh 已提交
18
    #   (rarely needed) or named associations in the same form used for the <tt>:include</tt> option, which will
E
Emilio Tagua 已提交
19
    #   perform an INNER JOIN on the associated table(s). If the value is a string, then the records
N
Neeraj Singh 已提交
20
    #   will be returned read-only since they will have attributes that do not correspond to the table's columns.
P
Pratik Naik 已提交
21
    #   Pass <tt>:readonly => false</tt> to override.
22 23
    # * <tt>:include</tt>: Named associations that should be loaded alongside using LEFT OUTER JOINs.
    #   The symbols named refer to already defined associations. When using named associations, count
24
    #   returns the number of DISTINCT items for the model you're counting.
P
Pratik Naik 已提交
25 26 27
    #   See eager loading under Associations.
    # * <tt>:order</tt>: An SQL fragment like "created_at DESC, name" (really only used with GROUP BY calculations).
    # * <tt>:group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.
28
    # * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you, for example,
N
Neeraj Singh 已提交
29
    #   want to do a join but not include the joined columns.
30
    # * <tt>:distinct</tt>: Set this to true to make this a distinct calculation, such as
31
    #   SELECT COUNT(DISTINCT posts.id) ...
32
    # * <tt>:from</tt> - By default, this is the table name of the class, but can be changed to an
33
    #   alternate table name (or even the name of a database view).
P
Pratik Naik 已提交
34 35 36 37 38 39 40 41 42
    #
    # Examples for counting all:
    #   Person.count         # returns the total count of all people
    #
    # Examples for counting by column:
    #   Person.count(:age)  # returns the total count of all people whose age is present in database
    #
    # Examples for count with options:
    #   Person.count(:conditions => "age > 26")
43 44
    #
    #   # because of the named association, it finds the DISTINCT count using LEFT OUTER JOIN.
45
    #   Person.count(:conditions => "age > 26 AND job.salary > 60000", :include => :job)
46 47
    #
    #   # finds the number of rows matching the conditions and joins.
48 49
    #   Person.count(:conditions => "age > 26 AND job.salary > 60000",
    #                :joins => "LEFT JOIN jobs on jobs.person_id = person.id")
50
    #
P
Pratik Naik 已提交
51 52 53
    #   Person.count('id', :conditions => "age > 26") # Performs a COUNT(id)
    #   Person.count(:all, :conditions => "age > 26") # Performs a COUNT(*) (:all is an alias for '*')
    #
54
    # Note: <tt>Person.count(:all)</tt> will not work because it will use <tt>:all</tt> as the condition.
55
    # Use Person.count instead.
P
Pratik Naik 已提交
56 57
    def count(column_name = nil, options = {})
      column_name, options = nil, column_name if column_name.is_a?(Hash)
58
      calculate(:count, column_name, options)
59 60
    end

61 62
    # Calculates the average value on a given column. Returns +nil+ if there's
    # no row. See +calculate+ for examples with options.
63 64 65
    #
    #   Person.average('age') # => 35.8
    def average(column_name, options = {})
66
      calculate(:average, column_name, options)
67 68
    end

69
    # Calculates the minimum value on a given column. The value is returned
70 71 72 73 74
    # with the same data type of the column, or +nil+ if there's no row. See
    # +calculate+ for examples with options.
    #
    #   Person.minimum('age') # => 7
    def minimum(column_name, options = {})
75
      calculate(:minimum, column_name, options)
76 77
    end

78 79 80 81 82 83
    # Calculates the maximum value on a given column. The value is returned
    # with the same data type of the column, or +nil+ if there's no row. See
    # +calculate+ for examples with options.
    #
    #   Person.maximum('age') # => 93
    def maximum(column_name, options = {})
84
      calculate(:maximum, column_name, options)
85 86
    end

87 88 89 90 91
    # Calculates the sum of values on a given column. The value is returned
    # with the same data type of the column, 0 if there's no row. See
    # +calculate+ for examples with options.
    #
    #   Person.sum('age') # => 4562
92 93 94 95 96 97
    def sum(*args)
      if block_given?
        self.to_a.sum(*args) {|*block_args| yield(*block_args)}
      else
        calculate(:sum, *args)
      end
98 99
    end

100
    # This calculates aggregate values in the given column. Methods for count, sum, average,
101
    # minimum, and maximum have been added as shortcuts. Options such as <tt>:conditions</tt>,
102
    # <tt>:order</tt>, <tt>:group</tt>, <tt>:having</tt>, and <tt>:joins</tt> can be passed to customize the query.
103 104
    #
    # There are two basic forms of output:
105
    #   * Single aggregate value: The single value is type cast to Fixnum for COUNT, Float
106
    #     for AVG, and the given column's type for everything else.
107
    #   * Grouped values: This returns an ordered hash of the values and groups them by the
108
    #     <tt>:group</tt> option. It takes either a column name, or the name of a belongs_to association.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    #
    #       values = Person.maximum(:age, :group => 'last_name')
    #       puts values["Drake"]
    #       => 43
    #
    #       drake  = Family.find_by_last_name('Drake')
    #       values = Person.maximum(:age, :group => :family) # Person belongs_to :family
    #       puts values[drake]
    #       => 43
    #
    #       values.each do |family, max_age|
    #       ...
    #       end
    #
    # Options:
124
    # * <tt>:conditions</tt> - An SQL fragment like "administrator = 1" or [ "user_name = ?", username ].
125
    #   See conditions in the intro to ActiveRecord::Base.
126
    # * <tt>:include</tt>: Eager loading, see Associations for details. Since calculations don't load anything,
127
    #   the purpose of this is to access fields on joined tables in your conditions, order, or group clauses.
128
    # * <tt>:joins</tt> - An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id".
129
    #   (Rarely needed).
130
    #   The records will be returned read-only since they will have attributes that do not correspond to the
131
    #   table's columns.
132 133
    # * <tt>:order</tt> - An SQL fragment like "created_at DESC, name" (really only used with GROUP BY calculations).
    # * <tt>:group</tt> - An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.
134
    # * <tt>:select</tt> - By default, this is * as in SELECT * FROM, but can be changed if you for example
135
    #   want to do a join, but not include the joined columns.
136
    # * <tt>:distinct</tt> - Set this to true to make this a distinct calculation, such as
137
    #   SELECT COUNT(DISTINCT posts.id) ...
138 139 140 141
    #
    # Examples:
    #   Person.calculate(:count, :all) # The same as Person.count
    #   Person.average(:age) # SELECT AVG(age) FROM people...
142
    #   Person.minimum(:age, :conditions => ['last_name != ?', 'Drake']) # Selects the minimum age for
143 144 145
    #                                                                    # everyone with a last name other than 'Drake'
    #
    #   # Selects the minimum age for any family without any minors
146
    #   Person.minimum(:age, :having => 'min(age) > 17', :group => :last_name)
147
    #
148
    #   Person.sum("2 * age")
149
    def calculate(operation, column_name, options = {})
150 151 152
      if options.except(:distinct).present?
        apply_finder_options(options.except(:distinct)).calculate(operation, column_name, :distinct => options[:distinct])
      else
153 154 155 156 157 158 159 160
        relation = with_default_scope

        if relation.equal?(self)
          if eager_loading? || (includes_values.present? && references_eager_loaded_tables?)
            construct_relation_for_association_calculations.calculate(operation, column_name, options)
          else
            perform_calculation(operation, column_name, options)
          end
161
        else
162
          relation.calculate(operation, column_name, options)
163 164 165 166 167 168
        end
      end
    rescue ThrowResult
      0
    end

169 170
    # This method is designed to perform select by a single column as direct SQL query
    # Returns <tt>Array</tt> with values of the specified column name
171
    # The values has same data type as column.
172 173 174 175 176
    #
    # Examples:
    #
    #   Person.pluck(:id) # SELECT people.id FROM people
    #   Person.uniq.pluck(:role) # SELECT DISTINCT role FROM people
177
    #   Person.where(:age => 21).limit(5).pluck(:id) # SELECT people.id FROM people WHERE people.age = 21 LIMIT 5
178 179
    #
    def pluck(column_name)
180 181
      key = column_name.to_s.split('.', 2).last

182 183 184
      if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
        column_name = "#{table_name}.#{column_name}"
      end
185

186
      result = klass.connection.select_all(select(column_name).arel, nil, bind_values)
187 188 189 190 191 192 193 194 195 196
      types  = result.column_types.merge klass.column_types
      column = types[key]

      result.map do |attributes|
        value = klass.initialize_attributes(attributes)[key]
        if column
          column.type_cast value
        else
          value
        end
197 198 199
      end
    end

200 201 202
    private

    def perform_calculation(operation, column_name, options = {})
203 204
      operation = operation.to_s.downcase

205
      distinct = options[:distinct]
206

207
      if operation == "count"
208 209
        column_name ||= (select_for_count || :all)

210
        unless arel.ast.grep(Arel::Nodes::OuterJoin).empty?
211 212
          distinct = true
        end
213

214 215
        column_name = primary_key if column_name == :all && distinct

216
        distinct = nil if column_name =~ /\s*DISTINCT\s+/i
217 218
      end

219
      if @group_values.any?
220
        execute_grouped_calculation(operation, column_name, distinct)
221
      else
N
Neeraj Singh 已提交
222
        execute_simple_calculation(operation, column_name, distinct)
223
      end
P
Pratik Naik 已提交
224 225
    end

226
    def aggregate_column(column_name)
227
      if @klass.column_names.include?(column_name.to_s)
228
        Arel::Attribute.new(@klass.unscoped.table, column_name)
229
      else
230
        Arel.sql(column_name == :all ? "*" : column_name.to_s)
231
      end
232 233
    end

234 235 236 237
    def operation_over_aggregate_column(column, operation, distinct)
      operation == 'count' ? column.count(distinct) : column.send(operation)
    end

238
    def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
239
      # Postgresql doesn't like ORDER BY when there are no GROUP BY
240
      relation = reorder(nil)
241

242 243 244
      if operation == "count" && (relation.limit_value || relation.offset_value)
        # Shortcut when limit is zero.
        return 0 if relation.limit_value == 0
245

246 247 248
        query_builder = build_count_subquery(relation, column_name, distinct)
      else
        column = aggregate_column(column_name)
249

250
        select_value = operation_over_aggregate_column(column, operation, distinct)
251

252 253 254
        relation.select_values = [select_value]

        query_builder = relation.arel
255 256
      end

257 258
      result = @klass.connection.select_value(query_builder, nil, relation.bind_values)
      type_cast_calculated_value(result, column_for(column_name), operation)
259 260
    end

261
    def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
262 263 264
      group_attr      = @group_values
      association     = @klass.reflect_on_association(group_attr.first.to_sym)
      associated      = group_attr.size == 1 && association && association.macro == :belongs_to # only count belongs_to associations
265
      group_fields  = Array(associated ? association.foreign_key : group_attr)
A
Aaron Patterson 已提交
266 267 268 269
      group_aliases = group_fields.map { |field| column_alias_for(field) }
      group_columns = group_aliases.zip(group_fields).map { |aliaz,field|
        [aliaz, column_for(field)]
      }
270

271
      group = @klass.connection.adapter_name == 'FrontBase' ? group_aliases : group_fields
272

273 274
      if operation == 'count' && column_name == :all
        aggregate_alias = 'count_all'
275
      else
276
        aggregate_alias = column_alias_for(operation, column_name)
277 278
      end

A
Aaron Patterson 已提交
279 280 281 282 283 284
      select_values = [
        operation_over_aggregate_column(
          aggregate_column(column_name),
          operation,
          distinct).as(aggregate_alias)
      ]
285
      select_values += @select_values unless @having_values.empty?
A
Aaron Patterson 已提交
286 287 288 289 290

      select_values.concat group_fields.zip(group_aliases).map { |field,aliaz|
        "#{field} AS #{aliaz}"
      }

291
      relation = except(:group).group(group.join(','))
A
Aaron Patterson 已提交
292
      relation.select_values = select_values
293

294
      calculated_data = @klass.connection.select_all(relation, nil, bind_values)
295 296

      if association
297
        key_ids     = calculated_data.collect { |row| row[group_aliases.first] }
298
        key_records = association.klass.base_class.find(key_ids)
299
        key_records = Hash[key_records.map { |r| [r.id, r] }]
300 301
      end

R
Raghunadh 已提交
302
      Hash[calculated_data.map do |row|
A
Aaron Patterson 已提交
303 304 305
        key   = group_columns.map { |aliaz, column|
          type_cast_calculated_value(row[aliaz], column)
        }
306
        key   = key.first if key.size == 1
E
Emilio Tagua 已提交
307 308 309
        key = key_records[key] if associated
        [key, type_cast_calculated_value(row[aggregate_alias], column_for(column_name), operation)]
      end]
310
    end
311

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    # Converts the given keys to the value that the database adapter returns as
    # a usable column name:
    #
    #   column_alias_for("users.id")                 # => "users_id"
    #   column_alias_for("sum(id)")                  # => "sum_id"
    #   column_alias_for("count(distinct users.id)") # => "count_distinct_users_id"
    #   column_alias_for("count(*)")                 # => "count_all"
    #   column_alias_for("count", "id")              # => "count_id"
    def column_alias_for(*keys)
      table_name = keys.join(' ')
      table_name.downcase!
      table_name.gsub!(/\*/, 'all')
      table_name.gsub!(/\W+/, ' ')
      table_name.strip!
      table_name.gsub!(/ +/, '_')

      @klass.connection.table_alias_for(table_name)
    end

    def column_for(field)
      field_name = field.to_s.split('.').last
      @klass.columns.detect { |c| c.name.to_s == field_name }
    end

    def type_cast_calculated_value(value, column, operation = nil)
337 338 339
      case operation
        when 'count'   then value.to_i
        when 'sum'     then type_cast_using_column(value || '0', column)
340
        when 'average' then value.respond_to?(:to_d) ? value.to_d : value
341
        else type_cast_using_column(value, column)
342 343 344 345 346 347 348
      end
    end

    def type_cast_using_column(value, column)
      column ? column.type_cast(value) : value
    end

P
Pratik Naik 已提交
349 350
    def select_for_count
      if @select_values.present?
351
        select = @select_values.join(", ")
352
        select if select !~ /[,*]/
P
Pratik Naik 已提交
353 354
      end
    end
355 356

    def build_count_subquery(relation, column_name, distinct)
357 358 359 360 361 362 363 364 365 366
      column_alias = Arel.sql('count_column')
      subquery_alias = Arel.sql('subquery_for_count')

      aliased_column = aggregate_column(column_name == :all ? 1 : column_name).as(column_alias)
      relation.select_values = [aliased_column]
      subquery = relation.arel.as(subquery_alias)

      sm = Arel::SelectManager.new relation.engine
      select_value = operation_over_aggregate_column(column_alias, 'count', distinct)
      sm.project(select_value).from(subquery)
367
    end
368 369
  end
end