aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/arel.rb1
-rw-r--r--lib/arel/compatibility/wheres.rb29
-rw-r--r--lib/arel/nodes.rb3
-rw-r--r--lib/arel/nodes/binary.rb26
-rw-r--r--lib/arel/nodes/equality.rb13
-rw-r--r--lib/arel/nodes/or.rb6
-rw-r--r--lib/arel/select_manager.rb4
-rw-r--r--lib/arel/visitors/to_sql.rb4
-rw-r--r--spec/arel/activerecord_compat_spec.rb18
-rw-r--r--spec/arel/nodes/equality_spec.rb16
-rw-r--r--spec/arel/nodes/or_spec.rb20
-rw-r--r--spec/arel/visitors/to_sql_spec.rb7
12 files changed, 135 insertions, 12 deletions
diff --git a/lib/arel.rb b/lib/arel.rb
index c4e377b5bc..acb270728f 100644
--- a/lib/arel.rb
+++ b/lib/arel.rb
@@ -3,6 +3,7 @@ require 'arel/crud'
require 'arel/version'
require 'arel/table'
require 'arel/attributes'
+require 'arel/compatibility/wheres'
#### these are deprecated
# The Arel::Relation constant is referenced in Rails
diff --git a/lib/arel/compatibility/wheres.rb b/lib/arel/compatibility/wheres.rb
new file mode 100644
index 0000000000..eeb7a2fa38
--- /dev/null
+++ b/lib/arel/compatibility/wheres.rb
@@ -0,0 +1,29 @@
+module Arel
+ module Compatibility # :nodoc:
+ class Wheres # :nodoc:
+ include Enumerable
+
+ module Value # :nodoc:
+ attr_accessor :visitor
+ def value
+ visitor.accept self
+ end
+ end
+
+ def initialize engine, collection
+ @engine = engine
+ @collection = collection
+ end
+
+ def each
+ to_sql = Visitors::ToSql.new @engine
+
+ @collection.each { |c|
+ c.extend(Value)
+ c.visitor = to_sql
+ yield c
+ }
+ end
+ end
+ end
+end
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb
index d1868adfa9..12759a462f 100644
--- a/lib/arel/nodes.rb
+++ b/lib/arel/nodes.rb
@@ -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'
diff --git a/lib/arel/nodes/binary.rb b/lib/arel/nodes/binary.rb
new file mode 100644
index 0000000000..fdfcab2d3f
--- /dev/null
+++ b/lib/arel/nodes/binary.rb
@@ -0,0 +1,26 @@
+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.
+ #
+ # Maybe we should just use `Table.engine`? :'(
+ def to_sql
+ viz = Visitors::ToSql.new Table.engine
+ viz.accept self
+ end
+ end
+ end
+end
diff --git a/lib/arel/nodes/equality.rb b/lib/arel/nodes/equality.rb
index d778380054..b8d8281434 100644
--- a/lib/arel/nodes/equality.rb
+++ b/lib/arel/nodes/equality.rb
@@ -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
diff --git a/lib/arel/nodes/or.rb b/lib/arel/nodes/or.rb
new file mode 100644
index 0000000000..bdf7f6d9b3
--- /dev/null
+++ b/lib/arel/nodes/or.rb
@@ -0,0 +1,6 @@
+module Arel
+ module Nodes
+ class Or < Arel::Nodes::Binary
+ end
+ end
+end
diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb
index 23d85e3386..f278465c51 100644
--- a/lib/arel/select_manager.rb
+++ b/lib/arel/select_manager.rb
@@ -23,6 +23,10 @@ module Arel
self
end
+ def wheres
+ Compatibility::Wheres.new @engine, @ctx.wheres
+ end
+
def take limit
@head.limit = limit
self
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index b0fed4936b..9dc7696ad8 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -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
diff --git a/spec/arel/activerecord_compat_spec.rb b/spec/arel/activerecord_compat_spec.rb
new file mode 100644
index 0000000000..a7eeb7e5ee
--- /dev/null
+++ b/spec/arel/activerecord_compat_spec.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+
+module Arel
+ describe 'activerecord compatibility' do
+ describe 'select manager' do
+ it 'provides wheres' do
+ table = Table.new :users
+ manager = Arel::SelectManager.new Table.engine
+ manager.where table[:id].eq 1
+ manager.where table[:name].eq 'Aaron'
+
+ check manager.wheres.map { |x|
+ x.value
+ }.join(', ').should == "\"users\".\"id\" = 1, \"users\".\"name\" = 'Aaron'"
+ end
+ end
+ end
+end
diff --git a/spec/arel/nodes/equality_spec.rb b/spec/arel/nodes/equality_spec.rb
new file mode 100644
index 0000000000..b74af3a039
--- /dev/null
+++ b/spec/arel/nodes/equality_spec.rb
@@ -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
diff --git a/spec/arel/nodes/or_spec.rb b/spec/arel/nodes/or_spec.rb
new file mode 100644
index 0000000000..e855b92453
--- /dev/null
+++ b/spec/arel/nodes/or_spec.rb
@@ -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
diff --git a/spec/arel/visitors/to_sql_spec.rb b/spec/arel/visitors/to_sql_spec.rb
index 554b69a5bd..b70c392630 100644
--- a/spec/arel/visitors/to_sql_spec.rb
+++ b/spec/arel/visitors/to_sql_spec.rb
@@ -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