# class ActiveRecord::LogSubscriber

### Constants

IGNORE_PAYLOAD_NAMES

### Public Class Methods

reset_runtime() click to toggle source

    # File lib/active_record/log_subscriber.rb, line 13
    def self.reset_runtime
      rt, self.runtime = runtime, 0
      rt
    end

runtime() click to toggle source

    # File lib/active_record/log_subscriber.rb, line 9
    def self.runtime
      ActiveRecord::RuntimeRegistry.sql_runtime ||= 0
    end

runtime=(value) click to toggle source

    # File lib/active_record/log_subscriber.rb, line 5
    def self.runtime=(value)
      ActiveRecord::RuntimeRegistry.sql_runtime = value
    end

### Public Instance Methods

sql(event) click to toggle source

    # File lib/active_record/log_subscriber.rb, line 18
    def sql(event)
      self.class.runtime += event.duration
      return unless logger.debug?

      payload = event.payload

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

      name  = "#{payload[:name]} (#{event.duration.round(1)}ms)"
      name  = "CACHE #{name}" if payload[:cached]
      sql   = payload[:sql]
      binds = nil

      unless (payload[:binds] || []).empty?
        casted_params = type_casted_binds(payload[:binds], payload[:type_casted_binds])
        binds = "  " + payload[:binds].zip(casted_params).map { |attr, value|
          render_bind(attr, value)
        }.inspect
      end

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

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

### Private Instance Methods

colorize_payload_name(name, payload_name) click to toggle source

    # File lib/active_record/log_subscriber.rb, line 60
    def colorize_payload_name(name, payload_name)
      if payload_name.blank? || payload_name == "SQL" # SQL vs Model Load/Exists
        color(name, MAGENTA, true)
      else
        color(name, CYAN, true)
      end
    end

logger() click to toggle source

    # File lib/active_record/log_subscriber.rb, line 89
    def logger
      ActiveRecord::Base.logger
    end

render_bind(attr, value) click to toggle source

    # File lib/active_record/log_subscriber.rb, line 50
    def render_bind(attr, value)
      if attr.is_a?(Array)
        attr = attr.first
      elsif attr.type.binary? && attr.value
        value = "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>"
      end

      [attr && attr.name, value]
    end

sql_color(sql) click to toggle source

    # File lib/active_record/log_subscriber.rb, line 68
    def sql_color(sql)
      case sql
      when /\A\s*rollback/mi
        RED
      when /select .*for update/mi, /\A\s*lock/mi
        WHITE
      when /\A\s*select/i
        BLUE
      when /\A\s*insert/i
        GREEN
      when /\A\s*update/i
        YELLOW
      when /\A\s*delete/i
        RED
      when /transaction\s*\Z/i
        CYAN
      else
        MAGENTA
      end
    end

type_casted_binds(binds, casted_binds) click to toggle source

    # File lib/active_record/log_subscriber.rb, line 46
    def type_casted_binds(binds, casted_binds)
      casted_binds || ActiveRecord::Base.connection.type_casted_binds(binds)
    end