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:
parent
3283adc6d0
commit
a3b8ef8375
7 changed files with 49 additions and 11 deletions
|
@ -2,11 +2,12 @@ module Arel
|
||||||
module Nodes
|
module Nodes
|
||||||
class SelectStatement
|
class SelectStatement
|
||||||
attr_reader :cores
|
attr_reader :cores
|
||||||
attr_accessor :limit
|
attr_accessor :limit, :orders
|
||||||
|
|
||||||
def initialize cores = [SelectCore.new]
|
def initialize cores = [SelectCore.new]
|
||||||
@cores = cores
|
@cores = cores
|
||||||
@limit = nil
|
@orders = []
|
||||||
|
@limit = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize_copy other
|
def initialize_copy other
|
||||||
|
|
|
@ -28,6 +28,11 @@ module Arel
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def order *expr
|
||||||
|
@head.orders.concat expr
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
def wheres
|
def wheres
|
||||||
Compatibility::Wheres.new @engine, @ctx.wheres
|
Compatibility::Wheres.new @engine, @ctx.wheres
|
||||||
end
|
end
|
||||||
|
|
|
@ -45,6 +45,7 @@ module Arel
|
||||||
def visit_Arel_Nodes_SelectStatement o
|
def visit_Arel_Nodes_SelectStatement o
|
||||||
[
|
[
|
||||||
o.cores.map { |x| visit x }.join,
|
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)
|
("LIMIT #{o.limit}" if o.limit)
|
||||||
].compact.join ' '
|
].compact.join ' '
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,8 +15,8 @@ describe Arel::Nodes::InsertStatement do
|
||||||
end
|
end
|
||||||
|
|
||||||
dolly = statement.clone
|
dolly = statement.clone
|
||||||
dolly.columns.should == %w[a0 b1 c2]
|
check dolly.columns.should == %w[a0 b1 c2]
|
||||||
dolly.values.should == %w[x0 y1 z2]
|
check dolly.values.should == %w[x0 y1 z2]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,9 +15,9 @@ describe Arel::Nodes::SelectCore do
|
||||||
end
|
end
|
||||||
|
|
||||||
dolly = core.clone
|
dolly = core.clone
|
||||||
dolly.froms.should == %w[a0 b1 c2]
|
check dolly.froms.should == %w[a0 b1 c2]
|
||||||
dolly.projections.should == %w[d0 e1 f2]
|
check dolly.projections.should == %w[d0 e1 f2]
|
||||||
dolly.wheres.should == %w[g0 h1 i2]
|
check dolly.wheres.should == %w[g0 h1 i2]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,8 +15,8 @@ describe Arel::Nodes::UpdateStatement do
|
||||||
end
|
end
|
||||||
|
|
||||||
dolly = statement.clone
|
dolly = statement.clone
|
||||||
dolly.wheres.should == %w[a0 b1 c2]
|
check dolly.wheres.should == %w[a0 b1 c2]
|
||||||
dolly.values.should == %w[x0 y1 z2]
|
check dolly.values.should == %w[x0 y1 z2]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,6 +23,37 @@ module Arel
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'select manager' do
|
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
|
describe 'joins' do
|
||||||
it 'returns join sql' do
|
it 'returns join sql' do
|
||||||
table = Table.new :users
|
table = Table.new :users
|
||||||
|
|
Loading…
Reference in a new issue