Module: ArPrettySql::InstanceMethods

Defined in:
lib/ar_pretty_sql.rb

Instance Method Summary collapse

Instance Method Details

#pp(**options) ⇒ Object



59
60
61
# File 'lib/ar_pretty_sql.rb', line 59

def pp(**options)
  puts to_pretty_sql(**options)
end

#to_pretty_sql(**options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ar_pretty_sql.rb', line 28

def to_pretty_sql(**options)
  conf = ArPrettySql::Config.to_h.merge! options

  result = to_sql.dup

  if conf[:format]
    rule = AnbtSql::Rule.new

    rule.keyword = case conf[:keyword_case].to_sym
                   when :upper then AnbtSql::Rule::KEYWORD_UPPER_CASE
                   when :lower then AnbtSql::Rule::KEYWORD_LOWER_CASE
                   when :unchanged then AnbtSql::Rule::KEYWORD_NONE
                   end

    conf[:additional_sql_functions]&.each do |func_name|
      rule.function_names << func_name.upcase
    end

    formatter = AnbtSql::Formatter.new(rule)
    result = formatter.format(result)
  end

  if conf[:color] || conf[:colour]
    lexer = Rouge::Lexers::SQL.new
    formatter = Rouge::Formatters::Terminal256.new(conf[:theme])
    result = formatter.format(lexer.lex(result))
  end

  result
end