explain_parser 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57f9288b3203db28348942495a9ae6f64d8985a5
4
- data.tar.gz: ceb1de9f91b1003f1b1749bd3949f1a2130429ac
3
+ metadata.gz: d7024a1b0e295509049d0a46a29bb31f6d2a8602
4
+ data.tar.gz: 5b49a4664bb1bd3c157a1c09dfb50cbbd315c9f9
5
5
  SHA512:
6
- metadata.gz: 9cb28810513094751613be8eca502687d988992d58a8b1f3c35d92500935109df20d8124ea078de34aaac5d7e3df4770b03dc080cdb5825eb29d5726e6d68de5
7
- data.tar.gz: a93c89ec17ae94f65c924dd016c3e7191f0ccf1afd4828bd6be765d6e3addc83dcb633c7c176288172267ee72240d816da5c2380f241117fdbc6deec784dba5a
6
+ metadata.gz: eea2e1f0d790873b215aaceed35c080fd258319dd5100582471d3b88bf28e225aadf9826c553f0bfd13b6a3c517c12240ba25ec33a3c422dcb99dcb3c94a0494
7
+ data.tar.gz: e3f1edeb1d5a3ad57774f3bf1a3039e5d39ff6344190492793e0e4696e393892b6570c3b010a9daf342869973230808041b3936f59a456a4d15f9f4534fdfa41
data/README.md CHANGED
@@ -28,8 +28,8 @@ input <<-EOS
28
28
  1 row in set (0.00 sec)
29
29
  EOS
30
30
 
31
- parser = ExplainParser.new(input)
32
- explains = parser.parse
31
+ explains = ExplainParser.call(input) # return result as Array
32
+
33
33
  explain = explains.first
34
34
 
35
35
  explain.id #=> 1
@@ -43,7 +43,6 @@ class ExplainParser
43
43
  end
44
44
 
45
45
  def initialize(explain)
46
- raise ArgumentError unless explain
47
46
  @explain = explain
48
47
  end
49
48
 
@@ -52,36 +51,78 @@ class ExplainParser
52
51
  end
53
52
 
54
53
  def call
55
- return unless rows && !rows.empty?
56
- build
54
+ case @explain
55
+ when ::String
56
+ FromTableString.new(@explain).call
57
+ when ::Mysql2::Result
58
+ FromMysql2Result.new(@explain).call
59
+ end
57
60
  end
58
61
 
59
- def lines
60
- @explain.each_line.to_a
61
- end
62
+ class Base
63
+ def self.call(explain)
64
+ new(explain).call
65
+ end
62
66
 
63
- def rows
64
- @rows ||= lines.select{|line| line =~ /\w+/ && line !~ /\d+ row in set/ }
65
- end
67
+ def call
68
+ build
69
+ end
66
70
 
67
- def keys()
68
- @keys ||= rows[0].chomp.split('|').compact.map(&:strip).reject(&:empty?).map(&:downcase)
69
- end
71
+ def keys
72
+ 'override me'
73
+ end
70
74
 
71
- def values_list()
72
- rows[1..-1].map{|row| cleanup_values(row.chomp.split('|').compact) }
73
- end
75
+ def values_list
76
+ 'override me'
77
+ end
78
+
79
+ def cleanup_values(dirty_values)
80
+ dirty_values.map(&:strip).reject(&:empty?).map {|val| val == 'NULL' ? nil : val }
81
+ end
74
82
 
75
- def cleanup_values(dirty_values)
76
- dirty_values.map(&:strip).reject(&:empty?).map {|val| val == 'NULL' ? nil : val }
83
+ def build
84
+ values_list.reduce([]) do |explains, values|
85
+ params = keys.zip(cleanup_values(values)).inject({}) { |h, (k, v)| h[k] = v; h }
86
+ explains << ExplainParser::Explain.new(params)
87
+ explains
88
+ end
89
+ end
77
90
  end
78
91
 
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
92
+ class FromMysql2Result < Base
93
+ def initialize(explain)
94
+ @explain = explain
95
+ end
96
+
97
+ def keys
98
+ @explain.fields
99
+ end
100
+
101
+ def values_list
102
+ @exlpain.to_a
84
103
  end
85
104
  end
86
105
 
106
+ class FromTableString < Base
107
+ def initialize(explain)
108
+ raise ArgumentError unless explain
109
+ @explain = explain
110
+ end
111
+
112
+ def lines
113
+ @explain.each_line.to_a
114
+ end
115
+
116
+ def rows
117
+ @rows ||= lines.select{|line| line =~ /\w+/ && line !~ /\d+ row in set/ }
118
+ end
119
+
120
+ def keys()
121
+ @keys ||= rows[0].chomp.split('|').compact.map(&:strip).reject(&:empty?).map(&:downcase)
122
+ end
123
+
124
+ def values_list()
125
+ rows[1..-1].map{|row| row.chomp.split('|').compact }
126
+ end
127
+ end
87
128
  end
@@ -1,3 +1,3 @@
1
1
  class ExplainParser
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: explain_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ainame