Sha256: 4feda9bd08b225549dfdcd1f92a862b7d098f25b3274eefad851a8dfa5cbcd2d

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

module ActiveRecord
  module Calculations
    def execute_simple_calculation(operation, column_name, distinct)
      # CPK changes
      if column_name.kind_of?(Array)
        columns = column_name.map do |primary_key_column|
          table[primary_key_column].to_sql
        end
        projection = "DISTINCT #{columns.join(',')}"
        subquery = "(#{table.project(projection).to_sql}) AS subquery"
        relation = Arel::Table.new(subquery).project(Arel::SqlLiteral.new('*').count)
        type_cast_calculated_value(@klass.connection.select_value(relation.to_sql),
                                   column_for(column_name.first), operation)
      else
        column = if @klass.column_names.include?(column_name.to_s)
          Arel::Attribute.new(@klass.unscoped, column_name)
        else
          Arel::SqlLiteral.new(column_name == :all ? "*" : column_name.to_s)
        end

        # Postgresql doesn't like ORDER BY when there are no GROUP BY
        relation = except(:order).select(operation == 'count' ? column.count(distinct) : column.send(operation))
        type_cast_calculated_value(@klass.connection.select_value(relation.to_sql), column_for(column_name), operation)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
composite_primary_keys-3.0.0.b3 lib/composite_primary_keys/calculations.rb
composite_primary_keys-3.0.0.b2 lib/composite_primary_keys/calculations.rb