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

updating nodes and depth first visitor

This commit is contained in:
Aaron Patterson 2010-11-29 15:17:59 -08:00
parent 963ca860d5
commit 039947e5ac
10 changed files with 154 additions and 54 deletions

View file

@ -58,6 +58,7 @@ lib/arel/nodes/sql_literal.rb
lib/arel/nodes/string_join.rb
lib/arel/nodes/sum.rb
lib/arel/nodes/table_alias.rb
lib/arel/nodes/unary.rb
lib/arel/nodes/unqualified_column.rb
lib/arel/nodes/update_statement.rb
lib/arel/nodes/values.rb
@ -70,6 +71,7 @@ lib/arel/table.rb
lib/arel/tree_manager.rb
lib/arel/update_manager.rb
lib/arel/visitors.rb
lib/arel/visitors/depth_first.rb
lib/arel/visitors/dot.rb
lib/arel/visitors/join_sql.rb
lib/arel/visitors/mysql.rb
@ -103,6 +105,7 @@ test/test_insert_manager.rb
test/test_select_manager.rb
test/test_table.rb
test/test_update_manager.rb
test/visitors/test_depth_first.rb
test/visitors/test_join_sql.rb
test/visitors/test_mysql.rb
test/visitors/test_oracle.rb

View file

@ -1,16 +1,18 @@
module Arel
module Nodes
class DeleteStatement
attr_accessor :relation, :wheres
class DeleteStatement < Arel::Nodes::Binary
alias :relation :left
alias :relation= :left=
alias :wheres :right
alias :wheres= :right=
def initialize
@from = nil
@wheres = []
def initialize relation = nil, wheres = []
super
end
def initialize_copy other
super
@wheres = @wheres.clone
@right = @right.clone
end
end
end

View file

@ -1,6 +1,6 @@
module Arel
module Nodes
class Lock
class Lock < Arel::Nodes::Node
end
end
end

View file

@ -1,11 +1,6 @@
module Arel
module Nodes
class Not < Arel::Nodes::Node
attr_reader :expr
def initialize expr
@expr = expr
end
class Not < Arel::Nodes::Unary
end
end
end

View file

@ -1,11 +1,6 @@
module Arel
module Nodes
class On
attr_accessor :expr
def initialize expr
@expr = expr
end
class On < Arel::Nodes::Unary
end
end
end

View file

@ -1,14 +1,10 @@
module Arel
module Nodes
class TableAlias
attr_reader :name, :relation
class TableAlias < Arel::Nodes::Binary
alias :name :left
alias :relation :right
alias :table_alias :name
def initialize name, relation
@name = name
@relation = relation
end
def [] name
Attribute.new self, name
end

View file

@ -1,18 +1,15 @@
module Arel
module Nodes
class UnqualifiedColumn
attr_accessor :attribute
def initialize attribute
@attribute = attribute
end
class UnqualifiedColumn < Arel::Nodes::Unary
alias :attribute :expr
alias :attribute= :expr=
def column
@attribute.column
@expr.column
end
def name
@attribute.name
@expr.name
end
end
end

View file

@ -1,11 +1,13 @@
module Arel
module Nodes
class Values
attr_accessor :expressions, :columns
class Values < Arel::Nodes::Binary
alias :expressions :left
alias :expressions= :left=
alias :columns :right
alias :columns= :right=
def initialize exprs, columns = []
@expressions = exprs
@columns = columns
super
end
end
end

View file

