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

orders are working

This commit is contained in:
Aaron Patterson 2010-08-22 18:13:39 -07:00
parent 3283adc6d0
commit a3b8ef8375
7 changed files with 49 additions and 11 deletions

View file

@ -2,11 +2,12 @@ module Arel
module Nodes
class SelectStatement
attr_reader :cores
attr_accessor :limit
attr_accessor :limit, :orders
def initialize cores = [SelectCore.new]
@cores = cores
@limit = nil
@cores = cores
@orders = []
@limit = nil
end
def initialize_copy other

View file

@ -28,6 +28,11 @@ module Arel
self
end
def order *expr
@head.orders.concat expr
self
end
def wheres
Compatibility::Wheres.new @engine, @ctx.wheres
end

View file

@ -45,6 +45,7 @@ module Arel
def visit_Arel_Nodes_SelectStatement o
[
o.cores.map { |x| visit x }.join,
("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?),
("LIMIT #{o.limit}" if o.limit)
].compact.join ' '
end

View file

@ -15,8 +15,8 @@ describe Arel::Nodes::InsertStatement do
end
dolly = statement.clone
dolly.columns.should == %w[a0 b1 c2]
dolly.values.should == %w[x0 y1 z2]
check dolly.columns.should == %w[a0 b1 c2]
check dolly.values.should == %w[x0 y1 z2]
end
end
end

View file

@ -13,11 +13,11 @@ describe Arel::Nodes::SelectCore do
o.should_receive(:clone).and_return("#{o}#{j}")
end
end
dolly = core.clone
dolly.froms.should == %w[a0 b1 c2]
dolly.projections.should == %w[d0 e1 f2]
dolly.wheres.should == %w[g0 h1 i2]
check dolly.froms.should == %w[a0 b1 c2]
check dolly.projections.should == %w[d0 e1 f2]
check dolly.wheres.should == %w[g0 h1 i2]
end
end
end

View file

@ -15,8 +15,8 @@ describe Arel::Nodes::UpdateStatement do
end
dolly = statement.clone
dolly.wheres.should == %w[a0 b1 c2]
dolly.values.should == %w[x0 y1 z2]
check dolly.wheres.should == %w[a0 b1 c2]
check dolly.values.should == %w[x0 y1 z2]
end
end
end

View file

@ -23,6 +23,37 @@ module Arel
end
describe 'select manager' do
describe 'order' do
it 'generates order clauses' do
table = Table.new :users
manager = Arel::SelectManager.new Table.engine
manager.project SqlLiteral.new '*'
manager.from table
manager.order table[:id]
manager.to_sql.should be_like %{
SELECT * FROM "users" ORDER BY "users"."id"
}
end
# FIXME: I would like to deprecate this
it 'takes *args' do
table = Table.new :users
manager = Arel::SelectManager.new Table.engine
manager.project SqlLiteral.new '*'
manager.from table
manager.order table[:id], table[:name]
manager.to_sql.should be_like %{
SELECT * FROM "users" ORDER BY "users"."id", "users"."name"
}
end
it 'chains' do
table = Table.new :users
manager = Arel::SelectManager.new Table.engine
check manager.order(table[:id]).should == manager
end
end
describe 'joins' do
it 'returns join sql' do
table = Table.new :users