Module: Aggrobot::SqlFunctions
- Extended by:
- SqlFunctions
- Included in:
- Aggrobot, SqlFunctions
- Defined in:
- lib/aggrobot/sql_functions.rb
Instance Method Summary collapse
- #avg(attr, rounding = ROUNDING_DIGITS) ⇒ Object
- #count(attr = '*') ⇒ Object
- #desc(attr) ⇒ Object
- #divide(attr, divider, rounding = ROUNDING_DIGITS) ⇒ Object
- #group_collect(attr) ⇒ Object
- #max(attr) ⇒ Object
- #min(attr) ⇒ Object
- #multiply(attr, multiplier, rounding = ROUNDING_DIGITS) ⇒ Object
- #percent(total, attr = count, rounding = ROUNDING_DIGITS) ⇒ Object
- #sanitize(attr) ⇒ Object
- #sum(attr = count) ⇒ Object
- #unique_count(attr = '*') ⇒ Object
Instance Method Details
#avg(attr, rounding = ROUNDING_DIGITS) ⇒ Object
34 35 36 |
# File 'lib/aggrobot/sql_functions.rb', line 34 def avg(attr, rounding = ROUNDING_DIGITS) "ROUND(AVG(#{attr}), #{rounding})" end |
#count(attr = '*') ⇒ Object
14 15 16 |
# File 'lib/aggrobot/sql_functions.rb', line 14 def count(attr = '*') "COUNT(#{attr})" end |
#desc(attr) ⇒ Object
10 11 12 |
# File 'lib/aggrobot/sql_functions.rb', line 10 def desc(attr) "#{attr} desc" end |
#divide(attr, divider, rounding = ROUNDING_DIGITS) ⇒ Object
50 51 52 |
# File 'lib/aggrobot/sql_functions.rb', line 50 def divide(attr, divider, rounding = ROUNDING_DIGITS) "ROUND(#{attr}/#{divider}, #{rounding})" end |
#group_collect(attr) ⇒ Object
38 39 40 |
# File 'lib/aggrobot/sql_functions.rb', line 38 def group_collect(attr) "GROUP_CONCAT(DISTINCT #{attr})" end |
#max(attr) ⇒ Object
22 23 24 |
# File 'lib/aggrobot/sql_functions.rb', line 22 def max(attr) "MAX(#{attr})" end |
#min(attr) ⇒ Object
26 27 28 |
# File 'lib/aggrobot/sql_functions.rb', line 26 def min(attr) "MIN(#{attr})" end |
#multiply(attr, multiplier, rounding = ROUNDING_DIGITS) ⇒ Object
46 47 48 |
# File 'lib/aggrobot/sql_functions.rb', line 46 def multiply(attr, multiplier, rounding = ROUNDING_DIGITS) "ROUND(#{attr}*#{multiplier}, #{rounding})" end |
#percent(total, attr = count, rounding = ROUNDING_DIGITS) ⇒ Object
42 43 44 |
# File 'lib/aggrobot/sql_functions.rb', line 42 def percent(total, attr = count, rounding = ROUNDING_DIGITS) total == 0 ? "0" : "ROUND((#{attr}*100.0)/#{total}, #{rounding})" end |
#sanitize(attr) ⇒ Object
6 7 8 |
# File 'lib/aggrobot/sql_functions.rb', line 6 def sanitize(attr) "'#{attr}'" end |
#sum(attr = count) ⇒ Object
30 31 32 |
# File 'lib/aggrobot/sql_functions.rb', line 30 def sum(attr = count) "SUM(#{attr})" end |
#unique_count(attr = '*') ⇒ Object
18 19 20 |
# File 'lib/aggrobot/sql_functions.rb', line 18 def unique_count(attr = '*') "COUNT(DISTINCT #{attr})" end |