Make where_sql more few code

To be honest, it is hardly worth having a dedicated method for
`where_sql`, since it is a reduced version of `to_sql`.

I've left it for now, but just made it more few code (used `to_sql`).
This commit is contained in:
Ryuta Kamizono 2020-05-25 08:18:06 +09:00
parent bd49578cf5
commit b1646a48fa
4 changed files with 3 additions and 27 deletions

@ -328,8 +328,8 @@ def exists?(conditions = :none)
# the expected number of results should be provided in the +expected_size+
# argument.
def raise_record_not_found_exception!(ids = nil, result_size = nil, expected_size = nil, key = primary_key, not_found_ids = nil) # :nodoc:
conditions = arel.where_sql(@klass)
conditions = " [#{conditions}]" if conditions
conditions = " [#{arel.where_sql(klass)}]" unless where_clause.empty?
name = @klass.name
if ids.nil?

@ -186,8 +186,7 @@ def orders
def where_sql(engine = Table.engine)
return if @ctx.wheres.empty?
viz = Visitors::WhereSql.new(engine.connection.visitor, engine.connection)
Nodes::SqlLiteral.new viz.accept(@ctx, Collectors::SQLString.new).value
Nodes::SqlLiteral.new("WHERE #{Nodes::And.new(@ctx.wheres).to_sql(engine)}")
end
def union(operation, other = nil)

@ -5,7 +5,6 @@
require "arel/visitors/sqlite"
require "arel/visitors/postgresql"
require "arel/visitors/mysql"
require "arel/visitors/where_sql"
require "arel/visitors/dot"
module Arel # :nodoc: all

@ -1,22 +0,0 @@
# frozen_string_literal: true
module Arel # :nodoc: all
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(@inner_visitor.accept(where, collector.class.new).value)
end
inject_join wheres, collector, " AND "
end
end
end
end