Sha256: 988c62f8d34a6b516d7274e7b06972bb9f32e9a2c4c5b11a6ece8dc9de143f96
Contents?: true
Size: 1.6 KB
Versions: 1
Compression:
Stored size: 1.6 KB
Contents
require 'yaml' require 'mysql2_query_filter' require 'term/ansicolor' module Mysql2QueryFilter::Plugin class CasualLog < Filter Mysql2QueryFilter.register(:casual_log, self) REGEXPS = { 'select_type' => Regexp.union( /DEPENDENT\sUNION/, /DEPENDENT\sSUBQUERY/, /UNCACHEABLE\sUNION/, /UNCACHEABLE\sSUBQUERY/ ), 'type' => Regexp.union( /index/, /ALL/ ), 'possible_keys' => Regexp.union( /NULL/ ), 'key' => Regexp.union( /NULL/ ), 'Extra' => Regexp.union( /Using\sfilesort/, /Using\stemporary/ ) } def initialize(options) super @out = @options.delete(:out) || $stderr @client = Mysql2::Client.new(@options) end def filter(sql) if sql =~ /\A\s*select\b/i result = @client.query("EXPLAIN #{sql}").first badquery = colorize_explain(result) output_message(sql, result) if badquery end end private def colorize_explain(explain) badquery = false REGEXPS.each do |key, regexp| value = explain[key] || '' value.gsub!(regexp) do |m| badquery = true colored(m) end end badquery end def colored(str) Term::ANSIColor.red(Term::ANSIColor.bold(str)) end def output_message(sql, explain) message = "# #{sql}\n---\n" max_key_length = explain.keys.map(&:length).max explain.each do |key, value| message << "%*s: %s\n" % [max_key_length, key, value] end @out << message end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mysql2_query_filter-plugin-casual_log-0.0.2 | lib/mysql2_query_filter/plugin/casual_log.rb |