Sha256: b9c81786e708b806ef86b2bd1b85492674e19f603735a6156fa9339a27018707

Contents?: true

Size: 1002 Bytes

Versions: 1

Compression:

Stored size: 1002 Bytes

Contents

module Aggrobot
  module SqlFunctions

    extend self

    def sanitize(attr)
      "'#{attr}'"
    end

    def desc(attr)
      "#{attr} desc"
    end

    def count(attr = '*')
      "COUNT(#{attr})"
    end

    def unique_count(attr = '*')
      "COUNT(DISTINCT #{attr})"
    end

    def max(attr)
      "MAX(#{attr})"
    end

    def min(attr)
      "MIN(#{attr})"
    end

    def sum(attr = count)
      "SUM(#{attr})"
    end

    def avg(attr, rounding = ROUNDING_DIGITS)
      "ROUND(AVG(#{attr}), #{rounding})"
    end

    def group_collect(attr)
      "GROUP_CONCAT(DISTINCT #{attr})"
    end

    def percent(total, attr = count, rounding = ROUNDING_DIGITS)
      total == 0 ? "0" : "ROUND((#{attr}*100.0)/#{total}, #{rounding})"
    end

    def multiply(attr, multiplier, rounding = ROUNDING_DIGITS)
      "ROUND(#{attr}*#{multiplier}, #{rounding})"
    end

    def divide(attr, divider, rounding = ROUNDING_DIGITS)
      "ROUND(#{attr}/#{divider}, #{rounding})"
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aggrobot-0.0.2 lib/aggrobot/sql_functions.rb