Class: QueryCount::Counter

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/query_count/counter.rb

Constant Summary collapse

IGNORE_PAYLOAD_NAMES =
%w[SCHEMA EXPLAIN].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.counterObject



12
13
14
# File 'lib/query_count/counter.rb', line 12

def self.counter
  Thread.current['query_count'] ||= 0
end

.counter=(value) ⇒ Object



8
9
10
# File 'lib/query_count/counter.rb', line 8

def self.counter=(value)
  Thread.current['query_count'] = value
end

.counter_cacheObject



20
21
22
# File 'lib/query_count/counter.rb', line 20

def self.counter_cache
  Thread.current['query_count_cache'] ||= 0
end

.counter_cache=(value) ⇒ Object



16
17
18
# File 'lib/query_count/counter.rb', line 16

def self.counter_cache=(value)
  Thread.current['query_count_cache'] = value
end

.reset_counterObject



24
25
26
27
28
29
# File 'lib/query_count/counter.rb', line 24

def self.reset_counter
  rc = counter
  self.counter = 0

  rc
end

.reset_counter_cacheObject



31
32
33
34
35
36
# File 'lib/query_count/counter.rb', line 31

def self.reset_counter_cache
  rcc = counter_cache
  self.counter_cache = 0

  rcc
end

Instance Method Details

#sql(event) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/query_count/counter.rb', line 38

def sql(event)
  payload = event.payload

  return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])

  if payload[:cached]
    self.class.counter_cache += 1
  else
    self.class.counter += 1
  end
end