Class: ActiveRecord::StatementCache

Inherits:
Object
  • Object
show all
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(Book.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([], Book.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(Book.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"], Book.connection)

Defined Under Namespace

Classes: BindMap, Params, PartialQuery, PartialQueryCollector, Query, Substitute

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_builder, bind_map, klass) ⇒ StatementCache

Returns a new instance of StatementCache.



123
124
125
126
127
# File 'activerecord/lib/active_record/statement_cache.rb', line 123

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) ⇒ Object



116
117
118
119
120
121
# File 'activerecord/lib/active_record/statement_cache.rb', line 116

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) ⇒ Object



85
86
87
# File 'activerecord/lib/active_record/statement_cache.rb', line 85

def self.partial_query(values)
  PartialQuery.new(values)
end

.partial_query_collectorObject



89
90
91
# File 'activerecord/lib/active_record/statement_cache.rb', line 89

def self.partial_query_collector
  PartialQueryCollector.new
end

.query(sql) ⇒ Object



81
82
83
# File 'activerecord/lib/active_record/statement_cache.rb', line 81

def self.query(sql)
  Query.new(sql)
end

.unsupported_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
# File 'activerecord/lib/active_record/statement_cache.rb', line 139

def self.unsupported_value?(value)
  case value
  when NilClass, Array, Range, Hash, Relation, Base then true
  end
end

Instance Method Details

#execute(params, connection, &block) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'activerecord/lib/active_record/statement_cache.rb', line 129

def execute(params, connection, &block)
  bind_values = bind_map.bind params

  sql = query_builder.sql_for bind_values, connection

  klass.find_by_sql(sql, bind_values, preparable: true, &block)
rescue ::RangeError
  nil
end