提交 3befc7a9 编写于 作者: R Ryuta Kamizono

Use `connection#to_sql` for construct an SQL

上级 d8bc0964
require "arel/collectors/bind"
module ActiveRecord module ActiveRecord
# = Active Record \Relation # = Active Record \Relation
class Relation class Relation
...@@ -597,19 +595,16 @@ def reset ...@@ -597,19 +595,16 @@ def reset
# # => SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar' # # => SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar'
def to_sql def to_sql
@to_sql ||= begin @to_sql ||= begin
relation = self relation = self
connection = klass.connection
visitor = connection.visitor
if eager_loading? if eager_loading?
find_with_associations { |rel| relation = rel } find_with_associations { |rel| relation = rel }
end end
binds = relation.bound_attributes conn = klass.connection
binds = connection.prepare_binds_for_database(binds) conn.unprepared_statement {
binds.map! { |value| connection.quote(value) } conn.to_sql(relation.arel, relation.bound_attributes)
collect = visitor.accept(relation.arel.ast, Arel::Collectors::Bind.new) }
collect.substitute_binds(binds).join
end end
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册