Module: CalculateAll::Helpers

Defined in:
lib/calculate-all/helpers.rb

Class Method Summary collapse

Class Method Details

.decode_function_aliases(aliases) ⇒ Object

Method to convert function aliases like :count to SQL commands like ‘COUNT(*)’



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/calculate-all/helpers.rb', line 5

def decode_function_aliases(aliases)
  aliases.map do |key|
    function =
      case key
      when String
        key
      when :count
        'COUNT(*)'
      when /^(.*)_distinct_count$/, /^count_distinct_(.*)$/
        "COUNT(DISTINCT #{$1})"
      when /^(.*)_(count|sum|max|min|avg)$/
        "#{$2.upcase}(#{$1})"
      when /^(count|sum|max|min|avg)_(.*)$$/
        "#{$1.upcase}(#{$2})"
      when /^(.*)_average$/, /^average_(.*)$/
        "AVG(#{$1})"
      when /^(.*)_maximum$/, /^maximum_(.*)$/
        "MAX(#{$1})"
      when /^(.*)_minimum$/, /^minimum_(.*)$/
        "MIN(#{$1})"
      else
        raise ArgumentError, "Can't recognize function alias #{key}"
      end
    [key, function]
  end.to_h
end