diff options
-rw-r--r-- | lib/arel/nodes.rb | 2 | ||||
-rw-r--r-- | lib/arel/nodes/binary.rb | 2 | ||||
-rw-r--r-- | lib/arel/nodes/full_outer_join.rb | 6 | ||||
-rw-r--r-- | lib/arel/nodes/right_outer_join.rb | 6 | ||||
-rw-r--r-- | lib/arel/select_manager.rb | 7 | ||||
-rw-r--r-- | lib/arel/visitors.rb | 1 | ||||
-rw-r--r-- | lib/arel/visitors/bind_substitute.rb | 9 | ||||
-rw-r--r-- | lib/arel/visitors/depth_first.rb | 4 | ||||
-rw-r--r-- | lib/arel/visitors/dot.rb | 4 | ||||
-rw-r--r-- | lib/arel/visitors/order_clauses.rb | 11 | ||||
-rw-r--r-- | lib/arel/visitors/postgresql.rb | 8 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 20 | ||||
-rw-r--r-- | test/test_select_manager.rb | 26 | ||||
-rw-r--r-- | test/test_table.rb | 14 | ||||
-rw-r--r-- | test/visitors/test_depth_first.rb | 12 | ||||
-rw-r--r-- | test/visitors/test_join_sql.rb | 26 | ||||
-rw-r--r-- | test/visitors/test_postgres.rb | 34 | ||||
-rw-r--r-- | test/visitors/test_to_sql.rb | 20 |
18 files changed, 180 insertions, 32 deletions
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index 6ce1ed7dba..a68e327983 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -45,8 +45,10 @@ require 'arel/nodes/named_function' require 'arel/nodes/window' # joins +require 'arel/nodes/full_outer_join' require 'arel/nodes/inner_join' require 'arel/nodes/outer_join' +require 'arel/nodes/right_outer_join' require 'arel/nodes/string_join' require 'arel/nodes/sql_literal' diff --git a/lib/arel/nodes/binary.rb b/lib/arel/nodes/binary.rb index 0e7e281b4b..939684957f 100644 --- a/lib/arel/nodes/binary.rb +++ b/lib/arel/nodes/binary.rb @@ -40,7 +40,9 @@ module Arel Matches NotEqual NotIn + NotRegexp Or + Regexp Union UnionAll Intersect diff --git a/lib/arel/nodes/full_outer_join.rb b/lib/arel/nodes/full_outer_join.rb new file mode 100644 index 0000000000..708f161c9a --- /dev/null +++ b/lib/arel/nodes/full_outer_join.rb @@ -0,0 +1,6 @@ +module Arel + module Nodes + class FullOuterJoin < Arel::Nodes::Join + end + end +end diff --git a/lib/arel/nodes/right_outer_join.rb b/lib/arel/nodes/right_outer_join.rb new file mode 100644 index 0000000000..ea1ddb7d52 --- /dev/null +++ b/lib/arel/nodes/right_outer_join.rb @@ -0,0 +1,6 @@ +module Arel + module Nodes + class RightOuterJoin < Arel::Nodes::Join + end + end +end diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb index 5ab2778778..f49f76d98f 100644 --- a/lib/arel/select_manager.rb +++ b/lib/arel/select_manager.rb @@ -220,13 +220,6 @@ module Arel Nodes::SqlLiteral.new sql end - def order_clauses - visitor = Visitors::OrderClauses.new(@engine.connection) - visitor.accept(@ast).map { |x| - Nodes::SqlLiteral.new x - } - end - def join_sources @ctx.source.right end diff --git a/lib/arel/visitors.rb b/lib/arel/visitors.rb index 8276eace2b..3986ad9ce5 100644 --- a/lib/arel/visitors.rb +++ b/lib/arel/visitors.rb @@ -8,7 +8,6 @@ require 'arel/visitors/mssql' require 'arel/visitors/oracle' require 'arel/visitors/join_sql' require 'arel/visitors/where_sql' -require 'arel/visitors/order_clauses' require 'arel/visitors/dot' require 'arel/visitors/ibm_db' require 'arel/visitors/informix' diff --git a/lib/arel/visitors/bind_substitute.rb b/lib/arel/visitors/bind_substitute.rb new file mode 100644 index 0000000000..0503a9c986 --- /dev/null +++ b/lib/arel/visitors/bind_substitute.rb @@ -0,0 +1,9 @@ +module Arel + module Visitors + class BindSubstitute + def initialize delegte + @delegate = delegate + end + end + end +end diff --git a/lib/arel/visitors/depth_first.rb b/lib/arel/visitors/depth_first.rb index 1770ab21d5..4d2ecfa5e1 100644 --- a/lib/arel/visitors/depth_first.rb +++ b/lib/arel/visitors/depth_first.rb @@ -67,6 +67,7 @@ module Arel alias :visit_Arel_Nodes_DeleteStatement :binary alias :visit_Arel_Nodes_DoesNotMatch :binary alias :visit_Arel_Nodes_Equality :binary + alias :visit_Arel_Nodes_FullOuterJoin :binary alias :visit_Arel_Nodes_GreaterThan :binary alias :visit_Arel_Nodes_GreaterThanOrEqual :binary alias :visit_Arel_Nodes_In :binary @@ -78,8 +79,11 @@ module Arel alias :visit_Arel_Nodes_Matches :binary alias :visit_Arel_Nodes_NotEqual :binary alias :visit_Arel_Nodes_NotIn :binary + alias :visit_Arel_Nodes_NotRegexp :binary alias :visit_Arel_Nodes_Or :binary alias :visit_Arel_Nodes_OuterJoin :binary + alias :visit_Arel_Nodes_Regexp :binary + alias :visit_Arel_Nodes_RightOuterJoin :binary alias :visit_Arel_Nodes_TableAlias :binary alias :visit_Arel_Nodes_Values :binary diff --git a/lib/arel/visitors/dot.rb b/lib/arel/visitors/dot.rb index b2fa6bfbd5..99f4c467d2 100644 --- a/lib/arel/visitors/dot.rb +++ b/lib/arel/visitors/dot.rb @@ -54,7 +54,9 @@ module Arel visit_edge o, "left" visit_edge o, "right" end - alias :visit_Arel_Nodes_OuterJoin :visit_Arel_Nodes_InnerJoin + alias :visit_Arel_Nodes_FullOuterJoin :visit_Arel_Nodes_InnerJoin + alias :visit_Arel_Nodes_OuterJoin :visit_Arel_Nodes_InnerJoin + alias :visit_Arel_Nodes_RightOuterJoin :visit_Arel_Nodes_InnerJoin def visit_Arel_Nodes_DeleteStatement o visit_edge o, "relation" diff --git a/lib/arel/visitors/order_clauses.rb b/lib/arel/visitors/order_clauses.rb deleted file mode 100644 index 11dbfdad2a..0000000000 --- a/lib/arel/visitors/order_clauses.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Arel - module Visitors - class OrderClauses < Arel::Visitors::ToSql - private - - def visit_Arel_Nodes_SelectStatement o - o.orders.map { |x| visit x } - end - end - end -end diff --git a/lib/arel/visitors/postgresql.rb b/lib/arel/visitors/postgresql.rb index 812710181c..49f7482e7d 100644 --- a/lib/arel/visitors/postgresql.rb +++ b/lib/arel/visitors/postgresql.rb @@ -11,6 +11,14 @@ module Arel "#{visit o.left} NOT ILIKE #{visit o.right}" end + def visit_Arel_Nodes_Regexp o + "#{visit o.left} ~ #{visit o.right}" + end + + def visit_Arel_Nodes_NotRegexp o + "#{visit o.left} !~ #{visit o.right}" + end + def visit_Arel_Nodes_DistinctOn o "DISTINCT ON ( #{visit o.expr} )" end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 545f95df2a..d8883073d0 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -470,19 +470,35 @@ module Arel collector << " " end if o.right.any? - o.right.map { |j| visit j }.join(' ') + collector = inject_join o.right, collector, ' ' end collector end + def visit_Arel_Nodes_Regexp o, collector + raise NotImplementedError, '~ not implemented for this db' + end + + def visit_Arel_Nodes_NotRegexp o, collector + raise NotImplementedError, '!~ not implemented for this db' + end + def visit_Arel_Nodes_StringJoin o visit o.left end + def visit_Arel_Nodes_FullOuterJoin o + "FULL OUTER JOIN #{visit o.left} #{visit o.right}" + end + def visit_Arel_Nodes_OuterJoin o "LEFT OUTER JOIN #{visit o.left} #{visit o.right}" end + def visit_Arel_Nodes_RightOuterJoin o + "RIGHT OUTER JOIN #{visit o.left} #{visit o.right}" + end + def visit_Arel_Nodes_InnerJoin o s = "INNER JOIN #{visit o.left}" if o.right @@ -612,7 +628,7 @@ module Arel quote(o, column_for(a)) end - def unsupported o, a + def unsupported o raise "unsupported: #{o.class.name}" end diff --git a/test/test_select_manager.rb b/test/test_select_manager.rb index 7f025f7a02..6258705e2f 100644 --- a/test/test_select_manager.rb +++ b/test/test_select_manager.rb @@ -519,12 +519,28 @@ module Arel it 'should create join nodes with a klass' do relation = Arel::SelectManager.new Table.engine + join = relation.create_join 'foo', 'bar', Arel::Nodes::FullOuterJoin + assert_kind_of Arel::Nodes::FullOuterJoin, join + assert_equal 'foo', join.left + assert_equal 'bar', join.right + end + + it 'should create join nodes with a klass' do + relation = Arel::SelectManager.new Table.engine join = relation.create_join 'foo', 'bar', Arel::Nodes::OuterJoin assert_kind_of Arel::Nodes::OuterJoin, join assert_equal 'foo', join.left assert_equal 'bar', join.right end + it 'should create join nodes with a klass' do + relation = Arel::SelectManager.new Table.engine + join = relation.create_join 'foo', 'bar', Arel::Nodes::RightOuterJoin + assert_kind_of Arel::Nodes::RightOuterJoin, join + assert_equal 'foo', join.left + assert_equal 'bar', join.right + end + describe 'join' do it 'responds to join' do left = Table.new :users @@ -612,16 +628,6 @@ module Arel end end - describe 'order_clauses' do - it 'returns order clauses as a list' do - table = Table.new :users - manager = Arel::SelectManager.new Table.engine - manager.from table - manager.order table[:id] - manager.order_clauses.first.must_be_like %{ "users"."id" } - end - end - describe 'group' do it 'takes an attribute' do table = Table.new :users diff --git a/test/test_table.rb b/test/test_table.rb index 431a919de1..b4c2a65fcd 100644 --- a/test/test_table.rb +++ b/test/test_table.rb @@ -20,12 +20,26 @@ module Arel end it 'should create join nodes with a klass' do + join = @relation.create_join 'foo', 'bar', Arel::Nodes::FullOuterJoin + assert_kind_of Arel::Nodes::FullOuterJoin, join + assert_equal 'foo', join.left + assert_equal 'bar', join.right + end + + it 'should create join nodes with a klass' do join = @relation.create_join 'foo', 'bar', Arel::Nodes::OuterJoin assert_kind_of Arel::Nodes::OuterJoin, join assert_equal 'foo', join.left assert_equal 'bar', join.right end + it 'should create join nodes with a klass' do + join = @relation.create_join 'foo', 'bar', Arel::Nodes::RightOuterJoin + assert_kind_of Arel::Nodes::RightOuterJoin, join + assert_equal 'foo', join.left + assert_equal 'bar', join.right + end + it 'should return an insert manager' do im = @relation.compile_insert 'VALUES(NULL)' assert_kind_of Arel::InsertManager, im diff --git a/test/visitors/test_depth_first.rb b/test/visitors/test_depth_first.rb index cbaa780dae..baa8f64184 100644 --- a/test/visitors/test_depth_first.rb +++ b/test/visitors/test_depth_first.rb @@ -81,12 +81,24 @@ module Arel assert_equal [:a, :b, join], @collector.calls end + def test_full_outer_join + join = Nodes::FullOuterJoin.new :a, :b + @visitor.accept join + assert_equal [:a, :b, join], @collector.calls + end + def test_outer_join join = Nodes::OuterJoin.new :a, :b @visitor.accept join assert_equal [:a, :b, join], @collector.calls end + def test_right_outer_join + join = Nodes::RightOuterJoin.new :a, :b + @visitor.accept join + assert_equal [:a, :b, join], @collector.calls + end + [ Arel::Nodes::Assignment, Arel::Nodes::Between, diff --git a/test/visitors/test_join_sql.rb b/test/visitors/test_join_sql.rb index ea71c05d79..34378dafe7 100644 --- a/test/visitors/test_join_sql.rb +++ b/test/visitors/test_join_sql.rb @@ -25,6 +25,19 @@ module Arel end end + describe 'FULL outer join' do + it 'should visit left if left is a join' do + t = Table.new :users + sm = t.select_manager + sm.join(t, Nodes::FullOuterJoin).on(t[:id]).join( + t, Nodes::FullOuterJoin).on(t[:id]) + sm.join_sql.must_be_like %{ + FULL OUTER JOIN "users" ON "users"."id" + FULL OUTER JOIN "users" ON "users"."id" + } + end + end + describe 'outer join' do it 'should visit left if left is a join' do t = Table.new :users @@ -37,6 +50,19 @@ module Arel } end end + + describe 'right outer join' do + it 'should visit left if left is a join' do + t = Table.new :users + sm = t.select_manager + sm.join(t, Nodes::RightOuterJoin).on(t[:id]).join( + t, Nodes::RightOuterJoin).on(t[:id]) + sm.join_sql.must_be_like %{ + RIGHT OUTER JOIN "users" ON "users"."id" + RIGHT OUTER JOIN "users" ON "users"."id" + } + end + end end end end diff --git a/test/visitors/test_postgres.rb b/test/visitors/test_postgres.rb index 4287baaf14..995e9bf515 100644 --- a/test/visitors/test_postgres.rb +++ b/test/visitors/test_postgres.rb @@ -79,6 +79,40 @@ module Arel } end end + + describe "Nodes::Regexp" do + it "should know how to visit" do + node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%')) + @visitor.accept(node).must_be_like %{ + "users"."name" ~ 'foo%' + } + end + + it 'can handle subqueries' do + subquery = @table.project(:id).where(Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%'))) + node = @attr.in subquery + @visitor.accept(node).must_be_like %{ + "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" ~ 'foo%') + } + end + end + + describe "Nodes::NotRegexp" do + it "should know how to visit" do + node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%')) + @visitor.accept(node).must_be_like %{ + "users"."name" !~ 'foo%' + } + end + + it 'can handle subqueries' do + subquery = @table.project(:id).where(Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%'))) + node = @attr.in subquery + @visitor.accept(node).must_be_like %{ + "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" !~ 'foo%') + } + end + end end end end diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb index 33c7bb9e93..b6d91046b8 100644 --- a/test/visitors/test_to_sql.rb +++ b/test/visitors/test_to_sql.rb @@ -531,6 +531,26 @@ module Arel end end end + + describe 'Nodes::Regexp' do + it 'raises not implemented error' do + node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%')) + + assert_raises(NotImplementedError) do + compile(node) + end + end + end + + describe 'Nodes::NotRegexp' do + it 'raises not implemented error' do + node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%')) + + assert_raises(NotImplementedError) do + compile(node) + end + end + end end end end |