mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
OR nodes are somewhat working
This commit is contained in:
parent
66243e491d
commit
ae547dc139
8 changed files with 81 additions and 12 deletions
|
@ -1,4 +1,7 @@
|
|||
require 'arel/nodes/binary'
|
||||
require 'arel/nodes/equality'
|
||||
require 'arel/nodes/or'
|
||||
|
||||
require 'arel/nodes/in'
|
||||
require 'arel/nodes/sql_literal'
|
||||
require 'arel/nodes/select_core'
|
||||
|
|
24
lib/arel/nodes/binary.rb
Normal file
24
lib/arel/nodes/binary.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
module Arel
|
||||
module Nodes
|
||||
class Binary
|
||||
attr_accessor :left, :right
|
||||
|
||||
def initialize left, right
|
||||
@left = left
|
||||
@right = right
|
||||
end
|
||||
|
||||
def or right
|
||||
Nodes::Or.new self, right
|
||||
end
|
||||
|
||||
# FIXME: this method should go away. I don't like people calling
|
||||
# to_sql on non-head nodes. This forces us to walk the AST until we
|
||||
# can find a node that has a "relation" member.
|
||||
def to_sql
|
||||
viz = Visitors::ToSql.new left.relation.engine
|
||||
viz.accept self
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,17 +1,6 @@
|
|||
module Arel
|
||||
module Nodes
|
||||
class Equality
|
||||
attr_accessor :left, :right
|
||||
|
||||
def initialize left, right
|
||||
@left = left
|
||||
@right = right
|
||||
end
|
||||
|
||||
def to_sql
|
||||
viz = Visitors::ToSql.new left.relation.engine
|
||||
viz.accept self
|
||||
end
|
||||
class Equality < Arel::Nodes::Binary
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
6
lib/arel/nodes/or.rb
Normal file
6
lib/arel/nodes/or.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
module Arel
|
||||
module Nodes
|
||||
class Or < Arel::Nodes::Binary
|
||||
end
|
||||
end
|
||||
end
|
|
@ -60,6 +60,10 @@ module Arel
|
|||
"#{visit o.left} IN (#{o.right.map { |x| quote visit x }.join ', '})"
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_Or o
|
||||
"#{visit o.left} OR #{visit o.right}"
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_Equality o
|
||||
"#{visit o.left} = #{quote visit o.right}"
|
||||
end
|
||||
|
|
16
spec/arel/nodes/equality_spec.rb
Normal file
16
spec/arel/nodes/equality_spec.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
module Arel
|
||||
module Nodes
|
||||
describe 'equality' do
|
||||
describe 'or' do
|
||||
it 'makes an OR node' do
|
||||
attr = Table.new(:users)[:id]
|
||||
left = attr.eq(10)
|
||||
right = attr.eq(11)
|
||||
node = left.or right
|
||||
check node.left.should == left
|
||||
check node.right.should == right
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
20
spec/arel/nodes/or_spec.rb
Normal file
20
spec/arel/nodes/or_spec.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
module Arel
|
||||
module Nodes
|
||||
describe 'or' do
|
||||
describe '#or' do
|
||||
it 'makes an OR node' do
|
||||
attr = Table.new(:users)[:id]
|
||||
left = attr.eq(10)
|
||||
right = attr.eq(11)
|
||||
node = left.or right
|
||||
check node.left.should == left
|
||||
check node.right.should == right
|
||||
|
||||
oror = node.or(right)
|
||||
check oror.left == node
|
||||
check oror.right == right
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -8,6 +8,13 @@ module Arel
|
|||
@attr = Table.new(:users)[:id]
|
||||
end
|
||||
|
||||
it "should visit_Arel_Nodes_Or" do
|
||||
node = Nodes::Or.new @attr.eq(10), @attr.eq(11)
|
||||
@visitor.accept(node).should be_like %{
|
||||
"users"."id" = 10 OR "users"."id" = 11
|
||||
}
|
||||
end
|
||||
|
||||
it "should visit visit_Arel_Attributes_Time" do
|
||||
attr = Attributes::Time.new(@attr.relation, @attr.name, @attr.column)
|
||||
@visitor.accept attr
|
||||
|
|
Loading…
Reference in a new issue