5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
58
59
60
61
62
63
|
# File 'lib/activerecord-analyze/main.rb', line 5
def analyze(arel, binds = [], opts = {})
format_sql = if fmt = opts[:format].presence
case fmt
when :json
"FORMAT JSON, "
when :hash
"FORMAT JSON, "
when :pretty_json
"FORMAT JSON, "
when :yaml
"FORMAT YAML, "
when :text
"FORMAT TEXT, "
when :xml
"FORMAT XML, "
else
""
end
end
verbose_sql = if opts[:verbose] == true
", VERBOSE"
end
costs_sql = if opts[:costs] == true
", COSTS"
end
settings_sql = if opts[:settings] == true
", SETTINGS"
end
buffers_sql = if opts[:buffers] == true
", BUFFERS"
end
timing_sql = if opts[:timing] == true
", TIMING"
end
summary_sql = if opts[:summary] == true
", SUMMARY"
end
analyze_sql = if opts[:analyze] == false
""
else
"ANALYZE"
end
opts_sql = "(#{format_sql}#{analyze_sql}#{verbose_sql}#{costs_sql}#{settings_sql}#{buffers_sql}#{timing_sql}#{summary_sql})"
.strip.gsub(/\s+/, " ")
.gsub(/\(\s?\s?\s?,/, "(")
.gsub(/\s,\s/, " ")
.gsub(/\(\s?\)/, "")
sql = "EXPLAIN #{opts_sql} #{to_sql(arel, binds)}"
PostgreSQL::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN #{opts_sql}".strip, binds))
end
|