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

Delegate to Connection Visitor in WhereSQL Visitor

The WhereSQL visitor always uses the generic ToSQL visitor to create
the where clause sql statement. This means that it'll miss database
specific statements, such as 'ILIKE' in PostgreSQL. Since the
`#where_sql` method is mainly used for ActiveRecord error reporting,
this discrepancy could be confusing to users.

This patch changes the WhereSQL visitor to use the its connection
visitor to generate SQL for each statement in the SelectManager's wheres
array. Then lets them be joined together with ' AND '.
This commit is contained in:
Edward Paget 2015-01-14 12:23:37 -06:00 committed by Sean Griffin
parent dad0e081c4
commit f726dbe080
2 changed files with 26 additions and 1 deletions

View file

@ -5,7 +5,11 @@ module Arel
def visit_Arel_Nodes_SelectCore o, collector
collector << "WHERE "
inject_join o.wheres, collector, ' AND '
wheres = o.wheres.map do |where|
Nodes::SqlLiteral.new(@connection.visitor.accept(where, collector.class.new).value)
end
inject_join wheres, collector, ' AND '
end
end
end

View file

@ -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