lib/active_median/model.rb in active_median-0.2.1 vs lib/active_median/model.rb in active_median-0.2.2
- old
+ new
@@ -4,18 +4,35 @@
group_values = all.group_values
relation =
case connection.adapter_name
when /mysql/i
+ # assume mariadb by default
+ # use send as this method is private in Rails 4.2
+ mariadb = connection.send(:mariadb?) rescue true
+
+ if mariadb
+ if group_values.any?
+ over = "PARTITION BY #{group_values.join(", ")}"
+ end
+
+ select(*group_values, "PERCENTILE_CONT(0.50) WITHIN GROUP (ORDER BY #{column}) OVER (#{over})").unscope(:group)
+ else
+ # if mysql gets native function, check (and memoize) version first
+ select(*group_values, "PERCENTILE_CONT(#{column}, 0.50)")
+ end
+ when /sqlserver/i
if group_values.any?
over = "PARTITION BY #{group_values.join(", ")}"
end
select(*group_values, "PERCENTILE_CONT(0.50) WITHIN GROUP (ORDER BY #{column}) OVER (#{over})").unscope(:group)
when /sqlite/i
select(*group_values, "MEDIAN(#{column})")
- else
+ when /postg/i, /redshift/i # postgis too
select(*group_values, "PERCENTILE_CONT(0.50) WITHIN GROUP (ORDER BY #{column})")
+ else
+ raise "Connection adapter not supported: #{connection.adapter_name}"
end
result = connection.select_all(relation.to_sql)
# typecast