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 +4 -4
- data/README.md +2 -2
- data/lib/explain_parser.rb +63 -22
- data/lib/explain_parser/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7024a1b0e295509049d0a46a29bb31f6d2a8602
|
4
|
+
data.tar.gz: 5b49a4664bb1bd3c157a1c09dfb50cbbd315c9f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eea2e1f0d790873b215aaceed35c080fd258319dd5100582471d3b88bf28e225aadf9826c553f0bfd13b6a3c517c12240ba25ec33a3c422dcb99dcb3c94a0494
|
7
|
+
data.tar.gz: e3f1edeb1d5a3ad57774f3bf1a3039e5d39ff6344190492793e0e4696e393892b6570c3b010a9daf342869973230808041b3936f59a456a4d15f9f4534fdfa41
|
data/README.md
CHANGED
data/lib/explain_parser.rb
CHANGED
@@ -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
|
-
|
56
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
62
|
+
class Base
|
63
|
+
def self.call(explain)
|
64
|
+
new(explain).call
|
65
|
+
end
|
62
66
|
|
63
|
-
|
64
|
-
|
65
|
-
|
67
|
+
def call
|
68
|
+
build
|
69
|
+
end
|
66
70
|
|
67
|
-
|
68
|
-
|
69
|
-
|
71
|
+
def keys
|
72
|
+
'override me'
|
73
|
+
end
|
70
74
|
|
71
|
-
|
72
|
-
|
73
|
-
|
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
|
-
|
76
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
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
|