anbt-sql-formatter 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/anbt-sql-formatter +0 -3
- data/lib/anbt-sql-formatter/coarse-tokenizer.rb +1 -3
- data/lib/anbt-sql-formatter/exception.rb +0 -2
- data/lib/anbt-sql-formatter/formatter.rb +50 -57
- data/lib/anbt-sql-formatter/helper.rb +42 -56
- data/lib/anbt-sql-formatter/parser.rb +40 -52
- data/lib/anbt-sql-formatter/rule.rb +5 -2
- data/lib/anbt-sql-formatter/token.rb +1 -10
- data/lib/anbt-sql-formatter/version.rb +1 -1
- data/test/helper.rb +7 -1
- data/test/test_coarse-tokenizer.rb +327 -228
- data/test/test_formatter.rb +363 -276
- data/test/test_helper.rb +2 -2
- data/test/test_parser.rb +302 -213
- data/test/test_rule.rb +10 -8
- metadata +21 -31
@@ -1,6 +1,6 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
|
3
|
-
require "
|
3
|
+
require "anbt-sql-formatter/helper"
|
4
4
|
|
5
5
|
=begin
|
6
6
|
AnbtSqlFormatter: SQL整形ツール. SQL文を決められたルールに従い整形します。
|
@@ -19,6 +19,9 @@ http://homepage2.nifty.com/igat/igapyon/diary/2005/ig050613.html
|
|
19
19
|
|
20
20
|
class AnbtSql
|
21
21
|
class Rule
|
22
|
+
|
23
|
+
include StringUtil
|
24
|
+
|
22
25
|
attr_accessor :keyword, :indent_string, :function_names, :space_after_comma
|
23
26
|
attr_accessor :kw_multi_words
|
24
27
|
|
@@ -110,7 +113,7 @@ class AnbtSql
|
|
110
113
|
end
|
111
114
|
|
112
115
|
for i in 0...(@function_names.length)
|
113
|
-
if (@function_names[i]
|
116
|
+
if (equals_ignore_case(@function_names[i], name))
|
114
117
|
return true
|
115
118
|
end
|
116
119
|
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
|
3
|
-
require "pp"
|
4
|
-
|
5
3
|
class AnbtSql
|
6
4
|
class TokenConstants
|
7
5
|
|
@@ -56,14 +54,7 @@ class AnbtSql
|
|
56
54
|
# @return:: バリューオブジェクトの文字列表現。
|
57
55
|
#
|
58
56
|
def to_s
|
59
|
-
|
60
|
-
buf << "AbstractAnbtSqlToken["
|
61
|
-
buf << "_type=" + @_type
|
62
|
-
buf << ",string=" + @string
|
63
|
-
buf << ",pos=" + @pos
|
64
|
-
buf << "]"
|
65
|
-
|
66
|
-
buf
|
57
|
+
@string
|
67
58
|
end
|
68
59
|
end
|
69
60
|
|
data/test/helper.rb
CHANGED
@@ -6,7 +6,7 @@ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
|
6
6
|
class Helper
|
7
7
|
def Helper.format_tokens(list)
|
8
8
|
list.map{|token|
|
9
|
-
"
|
9
|
+
"#{token._type} (#{token.string})"
|
10
10
|
}.join("\n")
|
11
11
|
end
|
12
12
|
end
|
@@ -15,3 +15,9 @@ end
|
|
15
15
|
def assert_equals(a,b,c)
|
16
16
|
assert_equal(b,c,a)
|
17
17
|
end
|
18
|
+
|
19
|
+
def strip_indent(text)
|
20
|
+
text.split("\n").map{|line|
|
21
|
+
line.sub(/^ */, "")
|
22
|
+
}.join("\n")
|
23
|
+
end
|
@@ -9,16 +9,18 @@ class CoarseTokenizer
|
|
9
9
|
end
|
10
10
|
|
11
11
|
|
12
|
-
def format(tokens)
|
13
|
-
tokens.map{|t| t.to_s }.join("\n")
|
14
|
-
end
|
15
|
-
|
16
12
|
|
17
13
|
class TestCoarseTokenizer < Test::Unit::TestCase
|
18
14
|
def setup
|
19
15
|
@tok = CoarseTokenizer.new
|
20
16
|
end
|
21
17
|
|
18
|
+
def _format(tokens)
|
19
|
+
tokens.map{|t|
|
20
|
+
"#{t._type} (#{t.string})"
|
21
|
+
}.join("\n")
|
22
|
+
end
|
23
|
+
|
22
24
|
|
23
25
|
def test_shift_to_buf
|
24
26
|
@tok.buf = ""
|
@@ -62,78 +64,102 @@ class TestCoarseTokenizer < Test::Unit::TestCase
|
|
62
64
|
def test_tokenize
|
63
65
|
msg = "tokenize - "
|
64
66
|
|
65
|
-
assert_equals(
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
67
|
+
assert_equals(
|
68
|
+
msg,
|
69
|
+
strip_indent(
|
70
|
+
<<-EOB
|
71
|
+
plain (aa)
|
72
|
+
EOB
|
73
|
+
),
|
74
|
+
_format(@tok.tokenize(strip_indent(
|
75
|
+
<<-EOB
|
76
|
+
aa
|
77
|
+
EOB
|
78
|
+
)))
|
79
|
+
)
|
74
80
|
|
75
81
|
########
|
76
|
-
assert_equals(
|
77
|
-
|
78
|
-
|
79
|
-
EOB
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
82
|
+
assert_equals(
|
83
|
+
msg,
|
84
|
+
strip_indent(
|
85
|
+
<<-EOB
|
86
|
+
plain (aa )
|
87
|
+
quote_double ("bb")
|
88
|
+
EOB
|
89
|
+
),
|
90
|
+
_format(@tok.tokenize(strip_indent(
|
91
|
+
<<-EOB
|
92
|
+
aa "bb"
|
93
|
+
EOB
|
94
|
+
)))
|
95
|
+
)
|
86
96
|
|
87
97
|
########
|
88
|
-
assert_equals(
|
89
|
-
|
90
|
-
|
91
|
-
EOB
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
+
assert_equals(
|
99
|
+
msg,
|
100
|
+
strip_indent(
|
101
|
+
<<-EOB
|
102
|
+
plain (aa )
|
103
|
+
quote_single ('bb')
|
104
|
+
EOB
|
105
|
+
),
|
106
|
+
_format(@tok.tokenize(strip_indent(
|
107
|
+
<<-EOB
|
108
|
+
aa 'bb'
|
109
|
+
EOB
|
110
|
+
)))
|
111
|
+
)
|
98
112
|
|
99
113
|
########
|
100
|
-
assert_equals(
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
114
|
+
assert_equals(
|
115
|
+
msg,
|
116
|
+
strip_indent(
|
117
|
+
<<-EOB
|
118
|
+
plain (aa )
|
119
|
+
comment_single (--bb\n)
|
120
|
+
plain (cc)
|
121
|
+
EOB
|
122
|
+
),
|
123
|
+
_format(@tok.tokenize(strip_indent(
|
124
|
+
<<-EOB
|
125
|
+
aa --bb
|
126
|
+
cc
|
127
|
+
EOB
|
128
|
+
)))
|
129
|
+
)
|
112
130
|
|
113
131
|
########
|
114
|
-
assert_equals(
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
132
|
+
assert_equals(
|
133
|
+
msg,
|
134
|
+
strip_indent(
|
135
|
+
<<-EOB
|
136
|
+
plain (aa )
|
137
|
+
comment_multi (/* bb */)
|
138
|
+
plain ( cc)
|
139
|
+
EOB
|
140
|
+
),
|
141
|
+
_format(@tok.tokenize(strip_indent(
|
142
|
+
<<-EOB
|
143
|
+
aa /* bb */ cc
|
144
|
+
EOB
|
145
|
+
)))
|
146
|
+
)
|
125
147
|
|
126
148
|
########
|
127
|
-
assert_equals(
|
128
|
-
|
129
|
-
|
130
|
-
EOB
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
149
|
+
assert_equals(
|
150
|
+
msg + "begin with multiline comment",
|
151
|
+
strip_indent(
|
152
|
+
<<-EOB
|
153
|
+
comment_multi (/* bb */)
|
154
|
+
plain ( cc)
|
155
|
+
EOB
|
156
|
+
),
|
157
|
+
_format(@tok.tokenize(strip_indent(
|
158
|
+
<<-EOB
|
159
|
+
/* bb */ cc
|
160
|
+
EOB
|
161
|
+
)))
|
162
|
+
)
|
137
163
|
end
|
138
164
|
|
139
165
|
|
@@ -141,74 +167,99 @@ EOB
|
|
141
167
|
msg = "string_in_string"
|
142
168
|
|
143
169
|
########
|
144
|
-
assert_equals(
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
170
|
+
assert_equals(
|
171
|
+
msg,
|
172
|
+
strip_indent(
|
173
|
+
<<-EOB
|
174
|
+
quote_double ("aa'bb'cc")
|
175
|
+
EOB
|
176
|
+
),
|
177
|
+
_format(@tok.tokenize(strip_indent(
|
178
|
+
<<-EOB
|
179
|
+
"aa'bb'cc"
|
180
|
+
EOB
|
181
|
+
)))
|
182
|
+
)
|
153
183
|
|
154
184
|
########
|
155
|
-
assert_equals(
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
185
|
+
assert_equals(
|
186
|
+
msg,
|
187
|
+
strip_indent(
|
188
|
+
<<-EOB
|
189
|
+
quote_single ('aa"bb"cc')
|
190
|
+
EOB
|
191
|
+
),
|
192
|
+
_format(@tok.tokenize(strip_indent(
|
193
|
+
<<-EOB
|
194
|
+
'aa"bb"cc'
|
195
|
+
EOB
|
196
|
+
)))
|
197
|
+
)
|
164
198
|
end
|
165
199
|
|
166
200
|
|
167
201
|
def test_comment_in_comment
|
168
202
|
msg = "comment_in_comment - "
|
203
|
+
|
169
204
|
########
|
170
|
-
assert_equals(
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
205
|
+
assert_equals(
|
206
|
+
msg,
|
207
|
+
strip_indent(
|
208
|
+
<<-EOB
|
209
|
+
comment_single (--a--b)
|
210
|
+
EOB
|
211
|
+
),
|
212
|
+
_format(@tok.tokenize(strip_indent(
|
213
|
+
<<-EOB
|
214
|
+
--a--b
|
215
|
+
EOB
|
216
|
+
)))
|
217
|
+
)
|
179
218
|
|
180
219
|
########
|
181
|
-
assert_equals(
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
220
|
+
assert_equals(
|
221
|
+
msg,
|
222
|
+
strip_indent(
|
223
|
+
<<-EOB
|
224
|
+
comment_single (-- aa /* bb */)
|
225
|
+
EOB
|
226
|
+
),
|
227
|
+
_format(@tok.tokenize(strip_indent(
|
228
|
+
<<-EOB
|
229
|
+
-- aa /* bb */
|
230
|
+
EOB
|
231
|
+
)))
|
232
|
+
)
|
190
233
|
|
191
234
|
########
|
192
|
-
assert_equals(
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
235
|
+
assert_equals(
|
236
|
+
msg,
|
237
|
+
strip_indent(
|
238
|
+
<<-EOB
|
239
|
+
comment_multi (/* aa /* bb */)
|
240
|
+
EOB
|
241
|
+
),
|
242
|
+
_format(@tok.tokenize(strip_indent(
|
243
|
+
<<-EOB
|
244
|
+
/* aa /* bb */
|
245
|
+
EOB
|
246
|
+
)))
|
247
|
+
)
|
201
248
|
|
202
249
|
########
|
203
|
-
assert_equals(
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
250
|
+
assert_equals(
|
251
|
+
msg,
|
252
|
+
strip_indent(
|
253
|
+
<<-EOB
|
254
|
+
comment_multi (/* aa -- bb */)
|
255
|
+
EOB
|
256
|
+
),
|
257
|
+
_format(@tok.tokenize(strip_indent(
|
258
|
+
<<-EOB
|
259
|
+
/* aa -- bb */
|
260
|
+
EOB
|
261
|
+
)))
|
262
|
+
)
|
212
263
|
end
|
213
264
|
|
214
265
|
|
@@ -216,48 +267,64 @@ EOB
|
|
216
267
|
msg = "string_in_comment - "
|
217
268
|
|
218
269
|
########
|
219
|
-
assert_equals(
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
270
|
+
assert_equals(
|
271
|
+
msg,
|
272
|
+
strip_indent(
|
273
|
+
<<-EOB
|
274
|
+
comment_single (-- aa "bb" cc)
|
275
|
+
EOB
|
276
|
+
),
|
277
|
+
_format(@tok.tokenize(strip_indent(
|
278
|
+
<<-EOB
|
279
|
+
-- aa "bb" cc
|
280
|
+
EOB
|
281
|
+
)))
|
282
|
+
)
|
228
283
|
|
229
284
|
########
|
230
|
-
assert_equals(
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
285
|
+
assert_equals(
|
286
|
+
msg,
|
287
|
+
strip_indent(
|
288
|
+
<<-EOB
|
289
|
+
comment_single (-- aa 'bb' cc)
|
290
|
+
EOB
|
291
|
+
),
|
292
|
+
_format(@tok.tokenize(strip_indent(
|
293
|
+
<<-EOB
|
294
|
+
-- aa 'bb' cc
|
295
|
+
EOB
|
296
|
+
)))
|
297
|
+
)
|
239
298
|
|
240
299
|
########
|
241
|
-
assert_equals(
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
300
|
+
assert_equals(
|
301
|
+
msg,
|
302
|
+
strip_indent(
|
303
|
+
<<-EOB
|
304
|
+
comment_multi (/* aa "bb" cc */)
|
305
|
+
EOB
|
306
|
+
),
|
307
|
+
_format(@tok.tokenize(strip_indent(
|
308
|
+
<<-EOB
|
309
|
+
/* aa "bb" cc */
|
310
|
+
EOB
|
311
|
+
)))
|
312
|
+
)
|
250
313
|
|
251
314
|
########
|
252
|
-
assert_equals(
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
315
|
+
assert_equals(
|
316
|
+
msg,
|
317
|
+
strip_indent(
|
318
|
+
<<-EOB
|
319
|
+
comment_multi (/* aa 'bb' cc */)
|
320
|
+
EOB
|
321
|
+
),
|
322
|
+
_format(@tok.tokenize(strip_indent(
|
323
|
+
<<-EOB
|
324
|
+
/* aa 'bb' cc */
|
325
|
+
EOB
|
326
|
+
)))
|
327
|
+
)
|
261
328
|
end
|
262
329
|
|
263
330
|
|
@@ -265,48 +332,64 @@ EOB
|
|
265
332
|
msg = "comment_in_string - "
|
266
333
|
|
267
334
|
########
|
268
|
-
assert_equals(
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
335
|
+
assert_equals(
|
336
|
+
msg + "comment_single in quote_single",
|
337
|
+
strip_indent(
|
338
|
+
<<-EOB
|
339
|
+
quote_single ('aa--bb')
|
340
|
+
EOB
|
341
|
+
),
|
342
|
+
_format(@tok.tokenize(strip_indent(
|
343
|
+
<<-EOB
|
344
|
+
'aa--bb'
|
345
|
+
EOB
|
346
|
+
)))
|
347
|
+
)
|
277
348
|
|
278
349
|
########
|
279
|
-
assert_equals(
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
350
|
+
assert_equals(
|
351
|
+
msg + "comment_single in quote_double",
|
352
|
+
strip_indent(
|
353
|
+
<<-EOB
|
354
|
+
quote_double ("aa--bb")
|
355
|
+
EOB
|
356
|
+
),
|
357
|
+
_format(@tok.tokenize(strip_indent(
|
358
|
+
<<-EOB
|
359
|
+
"aa--bb"
|
360
|
+
EOB
|
361
|
+
)))
|
362
|
+
)
|
288
363
|
|
289
364
|
########
|
290
|
-
assert_equals(
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
365
|
+
assert_equals(
|
366
|
+
msg + "comment_multi in quote_double",
|
367
|
+
strip_indent(
|
368
|
+
<<-EOB
|
369
|
+
quote_double ("aa /* bb */ cc")
|
370
|
+
EOB
|
371
|
+
),
|
372
|
+
_format(@tok.tokenize(strip_indent(
|
373
|
+
<<-EOB
|
374
|
+
"aa /* bb */ cc"
|
375
|
+
EOB
|
376
|
+
)))
|
377
|
+
)
|
299
378
|
|
300
379
|
########
|
301
|
-
assert_equals(
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
380
|
+
assert_equals(
|
381
|
+
msg + "comment_multi in quote_double",
|
382
|
+
strip_indent(
|
383
|
+
<<-EOB
|
384
|
+
quote_single ('aa /* bb */ cc')
|
385
|
+
EOB
|
386
|
+
),
|
387
|
+
_format(@tok.tokenize(strip_indent(
|
388
|
+
<<-EOB
|
389
|
+
'aa /* bb */ cc'
|
390
|
+
EOB
|
391
|
+
)))
|
392
|
+
)
|
310
393
|
end
|
311
394
|
|
312
395
|
|
@@ -314,47 +397,63 @@ EOB
|
|
314
397
|
msg = "string_escape"
|
315
398
|
|
316
399
|
########
|
317
|
-
assert_equals(
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
400
|
+
assert_equals(
|
401
|
+
msg,
|
402
|
+
strip_indent(
|
403
|
+
<<-EOB
|
404
|
+
quote_double ("_a_\\\\_b_\n_c_\\'_d_")
|
405
|
+
EOB
|
406
|
+
),
|
407
|
+
_format(@tok.tokenize(strip_indent(
|
408
|
+
<<-EOB
|
409
|
+
"_a_\\\\_b_\n_c_\\'_d_"
|
410
|
+
EOB
|
411
|
+
)))
|
412
|
+
)
|
326
413
|
|
327
414
|
########
|
328
|
-
assert_equals(
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
415
|
+
assert_equals(
|
416
|
+
msg,
|
417
|
+
strip_indent(
|
418
|
+
<<-EOB
|
419
|
+
quote_single ('_a_\\\\_b_\n_c_\\'_d_')
|
420
|
+
EOB
|
421
|
+
),
|
422
|
+
_format(@tok.tokenize(strip_indent(
|
423
|
+
<<-EOB
|
424
|
+
'_a_\\\\_b_\n_c_\\'_d_'
|
425
|
+
EOB
|
426
|
+
)))
|
427
|
+
)
|
337
428
|
|
338
429
|
########
|
339
|
-
assert_equals(
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
430
|
+
assert_equals(
|
431
|
+
msg,
|
432
|
+
strip_indent(
|
433
|
+
<<-EOB
|
434
|
+
quote_double ("_a_""_b_")
|
435
|
+
EOB
|
436
|
+
),
|
437
|
+
_format(@tok.tokenize(strip_indent(
|
438
|
+
<<-EOB
|
439
|
+
"_a_""_b_"
|
440
|
+
EOB
|
441
|
+
)))
|
442
|
+
)
|
348
443
|
|
349
444
|
########
|
350
|
-
assert_equals(
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
445
|
+
assert_equals(
|
446
|
+
msg,
|
447
|
+
strip_indent(
|
448
|
+
<<-EOB
|
449
|
+
quote_single ('_a_''_b_')
|
450
|
+
EOB
|
451
|
+
),
|
452
|
+
_format(@tok.tokenize(strip_indent(
|
453
|
+
<<-EOB
|
454
|
+
'_a_''_b_'
|
455
|
+
EOB
|
456
|
+
)))
|
457
|
+
)
|
359
458
|
end
|
360
459
|
end
|