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:
parent
cb6d13877c
commit
24000a51a7
9 changed files with 63 additions and 50 deletions
|
@ -20,6 +20,10 @@ module Arel
|
||||||
def maximum
|
def maximum
|
||||||
Nodes::Max.new [self], Nodes::SqlLiteral.new('max_id')
|
Nodes::Max.new [self], Nodes::SqlLiteral.new('max_id')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def average
|
||||||
|
Nodes::Avg.new [self], Nodes::SqlLiteral.new('avg_id')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class String < Attribute; end
|
class String < Attribute; end
|
||||||
|
|
|
@ -4,9 +4,11 @@ require 'arel/nodes/or'
|
||||||
require 'arel/nodes/and'
|
require 'arel/nodes/and'
|
||||||
|
|
||||||
require 'arel/nodes/in'
|
require 'arel/nodes/in'
|
||||||
|
require 'arel/nodes/function'
|
||||||
require 'arel/nodes/count'
|
require 'arel/nodes/count'
|
||||||
require 'arel/nodes/sum'
|
require 'arel/nodes/sum'
|
||||||
require 'arel/nodes/max'
|
require 'arel/nodes/max'
|
||||||
|
require 'arel/nodes/avg'
|
||||||
require 'arel/nodes/having'
|
require 'arel/nodes/having'
|
||||||
require 'arel/nodes/sql_literal'
|
require 'arel/nodes/sql_literal'
|
||||||
require 'arel/nodes/select_core'
|
require 'arel/nodes/select_core'
|
||||||
|
|
6
lib/arel/nodes/avg.rb
Normal file
6
lib/arel/nodes/avg.rb
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
module Arel
|
||||||
|
module Nodes
|
||||||
|
class Avg < Arel::Nodes::Function
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,22 +1,11 @@
|
||||||
module Arel
|
module Arel
|
||||||
module Nodes
|
module Nodes
|
||||||
class Count
|
class Count < Arel::Nodes::Function
|
||||||
attr_accessor :expressions, :distinct, :alias
|
attr_accessor :distinct
|
||||||
|
|
||||||
def initialize expr, distinct = false
|
def initialize expr, distinct = false, aliaz = nil
|
||||||
@expressions = expr
|
super(expr, aliaz)
|
||||||
@distinct = distinct
|
@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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
22
lib/arel/nodes/function.rb
Normal file
22
lib/arel/nodes/function.rb
Normal 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
|
|
@ -1,22 +1,6 @@
|
||||||
module Arel
|
module Arel
|
||||||
module Nodes
|
module Nodes
|
||||||
class Max
|
class Max < Arel::Nodes::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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,22 +1,6 @@
|
||||||
module Arel
|
module Arel
|
||||||
module Nodes
|
module Nodes
|
||||||
class Sum
|
class Sum < Arel::Nodes::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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -86,6 +86,11 @@ module Arel
|
||||||
visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
|
visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
|
||||||
end
|
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
|
def visit_Arel_Nodes_TableAlias o
|
||||||
"#{visit o.relation} #{quote_table_name o.name}"
|
"#{visit o.relation} #{quote_table_name o.name}"
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,6 +3,23 @@ require 'spec_helper'
|
||||||
module Arel
|
module Arel
|
||||||
module Attributes
|
module Attributes
|
||||||
describe 'attribute' do
|
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
|
describe '#maximum' do
|
||||||
it 'should create a MAX node' do
|
it 'should create a MAX node' do
|
||||||
relation = Table.new(:users)
|
relation = Table.new(:users)
|
||||||
|
|
Loading…
Reference in a new issue