anbt-sql-formatter 0.1.0 → 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 +12 -0
- data/anbt-sql-formatter.gemspec +2 -0
- data/lib/anbt-sql-formatter/coarse-tokenizer.rb +27 -28
- 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/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 -441
- data/test.sh +1 -1
- metadata +18 -8
    
        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,3 +1,15 @@ | |
| 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 | 
            +
             | 
| 1 13 | 
             
            # 0.1.0 (2018-12-16)
         | 
| 2 14 |  | 
| 3 15 | 
             
            ## Breaking changes
         | 
    
        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,36 +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 ## schema.table
         | 
| 66 | 
            +
                    if /\A(".")/ =~ @str ## schema.table
         | 
| 68 67 | 
             
                      shift_to_buf(3)
         | 
| 69 | 
            -
                    elsif /\A("")/ =~ str ## escaped double quote
         | 
| 68 | 
            +
                    elsif /\A("")/ =~ @str ## escaped double quote
         | 
| 70 69 | 
             
                      shift_to_buf(2)
         | 
| 71 70 | 
             
                    else
         | 
| 72 71 | 
             
                      shift_token(length, :quote_double, :plain, :end)
         | 
| 73 72 | 
             
                      out_of_quote_double = true
         | 
| 74 73 | 
             
                    end
         | 
