提交 2a56b2d9 编写于 作者: S Sean Griffin

Ensure prepared statement caching still occurs with Adequate Record

In Rails 5, we're much more restrictive about when we do or don't cache
a prepared statement. In particular, we never cache when we are sending
an IN statement or a SQL string literal

However, in the case of Adequate Record, we are *always* sending a raw
SQL string, and we *always* want to cache the result.

Fixes #23507

/cc @tgxworld
上级 3a7a021e
......@@ -27,10 +27,10 @@ def cacheable_query(arel) # :nodoc:
end
# Returns an ActiveRecord::Result instance.
def select_all(arel, name = nil, binds = [])
def select_all(arel, name = nil, binds = [], preparable: nil)
arel, binds = binds_from_relation arel, binds
sql = to_sql(arel, binds)
if arel.is_a?(String)
if arel.is_a?(String) && preparable.nil?
preparable = false
else
preparable = visitor.preparable
......
......@@ -61,11 +61,11 @@ def clear_query_cache
@query_cache.clear
end
def select_all(arel, name = nil, binds = [])
def select_all(arel, name = nil, binds = [], preparable: nil)
if @query_cache_enabled && !locked?(arel)
arel, binds = binds_from_relation arel, binds
sql = to_sql(arel, binds)
cache_sql(sql, binds) { super(sql, name, binds) }
cache_sql(sql, binds) { super(sql, name, binds, preparable: visitor.preparable) }
else
super
end
......
......@@ -35,8 +35,8 @@ module Querying
#
# Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]
# Post.find_by_sql ["SELECT body FROM comments WHERE author = :user_id OR approved_by = :user_id", { :user_id => user_id }]
def find_by_sql(sql, binds = [])
result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds)
def find_by_sql(sql, binds = [], preparable: nil)
result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds, preparable: preparable)
column_types = result_set.column_types.dup
columns_hash.each_key { |k| column_types.delete k }
message_bus = ActiveSupport::Notifications.instrumenter
......
......@@ -106,7 +106,7 @@ def execute(params, klass, connection)
sql = query_builder.sql_for bind_values, connection
klass.find_by_sql sql, bind_values
klass.find_by_sql(sql, bind_values, preparable: true)
end
alias :call :execute
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册