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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4680b59adee7c6d2e6bcb649d73b095d3bbcd3d102f8517d50acefc89dfe089
4
- data.tar.gz: dac35873e1d94941ec19156d2738fe75c1b0e156e2357cc1a10e263157c253c6
3
+ metadata.gz: 0bd065b8b98fe3a87257b39677bd7ed0fc78233173c0a1f1e99792cec2e9868f
4
+ data.tar.gz: 999857541e0547429abb974f830fbba8ff0c31ae2284b89bfd840353e3029786
5
5
  SHA512:
6
- metadata.gz: 52692fbb602a719f52a13077bd72b7a4c8d065ba484c6fbd141f928ad3987364dd3517c6fd7d66f9283acee8ecf8fd7033d2df467ffe6a5e0aa4f9e401b7cc6d
7
- data.tar.gz: dcc161a7b754fd3937dfa8c799950aba2ea88a1db9990f38fb4e51ff6cb69e6c07ce619bd4843dba358e93f31d3787ba231ca076fbbdcd0216b224628f802b18
6
+ metadata.gz: fc5d07dc5eb05cc96c250e865bf36f2ae7b8e11457b3850d112b75925a00ccb93f498b1fc389288de875d77292b7139464227f35fa7db56fb602498ffe07e623
7
+ data.tar.gz: 276dc243eeaeab0ee353017e1f7d1bbcf6ec3e11e8b295f5a93331d4b5786f68a6725abe1ca89c81f72a53da821f318aeebde04ed2aa9eb205cfb497f256debe
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,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
- ## Feattues
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.
@@ -1,6 +1,6 @@
1
1
  = anbt-sql-formatter
2
2
 
3
- (C) 2010 sonota <yosiot8753@gmail.com>
3
+ (C) 2010-2018 sonota88 <yosiot8753@gmail.com>
4
4
 
5
5
 
6
6
  == 概要
@@ -95,7 +95,7 @@ sonota:: Ruby へ移植
95
95
 
96
96
  == テスト
97
97
 
98
- $ ruby setup.rb test
98
+ $ ./test.sh
99
99
 
100
100
 
101
101
  == その他
@@ -1,6 +1,6 @@
1
1
  = anbt-sql-formatter
2
2
 
3
- (C) 2010 sonota (yosiot8753@gmail.com)
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
- $ ruby setup.rb test
58
+ $ ./test.sh
@@ -21,4 +21,6 @@ Gem::Specification.new do |s|
21
21
  # specify any dependencies here; for example:
22
22
  # s.add_development_dependency "rspec"
23
23
  # s.add_runtime_dependency "rest-client"
24
+
25
+ s.add_development_dependency "test-unit", "~> 3.6.2"
24
26
  end
@@ -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 ## escaped double quote
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
- shift_token(length, :quote_single, :plain, :end)
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 << @str[0...n]
142
- @str[0...n] = ""
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[0..(length-1)] = ""
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
- http://homepage2.nifty.com/igat/igapyon/diary/2005/ig050613.html
9
+ https://www.igapyon.jp/igapyon/diary/2005/ig050613.html
10
10
 
11
11
  このクラスには ANSI SQLの予約語一覧が蓄えられます。
12
12
 
@@ -98,7 +98,7 @@ class AnbtSql
98
98
  is_next_char_space = false
99
99
  if @pos + 1 < @before.size &&
100
100
  space?(char_at(@before, @pos+1))
101
- is_next_char_space = true
101
+ is_next_char_space = true
102
102
  end
103
103
 
104
104
  if not is_next_char_space
@@ -8,7 +8,7 @@ AnbtSqlFormatter: SQL整形ツール. SQL文を決められたルールに従い
8
8
  フォーマットを実施するためには、入力されるSQLがSQL文として妥当であることが前提条件となります。
9
9
 
10
10
  このクラスが準拠するSQL整形のルールについては、下記URLを参照ください。
11
- http://homepage2.nifty.com/igat/igapyon/diary/2005/ig050613.html
11
+ https://www.igapyon.jp/igapyon/diary/2005/ig050613.html
12
12
 
13
13
  このクラスは SQLの変換規則を表します。
14
14
 
@@ -48,9 +48,9 @@ class AnbtSql
48
48
 
49
49
  #
50
50
  # このバリューオブジェクトの文字列表現を取得する。
51
- #
51
+ #
52
52
  # オブジェクトのシャロー範囲でしか to_s されない点に注意。
53
- #
53
+ #
54
54
  # @return:: バリューオブジェクトの文字列表現。
55
55
  #
56
56
  def to_s
@@ -1,7 +1,7 @@
1
1
  module Anbt
2
2
  module Sql
3
3
  module Formatter
4
- VERSION = "0.0.7"
4
+ VERSION = "0.1.1"
5
5
  end
6
6
  end
7
7
  end
@@ -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
@@ -118,3 +118,7 @@ foobar(a)
118
118
  foobar(a,b)
119
119
  ;
120
120
 
121
+ --------------------------------
122
+ -- {schema}.{table}, {table}.{column}
123
+ select t1.a, `t2`.`b`, "t3"."c"
124
+ from schema.t1, `schema`.`t2`, "schema"."t3"
@@ -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(