ruby-nuggets 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README +4 -5
- data/Rakefile +7 -9
- data/lib/nuggets/ansicolor2css.rb +125 -0
- data/lib/nuggets/cli.rb +240 -0
- data/lib/nuggets/content_type.rb +103 -0
- data/lib/nuggets/dotted_decimal.rb +78 -0
- data/lib/nuggets/i18n.rb +165 -0
- data/lib/nuggets/lazy_attr.rb +45 -0
- data/lib/nuggets/log_parser/apache.rb +102 -0
- data/lib/nuggets/log_parser/rails.rb +220 -0
- data/lib/nuggets/log_parser.rb +71 -0
- data/lib/nuggets/midos.rb +130 -0
- data/lib/nuggets/mysql.rb +209 -0
- data/lib/nuggets/pluggable.rb +92 -0
- data/lib/nuggets/ruby.rb +348 -0
- data/lib/nuggets/util/ansicolor2css.rb +3 -126
- data/lib/nuggets/util/cli.rb +3 -241
- data/lib/nuggets/util/content_type.rb +3 -104
- data/lib/nuggets/util/dotted_decimal.rb +3 -77
- data/lib/nuggets/util/i18n.rb +3 -166
- data/lib/nuggets/util/lazy_attr.rb +3 -44
- data/lib/nuggets/util/log_parser/apache.rb +3 -105
- data/lib/nuggets/util/log_parser/rails.rb +3 -223
- data/lib/nuggets/util/log_parser.rb +3 -72
- data/lib/nuggets/util/midos.rb +4 -0
- data/lib/nuggets/util/mysql.rb +3 -210
- data/lib/nuggets/util/pluggable.rb +3 -93
- data/lib/nuggets/util/ruby.rb +3 -347
- data/lib/nuggets/version.rb +1 -1
- metadata +19 -7
- data/lib/nuggets/util/added_methods/init.rb +0 -3
- data/lib/nuggets/util/added_methods.rb +0 -6
@@ -1,106 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2012 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@gmail.com> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU Affero General Public License as published by #
|
14
|
-
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
-
# option) any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
-
# for more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU Affero General Public License #
|
23
|
-
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
1
|
+
require 'nuggets/log_parser/apache'
|
2
|
+
module Util; LogParser = ::Nuggets::LogParser; end
|
27
3
|
|
28
|
-
|
29
|
-
|
30
|
-
module Util
|
31
|
-
|
32
|
-
module LogParser
|
33
|
-
|
34
|
-
module Apache
|
35
|
-
|
36
|
-
extend self
|
37
|
-
|
38
|
-
DEFAULT_RE = %r{(.*?)}
|
39
|
-
|
40
|
-
DIRECTIVES = {
|
41
|
-
'h' => [:ip, %r{(\d+(?:\.\d+){3}|[\w.-]+)}],
|
42
|
-
'l' => [:auth, DEFAULT_RE],
|
43
|
-
'u' => [:username, DEFAULT_RE],
|
44
|
-
't' => [:datetime, %r{\[(.*?)\]}],
|
45
|
-
'r' => [:request, DEFAULT_RE],
|
46
|
-
'R' => [:request, %r{(.*?)(?:"|\z)}],
|
47
|
-
's' => [:status, %r{(\d+)}],
|
48
|
-
'b' => [:bytecount, %r{(-|\d+)}],
|
49
|
-
'v' => [:domain, DEFAULT_RE],
|
50
|
-
'i' => [nil, DEFAULT_RE],
|
51
|
-
}
|
52
|
-
|
53
|
-
DIRECTIVES_RE = %r{%.*?(?:\{(.*?)\})?([#{DIRECTIVES.keys.join}])([\s\\"]*)}
|
54
|
-
|
55
|
-
ORDER = []
|
56
|
-
|
57
|
-
class << self
|
58
|
-
|
59
|
-
def register(name, format)
|
60
|
-
base = const_set(name, Module.new)
|
61
|
-
|
62
|
-
re, items = parse_format(format)
|
63
|
-
base.const_set(:RE, re)
|
64
|
-
base.const_set(:ITEMS, items)
|
65
|
-
|
66
|
-
ORDER << base
|
67
|
-
LogParser.register(base, self)
|
68
|
-
end
|
69
|
-
|
70
|
-
def parse_format(format)
|
71
|
-
re, items = '\A', []
|
72
|
-
|
73
|
-
format.scan(DIRECTIVES_RE) { |h, c, e|
|
74
|
-
i, r = DIRECTIVES[c]
|
75
|
-
re << r.source << e.gsub(/\s/, '\\s')
|
76
|
-
items << i ||= h.downcase.tr('-', '_').to_sym
|
77
|
-
}
|
78
|
-
|
79
|
-
[Regexp.new(re), items]
|
80
|
-
end
|
81
|
-
|
82
|
-
def detect_type(line)
|
83
|
-
ORDER.find { |type| line =~ type::RE }
|
84
|
-
end
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
[ [:Combined, '%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i"'],
|
89
|
-
[:Common, '%h %l %u %t "%r" %>s %b'],
|
90
|
-
[:Minimal, '%h %l %u %t "%R']
|
91
|
-
].each { |name, format| register(name, format) }
|
92
|
-
|
93
|
-
def parse_line(line, entry = {})
|
94
|
-
if md = self::RE.match(line)
|
95
|
-
self::ITEMS.each_with_index { |k, i| entry[k] = md[i + 1] }
|
96
|
-
yield if block_given?
|
97
|
-
end
|
98
|
-
|
99
|
-
entry
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|
105
|
-
|
106
|
-
end
|
4
|
+
warn "#{__FILE__}: Util::LogParser::Apache is deprecated, use Nuggets::LogParser::Apache instead."
|
@@ -1,224 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2012 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@gmail.com> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU Affero General Public License as published by #
|
14
|
-
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
-
# option) any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
-
# for more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU Affero General Public License #
|
23
|
-
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
1
|
+
require 'nuggets/log_parser/rails'
|
2
|
+
module Util; LogParser = ::Nuggets::LogParser; end
|
27
3
|
|
28
|
-
|
29
|
-
|
30
|
-
module Util
|
31
|
-
|
32
|
-
module LogParser
|
33
|
-
|
34
|
-
module Rails
|
35
|
-
|
36
|
-
LogParser.register(self)
|
37
|
-
|
38
|
-
# Log line prefix
|
39
|
-
PREFIX_RE = %r{
|
40
|
-
\A
|
41
|
-
(?:
|
42
|
-
\[
|
43
|
-
(\d+) # pid
|
44
|
-
:
|
45
|
-
(.*?) # host
|
46
|
-
\]
|
47
|
-
\s
|
48
|
-
)?
|
49
|
-
\s*
|
50
|
-
}x
|
51
|
-
|
52
|
-
# Log entry separator
|
53
|
-
SEPARATOR_RE = %r{
|
54
|
-
\s+\|\s+
|
55
|
-
}x
|
56
|
-
|
57
|
-
# Log line patterns
|
58
|
-
ITEMS = [
|
59
|
-
[:processing, {
|
60
|
-
:re => %r{
|
61
|
-
#{PREFIX_RE} # pid, host
|
62
|
-
Processing\s+
|
63
|
-
(\w+) # controller
|
64
|
-
\#
|
65
|
-
(\w+) # action
|
66
|
-
\s+
|
67
|
-
\(
|
68
|
-
for\s+
|
69
|
-
(.*?) # ip
|
70
|
-
\s+at\s+
|
71
|
-
(.*?) # datetime
|
72
|
-
\)
|
73
|
-
\s+
|
74
|
-
\[
|
75
|
-
(\w+) # request_method
|
76
|
-
\]
|
77
|
-
}xo,
|
78
|
-
:keys => [:controller, :action, :ip, :datetime, :request_method]
|
79
|
-
}],
|
80
|
-
[:session_id, {
|
81
|
-
:re => %r{
|
82
|
-
#{PREFIX_RE} # pid, host
|
83
|
-
Session\sID:\s+
|
84
|
-
(\S+) # sid
|
85
|
-
}xo,
|
86
|
-
:keys => [:sid]
|
87
|
-
}],
|
88
|
-
[:parameters, {
|
89
|
-
:re => %r{
|
90
|
-
#{PREFIX_RE} # pid, host
|
91
|
-
Parameters:\s+
|
92
|
-
(\{.*\}) # params
|
93
|
-
}xo, #}
|
94
|
-
:proc => lambda { |entry, md|
|
95
|
-
entry[:params_hash] = md[3].hash
|
96
|
-
entry[:params] = begin
|
97
|
-
eval("$SAFE = 3\n#{md[3].gsub(/#<.*?>/, '%q{\&}')}", nil, __FILE__, __LINE__) # !!!
|
98
|
-
rescue SyntaxError, SecurityError
|
99
|
-
{}
|
100
|
-
end
|
101
|
-
}
|
102
|
-
}],
|
103
|
-
[:client_info, {
|
104
|
-
:re => %r{
|
105
|
-
#{PREFIX_RE} # pid, host
|
106
|
-
Client\sinfo:\s+
|
107
|
-
UA\s+=\s+
|
108
|
-
(.*?) # user_agent
|
109
|
-
#{SEPARATOR_RE}
|
110
|
-
LANG\s+=\s+
|
111
|
-
(.*) # accept_language
|
112
|
-
}xo,
|
113
|
-
:keys => [:user_agent, :accept_language]
|
114
|
-
}],
|
115
|
-
[:referer, {
|
116
|
-
:re => %r{
|
117
|
-
#{PREFIX_RE} # pid, host
|
118
|
-
Referer:\s+
|
119
|
-
(.*) # referer
|
120
|
-
}xo,
|
121
|
-
:keys => [:referer]
|
122
|
-
}],
|
123
|
-
[:meta, {
|
124
|
-
:re => %r{
|
125
|
-
#{PREFIX_RE} # pid, host
|
126
|
-
Meta:\s+
|
127
|
-
User\s+=\s+
|
128
|
-
(.*?) # user_id
|
129
|
-
#{SEPARATOR_RE}
|
130
|
-
Institution\s+=\s+
|
131
|
-
(.*) # institution_id
|
132
|
-
}xo,
|
133
|
-
:keys => [:user_id, :institution_id]
|
134
|
-
}],
|
135
|
-
[:stats, {
|
136
|
-
:re => %r{
|
137
|
-
#{PREFIX_RE} # pid, host
|
138
|
-
Stats:\s+
|
139
|
-
(.*) # flags
|
140
|
-
}xo,
|
141
|
-
:proc => lambda { |entry, md|
|
142
|
-
entry[:flags] = md[3].split(SEPARATOR_RE)
|
143
|
-
}
|
144
|
-
}],
|
145
|
-
[:oauth, {
|
146
|
-
:re => %r{
|
147
|
-
#{PREFIX_RE} # pid, host
|
148
|
-
OAuth:\s+
|
149
|
-
Token\s+=\s+
|
150
|
-
(.*?)#(.*?) # token_type, token
|
151
|
-
#{SEPARATOR_RE}
|
152
|
-
User\s+=\s+
|
153
|
-
(.*?) # user_id
|
154
|
-
#{SEPARATOR_RE}
|
155
|
-
Client\s+=\s+
|
156
|
-
(.*) # client_id
|
157
|
-
}xo,
|
158
|
-
:keys => [:token_type, :token, :user_id, :client_id]
|
159
|
-
}],
|
160
|
-
[:benchmark, {
|
161
|
-
:re => %r{
|
162
|
-
#{PREFIX_RE} # pid, host
|
163
|
-
Completed\sin\s+
|
164
|
-
(\S+) # runtime
|
165
|
-
.*?
|
166
|
-
(?: #- OPTIONAL
|
167
|
-
#{SEPARATOR_RE}
|
168
|
-
Rendering:\s+
|
169
|
-
(\S+) # rendering_runtime
|
170
|
-
.*?
|
171
|
-
)?
|
172
|
-
(?: #- OPTIONAL
|
173
|
-
#{SEPARATOR_RE}
|
174
|
-
DB:\s+
|
175
|
-
(\S+) # db_runtime
|
176
|
-
.*?
|
177
|
-
)?
|
178
|
-
(?: #- OPTIONAL
|
179
|
-
#{SEPARATOR_RE}
|
180
|
-
Mem:\s+
|
181
|
-
\S+
|
182
|
-
.*?
|
183
|
-
)?
|
184
|
-
#{SEPARATOR_RE}
|
185
|
-
(.*?) # status
|
186
|
-
\s+
|
187
|
-
\[
|
188
|
-
(.*) # request_uri
|
189
|
-
\]
|
190
|
-
}xo,
|
191
|
-
:keys => [:runtime, :rendering_runtime, :db_runtime, :status, :request_uri]
|
192
|
-
}]
|
193
|
-
]
|
194
|
-
|
195
|
-
ITEMS.each { |_, item|
|
196
|
-
item[:proc] ||= lambda { |entry, md|
|
197
|
-
item[:keys].each_with_index { |k, i|
|
198
|
-
entry[k] = md[i + 3] # 1 = pid, 2 = host
|
199
|
-
}
|
200
|
-
}
|
201
|
-
}
|
202
|
-
|
203
|
-
def parse_line(line, entry = {})
|
204
|
-
ITEMS.each { |key, item|
|
205
|
-
if md = item[:re].match(line)
|
206
|
-
if key == :processing
|
207
|
-
yield if block_given?
|
208
|
-
entry[:pid], entry[:host] = md[1], md[2]
|
209
|
-
end
|
210
|
-
|
211
|
-
item[:proc][entry, md]
|
212
|
-
|
213
|
-
break
|
214
|
-
end
|
215
|
-
}
|
216
|
-
|
217
|
-
entry
|
218
|
-
end
|
219
|
-
|
220
|
-
end
|
221
|
-
|
222
|
-
end
|
223
|
-
|
224
|
-
end
|
4
|
+
warn "#{__FILE__}: Util::LogParser::Rails is deprecated, use Nuggets::LogParser::Rails instead."
|
@@ -1,73 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2012 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@gmail.com> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU Affero General Public License as published by #
|
14
|
-
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
-
# option) any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
-
# for more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU Affero General Public License #
|
23
|
-
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
1
|
+
require 'nuggets/log_parser'
|
2
|
+
module Util; LogParser = ::Nuggets::LogParser; end
|
27
3
|
|
28
|
-
|
29
|
-
|
30
|
-
module Util
|
31
|
-
|
32
|
-
module LogParser
|
33
|
-
|
34
|
-
extend self
|
35
|
-
|
36
|
-
GZ_EXT_RE = %r{\.gz\z}
|
37
|
-
|
38
|
-
def self.register(base, *modules)
|
39
|
-
base.send(:include, *modules << self)
|
40
|
-
base.extend(base)
|
41
|
-
end
|
42
|
-
|
43
|
-
def parse(input)
|
44
|
-
entry = {}
|
45
|
-
|
46
|
-
input.each { |line| parse_line(line, entry) {
|
47
|
-
unless entry.empty?
|
48
|
-
yield entry.dup
|
49
|
-
entry.clear
|
50
|
-
end
|
51
|
-
} }
|
52
|
-
|
53
|
-
yield entry unless entry.empty?
|
54
|
-
end
|
55
|
-
|
56
|
-
def parse_line(line, entry = {})
|
57
|
-
# Yield when entry complete. Preferrably return +entry+.
|
58
|
-
raise NotImplementedError, 'must be implemented by type'
|
59
|
-
end
|
60
|
-
|
61
|
-
def parse_file(file, &block)
|
62
|
-
block ||= (entries = []; lambda { |entry| entries << entry })
|
63
|
-
|
64
|
-
(file =~ GZ_EXT_RE ? Zlib::GzipReader : File).open(file) { |f|
|
65
|
-
block.arity == 1 ? parse(f, &block) : block[f, method(:parse)]
|
66
|
-
}
|
67
|
-
|
68
|
-
entries
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
4
|
+
warn "#{__FILE__}: Util::LogParser is deprecated, use Nuggets::LogParser instead."
|
data/lib/nuggets/util/mysql.rb
CHANGED
@@ -1,211 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# #
|
4
|
-
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
-
# language. #
|
6
|
-
# #
|
7
|
-
# Copyright (C) 2007-2013 Jens Wille #
|
8
|
-
# #
|
9
|
-
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@gmail.com> #
|
11
|
-
# #
|
12
|
-
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
-
# under the terms of the GNU Affero General Public License as published by #
|
14
|
-
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
-
# option) any later version. #
|
16
|
-
# #
|
17
|
-
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
-
# for more details. #
|
21
|
-
# #
|
22
|
-
# You should have received a copy of the GNU Affero General Public License #
|
23
|
-
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
-
# #
|
25
|
-
###############################################################################
|
26
|
-
#++
|
1
|
+
require 'nuggets/mysql'
|
2
|
+
module Util; MySQL = ::Nuggets::MySQL; end
|
27
3
|
|
28
|
-
|
29
|
-
|
30
|
-
module Util
|
31
|
-
|
32
|
-
module MySQL
|
33
|
-
|
34
|
-
class Parser
|
35
|
-
|
36
|
-
DEFAULT_NAME = '__DEFAULT__'
|
37
|
-
DEFAULT_TABLE = '__DEFAULT__'
|
38
|
-
|
39
|
-
USE_RE = /\AUSE\s+`(.+?)`/i
|
40
|
-
CREATE_TABLE_RE = /\ACREATE\s+TABLE\s+`(.+?)`/i
|
41
|
-
TABLE_COLUMN_RE = /\A\s+`(.+?)`/
|
42
|
-
FINISH_TABLE_RE = /\A\).*;\Z/
|
43
|
-
INSERT_VALUES_RE = /\AINSERT\s+INTO\s+`(.+?)`\s+(?:\((.+?)\)\s+)?VALUES\s*(.*);\Z/i
|
44
|
-
CLEAN_COLUMNS_RE = /[\s`]+/
|
45
|
-
|
46
|
-
def self.parse(input, &block)
|
47
|
-
parser = new.parse(input, &block)
|
48
|
-
block_given? ? parser : parser.tables
|
49
|
-
end
|
50
|
-
|
51
|
-
def initialize
|
52
|
-
reset
|
53
|
-
end
|
54
|
-
|
55
|
-
def reset
|
56
|
-
@name = DEFAULT_NAME
|
57
|
-
@table = DEFAULT_TABLE
|
58
|
-
@tables = {}
|
59
|
-
@columns = Hash.new { |h, k| h[k] = [] }
|
60
|
-
@value_parser = ValueParser.new
|
61
|
-
end
|
62
|
-
|
63
|
-
attr_reader :tables
|
64
|
-
|
65
|
-
def parse(input, &block)
|
66
|
-
unless block
|
67
|
-
tables, block = @tables, lambda { |_, name, table, columns, values|
|
68
|
-
((tables[name] ||= {})[table] ||= []) << fields = {}
|
69
|
-
|
70
|
-
values.each_with_index { |value, index|
|
71
|
-
if column = columns[index]
|
72
|
-
fields[column] = value
|
73
|
-
end
|
74
|
-
}
|
75
|
-
}
|
76
|
-
end
|
77
|
-
|
78
|
-
name, table, columns, value_parser, block_given =
|
79
|
-
@name, @table, @columns, @value_parser, block_given?
|
80
|
-
|
81
|
-
input.each { |line|
|
82
|
-
case line
|
83
|
-
when USE_RE
|
84
|
-
name = $1
|
85
|
-
yield :use, name if block_given
|
86
|
-
when CREATE_TABLE_RE
|
87
|
-
table = $1
|
88
|
-
when TABLE_COLUMN_RE
|
89
|
-
columns[table] << $1 if table
|
90
|
-
when FINISH_TABLE_RE
|
91
|
-
yield :table, name, table, columns[table] if block_given
|
92
|
-
table = nil
|
93
|
-
when INSERT_VALUES_RE
|
94
|
-
_table, _columns, _values = $1, $2, $3
|
95
|
-
|
96
|
-
_columns = _columns.nil? ? columns[_table] :
|
97
|
-
_columns.gsub(CLEAN_COLUMNS_RE, '').split(',')
|
98
|
-
|
99
|
-
value_parser.parse(_values) { |values|
|
100
|
-
block[:insert, name, _table, _columns, values]
|
101
|
-
} unless _columns.empty?
|
102
|
-
end
|
103
|
-
}
|
104
|
-
|
105
|
-
@name, @table = name, table
|
106
|
-
|
107
|
-
self
|
108
|
-
end
|
109
|
-
|
110
|
-
end
|
111
|
-
|
112
|
-
class ValueParser
|
113
|
-
|
114
|
-
AST = Struct.new(:value)
|
115
|
-
|
116
|
-
def self.parse(input)
|
117
|
-
new.parse(input)
|
118
|
-
end
|
119
|
-
|
120
|
-
def parse(input)
|
121
|
-
@input = StringScanner.new(input)
|
122
|
-
|
123
|
-
rows, block_given = [], block_given?
|
124
|
-
|
125
|
-
while result = parse_row
|
126
|
-
row = result.value
|
127
|
-
block_given ? yield(row) : rows << row
|
128
|
-
break unless @input.scan(/,\s*/)
|
129
|
-
end
|
130
|
-
|
131
|
-
@input.scan(/;/) # optional
|
132
|
-
|
133
|
-
error('Unexpected data') unless @input.eos?
|
134
|
-
|
135
|
-
rows unless block_given
|
136
|
-
end
|
137
|
-
|
138
|
-
def parse_row
|
139
|
-
return unless @input.scan(/\(/)
|
140
|
-
|
141
|
-
row = []
|
142
|
-
|
143
|
-
while result = parse_value
|
144
|
-
row << result.value
|
145
|
-
break unless @input.scan(/,\s*/)
|
146
|
-
end
|
147
|
-
|
148
|
-
error('Unclosed row') unless @input.scan(/\)/)
|
149
|
-
|
150
|
-
AST.new(row)
|
151
|
-
end
|
152
|
-
|
153
|
-
def parse_value
|
154
|
-
parse_string ||
|
155
|
-
parse_number ||
|
156
|
-
parse_keyword
|
157
|
-
end
|
158
|
-
|
159
|
-
def parse_string
|
160
|
-
return unless @input.scan(/'/)
|
161
|
-
|
162
|
-
string = ''
|
163
|
-
|
164
|
-
while contents = parse_string_content || parse_string_escape
|
165
|
-
string << contents.value
|
166
|
-
end
|
167
|
-
|
168
|
-
error('Unclosed string') unless @input.scan(/'/)
|
169
|
-
|
170
|
-
AST.new(string)
|
171
|
-
end
|
172
|
-
|
173
|
-
def parse_string_content
|
174
|
-
if @input.scan(/[^\\']+/)
|
175
|
-
AST.new(@input.matched)
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
def parse_string_escape
|
180
|
-
if @input.scan(/\\[abtnvfr]/)
|
181
|
-
AST.new(eval(%Q{"#{@input.matched}"}))
|
182
|
-
elsif @input.scan(/\\.|''/)
|
183
|
-
AST.new(@input.matched[-1, 1])
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
def parse_number
|
188
|
-
if @input.scan(/-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/)
|
189
|
-
AST.new(eval(@input.matched))
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
def parse_keyword
|
194
|
-
if @input.scan(/null/i)
|
195
|
-
AST.new(nil)
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
def error(message)
|
200
|
-
if @input.eos?
|
201
|
-
raise "Unexpected end of input (#{message})."
|
202
|
-
else
|
203
|
-
raise "#{message} at #{$.}:#{@input.pos}: #{@input.peek(16).inspect}"
|
204
|
-
end
|
205
|
-
end
|
206
|
-
|
207
|
-
end
|
208
|
-
|
209
|
-
end
|
210
|
-
|
211
|
-
end
|
4
|
+
warn "#{__FILE__}: Util::MySQL is deprecated, use Nuggets::MySQL instead."
|