diff options
Diffstat (limited to 'activerecord/lib/active_record/statement_cache.rb')
-rw-r--r-- | activerecord/lib/active_record/statement_cache.rb | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/statement_cache.rb b/activerecord/lib/active_record/statement_cache.rb index dd4ee0c4a0..90d4748d84 100644 --- a/activerecord/lib/active_record/statement_cache.rb +++ b/activerecord/lib/active_record/statement_cache.rb @@ -14,13 +14,25 @@ module ActiveRecord # 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. class StatementCache - def initialize - @relation = yield - raise ArgumentError.new("Statement cannot be nil") if @relation.nil? + def initialize(block = Proc.new) + @mutex = Mutex.new + @relation = nil + @block = block end - def execute - @relation.dup.to_a + def execute(*vals) + rel = relation vals + @mutex.synchronize do + rel.set_binds vals + rel.to_a + end + end + + private + def relation(values) + @relation || @mutex.synchronize { + @block.call(*values) + } end end end |