Sha256: 24a29cafd98ef2e09b123e3c66c297981a57326bd50fed7287ad014679107f09
Contents?: true
Size: 1.67 KB
Versions: 3
Compression:
Stored size: 1.67 KB
Contents
module ActiveRecord module ConnectionAdapters module PostgreSQL module DatabaseStatements def analyze(arel, binds = [], opts = {}) opts_sql = ActiveRecordAnalyze.build_prefix(opts) sql = "EXPLAIN #{opts_sql} #{to_sql(arel, binds)}" PostgreSQL::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN #{opts_sql}".strip, binds)) end end end end end module ActiveRecord class Relation def analyze(opts = {}) res = exec_analyze(collecting_queries_for_explain { exec_queries }, opts) if [:json, :hash, :pretty_json].include?(opts[:format]) start = res.index("[\n") finish = res.rindex("]") raw_json = res.slice(start, finish - start + 1) if opts[:format] == :json JSON.parse(raw_json).to_json elsif opts[:format] == :hash JSON.parse(raw_json) elsif opts[:format] == :pretty_json JSON.pretty_generate(JSON.parse(raw_json)) end else res end end end end module ActiveRecord module Explain def exec_analyze(queries, opts = {}) # :nodoc: str = queries.map do |sql, binds| analyze_msg = if opts[:analyze] == false "" else " ANALYZE" end msg = "EXPLAIN#{analyze_msg} for: #{sql}".dup unless binds.empty? msg << " " msg << binds.map { |attr| render_bind(attr) }.inspect end msg << "\n" msg << connection.analyze(sql, binds, opts) end.join("\n") # Overriding inspect to be more human readable, especially in the console. def str.inspect self end str end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
activerecord-analyze-0.10.2 | lib/activerecord-analyze/main.rb |
activerecord-analyze-0.10.1 | lib/activerecord-analyze/main.rb |
activerecord-analyze-0.10.0 | lib/activerecord-analyze/main.rb |