Sha256: ac69058b19b7527dad01e15d5be4b8178afd56e5e133d46c706e8fd09966041f

Contents?: true

Size: 1.73 KB

Versions: 13

Compression:

Stored size: 1.73 KB

Contents

module CompositePrimaryKeys
  module ActiveRecord
    module Calculations
      def execute_simple_calculation(operation, column_name, distinct)
        # Postgresql doesn't like ORDER BY when there are no GROUP BY
        relation = reorder(nil)

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

          query_builder = build_count_subquery(relation, column_name, distinct)
        else
          column = aggregate_column(column_name)

          select_value = operation_over_aggregate_column(column, operation, distinct)

          relation.select_values = [select_value]

          query_builder = relation.arel
        end

        type_cast_calculated_value(@klass.connection.select_value(query_builder.to_sql), column_for(column_name), operation)
      end

      def build_count_subquery(relation, column_name, distinct)
        # CPK
        # column_alias = Arel.sql('count_column')
        subquery_alias = Arel.sql('subquery_for_count')

        # CPK
        # aliased_column = aggregate_column(column_name == :all ? 1 : column_name).as(column_alias)
        # relation.select_values = [aliased_column]
        column = aggregate_column(column_name)
        relation.select_values = ["DISTINCT #{column.to_s}"]
        subquery = relation.arel.as(subquery_alias)

        sm = Arel::SelectManager.new relation.engine
        # CPK
        # select_value = operation_over_aggregate_column(column_alias, 'count', distinct)
        select_value = operation_over_aggregate_column(Arel.sql("*"), 'count', false)
        sm.project(select_value).from(subquery)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
composite_primary_keys-5.0.0.rc1 lib/composite_primary_keys/relation/calculations.rb
composite_primary_keys-4.1.2 lib/composite_primary_keys/relation/calculations.rb
composite_primary_keys-4.1.1 lib/composite_primary_keys/relation/calculations.rb
composite_primary_keys-4.0.0 lib/composite_primary_keys/relation/calculations.rb
composite_primary_keys-4.0.0.beta9 lib/composite_primary_keys/relation/calculations.rb
composite_primary_keys-4.0.0.beta8 lib/composite_primary_keys/relation/calculations.rb
composite_primary_keys-4.0.0.beta7 lib/composite_primary_keys/relation/calculations.rb
composite_primary_keys-4.0.0.beta6 lib/composite_primary_keys/relation/calculations.rb
composite_primary_keys-4.0.0.beta5 lib/composite_primary_keys/relation/calculations.rb
composite_primary_keys-4.0.0.beta4 lib/composite_primary_keys/relation/calculations.rb
composite_primary_keys-4.0.0.beta3 lib/composite_primary_keys/relation/calculations.rb
composite_primary_keys-4.0.0.beta2 lib/composite_primary_keys/relation/calculations.rb
composite_primary_keys-4.0.0.beta1 lib/composite_primary_keys/relation/calculations.rb