Module: PrettySql

Defined in:
lib/pretty_sql.rb

Constant Summary collapse

PYGMENTIZE_EXEC_NAME =
'pygmentize'

Instance Method Summary collapse

Instance Method Details

#prettify_sql(sql) ⇒ Object



7
8
9
# File 'lib/pretty_sql.rb', line 7

def prettify_sql(sql)
  pygmentize_sql(tidy_sql(sql))
end

#pygmentize_sql(sql) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pretty_sql.rb', line 11

def pygmentize_sql(sql)
  # Colorize SQL by piping to pygmentize if it exists.
  
  return sql unless pygmentize_path

  IO.popen("#{pygmentize_path} -l sql", 'r+') do |process|
    process.write(sql)
    process.close_write
    process.read
  end
end

#tidy_sql(sql) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pretty_sql.rb', line 23

def tidy_sql(sql)
  # Auto-indent SQL

  rule = AnbtSql::Rule.new
  rule.keyword = AnbtSql::Rule::KEYWORD_UPPER_CASE
  %w(count sum substr date).each do |func_name|
    rule.function_names << func_name.upcase
  end
  rule.indent_string = "    "
  formatter = AnbtSql::Formatter.new(rule)
  formatter.format(sql)
end