anbt-sql-formatter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/anbt-sql-formatter.gemspec +24 -0
- data/bin/anbt-sql-formatter +50 -0
- data/lgpl-2.1.txt +504 -0
- data/lib/anbt-sql-formatter/coarse-tokenizer.rb +174 -0
- data/lib/anbt-sql-formatter/constants.rb +81 -0
- data/lib/anbt-sql-formatter/exception.rb +30 -0
- data/lib/anbt-sql-formatter/formatter.rb +409 -0
- data/lib/anbt-sql-formatter/helper.rb +73 -0
- data/lib/anbt-sql-formatter/parser.rb +327 -0
- data/lib/anbt-sql-formatter/rule.rb +121 -0
- data/lib/anbt-sql-formatter/token.rb +79 -0
- data/lib/anbt-sql-formatter/version.rb +7 -0
- data/misc/anbt-sql-formatter-customize-example +65 -0
- data/misc/anbt-sql-formatter-for-sakura-editor.js +165 -0
- data/readme.ja.txt +107 -0
- data/readme.txt +58 -0
- data/sample.sql +120 -0
- data/setup.rb +1585 -0
- data/test/helper.rb +17 -0
- data/test/test_coarse-tokenizer.rb +360 -0
- data/test/test_formatter.rb +489 -0
- data/test/test_helper.rb +23 -0
- data/test/test_parser.rb +370 -0
- data/test/test_rule.rb +30 -0
- data/uninstall.rb +20 -0
- metadata +84 -0
data/test/test_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "helper")
|
4
|
+
|
5
|
+
require "anbt-sql-formatter/helper"
|
6
|
+
require "anbt-sql-formatter/exception"
|
7
|
+
|
8
|
+
|
9
|
+
class TestAnbtSqlHelper < Test::Unit::TestCase
|
10
|
+
def setup
|
11
|
+
end
|
12
|
+
|
13
|
+
# Java版では List を使っている
|
14
|
+
def test_array_get
|
15
|
+
arr = %w(a b c) # index = 0, 1, 2
|
16
|
+
assert_raise(IndexOutOfBoundsException, "値が範囲外なのに例外が発生しない"){
|
17
|
+
arr.get(3)
|
18
|
+
}
|
19
|
+
assert_raise(IndexOutOfBoundsException, "値が範囲外なのに例外が発生しない"){
|
20
|
+
arr.get(-1)
|
21
|
+
}
|
22
|
+
end
|
23
|
+
end
|
data/test/test_parser.rb
ADDED
@@ -0,0 +1,370 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "helper")
|
4
|
+
|
5
|
+
require "anbt-sql-formatter/rule"
|
6
|
+
require "anbt-sql-formatter/parser"
|
7
|
+
|
8
|
+
|
9
|
+
class AnbtSql
|
10
|
+
class Parser
|
11
|
+
attr_accessor :before, :pos
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
class TestAnbtSqlParser < Test::Unit::TestCase
|
17
|
+
def setup
|
18
|
+
@parser = AnbtSql::Parser.new(AnbtSql::Rule.new)
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def test_space?
|
23
|
+
msg = "space? - "
|
24
|
+
assert_equals( msg, true, @parser.space?(" ") )
|
25
|
+
assert_equals( msg, true, @parser.space?("\t") )
|
26
|
+
assert_equals( msg, true, @parser.space?("\r") )
|
27
|
+
assert_equals( msg, true, @parser.space?("\n") )
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def test_letter?
|
32
|
+
msg = "letter? - "
|
33
|
+
assert_equals( msg, false, @parser.letter?("'") )
|
34
|
+
assert_equals( msg, false, @parser.letter?('"') )
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
def test_symbol?
|
39
|
+
msg = "symbol?"
|
40
|
+
assert_equals( msg, true, @parser.symbol?('"') )
|
41
|
+
assert_equals( msg, true, @parser.symbol?("'") )
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
def test_digit?
|
46
|
+
msg = "digit?"
|
47
|
+
assert_equals( msg, true, @parser.digit?("0") )
|
48
|
+
assert_equals( msg, true, @parser.digit?("5") )
|
49
|
+
assert_equals( msg, true, @parser.digit?("9") )
|
50
|
+
assert_equals( msg, false, @parser.digit?("a") )
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
##
|
55
|
+
# コメントと文字列のテストは coarse tokenize で行う。
|
56
|
+
def test_next_sql_token_pos
|
57
|
+
msg = "token pos"
|
58
|
+
|
59
|
+
@parser.before = " "
|
60
|
+
@parser.pos = 0
|
61
|
+
token = @parser.next_sql_token
|
62
|
+
assert_equals(msg,
|
63
|
+
0, token.pos)
|
64
|
+
|
65
|
+
@parser.before = "a b"
|
66
|
+
@parser.pos = 1
|
67
|
+
token = @parser.next_sql_token
|
68
|
+
assert_equals(msg,
|
69
|
+
1, token.pos)
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
##
|
74
|
+
# コメントと文字列のテストは coarse tokenize で行う。
|
75
|
+
def test_next_sql_token
|
76
|
+
msg = "token type recognition"
|
77
|
+
|
78
|
+
########
|
79
|
+
@parser.before = " "
|
80
|
+
@parser.pos = 0
|
81
|
+
assert_equals( msg + "space",
|
82
|
+
(<<EOB
|
83
|
+
<space> </>
|
84
|
+
EOB
|
85
|
+
).chomp,
|
86
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
87
|
+
)
|
88
|
+
|
89
|
+
########
|
90
|
+
@parser.before = "a b"
|
91
|
+
@parser.pos = 1
|
92
|
+
assert_equals( msg + "space",
|
93
|
+
(<<EOB
|
94
|
+
<space> </>
|
95
|
+
EOB
|
96
|
+
).chomp,
|
97
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
98
|
+
)
|
99
|
+
|
100
|
+
########
|
101
|
+
@parser.before = ","
|
102
|
+
@parser.pos = 0
|
103
|
+
assert_equals( msg + "symbol",
|
104
|
+
(<<EOB
|
105
|
+
<symbol>,</>
|
106
|
+
EOB
|
107
|
+
).chomp,
|
108
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
109
|
+
)
|
110
|
+
|
111
|
+
########
|
112
|
+
@parser.before = "select"
|
113
|
+
@parser.pos = 0
|
114
|
+
assert_equals( msg + "keyword: select",
|
115
|
+
(<<EOB
|
116
|
+
<keyword>select</>
|
117
|
+
EOB
|
118
|
+
).chomp,
|
119
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
120
|
+
)
|
121
|
+
|
122
|
+
########
|
123
|
+
@parser.before = "case"
|
124
|
+
@parser.pos = 0
|
125
|
+
assert_equals( msg + "keyword: case",
|
126
|
+
(<<EOB
|
127
|
+
<keyword>case</>
|
128
|
+
EOB
|
129
|
+
).chomp,
|
130
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
131
|
+
)
|
132
|
+
|
133
|
+
########
|
134
|
+
@parser.before = "xxx123"
|
135
|
+
@parser.pos = 0
|
136
|
+
assert_equals( msg + "name",
|
137
|
+
(<<EOB
|
138
|
+
<name>xxx123</>
|
139
|
+
EOB
|
140
|
+
).chomp,
|
141
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
142
|
+
)
|
143
|
+
|
144
|
+
########
|
145
|
+
@parser.before = '123'
|
146
|
+
@parser.pos = 0
|
147
|
+
assert_equals( msg + "value",
|
148
|
+
(<<EOB
|
149
|
+
<value>123</>
|
150
|
+
EOB
|
151
|
+
).chomp,
|
152
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
153
|
+
)
|
154
|
+
|
155
|
+
########
|
156
|
+
@parser.before = '1.23'
|
157
|
+
@parser.pos = 0
|
158
|
+
assert_equals( msg + "value",
|
159
|
+
(<<EOB
|
160
|
+
<value>1.23</>
|
161
|
+
EOB
|
162
|
+
).chomp,
|
163
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
164
|
+
)
|
165
|
+
|
166
|
+
########
|
167
|
+
@parser.before = '-1.23 '
|
168
|
+
@parser.pos = 0
|
169
|
+
assert_equals( msg + "value",
|
170
|
+
(<<EOB
|
171
|
+
<value>-1.23</>
|
172
|
+
EOB
|
173
|
+
).chomp,
|
174
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
175
|
+
)
|
176
|
+
|
177
|
+
########
|
178
|
+
@parser.before = '1.23e45 '
|
179
|
+
@parser.pos = 0
|
180
|
+
assert_equals( msg + "value",
|
181
|
+
(<<EOB
|
182
|
+
<value>1.23e45</>
|
183
|
+
EOB
|
184
|
+
).chomp,
|
185
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
186
|
+
)
|
187
|
+
|
188
|
+
########
|
189
|
+
@parser.before = '1.23e-45 '
|
190
|
+
@parser.pos = 0
|
191
|
+
assert_equals( msg + "value",
|
192
|
+
(<<EOB
|
193
|
+
<value>1.23e-45</>
|
194
|
+
EOB
|
195
|
+
).chomp,
|
196
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
197
|
+
)
|
198
|
+
|
199
|
+
########
|
200
|
+
@parser.before = '-1.23e-45 '
|
201
|
+
@parser.pos = 0
|
202
|
+
assert_equals( msg + "value",
|
203
|
+
(<<EOB
|
204
|
+
<value>-1.23e-45</>
|
205
|
+
EOB
|
206
|
+
).chomp,
|
207
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
208
|
+
)
|
209
|
+
|
210
|
+
########
|
211
|
+
@parser.before = '0x01 '
|
212
|
+
@parser.pos = 0
|
213
|
+
assert_equals( msg + "value",
|
214
|
+
(<<EOB
|
215
|
+
<value>0x01</>
|
216
|
+
EOB
|
217
|
+
).chomp,
|
218
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
219
|
+
)
|
220
|
+
|
221
|
+
########
|
222
|
+
@parser.before = '1x'
|
223
|
+
@parser.pos = 0
|
224
|
+
assert_equals( msg + "value",
|
225
|
+
(<<EOB
|
226
|
+
<value>1</>
|
227
|
+
EOB
|
228
|
+
).chomp,
|
229
|
+
Helper.format_tokens([ @parser.next_sql_token ])
|
230
|
+
)
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
def test_parser
|
235
|
+
msg = "parser basic case - "
|
236
|
+
|
237
|
+
########
|
238
|
+
assert_equals( msg + "", (<<EOB
|
239
|
+
<keyword>select</>
|
240
|
+
<space> </>
|
241
|
+
<name>a</>
|
242
|
+
<space> </>
|
243
|
+
<keyword>from</>
|
244
|
+
<space> </>
|
245
|
+
<name>b</>
|
246
|
+
<symbol>;</>
|
247
|
+
EOB
|
248
|
+
).strip, Helper.format_tokens( @parser.parse( (<<EOB
|
249
|
+
select a from b;
|
250
|
+
EOB
|
251
|
+
).chop
|
252
|
+
))
|
253
|
+
)
|
254
|
+
|
255
|
+
########
|
256
|
+
assert_equals( msg + "minus + non-number", (<<EOB
|
257
|
+
<symbol>-</>
|
258
|
+
<name>a</>
|
259
|
+
EOB
|
260
|
+
).strip, Helper.format_tokens( @parser.parse( (<<EOB
|
261
|
+
-a
|
262
|
+
EOB
|
263
|
+
).chop
|
264
|
+
))
|
265
|
+
)
|
266
|
+
|
267
|
+
########
|
268
|
+
assert_equals( msg + "single comment", (<<EOB
|
269
|
+
<keyword>select</>
|
270
|
+
<space>\n</>
|
271
|
+
<comment>-- x</>
|
272
|
+
<name>a</>
|
273
|
+
<space>\n</>
|
274
|
+
EOB
|
275
|
+
).strip, Helper.format_tokens( @parser.parse(<<EOB
|
276
|
+
select
|
277
|
+
-- x
|
278
|
+
a
|
279
|
+
EOB
|
280
|
+
))
|
281
|
+
)
|
282
|
+
|
283
|
+
########
|
284
|
+
assert_equals( msg + "parenthesis in single quote", (<<EOB
|
285
|
+
<value>'()'</>
|
286
|
+
EOB
|
287
|
+
).strip, Helper.format_tokens( @parser.parse((<<EOB
|
288
|
+
'()'
|
289
|
+
EOB
|
290
|
+
).strip
|
291
|
+
))
|
292
|
+
)
|
293
|
+
|
294
|
+
########
|
295
|
+
assert_equals( msg + "parenthesis in double quote", (<<EOB
|
296
|
+
<name>"()"</>
|
297
|
+
EOB
|
298
|
+
).strip, Helper.format_tokens( @parser.parse((<<EOB
|
299
|
+
"()"
|
300
|
+
EOB
|
301
|
+
).strip
|
302
|
+
))
|
303
|
+
)
|
304
|
+
|
305
|
+
########
|
306
|
+
assert_equals( msg + "multiple line comment: 1", (<<EOB
|
307
|
+
<name>aa</>
|
308
|
+
<comment>/*bb*/</>
|
309
|
+
<name>cc</>
|
310
|
+
EOB
|
311
|
+
).strip, Helper.format_tokens( @parser.parse((<<EOB
|
312
|
+
aa/*bb*/cc
|
313
|
+
EOB
|
314
|
+
).strip
|
315
|
+
))
|
316
|
+
)
|
317
|
+
|
318
|
+
########
|
319
|
+
assert_equals( msg + "multiple line comment: 2", (<<EOB
|
320
|
+
<name>aa</>
|
321
|
+
<comment>/*b
|
322
|
+
b*/</>
|
323
|
+
<name>cc</>
|
324
|
+
EOB
|
325
|
+
).strip, Helper.format_tokens( @parser.parse((<<EOB
|
326
|
+
aa/*b
|
327
|
+
b*/cc
|
328
|
+
EOB
|
329
|
+
).strip
|
330
|
+
))
|
331
|
+
)
|
332
|
+
|
333
|
+
########
|
334
|
+
assert_equals( msg + "invalid paired double quote", (<<EOB
|
335
|
+
<name>aa</>
|
336
|
+
<name>"bb</>
|
337
|
+
EOB
|
338
|
+
).strip, Helper.format_tokens( @parser.parse((<<EOB
|
339
|
+
aa"bb
|
340
|
+
EOB
|
341
|
+
).strip
|
342
|
+
))
|
343
|
+
)
|
344
|
+
|
345
|
+
########
|
346
|
+
assert_equals( msg + "multiwords keyword", (<<EOB
|
347
|
+
<keyword>group by</>
|
348
|
+
EOB
|
349
|
+
).strip, Helper.format_tokens( @parser.parse((<<EOB
|
350
|
+
group by
|
351
|
+
EOB
|
352
|
+
).strip
|
353
|
+
))
|
354
|
+
)
|
355
|
+
|
356
|
+
########
|
357
|
+
assert_equals( msg + "multiwords keyword 2",
|
358
|
+
(<<EOB
|
359
|
+
<name>a</>
|
360
|
+
<space> </>
|
361
|
+
<keyword>group by</>
|
362
|
+
<space> </>
|
363
|
+
<name>B</>
|
364
|
+
EOB
|
365
|
+
).strip,
|
366
|
+
Helper.format_tokens( @parser.parse("a group by B") )
|
367
|
+
)
|
368
|
+
|
369
|
+
end
|
370
|
+
end
|
data/test/test_rule.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "helper")
|
4
|
+
|
5
|
+
require "anbt-sql-formatter/helper"
|
6
|
+
require "anbt-sql-formatter/rule"
|
7
|
+
|
8
|
+
|
9
|
+
class TestAnbtSqlRule < Test::Unit::TestCase
|
10
|
+
def setup
|
11
|
+
@rule = AnbtSql::Rule.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_function?
|
15
|
+
msg = "function? - "
|
16
|
+
|
17
|
+
func_name = "TEST_FUNCTION"
|
18
|
+
@rule.function_names << func_name
|
19
|
+
assert_equals( msg,
|
20
|
+
true,
|
21
|
+
@rule.function?(func_name)
|
22
|
+
)
|
23
|
+
|
24
|
+
@rule.function_names.delete(func_name)
|
25
|
+
assert_equals( msg,
|
26
|
+
false,
|
27
|
+
@rule.function?(func_name)
|
28
|
+
)
|
29
|
+
end
|
30
|
+
end
|
data/uninstall.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "fileutils"
|
2
|
+
|
3
|
+
installed_files = "InstalledFiles"
|
4
|
+
|
5
|
+
if not File.exist? installed_files
|
6
|
+
STDERR.puts "'InstalledFiles' not found."
|
7
|
+
exit 1
|
8
|
+
end
|
9
|
+
|
10
|
+
open( installed_files ){|f|
|
11
|
+
while path = f.gets
|
12
|
+
path.chomp!
|
13
|
+
|
14
|
+
if File.exist? path
|
15
|
+
FileUtils.rm path
|
16
|
+
end
|
17
|
+
end
|
18
|
+
}
|
19
|
+
|
20
|
+
FileUtils.mv installed_files, installed_files + ".old"
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: anbt-sql-formatter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- sonota
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-09-17 00:00:00 +09:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: A tool for SQL formatting written in Ruby. Ruby port of Blanco SQL Formatter.
|
18
|
+
email:
|
19
|
+
- yosiot8753@gmail.com
|
20
|
+
executables:
|
21
|
+
- anbt-sql-formatter
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- Gemfile
|
29
|
+
- Rakefile
|
30
|
+
- anbt-sql-formatter.gemspec
|
31
|
+
- bin/anbt-sql-formatter
|
32
|
+
- lgpl-2.1.txt
|
33
|
+
- lib/anbt-sql-formatter/coarse-tokenizer.rb
|
34
|
+
- lib/anbt-sql-formatter/constants.rb
|
35
|
+
- lib/anbt-sql-formatter/exception.rb
|
36
|
+
- lib/anbt-sql-formatter/formatter.rb
|
37
|
+
- lib/anbt-sql-formatter/helper.rb
|
38
|
+
- lib/anbt-sql-formatter/parser.rb
|
39
|
+
- lib/anbt-sql-formatter/rule.rb
|
40
|
+
- lib/anbt-sql-formatter/token.rb
|
41
|
+
- lib/anbt-sql-formatter/version.rb
|
42
|
+
- misc/anbt-sql-formatter-customize-example
|
43
|
+
- misc/anbt-sql-formatter-for-sakura-editor.js
|
44
|
+
- readme.ja.txt
|
45
|
+
- readme.txt
|
46
|
+
- sample.sql
|
47
|
+
- setup.rb
|
48
|
+
- test/helper.rb
|
49
|
+
- test/test_coarse-tokenizer.rb
|
50
|
+
- test/test_formatter.rb
|
51
|
+
- test/test_helper.rb
|
52
|
+
- test/test_parser.rb
|
53
|
+
- test/test_rule.rb
|
54
|
+
- uninstall.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: ""
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project: anbt-sql-formatter
|
79
|
+
rubygems_version: 1.5.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: A tool for SQL formatting written in Ruby.
|
83
|
+
test_files: []
|
84
|
+
|