Module: Footnotes::Extensions::QueryAnalyzer

Defined in:
lib/rails-footnotes/notes/queries_note.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



96
97
98
99
100
# File 'lib/rails-footnotes/notes/queries_note.rb', line 96

def self.included(base)
  base.class_eval do
    alias_method_chain :execute, :analyzer
  end
end

Instance Method Details

#execute_with_analyzer(query, name = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rails-footnotes/notes/queries_note.rb', line 102

def execute_with_analyzer(query, name = nil)
  query_results = nil
  time = Benchmark.realtime { query_results = execute_without_analyzer(query, name) }

  if query =~ /^(select|create|update|delete)\b/i
    type = $&.downcase.to_sym
    explain = nil

    if adapter_name == 'MySQL' && type == :select
      log_silence do
        explain = execute_without_analyzer("EXPLAIN #{query}", name)
      end
    end
    Footnotes::Notes::QueriesNote.sql << Footnotes::Extensions::Sql.new(type, name, time, query, explain)
  end

  query_results
end