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



144
145
146
147
148
# File 'lib/rails-footnotes/notes/queries_note.rb', line 144

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rails-footnotes/notes/queries_note.rb', line 150

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 && Footnotes::Notes::QueriesNote.sql_explain
      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