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

Use connection#to_sql for construct an SQL

This commit is contained in:
Ryuta Kamizono 2016-07-19 19:52:00 +09:00
parent d8bc0964d2
commit 3befc7a9ef

View file

@ -1,5 +1,3 @@
require "arel/collectors/bind"
module ActiveRecord
# = Active Record \Relation
class Relation
@ -597,19 +595,16 @@ module ActiveRecord
# # => SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar'
def to_sql
@to_sql ||= begin
relation = self
connection = klass.connection
visitor = connection.visitor
relation = self
if eager_loading?
find_with_associations { |rel| relation = rel }
end
binds = relation.bound_attributes
binds = connection.prepare_binds_for_database(binds)
binds.map! { |value| connection.quote(value) }
collect = visitor.accept(relation.arel.ast, Arel::Collectors::Bind.new)
collect.substitute_binds(binds).join
conn = klass.connection
conn.unprepared_statement {
conn.to_sql(relation.arel, relation.bound_attributes)
}
end
end