anbt-sql-formatter 0.0.7 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/CHANGELOG.md +38 -1
- data/{readme.ja.txt → README.ja.rdoc} +2 -2
- data/{readme.txt → README.rdoc} +2 -2
- data/anbt-sql-formatter.gemspec +2 -0
- data/lib/anbt-sql-formatter/coarse-tokenizer.rb +28 -27
- data/lib/anbt-sql-formatter/constants.rb +1 -1
- data/lib/anbt-sql-formatter/parser.rb +1 -1
- data/lib/anbt-sql-formatter/rule.rb +1 -1
- data/lib/anbt-sql-formatter/token.rb +2 -2
- data/lib/anbt-sql-formatter/version.rb +1 -1
- data/misc/anbt-sql-formatter-customize-example +4 -4
- data/sample.sql +4 -0
- data/test/test_coarse-tokenizer.rb +5 -5
- data/test/test_formatter.rb +506 -529
- data/test/test_in_values.rb +6 -6
- data/test/test_parser.rb +435 -411
- data/test.sh +10 -0
- metadata +21 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0bd065b8b98fe3a87257b39677bd7ed0fc78233173c0a1f1e99792cec2e9868f
|
4
|
+
data.tar.gz: 999857541e0547429abb974f830fbba8ff0c31ae2284b89bfd840353e3029786
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc5d07dc5eb05cc96c250e865bf36f2ae7b8e11457b3850d112b75925a00ccb93f498b1fc389288de875d77292b7139464227f35fa7db56fb602498ffe07e623
|
7
|
+
data.tar.gz: 276dc243eeaeab0ee353017e1f7d1bbcf6ec3e11e8b295f5a93331d4b5786f68a6725abe1ca89c81f72a53da821f318aeebde04ed2aa9eb205cfb497f256debe
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,45 @@
|
|
1
|
+
# 0.1.1 (2025-01-12)
|
2
|
+
|
3
|
+
No breaking changes.
|
4
|
+
|
5
|
+
## Improvements
|
6
|
+
|
7
|
+
- Avoid mutative string manipulation (coarse-tokenizer.rb: tokenize)
|
8
|
+
- Prevents frozen literal warnings in Ruby 3.4 (Issue #17)
|
9
|
+
- Update comments (PR #14)
|
10
|
+
- Some small fixes, cleanups, formatting improvements
|
11
|
+
|
12
|
+
|
13
|
+
# 0.1.0 (2018-12-16)
|
14
|
+
|
15
|
+
## Breaking changes
|
16
|
+
|
17
|
+
- Support `"{schema}"."{table}"` notation.
|
18
|
+
- Tokenize as a single name token. This affects formatter output:
|
19
|
+
|
20
|
+
```
|
21
|
+
echo 'select a from b.c, "d"."e"' | bin/anbt-sql-formatter
|
22
|
+
|
23
|
+
(before)
|
24
|
+
SELECT
|
25
|
+
a
|
26
|
+
FROM
|
27
|
+
b.c
|
28
|
+
,"d" . "e"
|
29
|
+
|
30
|
+
(after)
|
31
|
+
SELECT
|
32
|
+
a
|
33
|
+
FROM
|
34
|
+
b.c
|
35
|
+
,"d"."e"
|
36
|
+
```
|
37
|
+
|
1
38
|
# 0.0.7 (2018-08-11)
|
2
39
|
|
3
40
|
No breaking changes.
|
4
41
|
|
5
|
-
##
|
42
|
+
## Features
|
6
43
|
|
7
44
|
- New configuration parameter `Rule#in_values_num`
|
8
45
|
for controlling number of values in IN clause per line.
|
data/{readme.txt → README.rdoc}
RENAMED
@@ -1,6 +1,6 @@
|
|
1
1
|
= anbt-sql-formatter
|
2
2
|
|
3
|
-
(C) 2010
|
3
|
+
(C) 2010-2018 sonota88 (yosiot8753@gmail.com)
|
4
4
|
|
5
5
|
|
6
6
|
== Description
|
@@ -55,4 +55,4 @@ Following are Authors of BlancoSqlFormatter(original Java version).
|
|
55
55
|
|
56
56
|
== Test
|
57
57
|
|
58
|
-
$
|
58
|
+
$ ./test.sh
|
data/anbt-sql-formatter.gemspec
CHANGED
@@ -38,8 +38,7 @@ These are exclusive:
|
|
38
38
|
=end
|
39
39
|
|
40
40
|
def tokenize(str)
|
41
|
-
@str = str
|
42
|
-
@str.gsub!("\r\n", "\n")
|
41
|
+
@str = str.gsub("\r\n", "\n")
|
43
42
|
out_of_quote_single = true
|
44
43
|
out_of_quote_double = true
|
45
44
|
out_of_comment_single = true
|
@@ -51,34 +50,36 @@ These are exclusive:
|
|
51
50
|
|
52
51
|
while @str.size > 0
|
53
52
|
|
54
|
-
if /\A(")/ =~ str && out_of_quote_double &&
|
53
|
+
if /\A(")/ =~ @str && out_of_quote_double &&
|
55
54
|
out_of_quote_single && out_of_comment_single && out_of_comment_multi
|
56
55
|
## begin double quote
|
57
56
|
|
58
57
|
length = $1.size
|
59
58
|
shift_token(length, :plain, :quote_double, :start)
|
60
59
|
out_of_quote_double = false
|
61
|
-
|
62
|
-
elsif /\A(")/ =~ str && !(out_of_quote_double) &&
|
60
|
+
|
61
|
+
elsif /\A(")/ =~ @str && !(out_of_quote_double) &&
|
63
62
|
out_of_quote_single && out_of_comment_single && out_of_comment_multi
|
64
63
|
## end double quote
|
65
|
-
|
64
|
+
|
66
65
|
length = $1.size
|
67
|
-
if /\A("")/ =~ str ##
|
66
|
+
if /\A(".")/ =~ @str ## schema.table
|
67
|
+
shift_to_buf(3)
|
68
|
+
elsif /\A("")/ =~ @str ## escaped double quote
|
68
69
|
shift_to_buf(2)
|
69
70
|
else
|
70
71
|
shift_token(length, :quote_double, :plain, :end)
|
71
72
|
out_of_quote_double = true
|
72
73
|
end
|
73
74
|
|
74
|
-
elsif /\A(')/ =~ str && out_of_quote_single &&
|
75
|
+
elsif /\A(')/ =~ @str && out_of_quote_single &&
|
75
76
|
out_of_quote_double && out_of_comment_single && out_of_comment_multi
|
76
77
|
## begin single quote
|
77
|
-
|
78
|
+
|
78
79
|
length = $1.size
|
79
80
|
shift_token(length, :plain, :quote_single, :start)
|
80
81
|
out_of_quote_single = false
|
81
|
-
elsif /\A(')/ =~ str && !(out_of_quote_single) &&
|
82
|
+
elsif /\A(')/ =~ @str && !(out_of_quote_single) &&
|
82
83
|
out_of_quote_double && out_of_comment_single && out_of_comment_multi
|
83
84
|
## end single quote
|
84
85
|
|
@@ -86,49 +87,49 @@ These are exclusive:
|
|
86
87
|
if /\A('')/ =~ @str ## escaped single quote
|
87
88
|
shift_to_buf(2)
|
88
89
|
else
|
89
|
-
|
90
|
+
shift_token(length, :quote_single, :plain, :end)
|
90
91
|
out_of_quote_single = true
|
91
92
|
end
|
92
|
-
|
93
|
-
elsif /\A(#{@comment_single_start})/ =~ str && out_of_comment_single &&
|
93
|
+
|
94
|
+
elsif /\A(#{@comment_single_start})/ =~ @str && out_of_comment_single &&
|
94
95
|
out_of_quote_single && out_of_quote_double && out_of_comment_multi
|
95
96
|
## begin single line comment
|
96
|
-
|
97
|
+
|
97
98
|
length = $1.size
|
98
99
|
shift_token(length, :plain, :comment_single, :start)
|
99
100
|
out_of_comment_single = false
|
100
101
|
|
101
|
-
elsif /\A(\n)/ =~ str && !(out_of_comment_single) &&
|
102
|
+
elsif /\A(\n)/ =~ @str && !(out_of_comment_single) &&
|
102
103
|
out_of_quote_single && out_of_quote_double && out_of_comment_multi
|
103
104
|
## end single line comment
|
104
|
-
|
105
|
+
|
105
106
|
length = $1.size
|
106
107
|
shift_token(length, :comment_single, :plain, :end)
|
107
108
|
out_of_comment_single = true
|
108
109
|
|
109
|
-
elsif /\A(#{@comment_multi_start})/ =~ str &&
|
110
|
+
elsif /\A(#{@comment_multi_start})/ =~ @str &&
|
110
111
|
out_of_quote_single && out_of_quote_double && out_of_comment_single && out_of_comment_multi
|
111
112
|
## begin multi line comment
|
112
|
-
|
113
|
+
|
113
114
|
length = $1.size
|
114
115
|
shift_token(length, :plain, :comment_multi, :start)
|
115
116
|
out_of_comment_multi = false
|
116
117
|
|
117
|
-
elsif /\A(#{@comment_multi_end})/ =~ str &&
|
118
|
+
elsif /\A(#{@comment_multi_end})/ =~ @str &&
|
118
119
|
out_of_quote_single && out_of_quote_double && out_of_comment_single && !(out_of_comment_multi)
|
119
120
|
## end multi line comment
|
120
|
-
|
121
|
+
|
121
122
|
length = $1.size
|
122
123
|
shift_token(length, :comment_multi, :plain, :end)
|
123
124
|
out_of_comment_multi = true
|
124
125
|
|
125
|
-
elsif /\A\\/ =~ str
|
126
|
+
elsif /\A\\/ =~ @str
|
126
127
|
## escape char
|
127
128
|
shift_to_buf(2)
|
128
129
|
|
129
130
|
else
|
130
131
|
shift_to_buf(1)
|
131
|
-
|
132
|
+
|
132
133
|
end
|
133
134
|
end
|
134
135
|
@result << CoarseToken.new(@mode, @buf+@str) if (@buf+@str).size > 0
|
@@ -138,11 +139,11 @@ These are exclusive:
|
|
138
139
|
|
139
140
|
|
140
141
|
def shift_to_buf(n)
|
141
|
-
@buf
|
142
|
-
@str[
|
142
|
+
@buf += @str[0...n]
|
143
|
+
@str = @str[n..]
|
143
144
|
end
|
144
|
-
|
145
|
-
|
145
|
+
|
146
|
+
|
146
147
|
def shift_token(length, type, mode, flag)
|
147
148
|
case flag
|
148
149
|
when :start
|
@@ -155,7 +156,7 @@ These are exclusive:
|
|
155
156
|
raise "must not happen"
|
156
157
|
end
|
157
158
|
|
158
|
-
@str[
|
159
|
+
@str = @str[length..]
|
159
160
|
@mode = mode
|
160
161
|
end
|
161
162
|
end
|
@@ -6,7 +6,7 @@ AnbtSqlFormatter: SQL整形ツール. SQL文を決められたルールに従い
|
|
6
6
|
フォーマットを実施するためには、入力されるSQLがSQL文として妥当であることが前提条件となります。
|
7
7
|
|
8
8
|
このクラスが準拠するSQL整形のルールについては、下記URLを参照ください。
|
9
|
-
|
9
|
+
https://www.igapyon.jp/igapyon/diary/2005/ig050613.html
|
10
10
|
|
11
11
|
このクラスには ANSI SQLの予約語一覧が蓄えられます。
|
12
12
|
|
@@ -8,7 +8,7 @@ AnbtSqlFormatter: SQL整形ツール. SQL文を決められたルールに従い
|
|
8
8
|
フォーマットを実施するためには、入力されるSQLがSQL文として妥当であることが前提条件となります。
|
9
9
|
|
10
10
|
このクラスが準拠するSQL整形のルールについては、下記URLを参照ください。
|
11
|
-
|
11
|
+
https://www.igapyon.jp/igapyon/diary/2005/ig050613.html
|
12
12
|
|
13
13
|
このクラスは SQLの変換規則を表します。
|
14
14
|
|
@@ -8,7 +8,7 @@ require "anbt-sql-formatter/formatter"
|
|
8
8
|
=begin
|
9
9
|
rule のプロパティを修正するだけではなく
|
10
10
|
さらに踏み込んでカスタマイズしたい場合は
|
11
|
-
formatter.rb 内の AnbtSql::Formatter#format_list_main_loop
|
11
|
+
formatter.rb 内の AnbtSql::Formatter#format_list_main_loop
|
12
12
|
などをオーバーライドすると良いでしょう。
|
13
13
|
|
14
14
|
Ruby に詳しくない場合は「Ruby オープンクラス」
|
@@ -39,7 +39,7 @@ if $0 == __FILE__
|
|
39
39
|
|
40
40
|
# キーワードを大文字・小文字に変換しない
|
41
41
|
rule.keyword = AnbtSql::Rule::KEYWORD_NONE
|
42
|
-
|
42
|
+
|
43
43
|
# 複数単語のキーワードを登録
|
44
44
|
rule.kw_multi_words << "INNER JOIN"
|
45
45
|
rule.kw_nl_x << "INNER JOIN"
|
@@ -50,7 +50,7 @@ if $0 == __FILE__
|
|
50
50
|
rule.kw_nl_x << "WHEN"
|
51
51
|
|
52
52
|
# User defined additional functions:
|
53
|
-
# ユーザ定義関数の追加:
|
53
|
+
# ユーザ定義関数の追加:
|
54
54
|
%w(count sum substr date).each{|func_name|
|
55
55
|
rule.function_names << func_name.upcase
|
56
56
|
}
|
@@ -58,7 +58,7 @@ if $0 == __FILE__
|
|
58
58
|
#rule.indent_string = " "
|
59
59
|
#rule.indent_string = "('-')"
|
60
60
|
rule.indent_string = "| "
|
61
|
-
|
61
|
+
|
62
62
|
formatter = AnbtSql::Formatter.new(rule)
|
63
63
|
result = formatter.format(src)
|
64
64
|
print result
|
data/sample.sql
CHANGED
@@ -21,7 +21,7 @@ class TestCoarseTokenizer < Test::Unit::TestCase
|
|
21
21
|
}.join("\n")
|
22
22
|
end
|
23
23
|
|
24
|
-
|
24
|
+
|
25
25
|
def test_shift_to_buf
|
26
26
|
@tok.buf = ""
|
27
27
|
@tok.str = "abcdefg"
|
@@ -52,14 +52,14 @@ class TestCoarseTokenizer < Test::Unit::TestCase
|
|
52
52
|
@tok.result = []
|
53
53
|
@tok.buf = "'ABC"
|
54
54
|
@tok.str = "'def"
|
55
|
-
|
55
|
+
|
56
56
|
@tok.shift_token(1, :comment, :plain, :end)
|
57
57
|
assert_equals( msg, :comment, @tok.result.last._type)
|
58
58
|
assert_equals( msg, "'ABC'", @tok.result.last.string)
|
59
59
|
assert_equals( msg, "", @tok.buf)
|
60
60
|
assert_equals( msg, "def", @tok.str)
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
|
64
64
|
def test_tokenize_1
|
65
65
|
assert_equals(
|
@@ -327,7 +327,7 @@ class TestCoarseTokenizer < Test::Unit::TestCase
|
|
327
327
|
)))
|
328
328
|
)
|
329
329
|
end
|
330
|
-
|
330
|
+
|
331
331
|
|
332
332
|
def test_comment_in_string_1
|
333
333
|
assert_equals(
|
@@ -392,7 +392,7 @@ class TestCoarseTokenizer < Test::Unit::TestCase
|
|
392
392
|
)))
|
393
393
|
)
|
394
394
|
end
|
395
|
-
|
395
|
+
|
396
396
|
|
397
397
|
def test_string_escape_1
|
398
398
|
assert_equals(
|