module Flog::SqlFormattable

SqlFormattable enables to format SQL log

Public Instance Methods

sql(event) click to toggle source
Calls superclass method
# File lib/flog/sql_formattable.rb, line 11
def sql(event)
  return super(event) unless formattable?(event)

  formatted = format_sql(event.payload[:sql])

  shunt_payload_value(event.payload, :sql, "\n#{Flog.config.sql_indent}#{formatted}") do
    super(event)
  end
end

Private Instance Methods

duration_over?(event) click to toggle source
# File lib/flog/sql_formattable.rb, line 50
def duration_over?(event)
  event.duration >= Flog.config.query_duration_threshold.to_f
end
format_sql(sql) click to toggle source
# File lib/flog/sql_formattable.rb, line 23
def format_sql(sql)
  return sql if sql.blank?

  require 'anbt-sql-formatter/formatter'
  rule = AnbtSql::Rule.new
  rule.keyword = AnbtSql::Rule::KEYWORD_UPPER_CASE
  rule.indent_string = Flog.config.sql_indent
  rule.in_values_num = Flog.config.sql_in_values_num
  %w[count sum].each do |function_name|
    rule.function_names << function_name
  end
  AnbtSql::Formatter.new(rule).format(sql.squeeze(' '))
end
formattable?(event) click to toggle source
# File lib/flog/sql_formattable.rb, line 37
def formattable?(event)
  return false if Flog.config.ignore_query?
  return false unless Flog::Status.sql_formattable?

  return false if ignore_by_cached_query?(event)

  duration_over?(event)
end
ignore_by_cached_query?(event) click to toggle source
# File lib/flog/sql_formattable.rb, line 46
def ignore_by_cached_query?(event)
  (event.payload[:name] == 'CACHE' || event.payload[:cached]) && Flog.config.ignore_cached_query?
end