diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-12-17 12:55:27 -0700 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-12-17 12:55:34 -0700 |
commit | 1dba173bbbf53b8734e01ff49479c58a55234ffa (patch) | |
tree | 55e2b126325cbfea1348bcd856610ce8d9f8bcc0 | |
parent | dad0e081c4dc9dbbcdb195ff4e7f9ad4d2bc6de3 (diff) | |
parent | cd395b9419ac3a860ee78cf9706af1bf7d86fe92 (diff) | |
download | rails-1dba173bbbf53b8734e01ff49479c58a55234ffa.tar.gz rails-1dba173bbbf53b8734e01ff49479c58a55234ffa.tar.bz2 rails-1dba173bbbf53b8734e01ff49479c58a55234ffa.zip |
Merge pull request #349 from edpaget/delegate-where-sql-visitor
Delegate to Connection Visitor in WhereSQL Visitor
-rw-r--r-- | lib/arel/select_manager.rb | 2 | ||||
-rw-r--r-- | lib/arel/visitors/where_sql.rb | 11 | ||||
-rw-r--r-- | test/test_select_manager.rb | 21 |
3 files changed, 32 insertions, 2 deletions
diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb index f7dec87ca3..eae3bc8cbc 100644 --- a/lib/arel/select_manager.rb +++ b/lib/arel/select_manager.rb @@ -179,7 +179,7 @@ module Arel def where_sql engine = Table.engine return if @ctx.wheres.empty? - viz = Visitors::WhereSql.new engine.connection + viz = Visitors::WhereSql.new(engine.connection.visitor, engine.connection) Nodes::SqlLiteral.new viz.accept(@ctx, Collectors::SQLString.new).value end diff --git a/lib/arel/visitors/where_sql.rb b/lib/arel/visitors/where_sql.rb index afde15a6c5..41972d5836 100644 --- a/lib/arel/visitors/where_sql.rb +++ b/lib/arel/visitors/where_sql.rb @@ -1,11 +1,20 @@ module Arel module Visitors class WhereSql < Arel::Visitors::ToSql + def initialize(inner_visitor, *args, &block) + @inner_visitor = inner_visitor + super(*args, &block) + end + private def visit_Arel_Nodes_SelectCore o, collector collector << "WHERE " - inject_join o.wheres, collector, ' AND ' + wheres = o.wheres.map do |where| + Nodes::SqlLiteral.new(@inner_visitor.accept(where, collector.class.new).value) + end + + inject_join wheres, collector, ' AND ' end end end diff --git a/test/test_select_manager.rb b/test/test_select_manager.rb index 8425cee031..e6b13e748d 100644 --- a/test/test_select_manager.rb +++ b/test/test_select_manager.rb @@ -968,6 +968,27 @@ module Arel manager.where_sql.must_be_like %{ WHERE "users"."id" = 10 } end + it 'joins wheres with AND' do + table = Table.new :users + manager = Arel::SelectManager.new + manager.from table + manager.where table[:id].eq 10 + manager.where table[:id].eq 11 + manager.where_sql.must_be_like %{ WHERE "users"."id" = 10 AND "users"."id" = 11} + end + + it 'handles database specific statements' do + old_visitor = Table.engine.connection.visitor + Table.engine.connection.visitor = Visitors::PostgreSQL.new Table.engine.connection + table = Table.new :users + manager = Arel::SelectManager.new + manager.from table + manager.where table[:id].eq 10 + manager.where table[:name].matches 'foo%' + manager.where_sql.must_be_like %{ WHERE "users"."id" = 10 AND "users"."name" ILIKE 'foo%' } + Table.engine.connection.visitor = old_visitor + end + it 'returns nil when there are no wheres' do table = Table.new :users manager = Arel::SelectManager.new |