From f726dbe0802395aabf9cc99fe34bc69b375bf344 Mon Sep 17 00:00:00 2001 From: Edward Paget Date: Wed, 14 Jan 2015 12:23:37 -0600 Subject: 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 '. --- lib/arel/visitors/where_sql.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/arel/visitors/where_sql.rb b/lib/arel/visitors/where_sql.rb index afde15a6c5..80797205c9 100644 --- a/lib/arel/visitors/where_sql.rb +++ b/lib/arel/visitors/where_sql.rb @@ -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 -- cgit v1.2.3 From cd395b9419ac3a860ee78cf9706af1bf7d86fe92 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Thu, 17 Dec 2015 12:54:27 -0700 Subject: Inject the visitor rather than relying on other objects internals This is ultimately messy no matter what, and increases the coupling to the database backend, but we can at least contain it somewhat into an object that's already coupled. --- lib/arel/select_manager.rb | 2 +- lib/arel/visitors/where_sql.rb | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'lib') 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 80797205c9..41972d5836 100644 --- a/lib/arel/visitors/where_sql.rb +++ b/lib/arel/visitors/where_sql.rb @@ -1,12 +1,17 @@ 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 " wheres = o.wheres.map do |where| - Nodes::SqlLiteral.new(@connection.visitor.accept(where, collector.class.new).value) + Nodes::SqlLiteral.new(@inner_visitor.accept(where, collector.class.new).value) end inject_join wheres, collector, ' AND ' -- cgit v1.2.3