Module: Switchman::ActiveRecord::StatementCache
- Defined in:
- lib/switchman/active_record/statement_cache.rb
Defined Under Namespace
Modules: BindMap, ClassMethods
Instance Method Summary
collapse
Instance Method Details
#execute(params, klass, connection) ⇒ Object
since the StatememtCache is only implemented for basic relations in AR::Base#find, AR::Base#find_by and AR::Association#get_records, we can make some assumptions about the shard source (e.g. infer from the primary key or use the current shard)
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/switchman/active_record/statement_cache.rb', line 25
def execute(params, klass, connection)
target_shard = nil
if primary_index = bind_map.primary_value_index
primary_value = params[primary_index]
target_shard = Shard.local_id_for(primary_value)[1]
end
current_shard = Shard.current(klass.shard_category)
target_shard ||= current_shard
bind_values = bind_map.bind(params, current_shard, target_shard)
target_shard.activate(klass.shard_category) do
if connection.use_qualified_names?
sql = qualified_query_builder(target_shard, klass).sql_for(bind_values, connection)
klass.find_by_sql(sql, bind_values)
else
sql = generic_query_builder(connection).sql_for(bind_values, connection)
klass.find_by_sql(sql, bind_values)
end
end
end
|
#generic_query_builder(connection) ⇒ Object
47
48
49
|
# File 'lib/switchman/active_record/statement_cache.rb', line 47
def generic_query_builder(connection)
@query_builder ||= connection.cacheable_query(@arel)
end
|
#initialize(arel, bind_map) ⇒ Object
14
15
16
17
18
|
# File 'lib/switchman/active_record/statement_cache.rb', line 14
def initialize(arel, bind_map)
@arel = arel
@bind_map = bind_map
@qualified_query_builders = {}
end
|
#qualified_query_builder(shard, klass) ⇒ Object
51
52
53
|
# File 'lib/switchman/active_record/statement_cache.rb', line 51
def qualified_query_builder(shard, klass)
@qualified_query_builders[shard.id] ||= klass.connection.cacheable_query(@arel)
end
|