提交 7b122f9a 编写于 作者: A Aaron Patterson

adding maximum nodes

上级 301dfa5a
......@@ -16,6 +16,10 @@ def count distinct = false
def sum
Nodes::Sum.new [self], Nodes::SqlLiteral.new('sum_id')
end
def maximum
Nodes::Max.new [self], Nodes::SqlLiteral.new('max_id')
end
end
class String < Attribute; end
......
......@@ -6,6 +6,7 @@
require 'arel/nodes/in'
require 'arel/nodes/count'
require 'arel/nodes/sum'
require 'arel/nodes/max'
require 'arel/nodes/sql_literal'
require 'arel/nodes/select_core'
require 'arel/nodes/select_statement'
......
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
end
end
end
......@@ -76,6 +76,11 @@ def visit_Arel_Nodes_Sum o
visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
end
def visit_Arel_Nodes_Max o
"MAX(#{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
......
......@@ -3,6 +3,23 @@
module Arel
module Attributes
describe 'attribute' do
describe '#maximum' do
it 'should create a MAX node' do
relation = Table.new(:users)
relation[:id].maximum.should be_kind_of Nodes::Max
end
# FIXME: backwards compat. Is this really necessary?
it 'should set the alias to "max_id"' do
relation = Table.new(:users)
mgr = relation.project relation[:id].maximum
mgr.to_sql.should be_like %{
SELECT MAX("users"."id") AS max_id
FROM "users"
}
end
end
describe '#sum' do
it 'should create a SUM node' do
relation = Table.new(:users)
......
require 'spec_helper'
describe Arel::Nodes::Sum do
describe "as" do
it 'should alias the sum' do
table = Arel::Table.new :users
table[:id].sum.as('foo').to_sql.should be_like %{
SUM("users"."id") AS foo
}
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册