@ -7,14 +7,55 @@ module Arel
private
def unary o
visit o.expr
@block.call o
end
alias :visit_Arel_Nodes_Group :unary
alias :visit_Arel_Nodes_Grouping :unary
alias :visit_Arel_Nodes_Having :unary
alias :visit_Arel_Nodes_Not :unary
alias :visit_Arel_Nodes_Offset :unary
alias :visit_Arel_Nodes_On :unary
alias :visit_Arel_Nodes_UnqualifiedColumn :unary
def function o
visit o.expressions
visit o.alias
@block.call o
end
alias :visit_Arel_Nodes_Avg :function
alias :visit_Arel_Nodes_Exists :function
alias :visit_Arel_Nodes_Max :function
alias :visit_Arel_Nodes_Min :function
alias :visit_Arel_Nodes_Sum :function
def visit_Arel_Nodes_Count o
visit o.expressions
visit o.alias
visit o.distinct
@block.call o
end
def join o
visit o.left
visit o.right
visit o.constraint
@block.call o
end
alias :visit_Arel_Nodes_InnerJoin :join
alias :visit_Arel_Nodes_OuterJoin :join
def binary o
visit o.left
visit o.right
@block.call o
end
alias :visit_Arel_Nodes_And :binary
alias :visit_Arel_Nodes_As :binary
alias :visit_Arel_Nodes_Assignment :binary
alias :visit_Arel_Nodes_Between :binary
alias :visit_Arel_Nodes_DeleteStatement :binary
alias :visit_Arel_Nodes_DoesNotMatch :binary
alias :visit_Arel_Nodes_Equality :binary
alias :visit_Arel_Nodes_GreaterThan :binary
@ -26,6 +67,10 @@ module Arel
alias :visit_Arel_Nodes_NotEqual :binary
alias :visit_Arel_Nodes_NotIn :binary
alias :visit_Arel_Nodes_Or :binary
alias :visit_Arel_Nodes_Ordering :binary
alias :visit_Arel_Nodes_StringJoin :binary
alias :visit_Arel_Nodes_TableAlias :binary
alias :visit_Arel_Nodes_Values :binary
def visit_Arel_Attribute o
visit o.relation
@ -38,6 +83,7 @@ module Arel
alias :visit_Arel_Attributes_Time :visit_Arel_Attribute
alias :visit_Arel_Attributes_Boolean :visit_Arel_Attribute
alias :visit_Arel_Attributes_Attribute :visit_Arel_Attribute
alias :visit_Arel_Attributes_Decimal :visit_Arel_Attribute
def visit_Arel_Table o
visit o.name
@ -47,19 +93,24 @@ module Arel
def terminal o
@block.call o
end
alias :visit_Arel_Nodes_SqlLiteral :terminal
alias :visit_Arel_SqlLiteral :terminal
alias :visit_BigDecimal :terminal
alias :visit_Date :terminal
alias :visit_DateTime :terminal
alias :visit_FalseClass :terminal
alias :visit_Fixnum :terminal
alias :visit_Float :terminal
alias :visit_NilClass :terminal
alias :visit_String :terminal
alias :visit_Symbol :terminal
alias :visit_Time :terminal
alias :visit_TrueClass :terminal
alias :visit_ActiveSupport_Multibyte_Chars :terminal
alias :visit_ActiveSupport_StringInquirer :terminal
alias :visit_Arel_Nodes_Lock :terminal
alias :visit_Arel_Nodes_SqlLiteral :terminal
alias :visit_Arel_SqlLiteral :terminal
alias :visit_BigDecimal :terminal
alias :visit_Bignum :terminal
alias :visit_Class :terminal
alias :visit_Date :terminal
alias :visit_DateTime :terminal
alias :visit_FalseClass :terminal
alias :visit_Fixnum :terminal
alias :visit_Float :terminal
alias :visit_NilClass :terminal
alias :visit_String :terminal
alias :visit_Symbol :terminal
alias :visit_Time :terminal
alias :visit_TrueClass :terminal
def visit_Arel_Nodes_InsertStatement o
visit o.relation

View file

@ -14,6 +14,62 @@ module Arel
@visitor = Visitors::DepthFirst.new @collector
end
# unary ops
[
Arel::Nodes::Not,
Arel::Nodes::Group,
Arel::Nodes::On,
Arel::Nodes::Grouping,
Arel::Nodes::Offset,
Arel::Nodes::Having,
Arel::Nodes::UnqualifiedColumn,
].each do |klass|
define_method("test_#{klass.name.gsub('::', '_')}") do
op = klass.new(:a)
@visitor.accept op
assert_equal [:a, op], @collector.calls
end
end
# functions
[
Arel::Nodes::Exists,
Arel::Nodes::Avg,
Arel::Nodes::Min,
Arel::Nodes::Max,
Arel::Nodes::Sum,
].each do |klass|
define_method("test_#{klass.name.gsub('::', '_')}") do
func = klass.new(:a, :b)
@visitor.accept func
assert_equal [:a, :b, func], @collector.calls
end
end
def test_lock
lock = Nodes::Lock.new
@visitor.accept lock
assert_equal [lock], @collector.calls
end
def test_count
count = Nodes::Count.new :a, :b, :c
@visitor.accept count
assert_equal [:a, :c, :b, count], @collector.calls
end
def test_inner_join
join = Nodes::InnerJoin.new :a, :b, :c
@visitor.accept join
assert_equal [:a, :b, :c, join], @collector.calls
end
def test_outer_join
join = Nodes::OuterJoin.new :a, :b, :c
@visitor.accept join
assert_equal [:a, :b, :c, join], @collector.calls
end
[
Arel::Nodes::And,
Arel::Nodes::Assignment,
@ -29,6 +85,12 @@ module Arel
Arel::Nodes::NotEqual,
Arel::Nodes::NotIn,
Arel::Nodes::Or,
Arel::Nodes::StringJoin,
Arel::Nodes::TableAlias,
Arel::Nodes::Values,
Arel::Nodes::As,
Arel::Nodes::DeleteStatement,
Arel::Nodes::Ordering,
].each do |klass|
define_method("test_#{klass.name.gsub('::', '_')}") do
binary = klass.new(:a, :b)
@ -130,9 +192,6 @@ module Arel
@visitor.accept stmt
assert_equal [:a, :b, stmt.columns, :c, stmt], @collector.calls
end
def test_offset
end
end
end
end