mysql2_query_filter-plugin-casual_log 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7deeddc1b117319fe5c95c760314bee02b69c5b7
4
+ data.tar.gz: ab110e16e8da18ef0127373eac9dbe3820fa0bf6
5
+ SHA512:
6
+ metadata.gz: f5f74d16993aced999182cf6ec4f242f099c02e2e5b9c46a8f84a63a29a830f3ad7131166e07b64439759a065fefd31158575c93787dd0e8301cef276673f8cb
7
+ data.tar.gz: 7c17a7148e4af433c41c5341476ab16cbccd6e5f6b01fb524e729f6f1a1889c3f6a2f07afffeacbcd0d67aeb5899f7b6d105f981acdebc73df178927f3400b90
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ test.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mysql2_query_filter-plugin-casual_log.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Genki Sugawara
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Mysql2QueryFilter::Plugin::CasualLog
2
+
3
+ Plug-in that colorize the bad query for [Mysql2QueryFilter](https://github.com/winebarrel/mysql2_query_filter).
4
+
5
+ Porting of [MySQLCasualLog.pm](https://gist.github.com/kamipo/839e8a5b6d12bddba539).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'mysql2_query_filter-plugin-casual_log'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mysql2_query_filter-plugin-casual_log
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'mysql2_query_filter'
27
+
28
+ Mysql2QueryFilter.configure do |filter|
29
+ filter.plugin :casual_log , database: 'mysql'
30
+ end
31
+
32
+ Mysql2QueryFilter.enable
33
+
34
+ client = Mysql2::Client.new(host: 'localhost', username: 'root', database: 'mysql')
35
+ client.query('SELECT * FROM user')
36
+ # => # SELECT * FROM user
37
+ # ---
38
+ # id: 1
39
+ # select_type: SIMPLE
40
+ # table: user
41
+ # type: ALL # <- red/bold
42
+ # possible_keys:
43
+ # key:
44
+ # key_len:
45
+ # ref:
46
+ # rows: 4
47
+ # Extra:
48
+ ```
49
+
50
+ ![](http://i.gyazo.com/66a769f30eab5ff56655977d42a30f4d.png)
51
+
52
+ ## Retated links
53
+
54
+ * http://kamipo.github.io/talks/20140711-mysqlcasual6
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,77 @@
1
+ require 'yaml'
2
+ require 'mysql2_query_filter'
3
+ require 'term/ansicolor'
4
+
5
+ module Mysql2QueryFilter::Plugin
6
+ class CasualLog < Filter
7
+ Mysql2QueryFilter.register(:casual_log, self)
8
+
9
+ REGEXPS = {
10
+ 'select_type' => Regexp.union(
11
+ /DEPENDENT\sUNION/,
12
+ /DEPENDENT\sSUBQUERY/,
13
+ /UNCACHEABLE\sUNION/,
14
+ /UNCACHEABLE\sSUBQUERY/
15
+ ),
16
+ 'type' => Regexp.union(
17
+ /index/,
18
+ /ALL/
19
+ ),
20
+ 'possible_keys' => Regexp.union(
21
+ /NULL/
22
+ ),
23
+ 'key' => Regexp.union(
24
+ /NULL/
25
+ ),
26
+ 'Extra' => Regexp.union(
27
+ /Using\sfilesort/,
28
+ /Using\stemporary/
29
+ )
30
+ }
31
+
32
+ def initialize(options)
33
+ super
34
+ @out = @options.delete(:out) || $stderr
35
+ @client = Mysql2::Client.new(@options)
36
+ end
37
+
38
+ def filter(sql)
39
+ if sql =~ /\Aselect\b/i
40
+ result = @client.query("EXPLAIN #{sql}").first
41
+ badquery = colorize_explain(result)
42
+
43
+ if badquery
44
+ @out.puts "# #{sql}\n---"
45
+ max_key_length = result.keys.map(&:length).max
46
+
47
+ result.each do |key, value|
48
+ @out.puts "%*s: %s" % [max_key_length, key, value]
49
+ end
50
+
51
+ @out.puts
52
+ end
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def colorize_explain(explain)
59
+ badquery = false
60
+
61
+ REGEXPS.each do |key, regexp|
62
+ value = explain[key] || ''
63
+
64
+ value.gsub!(regexp) do |m|
65
+ badquery = true
66
+ colored(m)
67
+ end
68
+ end
69
+
70
+ badquery
71
+ end
72
+
73
+ def colored(str)
74
+ Term::ANSIColor.red(Term::ANSIColor.bold(str))
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = 'mysql2_query_filter-plugin-casual_log'
4
+ spec.version = '0.0.1'
5
+ spec.authors = ['Genki Sugawara']
6
+ spec.email = ['sgwr_dts@yahoo.co.jp']
7
+ spec.summary = %q{Plug-in that colorize the bad query for Mysql2QueryFilter.}
8
+ spec.description = %q{Plug-in that colorize the bad query for Mysql2QueryFilter.}
9
+ spec.homepage = ''
10
+ spec.license = 'MIT'
11
+
12
+ spec.files = `git ls-files -z`.split("\x0")
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_dependency 'mysql2_query_filter'
18
+ spec.add_dependency 'term-ansicolor'
19
+ spec.add_development_dependency 'bundler'
20
+ spec.add_development_dependency 'rake'
21
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql2_query_filter-plugin-casual_log
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Genki Sugawara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mysql2_query_filter
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: term-ansicolor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Plug-in that colorize the bad query for Mysql2QueryFilter.
70
+ email:
71
+ - sgwr_dts@yahoo.co.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/mysql2_query_filter/plugin/casual_log.rb
82
+ - mysql2_query_filter-plugin-casual_log.gemspec
83
+ homepage: ''
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.0.14
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Plug-in that colorize the bad query for Mysql2QueryFilter.
107
+ test_files: []