Class: Sequel::Rails::Railties::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/sequel/rails/railties/log_subscriber.rb

Constant Summary collapse

IGNORE_PAYLOAD_NAMES =
["SCHEMA", "EXPLAIN"]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.countObject



21
22
23
# File 'lib/sequel/rails/railties/log_subscriber.rb', line 21

def self.count
  Thread.current["sequel_sql_count"] ||= 0
end

.count=(value) ⇒ Object



17
18
19
# File 'lib/sequel/rails/railties/log_subscriber.rb', line 17

def self.count=(value)
  Thread.current["sequel_sql_count"] = value
end

.reset_countObject



31
32
33
34
35
# File 'lib/sequel/rails/railties/log_subscriber.rb', line 31

def self.reset_count
  previous = count
  self.count = 0
  previous
end

.reset_runtimeObject



25
26
27
28
29
# File 'lib/sequel/rails/railties/log_subscriber.rb', line 25

def self.reset_runtime
  previous = runtime
  self.runtime = 0
  previous
end

.runtimeObject



13
14
15
# File 'lib/sequel/rails/railties/log_subscriber.rb', line 13

def self.runtime
  Thread.current["sequel_sql_runtime"] ||= 0
end

.runtime=(value) ⇒ Object



9
10
11
# File 'lib/sequel/rails/railties/log_subscriber.rb', line 9

def self.runtime=(value)
  Thread.current["sequel_sql_runtime"] = value
end

Instance Method Details

#sql(event) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sequel/rails/railties/log_subscriber.rb', line 37

def sql(event)
  self.class.runtime += event.duration
  self.class.count += 1
  return unless logger.debug?

  payload = event.payload

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

  name = format("%s (%.1fms)", payload[:name], event.duration)
  sql = payload[:sql].squeeze(" ")
  binds = nil

  if payload[:binds]&.any?
    casted_params = type_casted_binds(payload[:type_casted_binds])

    binds = []
    payload[:binds].each_with_index do |attr, i|
      attribute_name = if attr.respond_to?(:name)
        attr.name
      elsif attr.respond_to?(:[]) && attr[i].respond_to?(:name)
        attr[i].name
      end

      filtered_params = filter(attribute_name, casted_params[i])

      binds << render_bind(attr, filtered_params)
    end
    binds = binds.inspect
    binds.prepend("  ")
  end

  name = colorize_payload_name(name, payload[:name])
  sql = color(sql, sql_color(sql), bold: true) if colorize_logging

  debug "  #{name}  #{sql}#{binds}"
end