explain_parser 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: 57f9288b3203db28348942495a9ae6f64d8985a5
4
+ data.tar.gz: ceb1de9f91b1003f1b1749bd3949f1a2130429ac
5
+ SHA512:
6
+ metadata.gz: 9cb28810513094751613be8eca502687d988992d58a8b1f3c35d92500935109df20d8124ea078de34aaac5d7e3df4770b03dc080cdb5825eb29d5726e6d68de5
7
+ data.tar.gz: a93c89ec17ae94f65c924dd016c3e7191f0ccf1afd4828bd6be765d6e3addc83dcb633c7c176288172267ee72240d816da5c2380f241117fdbc6deec784dba5a
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in explain_parser.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 ainame
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
+ # ExplainParser
2
+
3
+ Parser for result of EXPLAIN of MySQL(inspired from http://search.cpan.org/~moznion/MySQL-Explain-Parser-0.02/lib/MySQL/Explain/Parser.pm)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'explain_parser'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install explain_parser
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ input <<-EOS
23
+ +----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------------+
24
+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
25
+ +----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------------+
26
+ | 1 | SIMPLE | users | ALL | NULL | NULL | NULL | NULL | 155 | Using where; Using index Using temporary; Using filesort |
27
+ +----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------------+
28
+ 1 row in set (0.00 sec)
29
+ EOS
30
+
31
+ parser = ExplainParser.new(input)
32
+ explains = parser.parse
33
+ explain = explains.first
34
+
35
+ explain.id #=> 1
36
+ explain.select_type #=> 'SIMPLE'
37
+
38
+ ...
39
+
40
+ explain.rows #=> 155
41
+ explain.extra #=> 'Using where; Using index Using temporary; Using filesort'
42
+ explain.using_filesort? #=> true
43
+ explain.using_where? #=> true
44
+ explain.using_temporary? #=> true
45
+ explain.using_index? #=> true
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/[my-github-username]/explain_parser/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'explain_parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "explain_parser"
8
+ spec.version = ExplainParser::VERSION
9
+ spec.authors = ["ainame"]
10
+ spec.email = ["s.namai.09@gmail.com"]
11
+ spec.summary = %q{Parser for result of EXPLAIN of MySQL}
12
+ spec.description = %q{Parser for result of EXPLAIN of MySQL
13
+ see: https://github.com/ainame/explain_parser
14
+ }
15
+ spec.homepage = "https://github.com/ainame/explain_parser"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,3 @@
1
+ class ExplainParser
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,87 @@
1
+ require "explain_parser/version"
2
+
3
+ class ExplainParser
4
+
5
+ class Explain
6
+ COLUMNS = [
7
+ :id, :select_type, :table, :type, :possible_keys, :key, :key_len, :len, :ref, :rows, :filtered, :extra
8
+ ].freeze
9
+
10
+ def initialize(params)
11
+ raise ArgumentError unless params
12
+ @params = params
13
+ end
14
+
15
+ COLUMNS.each do |column|
16
+ method_name = column.to_s
17
+ if [:id, :rows].include?(column)
18
+ define_method(column) do
19
+ @params[method_name].to_i
20
+ end
21
+ else
22
+ define_method(column) do
23
+ @params[method_name]
24
+ end
25
+ end
26
+ end
27
+
28
+ def using_filesort?
29
+ !!(extra =~ /Using filesort/)
30
+ end
31
+
32
+ def using_temporary?
33
+ !!(extra =~ /Using temporary/)
34
+ end
35
+
36
+ def using_where?
37
+ !!(extra =~ /Using where/)
38
+ end
39
+
40
+ def using_index?
41
+ !!(extra =~ /Using index/)
42
+ end
43
+ end
44
+
45
+ def initialize(explain)
46
+ raise ArgumentError unless explain
47
+ @explain = explain
48
+ end
49
+
50
+ def self.call(explain)
51
+ new(explain).call
52
+ end
53
+
54
+ def call
55
+ return unless rows && !rows.empty?
56
+ build
57
+ end
58
+
59
+ def lines
60
+ @explain.each_line.to_a
61
+ end
62
+
63
+ def rows
64
+ @rows ||= lines.select{|line| line =~ /\w+/ && line !~ /\d+ row in set/ }
65
+ end
66
+
67
+ def keys()
68
+ @keys ||= rows[0].chomp.split('|').compact.map(&:strip).reject(&:empty?).map(&:downcase)
69
+ end
70
+
71
+ def values_list()
72
+ rows[1..-1].map{|row| cleanup_values(row.chomp.split('|').compact) }
73
+ end
74
+
75
+ def cleanup_values(dirty_values)
76
+ dirty_values.map(&:strip).reject(&:empty?).map {|val| val == 'NULL' ? nil : val }
77
+ end
78
+
79
+ def build
80
+ values_list.reduce([]) do |explains, values|
81
+ params = keys.zip(values).inject({}) { |h, (k, v)| h[k] = v; h }
82
+ explains << ExplainParser::Explain.new(params)
83
+ explains
84
+ end
85
+ end
86
+
87
+ end
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+
3
+ describe ExplainParser do
4
+ context 'given explain string' do
5
+ let(:input) do
6
+ <<-EOS
7
+ +----+-------------+-------+------+---------------+------+---------+------+------+-------+
8
+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
9
+ +----+-------------+-------+------+---------------+------+---------+------+------+-------+
10
+ | 1 | SIMPLE | users | ALL | NULL | NULL | NULL | NULL | 155 | NULL |
11
+ +----+-------------+-------+------+---------------+------+---------+------+------+-------+
12
+ 1 row in set (0.00 sec)
13
+ EOS
14
+ end
15
+ subject { ExplainParser.new(input) }
16
+
17
+ describe '.new' do
18
+ it { is_expected.to be_kind_of(ExplainParser)}
19
+ end
20
+
21
+ describe '#call' do
22
+ it 'should ExplainParser::Explain' do
23
+ expect(subject.call).to be_kind_of(Array)
24
+ expect(subject.call[0]).to be_kind_of(ExplainParser::Explain)
25
+ end
26
+
27
+ it 'should ExplainParser::Explain' do
28
+ explain = subject.call[0]
29
+ expect(explain.id).to eq 1
30
+ expect(explain.select_type).to eq 'SIMPLE'
31
+ expect(explain.table).to eq 'users'
32
+ expect(explain.type).to eq 'ALL'
33
+ expect(explain.possible_keys).to eq nil
34
+ expect(explain.key).to eq nil
35
+ expect(explain.key_len).to eq nil
36
+ expect(explain.ref).to eq nil
37
+ expect(explain.rows).to eq 155
38
+ expect(explain.extra).to eq nil
39
+ expect(explain.using_filesort?).to be false
40
+ expect(explain.using_where?).to be false
41
+ expect(explain.using_temporary?).to be false
42
+ expect(explain.using_index?).to be false
43
+ end
44
+ end
45
+ end
46
+
47
+ context 'given explain string with extra' do
48
+ let(:input) do
49
+ <<-EOS
50
+ +----+-------------+-------+--------+----------------------------------------------------------+
51
+ | id | select_type | table | type | Extra |
52
+ +----+-------------+-------+--------+----------------------------------------------------------+
53
+ | 1 | SIMPLE | tc | ref | Using where; Using index Using temporary; Using filesort |
54
+ +----+-------------+-------+--------+----------------------------------------------------------+
55
+ 1 row in set (0.00 sec)
56
+ EOS
57
+ end
58
+ subject { ExplainParser.new(input) }
59
+
60
+ describe '#call' do
61
+ it 'should ExplainParser::Explain' do
62
+ explain = subject.call[0]
63
+ expect(explain.extra).to be_truthy
64
+ expect(explain.using_filesort?).to be true
65
+ expect(explain.using_where?).to be true
66
+ expect(explain.using_temporary?).to be true
67
+ expect(explain.using_index?).to be true
68
+ end
69
+ end
70
+ end
71
+
72
+ context 'given explain string with multi line' do
73
+ let(:input) do
74
+ <<-EOS
75
+ +----+-------------+-------+--------+----------------------------------------------------------+
76
+ | id | select_type | table | type | Extra |
77
+ +----+-------------+-------+--------+----------------------------------------------------------+
78
+ | 1 | SIMPLE | tc | ref | Using where; Using index Using temporary; Using filesort |
79
+ | 2 | SIMPLE | tc | ref | Using where; Using index Using temporary; Using filesort |
80
+ | 3 | SIMPLE | tc | ref | NULL |
81
+ +----+-------------+-------+--------+----------------------------------------------------------+
82
+ 2 row in set (0.00 sec)
83
+ EOS
84
+ end
85
+ subject { ExplainParser.new(input) }
86
+
87
+ describe '#call' do
88
+ it 'should ExplainParser::Explain' do
89
+ explains = subject.call
90
+ expect(explains.size).to be 3
91
+ end
92
+ end
93
+ end
94
+
95
+ context 'given explain string with multi line' do
96
+ let(:input) do
97
+ <<-EOS
98
+ +----+-------------+-------+--------+----------------------------------------------------------+
99
+ | id | select_type | table | type | Extra |
100
+ +----+-------------+-------+--------+----------------------------------------------------------+
101
+ | 1 | SIMPLE | tc | ref | Using where; Using index Using temporary; Using filesort |
102
+ | 2 | SIMPLE | tc | ref | Using where; Using index Using temporary; Using filesort |
103
+ | 3 | SIMPLE | tc | ref | NULL |
104
+ +----+-------------+-------+--------+----------------------------------------------------------+
105
+ 2 row in set (0.00 sec)
106
+ EOS
107
+ end
108
+
109
+ describe '.call' do
110
+ it 'should ExplainParser::Explain' do
111
+ explains = ExplainParser.call(input)
112
+ expect(explains.size).to be 3
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,5 @@
1
+ require 'rspec'
2
+ require 'explain_parser'
3
+
4
+ RSpec.configure do |config|
5
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: explain_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ainame
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rspec
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
+ description: |
56
+ Parser for result of EXPLAIN of MySQL
57
+ see: https://github.com/ainame/explain_parser
58
+ email:
59
+ - s.namai.09@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - explain_parser.gemspec
70
+ - lib/explain_parser.rb
71
+ - lib/explain_parser/version.rb
72
+ - spec/lib/explain_parser_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: https://github.com/ainame/explain_parser
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Parser for result of EXPLAIN of MySQL
98
+ test_files:
99
+ - spec/lib/explain_parser_spec.rb
100
+ - spec/spec_helper.rb