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

renaming @head to @ast

This commit is contained in:
Aaron Patterson 2010-11-05 14:09:09 -07:00
parent 424e39d688
commit a1a01e2bb3
7 changed files with 45 additions and 35 deletions

View file

@ -13,8 +13,8 @@ module Arel
end
um.table relation
um.set values
um.take @head.limit
um.order(*@head.orders)
um.take @ast.limit
um.order(*@ast.orders)
um.wheres = @ctx.wheres
@engine.connection.update um.to_sql, 'AREL'

View file

@ -2,21 +2,21 @@ module Arel
class DeleteManager < Arel::TreeManager
def initialize engine
super
@head = Nodes::DeleteStatement.new
@ast = Nodes::DeleteStatement.new
end
def from relation
@head.relation = relation
@ast.relation = relation
self
end
def where expression
@head.wheres << expression
@ast.wheres << expression
self
end
def wheres= list
@head.wheres = list
@ast.wheres = list
end
end
end

View file

@ -2,32 +2,32 @@ module Arel
class InsertManager < Arel::TreeManager
def initialize engine
super
@head = Nodes::InsertStatement.new
@ast = Nodes::InsertStatement.new
end
def into table
@head.relation = table
@ast.relation = table
self
end
def columns; @head.columns end
def values= val; @head.values = val; end
def columns; @ast.columns end
def values= val; @ast.values = val; end
def insert fields
return if fields.empty?
if String === fields
@head.values = SqlLiteral.new(fields)
@ast.values = SqlLiteral.new(fields)
else
@head.relation ||= fields.first.first.relation
@ast.relation ||= fields.first.first.relation
values = []
fields.each do |column, value|
@head.columns << column
@ast.columns << column
values << value
end
@head.values = Nodes::Values.new values, @head.columns
@ast.values = Nodes::Values.new values, @ast.columns
end
end
end

View file

@ -4,13 +4,13 @@ module Arel
def initialize engine, table = nil
super(engine)
@head = Nodes::SelectStatement.new
@ctx = @head.cores.last
@ast = Nodes::SelectStatement.new
@ctx = @ast.cores.last
from table
end
def taken
@head.limit
@ast.limit
end
def constraints
@ -18,7 +18,7 @@ module Arel
end
def skip amount
@head.offset = Nodes::Offset.new(amount)
@ast.offset = Nodes::Offset.new(amount)
self
end
@ -31,12 +31,12 @@ module Arel
def lock locking = true
# FIXME: do we even need to store this? If locking is +false+ shouldn't
# we just remove the node from the AST?
@head.lock = Nodes::Lock.new
@ast.lock = Nodes::Lock.new
self
end
def locked
@head.lock
@ast.lock
end
def on *exprs
@ -108,14 +108,14 @@ module Arel
def order *expr
# FIXME: We SHOULD NOT be converting these to SqlLiteral automatically
@head.orders.concat expr.map { |x|
@ast.orders.concat expr.map { |x|
String === x || Symbol === x ? Nodes::SqlLiteral.new(x.to_s) : x
}
self
end
def orders
@head.orders
@ast.orders
end
def wheres
@ -130,7 +130,7 @@ module Arel
end
def take limit
@head.limit = limit
@ast.limit = limit
self
end
@ -142,7 +142,7 @@ module Arel
end
def order_clauses
Visitors::OrderClauses.new(@engine).accept(@head).map { |x|
Visitors::OrderClauses.new(@engine).accept(@ast).map { |x|
Nodes::SqlLiteral.new x
}
end

View file

@ -4,6 +4,7 @@ module Arel
include Arel::Relation
attr_accessor :visitor
attr_reader :ast
def initialize engine
@engine = engine
@ -11,16 +12,16 @@ module Arel
end
def to_dot
Visitors::Dot.new.accept @head
Visitors::Dot.new.accept @ast
end
def to_sql
@visitor.accept @head
@visitor.accept @ast
end
def initialize_copy other
super
@head = @head.clone
@ast = @ast.clone
end
end
end

View file

@ -2,40 +2,40 @@ module Arel
class UpdateManager < Arel::TreeManager
def initialize engine
super
@head = Nodes::UpdateStatement.new
@ast = Nodes::UpdateStatement.new
end
def take limit
@head.limit = limit
@ast.limit = limit
self
end
def order *expr
@head.orders = expr
@ast.orders = expr
self
end
###
# UPDATE +table+
def table table
@head.relation = table
@ast.relation = table
self
end
def wheres= exprs
@head.wheres = exprs
@ast.wheres = exprs
end
def where expr
@head.wheres << expr
@ast.wheres << expr
self
end
def set values
if String === values
@head.values = [values]
@ast.values = [values]
else
@head.values = values.map { |column,value|
@ast.values = values.map { |column,value|
Nodes::Assignment.new(
Nodes::UnqualifiedColumn.new(column),
value

View file

@ -130,6 +130,15 @@ module Arel
end
end
describe 'ast' do
it 'should return the ast' do
table = Table.new :users
mgr = table.from table
ast = mgr.ast
mgr.visitor.accept(ast).must_equal mgr.to_sql
end
end
describe 'taken' do
it 'should return limit' do
manager = Arel::SelectManager.new Table.engine