1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

average node is added

This commit is contained in:
Aaron Patterson 2010-09-08 16:23:15 -07:00
parent cb6d13877c
commit 24000a51a7
9 changed files with 63 additions and 50 deletions

View file

@ -20,6 +20,10 @@ module Arel
def maximum
Nodes::Max.new [self], Nodes::SqlLiteral.new('max_id')
end
def average
Nodes::Avg.new [self], Nodes::SqlLiteral.new('avg_id')
end
end
class String < Attribute; end

View file

@ -4,9 +4,11 @@ require 'arel/nodes/or'
require 'arel/nodes/and'
require 'arel/nodes/in'
require 'arel/nodes/function'
require 'arel/nodes/count'
require 'arel/nodes/sum'
require 'arel/nodes/max'
require 'arel/nodes/avg'
require 'arel/nodes/having'
require 'arel/nodes/sql_literal'
require 'arel/nodes/select_core'

6
lib/arel/nodes/avg.rb Normal file
View file

@ -0,0 +1,6 @@
module Arel
module Nodes
class Avg < Arel::Nodes::Function
end
end
end

View file

@ -1,22 +1,11 @@
module Arel
module Nodes
class Count
attr_accessor :expressions, :distinct, :alias
class Count < Arel::Nodes::Function
attr_accessor :distinct
def initialize expr, distinct = false
@expressions = expr
@distinct = distinct
@alias = nil
end
def as aliaz
self.alias = SqlLiteral.new(aliaz)
self
end
def to_sql
viz = Visitors::ToSql.new Table.engine
viz.accept self
def initialize expr, distinct = false, aliaz = nil
super(expr, aliaz)
@distinct = distinct
end
end
end

View file

@ -0,0 +1,22 @@
module Arel
module Nodes
class Function
attr_accessor :expressions, :alias
def initialize expr, aliaz = nil
@expressions = expr
@alias = aliaz
end
def as aliaz
self.alias = SqlLiteral.new(aliaz)
self
end
def to_sql
viz = Visitors::ToSql.new Table.engine
viz.accept self
end
end
end
end

View file

@ -1,22 +1,6 @@
module Arel
module Nodes
class Max
attr_accessor :expressions, :alias
def initialize expr, aliaz = nil
@expressions = expr
@alias = aliaz
end
def as aliaz
self.alias = SqlLiteral.new(aliaz)
self
end
def to_sql
viz = Visitors::ToSql.new Table.engine
viz.accept self
end
class Max < Arel::Nodes::Function
end
end
end

View file

@ -1,22 +1,6 @@
module Arel
module Nodes
class Sum
attr_accessor :expressions, :alias
def initialize expr, aliaz = nil
@expressions = expr
@alias = aliaz
end
def as aliaz
self.alias = SqlLiteral.new(aliaz)
self
end
def to_sql
viz = Visitors::ToSql.new Table.engine
viz.accept self
end
class Sum < Arel::Nodes::Function
end
end
end

View file

@ -86,6 +86,11 @@ module Arel
visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
end
def visit_Arel_Nodes_Avg o
"AVG(#{o.expressions.map { |x|
visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
end
def visit_Arel_Nodes_TableAlias o
"#{visit o.relation} #{quote_table_name o.name}"
end

View file

@ -3,6 +3,23 @@ require 'spec_helper'
module Arel
module Attributes
describe 'attribute' do
describe '#average' do
it 'should create a AVG node' do
relation = Table.new(:users)
relation[:id].average.should be_kind_of Nodes::Avg
end
# FIXME: backwards compat. Is this really necessary?
it 'should set the alias to "avg_id"' do
relation = Table.new(:users)
mgr = relation.project relation[:id].average
mgr.to_sql.should be_like %{
SELECT AVG("users"."id") AS avg_id
FROM "users"
}
end
end
describe '#maximum' do
it 'should create a MAX node' do
relation = Table.new(:users)