mysql2_query_filter-plugin-casual_log 0.0.5 → 0.1.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 +4 -4
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/README.md +7 -7
- data/Rakefile +4 -0
- data/lib/mysql2_query_filter/plugin/casual_log.rb +31 -18
- data/mysql2_query_filter-plugin-casual_log.gemspec +4 -2
- data/spec/mysql2_query_filter-plugin-casual_log_spec.rb +47 -0
- data/spec/spec_helper.rb +17 -0
- metadata +39 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c9a11dd7e3e1c153515f7a4856d9bf10ab836e8
|
4
|
+
data.tar.gz: 92ad092a5a754b212ea682bbbf45aa2bacd0f66f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97a27c7ff048ff3fb148a05b33d03fad26f2676cc78d20c133b3308aeb74fd029550a4d1f69d7cb0309b2a6fe5308d76c775eadfa123075abff8f688c31a2cff
|
7
|
+
data.tar.gz: 1823dd345272de64cc1db24302c9cac952c71565454001777ae5a49159c8a156eda3c5fbcae3d99e1b3f5461dc986f371106ef39d5179921a7ce92483c5bde7a
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
# Mysql2QueryFilter::Plugin::CasualLog
|
2
2
|
|
3
3
|
Plug-in that colorize the bad query for [Mysql2QueryFilter](https://github.com/winebarrel/mysql2_query_filter).
|
4
|
+
It is porting of [MySQLCasualLog.pm](https://gist.github.com/kamipo/839e8a5b6d12bddba539).
|
4
5
|
|
5
|
-
|
6
|
+
see http://kamipo.github.io/talks/20140711-mysqlcasual6
|
7
|
+
|
8
|
+
[](http://badge.fury.io/rb/mysql2_query_filter-plugin-casual_log)
|
9
|
+
[](https://travis-ci.org/winebarrel/mysql2_query_filter-plugin-casual_log)
|
6
10
|
|
7
11
|
## Installation
|
8
12
|
|
@@ -26,10 +30,10 @@ Or install it yourself as:
|
|
26
30
|
require 'mysql2_query_filter'
|
27
31
|
|
28
32
|
Mysql2QueryFilter.configure do |filter|
|
29
|
-
filter.plugin :casual_log
|
33
|
+
filter.plugin :casual_log #, client: Mysql2::Client.new(...)
|
30
34
|
end
|
31
35
|
|
32
|
-
Mysql2QueryFilter.enable
|
36
|
+
Mysql2QueryFilter.enable!
|
33
37
|
|
34
38
|
client = Mysql2::Client.new(host: 'localhost', username: 'root', database: 'mysql')
|
35
39
|
client.query('SELECT * FROM user')
|
@@ -48,7 +52,3 @@ client.query('SELECT * FROM user')
|
|
48
52
|
```
|
49
53
|
|
50
54
|

|
51
|
-
|
52
|
-
## Retated links
|
53
|
-
|
54
|
-
* http://kamipo.github.io/talks/20140711-mysqlcasual6
|
data/Rakefile
CHANGED
@@ -3,7 +3,7 @@ require 'mysql2_query_filter'
|
|
3
3
|
require 'term/ansicolor'
|
4
4
|
|
5
5
|
module Mysql2QueryFilter::Plugin
|
6
|
-
class CasualLog <
|
6
|
+
class CasualLog < Mysql2QueryFilter::Base
|
7
7
|
Mysql2QueryFilter::Plugin.register(:casual_log, self)
|
8
8
|
|
9
9
|
REGEXPS = {
|
@@ -31,33 +31,46 @@ module Mysql2QueryFilter::Plugin
|
|
31
31
|
|
32
32
|
def initialize(options)
|
33
33
|
super
|
34
|
-
@out = @options
|
35
|
-
@matcher = @options
|
36
|
-
@client =
|
34
|
+
@out = @options[:out] || $stderr
|
35
|
+
@matcher = @options[:match] || proc {|sql, client| true }
|
36
|
+
@client = @options[:client]
|
37
37
|
end
|
38
38
|
|
39
|
-
def filter(sql,
|
40
|
-
if sql =~ /\A\s*SELECT\b/i and @matcher.call(sql,
|
39
|
+
def filter(sql, client)
|
40
|
+
if sql =~ /\A\s*SELECT\b/i and @matcher.call(sql, client)
|
41
|
+
conn = @client || client
|
42
|
+
badquery = false
|
41
43
|
explains = []
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
explains << format_explain(
|
45
|
+
conn.query("EXPLAIN #{sql}", :as => :hash).each_with_index do |result, i|
|
46
|
+
colorize_explain(result).tap {|bq| badquery ||= bq }
|
47
|
+
explains << format_explain(result, i + 1)
|
46
48
|
end
|
47
49
|
|
48
|
-
|
49
|
-
|
50
|
+
if badquery
|
51
|
+
query_options = conn.query_options.dup
|
52
|
+
query_options.delete(:password)
|
53
|
+
|
54
|
+
@out << <<-EOS
|
55
|
+
# Time: #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}
|
56
|
+
# Query options: #{query_options.inspect}
|
57
|
+
# Query: #{sql}
|
58
|
+
#{explains.join("\n")}
|
59
|
+
EOS
|
50
60
|
end
|
51
61
|
end
|
62
|
+
rescue => e
|
63
|
+
$stderr.puts colored([e.message, e.backtrace.first].join("\n"))
|
52
64
|
end
|
53
65
|
|
54
66
|
private
|
55
67
|
|
56
|
-
def colorize_explain(
|
68
|
+
def colorize_explain(explain_result)
|
57
69
|
badquery = false
|
58
70
|
|
59
71
|
REGEXPS.each do |key, regexp|
|
60
|
-
value =
|
72
|
+
value = explain_result[key] ||= 'NULL'
|
73
|
+
value = value.to_s
|
61
74
|
|
62
75
|
value.gsub!(regexp) do |m|
|
63
76
|
badquery = true
|
@@ -72,15 +85,15 @@ module Mysql2QueryFilter::Plugin
|
|
72
85
|
Term::ANSIColor.red(Term::ANSIColor.bold(str))
|
73
86
|
end
|
74
87
|
|
75
|
-
def format_explain(
|
76
|
-
message = "
|
88
|
+
def format_explain(explain, i)
|
89
|
+
message = "*************************** #{i}. row ***************************\n"
|
77
90
|
max_key_length = explain.keys.map(&:length).max
|
78
91
|
|
79
92
|
explain.each do |key, value|
|
80
93
|
message << "%*s: %s\n" % [max_key_length, key, value]
|
81
94
|
end
|
82
95
|
|
83
|
-
message
|
96
|
+
message.chomp
|
84
97
|
end
|
85
|
-
end
|
86
|
-
end
|
98
|
+
end # CasualLog
|
99
|
+
end # Mysql2QueryFilter::Plugin
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
Gem::Specification.new do |spec|
|
3
3
|
spec.name = 'mysql2_query_filter-plugin-casual_log'
|
4
|
-
spec.version = '0.
|
4
|
+
spec.version = '0.1.1'
|
5
5
|
spec.authors = ['Genki Sugawara']
|
6
6
|
spec.email = ['sgwr_dts@yahoo.co.jp']
|
7
7
|
spec.summary = %q{Plug-in that colorize the bad query for Mysql2QueryFilter.}
|
@@ -14,8 +14,10 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
15
15
|
spec.require_paths = ['lib']
|
16
16
|
|
17
|
-
spec.add_dependency 'mysql2_query_filter'
|
17
|
+
spec.add_dependency 'mysql2_query_filter', '>= 0.1.1'
|
18
18
|
spec.add_dependency 'term-ansicolor'
|
19
19
|
spec.add_development_dependency 'bundler'
|
20
20
|
spec.add_development_dependency 'rake'
|
21
|
+
spec.add_development_dependency 'rspec', '>= 3.0.0'
|
22
|
+
spec.add_development_dependency "timecop"
|
21
23
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
describe Mysql2QueryFilter::Plugin::CasualLog do
|
2
|
+
let(:client) { Mysql2::Client.new(host: "localhost", username: "root", database: "mysql") }
|
3
|
+
let(:today) { Time.parse("2015-05-15 20:15:55 +0000") }
|
4
|
+
let(:out) { StringIO.new }
|
5
|
+
|
6
|
+
before do
|
7
|
+
Mysql2QueryFilter.configure do |filter|
|
8
|
+
filter.plugin :casual_log, out: out
|
9
|
+
end
|
10
|
+
|
11
|
+
Timecop.freeze(today) do
|
12
|
+
client.query(sql)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
subject { out.string.sub(/Query options:.*/, "Query options:").sub(/rows: \d+/, "rows:") }
|
17
|
+
|
18
|
+
context "when bad query" do
|
19
|
+
let(:sql) { "select * from user" }
|
20
|
+
|
21
|
+
let(:explain) do
|
22
|
+
<<-EOS
|
23
|
+
# Time: 2015-05-15 20:15:55
|
24
|
+
# Query options:
|
25
|
+
# Query: select * from user
|
26
|
+
*************************** 1. row ***************************
|
27
|
+
id: 1
|
28
|
+
select_type: SIMPLE
|
29
|
+
table: user
|
30
|
+
type: #{red bold "ALL"}
|
31
|
+
possible_keys: #{red bold "NULL"}
|
32
|
+
key: #{red bold "NULL"}
|
33
|
+
key_len:\s
|
34
|
+
ref:\s
|
35
|
+
rows:
|
36
|
+
Extra:\s
|
37
|
+
EOS
|
38
|
+
end
|
39
|
+
|
40
|
+
it { is_expected.to eq explain }
|
41
|
+
end
|
42
|
+
|
43
|
+
context "when good query" do
|
44
|
+
let(:sql) { "select 1 from user where Host = 'localhost'" }
|
45
|
+
it { is_expected.to eq "" }
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'mysql2_query_filter/plugin/casual_log'
|
2
|
+
require 'stringio'
|
3
|
+
require "time"
|
4
|
+
require "timecop"
|
5
|
+
|
6
|
+
ENV["TZ"] = "UTC"
|
7
|
+
include Term::ANSIColor
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.before(:all) do
|
11
|
+
Mysql2QueryFilter.enable!
|
12
|
+
end
|
13
|
+
|
14
|
+
config.before(:each) do
|
15
|
+
Mysql2QueryFilter.clear!
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mysql2_query_filter-plugin-casual_log
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Genki Sugawara
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mysql2_query_filter
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.1.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.1.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: term-ansicolor
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +66,34 @@ dependencies:
|
|
66
66
|
- - '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.0.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.0.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: timecop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
69
97
|
description: Plug-in that colorize the bad query for Mysql2QueryFilter.
|
70
98
|
email:
|
71
99
|
- sgwr_dts@yahoo.co.jp
|
@@ -74,12 +102,16 @@ extensions: []
|
|
74
102
|
extra_rdoc_files: []
|
75
103
|
files:
|
76
104
|
- .gitignore
|
105
|
+
- .rspec
|
106
|
+
- .travis.yml
|
77
107
|
- Gemfile
|
78
108
|
- LICENSE.txt
|
79
109
|
- README.md
|
80
110
|
- Rakefile
|
81
111
|
- lib/mysql2_query_filter/plugin/casual_log.rb
|
82
112
|
- mysql2_query_filter-plugin-casual_log.gemspec
|
113
|
+
- spec/mysql2_query_filter-plugin-casual_log_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
83
115
|
homepage: https://github.com/winebarrel/mysql2_query_filter-plugin-casual_log
|
84
116
|
licenses:
|
85
117
|
- MIT
|
@@ -104,4 +136,6 @@ rubygems_version: 2.0.14
|
|
104
136
|
signing_key:
|
105
137
|
specification_version: 4
|
106
138
|
summary: Plug-in that colorize the bad query for Mysql2QueryFilter.
|
107
|
-
test_files:
|
139
|
+
test_files:
|
140
|
+
- spec/mysql2_query_filter-plugin-casual_log_spec.rb
|
141
|
+
- spec/spec_helper.rb
|