anbt-sql-formatter 0.1.0 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06d3d110d976d89ae531994e7a2e504028deb41c7e0576ad977445dd12a40e97
4
- data.tar.gz: 07754a3a2b3169853345b4ead8f2290f0b4279bd913359b637c2e2d87994cb80
3
+ metadata.gz: 84e2831d39ea0a114b7dd67a1ad69be88df0a575f017c6e9ec16ed87cdc8597b
4
+ data.tar.gz: b7f86dd15c4c5b421035f39b4f7bff403305e1f2f0ebd84eed5644b5be57e7a2
5
5
  SHA512:
6
- metadata.gz: 760fa7076565da7636d4dd0659a7ac7f6651e61e50860b7b61cb1fdeac45a0d7836a0c3a3b832bc32b4eb8bcd939eff2e37135b0508822f028f72c4a5753ecd6
7
- data.tar.gz: 3deda4f2457c216382e8c64d9343af513e60d762af57ab39f047a3b282756833ea7542b45d309375fa34f17ef4a5421ac57aee08c1c7439c94d6dcefed7c03cc
6
+ metadata.gz: 9b928f9e74ae4139c3f1408c6727832e352b2f78ad5479f38eb5959817c405f936faab338620536ac7c2a94dc931e839e3e58e2b460922e64fbb8da805d74aff
7
+ data.tar.gz: b978f80c52d14d067c150e48992dc81eaafc948d1103ff12fade72bb3d1f6835af86532393e658b5856dd75ee3d3dc03cf9498a01b66379011a09ffb9adc87cc
data/.gitignore CHANGED
@@ -5,3 +5,6 @@ pkg/*
5
5
 
6
6
  # Emacs
7
7
  *~
8
+
9
+ # Bundler
10
+ /vendor/bundle/
data/CHANGELOG.md CHANGED
@@ -1,3 +1,39 @@
1
+ # 0.1.2 (2025-01-xx)
2
+
3
+ ## Breaking changes
4
+
5
+ - Changed to exclusive branching to reduce complexity (`Formatter#format_list_main_loop`).
6
+ - If you have not customized the rules, there will be no change in behavior.
7
+ - This likely applies to most users.
8
+ - If you have customized the rules such that keywords overlap between conditions of the branches,
9
+ this update may change the behavior.
10
+
11
+ ```ruby
12
+ # Example of customization that may change behavior:
13
+
14
+ custom_rule = AnbtSql::Rule.new
15
+ custom_rule.kw_nl_x << "SOME_CUSTOM_KEYWORD"
16
+ custom_rule.kw_nl_x_plus1_indent << "SOME_CUSTOM_KEYWORD"
17
+ ```
18
+
19
+ ## Improvements
20
+
21
+ - Some cleanups, formatting improvements
22
+ - cleanup: Remove encoding magic comments
23
+
24
+
25
+ # 0.1.1 (2025-01-12)
26
+
27
+ No breaking changes.
28
+
29
+ ## Improvements
30
+
31
+ - Avoid mutative string manipulation (coarse-tokenizer.rb: tokenize)
32
+ - Prevents frozen literal warnings in Ruby 3.4 (Issue #17)
33
+ - Update comments (PR #14)
34
+ - Some small fixes, cleanups, formatting improvements
35
+
36
+
1
37
  # 0.1.0 (2018-12-16)
2
38
 
3
39
  ## Breaking changes
@@ -1,4 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
1
  $:.push File.expand_path("../lib", __FILE__)
3
2
  require "anbt-sql-formatter/version"
4
3
 
@@ -21,4 +20,6 @@ Gem::Specification.new do |s|
21
20
  # specify any dependencies here; for example:
22
21
  # s.add_development_dependency "rspec"
23
22
  # s.add_runtime_dependency "rest-client"
23
+
24
+ s.add_development_dependency "test-unit", "~> 3.6.2"
24
25
  end
@@ -1,5 +1,4 @@
1
1
  #! /usr/bin/env ruby
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  begin
5
4
  require "anbt-sql-formatter/formatter"
@@ -1,5 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  =begin
4
2
  エスケープ文字
5
3
  =end
@@ -38,8 +36,7 @@ These are exclusive:
38
36
  =end
39
37
 
40
38
  def tokenize(str)
41
- @str = str
42
- @str.gsub!("\r\n", "\n")
39
+ @str = str.gsub("\r\n", "\n")
43
40
  out_of_quote_single = true
44
41
  out_of_quote_double = true
45
42
  out_of_comment_single = true
@@ -51,36 +48,36 @@ These are exclusive:
51
48
 
52
49
  while @str.size > 0
53
50
 
54
- if /\A(")/ =~ str && out_of_quote_double &&
51
+ if /\A(")/ =~ @str && out_of_quote_double &&
55
52
  out_of_quote_single && out_of_comment_single && out_of_comment_multi
56
53
  ## begin double quote
57
54
 
58
55
  length = $1.size
59
56
  shift_token(length, :plain, :quote_double, :start)
60
57
  out_of_quote_double = false
61
-
62
- elsif /\A(")/ =~ str && !(out_of_quote_double) &&
58
+
59
+ elsif /\A(")/ =~ @str && !(out_of_quote_double) &&
63
60
  out_of_quote_single && out_of_comment_single && out_of_comment_multi
64
61
  ## end double quote
65
-
62
+
66
63
  length = $1.size
67
- if /\A(".")/ =~ str ## schema.table
64
+ if /\A(".")/ =~ @str ## schema.table
68
65
  shift_to_buf(3)
69
- elsif /\A("")/ =~ str ## escaped double quote
66
+ elsif /\A("")/ =~ @str ## escaped double quote
70
67
  shift_to_buf(2)
71
68
  else
72
69
  shift_token(length, :quote_double, :plain, :end)
73
70
  out_of_quote_double = true
74
71
  end
75
72
 
76
- elsif /\A(')/ =~ str && out_of_quote_single &&
73
+ elsif /\A(')/ =~ @str && out_of_quote_single &&
77
74
  out_of_quote_double && out_of_comment_single && out_of_comment_multi
78
75
  ## begin single quote
79
-
76
+
80
77
  length = $1.size
81
78
  shift_token(length, :plain, :quote_single, :start)
82
79
  out_of_quote_single = false
83
- elsif /\A(')/ =~ str && !(out_of_quote_single) &&
80
+ elsif /\A(')/ =~ @str && !(out_of_quote_single) &&
84
81
  out_of_quote_double && out_of_comment_single && out_of_comment_multi
85
82
  ## end single quote
86
83
 
@@ -88,49 +85,49 @@ These are exclusive:
88
85
  if /\A('')/ =~ @str ## escaped single quote
89
86
  shift_to_buf(2)
90
87
  else
91
- shift_token(length, :quote_single, :plain, :end)
88
+ shift_token(length, :quote_single, :plain, :end)
92
89
  out_of_quote_single = true
93
90
  end
94
-
95
- elsif /\A(#{@comment_single_start})/ =~ str && out_of_comment_single &&
91
+
92
+ elsif /\A(#{@comment_single_start})/ =~ @str && out_of_comment_single &&
96
93
  out_of_quote_single && out_of_quote_double && out_of_comment_multi
97
94
  ## begin single line comment
98
-
95
+
99
96
  length = $1.size
100
97
  shift_token(length, :plain, :comment_single, :start)
101
98
  out_of_comment_single = false
102
99
 
103
- elsif /\A(\n)/ =~ str && !(out_of_comment_single) &&
100
+ elsif /\A(\n)/ =~ @str && !(out_of_comment_single) &&
104
101
  out_of_quote_single && out_of_quote_double && out_of_comment_multi
105
102
  ## end single line comment
106
-
103
+
107
104
  length = $1.size
108
105
  shift_token(length, :comment_single, :plain, :end)
109
106
  out_of_comment_single = true
110
107
 
111
- elsif /\A(#{@comment_multi_start})/ =~ str &&
108
+ elsif /\A(#{@comment_multi_start})/ =~ @str &&
112
109
  out_of_quote_single && out_of_quote_double && out_of_comment_single && out_of_comment_multi
113
110
  ## begin multi line comment
114
-
111
+
115
112
  length = $1.size
116
113
  shift_token(length, :plain, :comment_multi, :start)
117
114
  out_of_comment_multi = false
118
115
 
119
- elsif /\A(#{@comment_multi_end})/ =~ str &&
116
+ elsif /\A(#{@comment_multi_end})/ =~ @str &&
120
117
  out_of_quote_single && out_of_quote_double && out_of_comment_single && !(out_of_comment_multi)
121
118
  ## end multi line comment
122
-
119
+
123
120
  length = $1.size
124
121
  shift_token(length, :comment_multi, :plain, :end)
125
122
  out_of_comment_multi = true
126
123
 
127
- elsif /\A\\/ =~ str
124
+ elsif /\A\\/ =~ @str
128
125
  ## escape char
129
126
  shift_to_buf(2)
130
127
 
131
128
  else
132
129
  shift_to_buf(1)
133
-
130
+
134
131
  end
135
132
  end
136
133
  @result << CoarseToken.new(@mode, @buf+@str) if (@buf+@str).size > 0
@@ -140,11 +137,11 @@ These are exclusive:
140
137
 
141
138
 
142
139
  def shift_to_buf(n)
143
- @buf << @str[0...n]
144
- @str[0...n] = ""
140
+ @buf += @str[0...n]
141
+ @str = @str[n..]
145
142
  end
146
-
147
-
143
+
144
+
148
145
  def shift_token(length, type, mode, flag)
149
146
  case flag
150
147
  when :start
@@ -157,7 +154,7 @@ These are exclusive:
157
154
  raise "must not happen"
158
155
  end
159
156
 
160
- @str[0..(length-1)] = ""
157
+ @str = @str[length..]
161
158
  @mode = mode
162
159
  end
163
160
  end
@@ -1,12 +1,10 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  =begin rdoc
4
2
  AnbtSqlFormatter: SQL整形ツール. SQL文を決められたルールに従い整形します。
5
3
 
6
4
  フォーマットを実施するためには、入力されるSQLがSQL文として妥当であることが前提条件となります。
7
5
 
8
6
  このクラスが準拠するSQL整形のルールについては、下記URLを参照ください。
9
- http://homepage2.nifty.com/igat/igapyon/diary/2005/ig050613.html
7
+ https://www.igapyon.jp/igapyon/diary/2005/ig050613.html
10
8
 
11
9
  このクラスには ANSI SQLの予約語一覧が蓄えられます。
12
10
 
@@ -76,6 +74,7 @@ class AnbtSql
76
74
  "ORDINALITY", "OUT", "PARAMETER", "PARAMETERS", "PATH", "POSTFIX",
77
75
  "PREFIX", "PREORDER", "RAW", "READS", "RECURSIVE", "REDO",
78
76
  # ANSI SQLではないのだが とても良く使われる構文
79
- "TRUNCATE" ]
77
+ "TRUNCATE"
78
+ ]
80
79
  end
81
80
  end
@@ -1,5 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  =begin rdoc
4
2
  BlancoSqlFormatterException : SQL整形ツールの例外を表します。
5
3
 
@@ -1,5 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  require "anbt-sql-formatter/rule"
4
2
  require "anbt-sql-formatter/parser"
5
3
  require "anbt-sql-formatter/exception"
@@ -222,56 +220,47 @@ class AnbtSql
222
220
  equals_ignore_case(token.string, "UPDATE") )
223
221
  indent += 2
224
222
  index += insert_return_and_indent(tokens, index + 1, indent, "+2")
225
- end
226
223
 
227
224
  # indentを1つ増やし、キーワードの後ろで改行
228
- if @rule.kw_plus1_indent_x_nl.any?{ |kw| equals_ignore_case(token.string, kw) }
225
+ elsif @rule.kw_plus1_indent_x_nl.any?{ |kw| equals_ignore_case(token.string, kw) }
229
226
  indent += 1
230
227
  index += insert_return_and_indent(tokens, index + 1, indent)
231
- end
232
228
 
233
229
  # キーワードの前でindentを1つ減らして改行、キーワードの後ろでindentを戻して改行。
234
- if @rule.kw_minus1_indent_nl_x_plus1_indent.any?{ |kw| equals_ignore_case(token.string, kw) }
230
+ elsif @rule.kw_minus1_indent_nl_x_plus1_indent.any?{ |kw| equals_ignore_case(token.string, kw) }
235
231
  index += insert_return_and_indent(tokens, index , indent - 1)
236
232
  index += insert_return_and_indent(tokens, index + 1, indent )
237
- end
238
233
 
239
234
  # キーワードの前でindentを1つ減らして改行、キーワードの後ろでindentを戻して改行。
240
- if (equals_ignore_case(token.string, "VALUES"))
235
+ elsif (equals_ignore_case(token.string, "VALUES"))
241
236
  indent -= 1
242
237
  index += insert_return_and_indent(tokens, index, indent)
243
- end
244
238
 
245
239
  # キーワードの前でindentを1つ減らして改行
246
- if (equals_ignore_case(token.string, "END"))
240
+ elsif (equals_ignore_case(token.string, "END"))
247
241
  indent -= 1
248
242
  index += insert_return_and_indent(tokens, index, indent)
249
- end
250
243
 
251
244
  # キーワードの前で改行
252
- if @rule.kw_nl_x.any?{ |kw| equals_ignore_case(token.string, kw) }
245
+ elsif @rule.kw_nl_x.any?{ |kw| equals_ignore_case(token.string, kw) }
253
246
  index += insert_return_and_indent(tokens, index, indent)
254
- end
255
247
 
256
248
  # キーワードの前で改行, インデント+1
257
- if @rule.kw_nl_x_plus1_indent.any?{ |kw| equals_ignore_case(token.string, kw) }
249
+ elsif @rule.kw_nl_x_plus1_indent.any?{ |kw| equals_ignore_case(token.string, kw) }
258
250
  index += insert_return_and_indent(tokens, index, indent + 1)
259
- end
260
251
 
261
252
  # キーワードの前で改行。indentを強制的に0にする。
262
- if (equals_ignore_case(token.string, "UNION" ) ||
253
+ elsif (equals_ignore_case(token.string, "UNION" ) ||
263
254
  equals_ignore_case(token.string, "INTERSECT") ||
264
255
  equals_ignore_case(token.string, "EXCEPT" ) )
265
256
  indent -= 2
266
257
  index += insert_return_and_indent(tokens, index , indent)
267
258
  index += insert_return_and_indent(tokens, index + 1, indent)
268
- end
269
259
 
270
- if equals_ignore_case(token.string, "BETWEEN")
260
+ elsif equals_ignore_case(token.string, "BETWEEN")
271
261
  encounter_between = true
272
- end
273
262
 
274
- if equals_ignore_case(token.string, "AND")
263
+ elsif equals_ignore_case(token.string, "AND")
275
264
  # BETWEEN のあとのANDは改行しない。
276
265
  if not encounter_between
277
266
  index += insert_return_and_indent(tokens, index, indent)
@@ -395,7 +384,7 @@ class AnbtSql
395
384
  if $DEBUG
396
385
  $stderr.puts e.message, e.backtrace
397
386
  $stderr.puts "tokens: "
398
- tokens.each_with_index{|t,i|
387
+ tokens.each_with_index{|t, i|
399
388
  $stderr.puts "index=%d: %s" % [i, t.inspect]
400
389
  }
401
390
  $stderr.puts "index/size: %d/%d / indent: %d / opt: %s" % [index, tokens.size, indent, opt]
@@ -1,5 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  require "pp"
4
2
 
5
3
  require "anbt-sql-formatter/token"
@@ -67,7 +65,7 @@ class AnbtSql
67
65
  # これ以降の文字の扱いは保留
68
66
  def symbol?(c)
69
67
  %w(" ? % & ' \( \) | * + , - . / : ; < = > !).include? c
70
- #"
68
+ # "
71
69
  end
72
70
 
73
71
 
@@ -98,7 +96,7 @@ class AnbtSql
98
96
  is_next_char_space = false
99
97
  if @pos + 1 < @before.size &&
100
98
  space?(char_at(@before, @pos+1))
101
- is_next_char_space = true
99
+ is_next_char_space = true
102
100
  end
103
101
 
104
102
  if not is_next_char_space
@@ -160,7 +158,7 @@ class AnbtSql
160
158
 
161
159
  # 2文字の記号かどうか調べる
162
160
  ch2 = char_at(@before, @pos)
163
- #for (int i = 0; i < two_character_symbol.length; i++) {
161
+ # for (int i = 0; i < two_character_symbol.length; i++) {
164
162
  for i in 0...@two_character_symbol.length
165
163
  if (char_at(@two_character_symbol[i], 0) == @char &&
166
164
  char_at(@two_character_symbol[i], 1) == ch2)
@@ -1,5 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  require "anbt-sql-formatter/helper"
4
2
 
5
3
  =begin
@@ -8,7 +6,7 @@ AnbtSqlFormatter: SQL整形ツール. SQL文を決められたルールに従い
8
6
  フォーマットを実施するためには、入力されるSQLがSQL文として妥当であることが前提条件となります。
9
7
 
10
8
  このクラスが準拠するSQL整形のルールについては、下記URLを参照ください。
11
- http://homepage2.nifty.com/igat/igapyon/diary/2005/ig050613.html
9
+ https://www.igapyon.jp/igapyon/diary/2005/ig050613.html
12
10
 
13
11
  このクラスは SQLの変換規則を表します。
14
12
 
@@ -1,5 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  class AnbtSql
4
2
  class TokenConstants
5
3
 
@@ -48,9 +46,9 @@ class AnbtSql
48
46
 
49
47
  #
50
48
  # このバリューオブジェクトの文字列表現を取得する。
51
- #
49
+ #
52
50
  # オブジェクトのシャロー範囲でしか to_s されない点に注意。
53
- #
51
+ #
54
52
  # @return:: バリューオブジェクトの文字列表現。
55
53
  #
56
54
  def to_s
@@ -1,7 +1,7 @@
1
1
  module Anbt
2
2
  module Sql
3
3
  module Formatter
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
6
6
  end
7
7
  end
@@ -1,5 +1,4 @@
1
1
  #! /usr/bin/ruby
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  require "pp"
5
4
 
@@ -8,7 +7,7 @@ require "anbt-sql-formatter/formatter"
8
7
  =begin
9
8
  rule のプロパティを修正するだけではなく
10
9
  さらに踏み込んでカスタマイズしたい場合は
11
- formatter.rb 内の AnbtSql::Formatter#format_list_main_loop
10
+ formatter.rb 内の AnbtSql::Formatter#format_list_main_loop
12
11
  などをオーバーライドすると良いでしょう。
13
12
 
14
13
  Ruby に詳しくない場合は「Ruby オープンクラス」
@@ -39,7 +38,7 @@ if $0 == __FILE__
39
38
 
40
39
  # キーワードを大文字・小文字に変換しない
41
40
  rule.keyword = AnbtSql::Rule::KEYWORD_NONE
42
-
41
+
43
42
  # 複数単語のキーワードを登録
44
43
  rule.kw_multi_words << "INNER JOIN"
45
44
  rule.kw_nl_x << "INNER JOIN"
@@ -50,15 +49,15 @@ if $0 == __FILE__
50
49
  rule.kw_nl_x << "WHEN"
51
50
 
52
51
  # User defined additional functions:
53
- # ユーザ定義関数の追加:
52
+ # ユーザ定義関数の追加:
54
53
  %w(count sum substr date).each{|func_name|
55
54
  rule.function_names << func_name.upcase
56
55
  }
57
56
 
58
- #rule.indent_string = " "
59
- #rule.indent_string = "('-')"
57
+ # rule.indent_string = " "
58
+ # rule.indent_string = "('-')"
60
59
  rule.indent_string = "| "
61
-
60
+
62
61
  formatter = AnbtSql::Formatter.new(rule)
63
62
  result = formatter.format(src)
64
63
  print result
data/test/helper.rb CHANGED
@@ -12,8 +12,8 @@ class Helper
12
12
  end
13
13
 
14
14
 
15
- def assert_equals(a,b,c)
16
- assert_equal(b,c,a)
15
+ def assert_equals(a, b, c)
16
+ assert_equal(b, c, a)
17
17
  end
18
18
 
19
19
  def strip_indent(text)
@@ -1,5 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  require File.join(File.expand_path(File.dirname(__FILE__)), "helper")
4
2
 
5
3
  require "anbt-sql-formatter/coarse-tokenizer"
@@ -21,7 +19,7 @@ class TestCoarseTokenizer < Test::Unit::TestCase
21
19
  }.join("\n")
22
20
  end
23
21
 
24
-
22
+
25
23
  def test_shift_to_buf
26
24
  @tok.buf = ""
27
25
  @tok.str = "abcdefg"
@@ -52,14 +50,14 @@ class TestCoarseTokenizer < Test::Unit::TestCase
52
50
  @tok.result = []
53
51
  @tok.buf = "'ABC"
54
52
  @tok.str = "'def"
55
-
53
+
56
54
  @tok.shift_token(1, :comment, :plain, :end)
57
55
  assert_equals( msg, :comment, @tok.result.last._type)
58
56
  assert_equals( msg, "'ABC'", @tok.result.last.string)
59
57
  assert_equals( msg, "", @tok.buf)
60
58
  assert_equals( msg, "def", @tok.str)
61
59
  end
62
-
60
+
63
61
 
64
62
  def test_tokenize_1
65
63
  assert_equals(
@@ -327,7 +325,7 @@ class TestCoarseTokenizer < Test::Unit::TestCase
327
325
  )))
328
326
  )
329
327
  end
330
-
328
+
331
329
 
332
330
  def test_comment_in_string_1
333
331
  assert_equals(
@@ -392,7 +390,7 @@ class TestCoarseTokenizer < Test::Unit::TestCase
392
390
  )))
393
391
  )
394
392
  end
395
-
393
+
396
394
 
397
395
  def test_string_escape_1
398
396
  assert_equals(