| 75 74 |  | 
| 76 | 
            -
                  elsif /\A(')/ =~ str && out_of_quote_single &&
         | 
| 75 | 
            +
                  elsif /\A(')/ =~ @str && out_of_quote_single &&
         | 
| 77 76 | 
             
                      out_of_quote_double && out_of_comment_single && out_of_comment_multi
         | 
| 78 77 | 
             
                    ## begin single quote
         | 
| 79 | 
            -
             | 
| 78 | 
            +
             | 
| 80 79 | 
             
                    length = $1.size
         | 
| 81 80 | 
             
                    shift_token(length, :plain, :quote_single, :start)
         | 
| 82 81 | 
             
                    out_of_quote_single = false
         | 
| 83 | 
            -
                  elsif /\A(')/ =~ str && !(out_of_quote_single) &&
         | 
| 82 | 
            +
                  elsif /\A(')/ =~ @str && !(out_of_quote_single) &&
         | 
| 84 83 | 
             
                      out_of_quote_double && out_of_comment_single && out_of_comment_multi
         | 
| 85 84 | 
             
                    ## end single quote
         | 
| 86 85 |  | 
| @@ -88,49 +87,49 @@ These are exclusive: | |
| 88 87 | 
             
                    if /\A('')/ =~ @str ## escaped single quote
         | 
| 89 88 | 
             
                      shift_to_buf(2)
         | 
| 90 89 | 
             
                    else
         | 
| 91 | 
            -
             | 
| 90 | 
            +
                      shift_token(length, :quote_single, :plain, :end)
         | 
| 92 91 | 
             
                      out_of_quote_single = true
         | 
| 93 92 | 
             
                    end
         | 
| 94 | 
            -
             | 
| 95 | 
            -
                  elsif /\A(#{@comment_single_start})/ =~ str && out_of_comment_single &&
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                  elsif /\A(#{@comment_single_start})/ =~ @str && out_of_comment_single &&
         | 
| 96 95 | 
             
                     out_of_quote_single && out_of_quote_double && out_of_comment_multi
         | 
| 97 96 | 
             
                    ## begin single line comment
         | 
| 98 | 
            -
             | 
| 97 | 
            +
             | 
| 99 98 | 
             
                    length = $1.size
         | 
| 100 99 | 
             
                    shift_token(length, :plain, :comment_single, :start)
         | 
| 101 100 | 
             
                    out_of_comment_single = false
         | 
| 102 101 |  | 
| 103 | 
            -
                  elsif /\A(\n)/ =~ str && !(out_of_comment_single) &&
         | 
| 102 | 
            +
                  elsif /\A(\n)/ =~ @str && !(out_of_comment_single) &&
         | 
| 104 103 | 
             
                      out_of_quote_single && out_of_quote_double && out_of_comment_multi
         | 
| 105 104 | 
             
                    ## end single line comment
         | 
| 106 | 
            -
             | 
| 105 | 
            +
             | 
| 107 106 | 
             
                    length = $1.size
         | 
| 108 107 | 
             
                    shift_token(length, :comment_single, :plain, :end)
         | 
| 109 108 | 
             
                    out_of_comment_single = true
         | 
| 110 109 |  | 
| 111 | 
            -
                  elsif /\A(#{@comment_multi_start})/ =~ str &&
         | 
| 110 | 
            +
                  elsif /\A(#{@comment_multi_start})/ =~ @str &&
         | 
| 112 111 | 
             
                      out_of_quote_single && out_of_quote_double && out_of_comment_single && out_of_comment_multi
         | 
| 113 112 | 
             
                    ## begin multi line comment
         | 
| 114 | 
            -
             | 
| 113 | 
            +
             | 
| 115 114 | 
             
                    length = $1.size
         | 
| 116 115 | 
             
                    shift_token(length, :plain, :comment_multi, :start)
         | 
| 117 116 | 
             
                    out_of_comment_multi = false
         | 
| 118 117 |  | 
| 119 | 
            -
                  elsif /\A(#{@comment_multi_end})/ =~ str &&
         | 
| 118 | 
            +
                  elsif /\A(#{@comment_multi_end})/ =~ @str &&
         | 
| 120 119 | 
             
                      out_of_quote_single && out_of_quote_double && out_of_comment_single && !(out_of_comment_multi)
         | 
| 121 120 | 
             
                    ## end multi line comment
         | 
| 122 | 
            -
             | 
| 121 | 
            +
             | 
| 123 122 | 
             
                    length = $1.size
         | 
| 124 123 | 
             
                    shift_token(length, :comment_multi, :plain, :end)
         | 
| 125 124 | 
             
                    out_of_comment_multi = true
         | 
| 126 125 |  | 
| 127 | 
            -
                  elsif /\A\\/ =~ str
         | 
| 126 | 
            +
                  elsif /\A\\/ =~ @str
         | 
| 128 127 | 
             
                    ## escape char
         | 
| 129 128 | 
             
                    shift_to_buf(2)
         | 
| 130 129 |  | 
| 131 130 | 
             
                  else
         | 
| 132 131 | 
             
                    shift_to_buf(1)
         | 
| 133 | 
            -
             | 
| 132 | 
            +
             | 
| 134 133 | 
             
                  end
         | 
| 135 134 | 
             
                end
         | 
| 136 135 | 
             
                @result << CoarseToken.new(@mode, @buf+@str) if (@buf+@str).size > 0
         | 
| @@ -140,11 +139,11 @@ These are exclusive: | |
| 140 139 |  | 
| 141 140 |  | 
| 142 141 | 
             
              def shift_to_buf(n)
         | 
| 143 | 
            -
                @buf  | 
| 144 | 
            -
                @str[ | 
| 142 | 
            +
                @buf += @str[0...n]
         | 
| 143 | 
            +
                @str = @str[n..]
         | 
| 145 144 | 
             
              end
         | 
| 146 | 
            -
             | 
| 147 | 
            -
             | 
| 145 | 
            +
             | 
| 146 | 
            +
             | 
| 148 147 | 
             
              def shift_token(length, type, mode, flag)
         | 
| 149 148 | 
             
                case flag
         | 
| 150 149 | 
             
                when :start
         | 
| @@ -157,7 +156,7 @@ These are exclusive: | |
| 157 156 | 
             
                  raise "must not happen"
         | 
| 158 157 | 
             
                end
         | 
| 159 158 |  | 
| 160 | 
            -
                @str[ | 
| 159 | 
            +
                @str = @str[length..]
         | 
| 161 160 | 
             
                @mode = mode
         | 
| 162 161 | 
             
              end
         | 
| 163 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
         | 
| @@ -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(
         |