anbt-sql-formatter 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/anbt-sql-formatter.gemspec +1 -1
- data/bin/anbt-sql-formatter +1 -1
- data/lib/anbt-sql-formatter/formatter.rb +14 -14
- data/lib/anbt-sql-formatter/version.rb +1 -1
- data/test/test_formatter.rb +8 -8
- metadata +3 -3
data/anbt-sql-formatter.gemspec
CHANGED
@@ -5,7 +5,7 @@ require "anbt-sql-formatter/version"
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "anbt-sql-formatter"
|
7
7
|
s.version = Anbt::Sql::Formatter::VERSION
|
8
|
-
s.authors = ["
|
8
|
+
s.authors = ["sonota88"]
|
9
9
|
s.email = ["yosiot8753@gmail.com"]
|
10
10
|
s.homepage = ""
|
11
11
|
s.summary = %q{A tool for SQL formatting written in Ruby.}
|
data/bin/anbt-sql-formatter
CHANGED
@@ -22,7 +22,7 @@ class AnbtSql
|
|
22
22
|
end
|
23
23
|
|
24
24
|
|
25
|
-
def
|
25
|
+
def split_to_statements(tokens)
|
26
26
|
statements = []
|
27
27
|
buf = []
|
28
28
|
tokens.each{|token|
|
@@ -48,14 +48,14 @@ class AnbtSql
|
|
48
48
|
def format(sql_str)
|
49
49
|
@function_bracket.clear()
|
50
50
|
begin
|
51
|
-
|
51
|
+
is_sql_ends_with_new_line = false
|
52
52
|
if sql_str.end_with?("\n")
|
53
|
-
|
53
|
+
is_sql_ends_with_new_line = true
|
54
54
|
end
|
55
55
|
|
56
56
|
tokens = @parser.parse(sql_str)
|
57
57
|
|
58
|
-
statements =
|
58
|
+
statements = split_to_statements(tokens)
|
59
59
|
|
60
60
|
statements = statements.map{|tokens|
|
61
61
|
format_list(tokens)
|
@@ -66,7 +66,7 @@ class AnbtSql
|
|
66
66
|
tokens.map{ |t| t.string }.join("")
|
67
67
|
}.join("\n;\n\n").sub( /\n\n\Z/, "" )
|
68
68
|
|
69
|
-
after += "\n" if
|
69
|
+
after += "\n" if is_sql_ends_with_new_line
|
70
70
|
|
71
71
|
return after
|
72
72
|
rescue => e
|
@@ -112,19 +112,19 @@ class AnbtSql
|
|
112
112
|
|
113
113
|
|
114
114
|
def remove_symbol_side_space(tokens)
|
115
|
-
|
115
|
+
prev_token = nil
|
116
116
|
|
117
117
|
(tokens.size - 1).downto(1){|index|
|
118
118
|
token = ArrayUtil.get(tokens, index)
|
119
|
-
|
119
|
+
prev_token = ArrayUtil.get(tokens, index - 1)
|
120
120
|
|
121
121
|
if (token._type == AnbtSql::TokenConstants::SPACE &&
|
122
|
-
(
|
123
|
-
|
122
|
+
(prev_token._type == AnbtSql::TokenConstants::SYMBOL ||
|
123
|
+
prev_token._type == AnbtSql::TokenConstants::COMMENT))
|
124
124
|
ArrayUtil.remove(tokens, index)
|
125
125
|
elsif ((token._type == AnbtSql::TokenConstants::SYMBOL ||
|
126
126
|
token._type == AnbtSql::TokenConstants::COMMENT) &&
|
127
|
-
|
127
|
+
prev_token._type == AnbtSql::TokenConstants::SPACE)
|
128
128
|
ArrayUtil.remove(tokens, index - 1)
|
129
129
|
elsif (token._type == AnbtSql::TokenConstants::SPACE)
|
130
130
|
token.string = " "
|
@@ -259,15 +259,15 @@ class AnbtSql
|
|
259
259
|
end
|
260
260
|
|
261
261
|
if equals_ignore_case(token.string, "BETWEEN")
|
262
|
-
|
262
|
+
encounter_between = true
|
263
263
|
end
|
264
264
|
|
265
265
|
if equals_ignore_case(token.string, "AND")
|
266
266
|
# BETWEEN のあとのANDは改行しない。
|
267
|
-
if not
|
267
|
+
if not encounter_between
|
268
268
|
index += insert_return_and_indent(tokens, index, indent)
|
269
269
|
end
|
270
|
-
|
270
|
+
encounter_between = false
|
271
271
|
end
|
272
272
|
|
273
273
|
elsif (token._type == AnbtSql::TokenConstants::COMMENT) # ****
|
@@ -337,7 +337,7 @@ class AnbtSql
|
|
337
337
|
remove_symbol_side_space(tokens)
|
338
338
|
concat_operator_for_oracle(tokens)
|
339
339
|
|
340
|
-
|
340
|
+
encounter_between = false
|
341
341
|
|
342
342
|
format_list_main_loop(tokens)
|
343
343
|
|
data/test/test_formatter.rb
CHANGED
@@ -500,8 +500,8 @@ class TestAnbtSqlFormatter < Test::Unit::TestCase
|
|
500
500
|
end
|
501
501
|
|
502
502
|
|
503
|
-
def
|
504
|
-
msg = "
|
503
|
+
def test_split_to_statements
|
504
|
+
msg = "split_to_statements - "
|
505
505
|
|
506
506
|
########
|
507
507
|
tokens = @parser.parse("a;b")
|
@@ -509,17 +509,17 @@ class TestAnbtSqlFormatter < Test::Unit::TestCase
|
|
509
509
|
assert_equals(
|
510
510
|
msg + "first statement",
|
511
511
|
"a",
|
512
|
-
@fmt.
|
512
|
+
@fmt.split_to_statements(tokens)[0][0].string
|
513
513
|
)
|
514
514
|
assert_equals(
|
515
515
|
msg + "second statement",
|
516
516
|
"b",
|
517
|
-
@fmt.
|
517
|
+
@fmt.split_to_statements(tokens)[1][0].string
|
518
518
|
)
|
519
519
|
|
520
520
|
########
|
521
521
|
tokens = @parser.parse(";")
|
522
|
-
statements = @fmt.
|
522
|
+
statements = @fmt.split_to_statements(tokens)
|
523
523
|
assert_equals(
|
524
524
|
msg,
|
525
525
|
[],
|
@@ -533,7 +533,7 @@ class TestAnbtSqlFormatter < Test::Unit::TestCase
|
|
533
533
|
|
534
534
|
########
|
535
535
|
tokens = @parser.parse("a;")
|
536
|
-
statements = @fmt.
|
536
|
+
statements = @fmt.split_to_statements(tokens)
|
537
537
|
assert_equals(
|
538
538
|
msg,
|
539
539
|
"name (a)",
|
@@ -547,7 +547,7 @@ class TestAnbtSqlFormatter < Test::Unit::TestCase
|
|
547
547
|
|
548
548
|
########
|
549
549
|
tokens = @parser.parse(";a")
|
550
|
-
statements = @fmt.
|
550
|
+
statements = @fmt.split_to_statements(tokens)
|
551
551
|
assert_equals(
|
552
552
|
msg,
|
553
553
|
[],
|
@@ -561,7 +561,7 @@ class TestAnbtSqlFormatter < Test::Unit::TestCase
|
|
561
561
|
|
562
562
|
########
|
563
563
|
tokens = @parser.parse("a;b")
|
564
|
-
statements = @fmt.
|
564
|
+
statements = @fmt.split_to_statements(tokens)
|
565
565
|
assert_equals(
|
566
566
|
msg,
|
567
567
|
"name (a)",
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: anbt-sql-formatter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
-
|
8
|
+
- sonota88
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-02-14 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A tool for SQL formatting written in Ruby. Ruby port of Blanco SQL Formatter.
|
15
15
|
email:
|