Class: ActiveRecord::ConnectionAdapters::StatementPool
  
  
  
  
  
    - Inherits:
 
    - 
      Object
      
        
          - Object
 
          
            - ActiveRecord::ConnectionAdapters::StatementPool
 
          
        
        show all
      
     
  
  
  
  
  
  
  
      - Includes:
 
      - Enumerable
 
  
  
  
  
  
  
    - Defined in:
 
    - activerecord/lib/active_record/connection_adapters/statement_pool.rb
 
  
  
 
Overview
  
  
    
      Constant Summary
      collapse
    
    
      
        - DEFAULT_STATEMENT_LIMIT =
          
        
 
        1000
 
      
    
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  Methods included from Enumerable
  #as_json, #compact_blank, #exclude?, #excluding, #in_order_of, #including, #index_by, #index_with, #many?, #maximum, #minimum, #pick, #pluck, #sole
  
  
  
  
  
  
  Constructor Details
  
    
  
  
    #initialize(statement_limit = nil)  ⇒ StatementPool 
  
  
  
  
    
Returns a new instance of StatementPool.
   
 
  
  
    
      
10
11
12
13 
     | 
    
      # File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 10
def initialize(statement_limit = nil)
  @cache = Hash.new { |h, pid| h[pid] = {} }
  @statement_limit = statement_limit || DEFAULT_STATEMENT_LIMIT
end
     | 
  
 
  
 
  
    Instance Method Details
    
      
  
  
    
      
23
24
25 
     | 
    
      # File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 23
def [](key)
  cache[key]
end 
     | 
  
 
    
      
  
  
    #[]=(sql, stmt)  ⇒ Object 
  
  
  
  
    
      
31
32
33
34
35
36 
     | 
    
      # File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 31
def []=(sql, stmt)
  while @statement_limit <= cache.size
    dealloc(cache.shift.last)
  end
  cache[sql] = stmt
end
     | 
  
 
    
      
  
  
    
      
38
39
40
41
42
43 
     | 
    
      # File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 38
def clear
  cache.each_value do |stmt|
    dealloc stmt
  end
  cache.clear
end
     | 
  
 
    
      
  
  
    #delete(key)  ⇒ Object 
  
  
  
  
    
      
52
53
54
55
56
57 
     | 
    
      # File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 52
def delete(key)
  if stmt = cache.delete(key)
    dealloc(stmt)
  end
  stmt
end
     | 
  
 
    
      
  
  
    #each(&block)  ⇒ Object 
  
  
  
  
    
      
15
16
17 
     | 
    
      # File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 15
def each(&block)
  cache.each(&block)
end 
     | 
  
 
    
      
  
  
    #key?(key)  ⇒ Boolean 
  
  
  
  
    
      
19
20
21 
     | 
    
      # File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 19
def key?(key)
  cache.key?(key)
end 
     | 
  
 
    
      
  
  
    
      
27
28
29 
     | 
    
      # File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 27
def length
  cache.length
end 
     | 
  
 
    
      
  
  
    
Clear the pool without deallocating; this is only safe when we know the server has independently deallocated all statements (e.g. due to a reconnect, or a DISCARD ALL)
   
 
  
  
    
      
48
49
50 
     | 
    
      # File 'activerecord/lib/active_record/connection_adapters/statement_pool.rb', line 48
def reset
  cache.clear
end 
     |