Class: ActiveRecord::StatementCache
Do not use. This class is for internal use only.
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Inherits: | Object |
Defined in: | activerecord/lib/active_record/statement_cache.rb |
Overview
Statement cache is used to cache a single statement in order to avoid creating the AST again. Initializing the cache is done by passing the statement in the create block:
cache = StatementCache.create(ClothingItem.lease_connection) do |params|
Book.where(name: "my book").where("author_id > 3")
end
The cached statement is executed by using the connection.execute method:
cache.execute([], ClothingItem.lease_connection)
The relation returned by the block is cached, and for each execute call the cached relation gets duped. Database is queried when to_a
is called on the relation.
If you want to cache the statement without the values you can use the bind
method of the block parameter.
cache = StatementCache.create(ClothingItem.lease_connection) do |params|
Book.where(name: params.bind)
end
And pass the bind values as the first argument of #execute call.
cache.execute(["my book"], ClothingItem.lease_connection)
Class Method Summary
Instance Attribute Summary
- #bind_map readonly private
- #klass readonly private
- #query_builder readonly private
Instance Method Summary
Constructor Details
.new(query_builder, bind_map, klass) ⇒ StatementCache
# File 'activerecord/lib/active_record/statement_cache.rb', line 139
def initialize(query_builder, bind_map, klass) @query_builder = query_builder @bind_map = bind_map @klass = klass end
Class Method Details
.create(connection, callable = nil, &block)
[ GitHub ]# File 'activerecord/lib/active_record/statement_cache.rb', line 132
def self.create(connection, callable = nil, &block) relation = (callable || block).call Params.new query_builder, binds = connection.cacheable_query(self, relation.arel) bind_map = BindMap.new(binds) new(query_builder, bind_map, relation.klass) end
.partial_query(values)
[ GitHub ]# File 'activerecord/lib/active_record/statement_cache.rb', line 101
def self.partial_query(values) PartialQuery.new(values) end
.partial_query_collector
[ GitHub ]# File 'activerecord/lib/active_record/statement_cache.rb', line 105
def self.partial_query_collector PartialQueryCollector.new end
.query(sql)
[ GitHub ]
.unsupported_value?(value) ⇒ Boolean
Instance Attribute Details
#bind_map (readonly, private)
[ GitHub ]# File 'activerecord/lib/active_record/statement_cache.rb', line 162
attr_reader :query_builder, :bind_map, :klass
#klass (readonly, private)
[ GitHub ]# File 'activerecord/lib/active_record/statement_cache.rb', line 162
attr_reader :query_builder, :bind_map, :klass
#query_builder (readonly, private)
[ GitHub ]Instance Method Details
#execute(params, connection, allow_retry: false, &block)
[ GitHub ]# File 'activerecord/lib/active_record/statement_cache.rb', line 145
def execute(params, connection, allow_retry: false, &block) bind_values = bind_map.bind params sql = query_builder.sql_for bind_values, connection klass.find_by_sql(sql, bind_values, preparable: true, allow_retry: allow_retry, &block) rescue ::RangeError [] end