Method: Og::SqlStore#aggregate
- Defined in:
- lib/og/store/sql.rb
#aggregate(term = 'COUNT(*)', options = {}) ⇒ Object Also known as: calculate
Perform an aggregation or calculation over query results. This low level method is used by the Entity calculation / aggregation methods.
Options
:field = the return type.
Example
calculate ‘COUNT(*)’ calculate ‘MIN(age)’ calculate ‘SUM(age)’, :group => :name
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/og/store/sql.rb', line 280 def aggregate(term = 'COUNT(*)', = {}) # Leave this .dup here, causes problems because options are changed = .dup klass = [:class] field = [:field] # Rename search term, SQL92 but _not_ SQL89 compatible .update(:select => "#{term} AS #{term[/^\w+/]}") unless [:group] || [:group_by] .delete(:order) .delete(:order_by) end sql = (klass, ) if anno = klass.ann[field] return_type = anno.class end return_type ||= Integer if [:group] || [:group_by] # This is an aggregation, so return the calculated values # as an array. values = [] res = query(sql) res.each_row do |row, idx| values << type_cast(return_type, row[0]) end return values else return type_cast(return_type, query(sql).first_value) end end |