tsql_parser 0.1.5 → 0.1.6
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/lib/parsing/config/defaults.rb +56 -0
- data/lib/parsing/formatter.rb +11 -9
- data/lib/parsing/formatters/format_factory.rb +32 -0
- data/lib/parsing/formatters/strategy/__defaults.rb +16 -0
- data/lib/parsing/formatters/strategy/__formatters.rb +31 -0
- data/lib/parsing/formatters/strategy/base_formatter.rb +12 -2
- data/lib/parsing/formatters/strategy/cte_formatter.rb +25 -0
- data/lib/parsing/formatters/strategy/insert_formatter.rb +2 -2
- data/lib/parsing/formatters/strategy/join_formatter.rb +3 -1
- data/lib/parsing/formatters/strategy/select_formatter.rb +2 -2
- data/lib/parsing/formatters/strategy/set_formatter.rb +3 -3
- data/lib/parsing/formatters/strategy/update_formatter.rb +2 -2
- data/lib/parsing/formatters/strategy/where_formatter.rb +2 -2
- data/lib/parsing/formatters/text_formatter.rb +9 -25
- data/lib/parsing/tokenizer.rb +3 -13
- data/lib/parsing/transformers/token_transformer.rb +87 -33
- data/lib/tsql_parser.rb +3 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d67275fb1f5b1ff478b0e8a79d99af69a2d33b53059822e882b6a0c94e68d0a1
|
4
|
+
data.tar.gz: 50b27357c58d8c0ca89b069ddef3a03e4565b1e354a7e322d79c9ba64440895d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd4314846a33bd5b4c0e99dd41ecb67c65494faf015fc5ec81ffcf74cd5f98c52215bc28fd1a8729e0a1694ad7a055b09f51b724b0d7f27ae945a3ed2f778d13
|
7
|
+
data.tar.gz: 664dce679b765bf73e229f5cafc4eb0059f540c85674d4b6d41f1e3508e7536d512c457b64075d6c0f12b70bf7d3bfabdf4887466894beceec4182f444a5b986
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# __ .__
|
2
|
+
# _/ |_ ___________| | ___________ _______ ______ ___________
|
3
|
+
# \ __\/ ___/ ____/ | ______ \____ \__ \\_ __ \/ ___// __ \_ __ \
|
4
|
+
# | | \___ < <_| | |__ /_____/ | |_> > __ \| | \/\___ \\ ___/| | \/
|
5
|
+
# |__| /____ >__ |____/ | __(____ /__| /____ >\___ >__|
|
6
|
+
# \/ |__| |__| \/ \/ \/
|
7
|
+
#
|
8
|
+
# A very light-weight and opinionated T-SQL parser and formatter.
|
9
|
+
#
|
10
|
+
# github.com/scstauf
|
11
|
+
#
|
12
|
+
# path:
|
13
|
+
# parsing/config/defaults.rb
|
14
|
+
# object:
|
15
|
+
# TSqlParser::Parsing::Defaults
|
16
|
+
|
17
|
+
module TSqlParser::Parsing
|
18
|
+
class Defaults
|
19
|
+
@@default_single_char_tokens = ["(", ",", ")", "=", "+", "-", "%", "/", "*", "<", "!", ">", "'", "[", "]", ";"]
|
20
|
+
@@default_delimiters = [" ", "\n", "\t"]
|
21
|
+
@@default_tab_count = 0
|
22
|
+
@@default_tab = " "
|
23
|
+
|
24
|
+
def self.set_default_tab_count(tab_count = 0)
|
25
|
+
@@default_tab_count = tab_count
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.set_default_tab(tab = " ")
|
29
|
+
@@default_tab = tab
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.set_default_single_char_tokens(single_char_tokens = ["(", ",", ")", "=", "+", "-", "%", "/", "*", "<", "!", ">", "'", "[", "]", ";"])
|
33
|
+
@@default_single_char_tokens = single_char_tokens
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.set_default_delimiters(delimiters = [" ", "\n", "\t"])
|
37
|
+
@@default_delimiters = delimiters
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.get_default_single_char_tokens
|
41
|
+
@@default_single_char_tokens
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.get_default_delimiters
|
45
|
+
@@default_delimiters
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.get_default_tab_count
|
49
|
+
@@default_tab_count
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.get_default_tab
|
53
|
+
@@default_tab
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/parsing/formatter.rb
CHANGED
@@ -15,23 +15,25 @@
|
|
15
15
|
# TSqlParser::Parsing::Formatter
|
16
16
|
|
17
17
|
module TSqlParser::Parsing
|
18
|
+
require_relative "config/defaults"
|
18
19
|
require_relative "parser"
|
19
20
|
require_relative "formatters/text_formatter"
|
20
21
|
require_relative "transformers/token_transformer"
|
21
22
|
|
22
23
|
class Formatter
|
23
|
-
def self.format(tokens, tab_count =
|
24
|
+
def self.format(tokens, tab_count = Defaults.get_default_tab_count, tab = Defaults.get_default_tab)
|
24
25
|
lines = TokenTransformer.transform(tokens)
|
25
26
|
lines = self.cleanup_whitespace(lines)
|
26
27
|
lines = self.insert_indentation(lines, tab_count, tab)
|
27
|
-
lines = self.insert_newlines(lines)
|
28
|
+
#lines = self.insert_newlines(lines)
|
28
29
|
text = lines.join("\n")
|
29
|
-
text = TextFormatter.
|
30
|
-
text = TextFormatter.
|
31
|
-
text = TextFormatter.
|
32
|
-
text = TextFormatter.
|
33
|
-
text = TextFormatter.
|
34
|
-
text = TextFormatter.
|
30
|
+
text = TextFormatter.new(JOIN, text, tab).format
|
31
|
+
text = TextFormatter.new(INSERT, text, tab).format
|
32
|
+
text = TextFormatter.new(UPDATE, text, tab).format
|
33
|
+
text = TextFormatter.new(WHERE, text, tab).format
|
34
|
+
text = TextFormatter.new(SELECT, text, tab).format
|
35
|
+
text = TextFormatter.new(SET, text, tab).format
|
36
|
+
puts text
|
35
37
|
text
|
36
38
|
end
|
37
39
|
|
@@ -60,7 +62,7 @@ module TSqlParser::Parsing
|
|
60
62
|
new_lines
|
61
63
|
end
|
62
64
|
|
63
|
-
def self.insert_indentation(lines, tab_count =
|
65
|
+
def self.insert_indentation(lines, tab_count = Defaults.get_default_tab_count, tab = Defaults.get_default_tab)
|
64
66
|
indented_lines = []
|
65
67
|
work_lines = []
|
66
68
|
lines.each do |line|
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# __ .__
|
2
|
+
# _/ |_ ___________| | ___________ _______ ______ ___________
|
3
|
+
# \ __\/ ___/ ____/ | ______ \____ \__ \\_ __ \/ ___// __ \_ __ \
|
4
|
+
# | | \___ < <_| | |__ /_____/ | |_> > __ \| | \/\___ \\ ___/| | \/
|
5
|
+
# |__| /____ >__ |____/ | __(____ /__| /____ >\___ >__|
|
6
|
+
# \/ |__| |__| \/ \/ \/
|
7
|
+
#
|
8
|
+
# A very light-weight and opinionated T-SQL parser and formatter.
|
9
|
+
#
|
10
|
+
# github.com/scstauf
|
11
|
+
#
|
12
|
+
# path:
|
13
|
+
# parsing/formatters/format_factory.rb
|
14
|
+
# object:
|
15
|
+
# TSqlParser::Parsing::FormatFactory
|
16
|
+
|
17
|
+
module TSqlParser::Parsing
|
18
|
+
require_relative "strategy/__formatters"
|
19
|
+
class FormatFactory
|
20
|
+
def self.get(type)
|
21
|
+
case type
|
22
|
+
when CTE then Formatters::CommonTableExpressionFormatter.new
|
23
|
+
when INSERT then Formatters::InsertFormatter.new
|
24
|
+
when JOIN then Formatters::JoinFormatter.new
|
25
|
+
when SELECT then Formatters::SelectFormatter.new
|
26
|
+
when SET then Formatters::SetFormatter.new
|
27
|
+
when UPDATE then Formatters::UpdateFormatter.new
|
28
|
+
when WHERE then Formatters::WhereFormatter.new
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# __ .__
|
2
|
+
# _/ |_ ___________| | ___________ _______ ______ ___________
|
3
|
+
# \ __\/ ___/ ____/ | ______ \____ \__ \\_ __ \/ ___// __ \_ __ \
|
4
|
+
# | | \___ < <_| | |__ /_____/ | |_> > __ \| | \/\___ \\ ___/| | \/
|
5
|
+
# |__| /____ >__ |____/ | __(____ /__| /____ >\___ >__|
|
6
|
+
# \/ |__| |__| \/ \/ \/
|
7
|
+
#
|
8
|
+
# A very light-weight and opinionated T-SQL parser and formatter.
|
9
|
+
#
|
10
|
+
# github.com/scstauf
|
11
|
+
#
|
12
|
+
# path:
|
13
|
+
# parsing/formatters/strategy/__defaults.rb
|
14
|
+
|
15
|
+
require_relative "../../config/defaults"
|
16
|
+
Defaults = TSqlParser::Parsing::Defaults
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# __ .__
|
2
|
+
# _/ |_ ___________| | ___________ _______ ______ ___________
|
3
|
+
# \ __\/ ___/ ____/ | ______ \____ \__ \\_ __ \/ ___// __ \_ __ \
|
4
|
+
# | | \___ < <_| | |__ /_____/ | |_> > __ \| | \/\___ \\ ___/| | \/
|
5
|
+
# |__| /____ >__ |____/ | __(____ /__| /____ >\___ >__|
|
6
|
+
# \/ |__| |__| \/ \/ \/
|
7
|
+
#
|
8
|
+
# A very light-weight and opinionated T-SQL parser and formatter.
|
9
|
+
#
|
10
|
+
# github.com/scstauf
|
11
|
+
#
|
12
|
+
# path:
|
13
|
+
# parsing/formatters/strategy/__formatters.rb
|
14
|
+
|
15
|
+
module TSqlParser::Parsing
|
16
|
+
require_relative "cte_formatter"
|
17
|
+
require_relative "set_formatter"
|
18
|
+
require_relative "join_formatter"
|
19
|
+
require_relative "insert_formatter"
|
20
|
+
require_relative "select_formatter"
|
21
|
+
require_relative "update_formatter"
|
22
|
+
require_relative "where_formatter"
|
23
|
+
|
24
|
+
CTE = 0
|
25
|
+
INSERT = 1
|
26
|
+
JOIN = 2
|
27
|
+
SELECT = 3
|
28
|
+
SET = 4
|
29
|
+
UPDATE = 5
|
30
|
+
WHERE = 6
|
31
|
+
end
|
@@ -15,14 +15,24 @@
|
|
15
15
|
# TSqlParser::Parsing::Formatters::SetFormatter
|
16
16
|
|
17
17
|
module TSqlParser::Parsing::Formatters
|
18
|
+
require_relative "__defaults"
|
19
|
+
|
18
20
|
class BaseFormatter
|
19
|
-
def
|
20
|
-
tab_count =
|
21
|
+
def get_tab_count(line, tab = Defaults.get_default_tab)
|
22
|
+
tab_count = Defaults.get_default_tab_count
|
21
23
|
while line.start_with? tab
|
22
24
|
tab_count += 1
|
23
25
|
line = line.sub(tab, "")
|
24
26
|
end
|
25
27
|
tab_count
|
26
28
|
end
|
29
|
+
|
30
|
+
# @abstract
|
31
|
+
#
|
32
|
+
# @param [String] text
|
33
|
+
# @param [String] tab
|
34
|
+
def format(text, tab = Defaults.get_default_tab)
|
35
|
+
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
|
36
|
+
end
|
27
37
|
end
|
28
38
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# __ .__
|
2
|
+
# _/ |_ ___________| | ___________ _______ ______ ___________
|
3
|
+
# \ __\/ ___/ ____/ | ______ \____ \__ \\_ __ \/ ___// __ \_ __ \
|
4
|
+
# | | \___ < <_| | |__ /_____/ | |_> > __ \| | \/\___ \\ ___/| | \/
|
5
|
+
# |__| /____ >__ |____/ | __(____ /__| /____ >\___ >__|
|
6
|
+
# \/ |__| |__| \/ \/ \/
|
7
|
+
#
|
8
|
+
# A very light-weight and opinionated T-SQL parser and formatter.
|
9
|
+
#
|
10
|
+
# github.com/scstauf
|
11
|
+
#
|
12
|
+
# path:
|
13
|
+
# parsing/formatters/cte_formatter.rb
|
14
|
+
# object:
|
15
|
+
# TSqlParser::Parsing::Formatters::CommonTableExpressionFormatter
|
16
|
+
|
17
|
+
module TSqlParser::Parsing::Formatters
|
18
|
+
require_relative "base_formatter"
|
19
|
+
|
20
|
+
class CommonTableExpressionFormatter < BaseFormatter
|
21
|
+
def format(text, tab = Defaults.get_default_tab)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -18,7 +18,7 @@ module TSqlParser::Parsing::Formatters
|
|
18
18
|
require_relative "base_formatter"
|
19
19
|
|
20
20
|
class InsertFormatter < BaseFormatter
|
21
|
-
def
|
21
|
+
def format(text, tab = Defaults.get_default_tab)
|
22
22
|
formatted = []
|
23
23
|
lines = text.split("\n")
|
24
24
|
search = "INSERT INTO"
|
@@ -43,7 +43,7 @@ module TSqlParser::Parsing::Formatters
|
|
43
43
|
|
44
44
|
private
|
45
45
|
|
46
|
-
def
|
46
|
+
def format_insert(s, tab_count = Defaults.get_default_tab_count, tab = Defaults.get_default_tab)
|
47
47
|
return s if s.nil?
|
48
48
|
formatted = []
|
49
49
|
if s.include? ") VALUES ("
|
@@ -18,9 +18,11 @@ module TSqlParser::Parsing::Formatters
|
|
18
18
|
require_relative "base_formatter"
|
19
19
|
|
20
20
|
class JoinFormatter < BaseFormatter
|
21
|
-
def
|
21
|
+
def format(text, tab = Defaults.get_default_tab)
|
22
22
|
text = text.gsub(/INNER\s+JOIN/, "INNER JOIN")
|
23
23
|
.gsub(/LEFT\s+JOIN/, "LEFT JOIN")
|
24
|
+
.gsub(/RIGHT\s+JOIN/, "RIGHT JOIN")
|
25
|
+
.gsub(/CROSS\s+JOIN/, "CROSS JOIN")
|
24
26
|
lines = text.split("\n")
|
25
27
|
new_text = []
|
26
28
|
|
@@ -18,7 +18,7 @@ module TSqlParser::Parsing::Formatters
|
|
18
18
|
require_relative "base_formatter"
|
19
19
|
|
20
20
|
class SelectFormatter < BaseFormatter
|
21
|
-
def
|
21
|
+
def format(text, tab = Defaults.get_default_tab)
|
22
22
|
formatted = []
|
23
23
|
lines = text.split("\n")
|
24
24
|
lines.each do |line|
|
@@ -42,7 +42,7 @@ module TSqlParser::Parsing::Formatters
|
|
42
42
|
|
43
43
|
private
|
44
44
|
|
45
|
-
def
|
45
|
+
def format_select(s, tab_count = Defaults.get_default_tab_count, tab = Defaults.get_default_tab)
|
46
46
|
return s if s.nil?
|
47
47
|
|
48
48
|
tokens = s.split(", ")
|
@@ -15,10 +15,10 @@
|
|
15
15
|
# TSqlParser::Parsing::Formatters::SetFormatter
|
16
16
|
|
17
17
|
module TSqlParser::Parsing::Formatters
|
18
|
-
require_relative
|
18
|
+
require_relative "base_formatter"
|
19
19
|
|
20
20
|
class SetFormatter < BaseFormatter
|
21
|
-
def
|
21
|
+
def format(text, tab = Defaults.get_default_tab)
|
22
22
|
formatted = []
|
23
23
|
lines = text.split("\n")
|
24
24
|
wait = false
|
@@ -71,7 +71,7 @@ module TSqlParser::Parsing::Formatters
|
|
71
71
|
|
72
72
|
private
|
73
73
|
|
74
|
-
def
|
74
|
+
def format_set(s, tab_count = Defaults.get_default_tab_count, tab = Defaults.get_default_tab)
|
75
75
|
return s if s.nil?
|
76
76
|
parts = []
|
77
77
|
builder = ""
|
@@ -18,7 +18,7 @@ module TSqlParser::Parsing::Formatters
|
|
18
18
|
require_relative "base_formatter"
|
19
19
|
|
20
20
|
class UpdateFormatter < BaseFormatter
|
21
|
-
def
|
21
|
+
def format(text, tab = Defaults.get_default_tab)
|
22
22
|
formatted = []
|
23
23
|
lines = text.split("\n")
|
24
24
|
lines.each do |line|
|
@@ -42,7 +42,7 @@ module TSqlParser::Parsing::Formatters
|
|
42
42
|
|
43
43
|
private
|
44
44
|
|
45
|
-
def
|
45
|
+
def format_update(s, tab_count = Defaults.get_default_tab_count, tab = Defaults.get_default_tab)
|
46
46
|
return s if s.nil?
|
47
47
|
"\n#{tab * (tab_count + 1)}#{s}"
|
48
48
|
end
|
@@ -18,7 +18,7 @@ module TSqlParser::Parsing::Formatters
|
|
18
18
|
require_relative "base_formatter"
|
19
19
|
|
20
20
|
class WhereFormatter < BaseFormatter
|
21
|
-
def
|
21
|
+
def format(text, tab = Defaults.get_default_tab)
|
22
22
|
formatted = []
|
23
23
|
text.split("\n").each do |line|
|
24
24
|
first = line.strip.split(" ").first
|
@@ -42,7 +42,7 @@ module TSqlParser::Parsing::Formatters
|
|
42
42
|
|
43
43
|
private
|
44
44
|
|
45
|
-
def
|
45
|
+
def format_predicate(s, tab_count = Defaults.get_default_tab_count, tab = Defaults.get_default_tab)
|
46
46
|
return s if s.nil?
|
47
47
|
indented = []
|
48
48
|
formatted = []
|
@@ -15,36 +15,20 @@
|
|
15
15
|
# TSqlParser::Parsing::Formatter
|
16
16
|
|
17
17
|
module TSqlParser::Parsing
|
18
|
-
require_relative
|
19
|
-
require_relative
|
20
|
-
require_relative 'strategy/insert_formatter'
|
21
|
-
require_relative 'strategy/select_formatter'
|
22
|
-
require_relative 'strategy/update_formatter'
|
23
|
-
require_relative 'strategy/where_formatter'
|
18
|
+
require_relative "strategy/__defaults"
|
19
|
+
require_relative "format_factory"
|
24
20
|
|
25
21
|
class TextFormatter
|
26
|
-
|
27
|
-
Formatters::SetFormatter.format(text, tab)
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.format_joins(text, tab = " ")
|
31
|
-
Formatters::JoinFormatter.format(text, tab)
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.format_updates(text, tab = " ")
|
35
|
-
Formatters::UpdateFormatter.format(text, tab)
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.format_inserts(text, tab = " ")
|
39
|
-
Formatters::InsertFormatter.format(text, tab)
|
40
|
-
end
|
22
|
+
attr_writer :strategy
|
41
23
|
|
42
|
-
def
|
43
|
-
|
24
|
+
def initialize(strategy, text, tab = Defaults.get_default_tab)
|
25
|
+
@strategy = FormatFactory.get(strategy)
|
26
|
+
@text = text
|
27
|
+
@tab = tab
|
44
28
|
end
|
45
29
|
|
46
|
-
def
|
47
|
-
|
30
|
+
def format
|
31
|
+
@strategy.format(@text, @tab)
|
48
32
|
end
|
49
33
|
end
|
50
34
|
end
|
data/lib/parsing/tokenizer.rb
CHANGED
@@ -15,20 +15,10 @@
|
|
15
15
|
# TSqlParser::Parsing::Tokenizer
|
16
16
|
|
17
17
|
module TSqlParser::Parsing
|
18
|
+
require_relative "config/defaults"
|
18
19
|
require_relative "transformers/token_categorizer"
|
19
20
|
|
20
21
|
class Tokenizer
|
21
|
-
@@default_char_delimiters = ["(", ",", ")", "=", "+", "-", "%", "/", "*", "<", "!", ">", "'", "[", "]", ";"]
|
22
|
-
@@default_skip_delimiters = [" ", "\n", "\t"]
|
23
|
-
|
24
|
-
def self.set_default_char_delimiters(char_delimiters = [])
|
25
|
-
@@default_char_delimiters = char_delimiters
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.set_default_skip_delimiters(skip_delimiters = [])
|
29
|
-
@@default_skip_delimiters = skip_delimiters
|
30
|
-
end
|
31
|
-
|
32
22
|
def self.tokenize(tsql_string)
|
33
23
|
Tokenizer.new.basic_tokenize(tsql_string).map { |t| TokenCategorizer.categorize(t) }
|
34
24
|
end
|
@@ -181,8 +171,8 @@ module TSqlParser::Parsing
|
|
181
171
|
@string = false
|
182
172
|
@string_count = 0
|
183
173
|
@skip_count = 0
|
184
|
-
@char_delimiters =
|
185
|
-
@skip_delimiters =
|
174
|
+
@char_delimiters = Defaults.get_default_single_char_tokens
|
175
|
+
@skip_delimiters = Defaults.get_default_delimiters
|
186
176
|
@delimiters = ([] << @char_delimiters << @skip_delimiters).flatten.uniq
|
187
177
|
@builder = ""
|
188
178
|
end
|
@@ -19,59 +19,113 @@ module TSqlParser::Parsing
|
|
19
19
|
require_relative "../models/flat_sql_container"
|
20
20
|
|
21
21
|
class TokenTransformer
|
22
|
+
def initialize(tokens = [])
|
23
|
+
@containers = []
|
24
|
+
@container = nil
|
25
|
+
@skip_count = 0
|
26
|
+
@cte = ""
|
27
|
+
@parenthesis = 0
|
28
|
+
@tokens = tokens
|
29
|
+
end
|
30
|
+
|
22
31
|
def self.transform(tokens)
|
23
|
-
|
24
|
-
self.combine_containers(containers)
|
32
|
+
TokenTransformer.new(tokens).transform
|
25
33
|
end
|
26
34
|
|
27
|
-
|
35
|
+
def transform
|
36
|
+
@tokens.each_with_index do |t, index|
|
37
|
+
@parenthesis += 1 if t[:open_parenthesis]
|
38
|
+
@parenthesis -= 1 if t[:close_parenthesis]
|
28
39
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
if skip_count > 0
|
35
|
-
skip_count -= 1
|
36
|
-
next
|
37
|
-
end
|
40
|
+
next if self.handle_skip
|
41
|
+
|
42
|
+
@t = t
|
43
|
+
@last_token = @tokens[index - 1] unless index - 1 < 0
|
44
|
+
@next_token = @tokens[index + 1] unless index + 1 > @tokens.size
|
38
45
|
|
39
|
-
|
46
|
+
next if self.handle_cte_end
|
40
47
|
|
41
48
|
if t[:string]
|
42
|
-
|
49
|
+
self.add_to_container
|
43
50
|
elsif Parser.is_new_node_keyword? t[:value]
|
44
|
-
if not next_token.nil? and Parser.is_new_node_keyword? next_token[:value]
|
45
|
-
if Parser.is_new_node_composite?(t[:value], next_token[:value])
|
46
|
-
|
47
|
-
container = SqlContainer.combine(t, next_token)
|
48
|
-
skip_count = 1
|
51
|
+
if not @next_token.nil? and Parser.is_new_node_keyword? @next_token[:value]
|
52
|
+
if Parser.is_new_node_composite?(@t[:value], @next_token[:value])
|
53
|
+
self.add_container
|
54
|
+
@container = SqlContainer.combine(@t, @next_token)
|
55
|
+
@skip_count = 1
|
49
56
|
next
|
50
57
|
end
|
51
58
|
end
|
52
|
-
|
53
|
-
|
59
|
+
self.add_container
|
60
|
+
self.new_container
|
54
61
|
elsif t[:value] == "WITH"
|
55
|
-
if not next_token.nil? and not next_token[:keyword] and not next_token[:parenthesis]
|
56
|
-
|
57
|
-
|
62
|
+
if not @next_token.nil? and not @next_token[:keyword] and not @next_token[:parenthesis]
|
63
|
+
self.add_container
|
64
|
+
self.new_container
|
65
|
+
@cte = @next_token[:value]
|
66
|
+
else
|
67
|
+
self.add_to_container
|
68
|
+
end
|
69
|
+
elsif @t[:label]
|
70
|
+
self.add_container
|
71
|
+
self.new_container
|
72
|
+
else
|
73
|
+
self.add_to_container
|
74
|
+
end
|
75
|
+
end
|
76
|
+
self.add_container
|
77
|
+
self.get_lines_from_containers
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def handle_skip
|
83
|
+
if @skip_count > 0
|
84
|
+
@skip_count -= 1
|
85
|
+
return true
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def add_to_container
|
90
|
+
@container.add @t unless @container.nil?
|
91
|
+
end
|
92
|
+
|
93
|
+
def add_container
|
94
|
+
@containers << @container unless @container.nil?
|
95
|
+
end
|
96
|
+
|
97
|
+
def new_container
|
98
|
+
@container = SqlContainer.new(@t)
|
99
|
+
end
|
100
|
+
|
101
|
+
def handle_cte_end
|
102
|
+
if not @cte.empty?
|
103
|
+
if not @last_token.nil? and @last_token[:close_parenthesis] and @parenthesis == 0
|
104
|
+
if @t[:keyword]
|
105
|
+
self.add_container
|
106
|
+
self.new_container
|
107
|
+
@cte = ""
|
58
108
|
else
|
59
|
-
|
109
|
+
self.add_to_container
|
60
110
|
end
|
61
|
-
elsif t[:
|
62
|
-
|
63
|
-
|
111
|
+
elsif not @last_token.nil? and not @last_token[:keyword] and @t[:value] == "AS"
|
112
|
+
self.add_to_container
|
113
|
+
elsif not @last_token.nil? and @last_token[:comma] and not @t[:keyword] and not @next_token.nil? and @next_token[:value] == "AS"
|
114
|
+
self.add_container
|
115
|
+
self.new_container
|
116
|
+
@cte = @t[:value]
|
64
117
|
else
|
65
|
-
|
118
|
+
self.add_to_container
|
66
119
|
end
|
120
|
+
return true
|
67
121
|
end
|
68
|
-
containers << container unless container.nil?
|
69
|
-
FlatSqlContainer.flatten_containers(containers)
|
70
122
|
end
|
71
123
|
|
72
|
-
def
|
124
|
+
def get_lines_from_containers
|
125
|
+
@containers = FlatSqlContainer.flatten_containers(@containers)
|
126
|
+
|
73
127
|
lines = []
|
74
|
-
containers.each do |c|
|
128
|
+
@containers.each do |c|
|
75
129
|
ct = c.get_token
|
76
130
|
|
77
131
|
builder = []
|
data/lib/tsql_parser.rb
CHANGED
@@ -15,13 +15,15 @@
|
|
15
15
|
# TSqlParser
|
16
16
|
|
17
17
|
module TSqlParser
|
18
|
+
require_relative "parsing/config/defaults"
|
19
|
+
|
18
20
|
# Formats a SQL string.
|
19
21
|
#
|
20
22
|
# @param sql [String] the SQL string to format.
|
21
23
|
# @param tab_count [Integer] the number of tabs to start with.
|
22
24
|
# @param tab [String] the tab string.
|
23
25
|
# @return [String] the formatted SQL string.
|
24
|
-
def self.format(sql, tab_count =
|
26
|
+
def self.format(sql, tab_count = Parsing::Defaults.get_default_tab_count, tab = Parsing::Defaults.get_default_tab)
|
25
27
|
require_relative "parsing/formatter"
|
26
28
|
tokens = self.parse(sql)
|
27
29
|
Parsing::Formatter.format(tokens, tab_count, tab)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tsql_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Stauffer
|
@@ -16,8 +16,13 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- lib/parsing/config/defaults.rb
|
19
20
|
- lib/parsing/formatter.rb
|
21
|
+
- lib/parsing/formatters/format_factory.rb
|
22
|
+
- lib/parsing/formatters/strategy/__defaults.rb
|
23
|
+
- lib/parsing/formatters/strategy/__formatters.rb
|
20
24
|
- lib/parsing/formatters/strategy/base_formatter.rb
|
25
|
+
- lib/parsing/formatters/strategy/cte_formatter.rb
|
21
26
|
- lib/parsing/formatters/strategy/insert_formatter.rb
|
22
27
|
- lib/parsing/formatters/strategy/join_formatter.rb
|
23
28
|
- lib/parsing/formatters/strategy/select_formatter.rb
|
@@ -54,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
59
|
- !ruby/object:Gem::Version
|
55
60
|
version: '0'
|
56
61
|
requirements: []
|
57
|
-
rubygems_version: 3.
|
62
|
+
rubygems_version: 3.3.5
|
58
63
|
signing_key:
|
59
64
|
specification_version: 4
|
60
65
|
summary: A very light-weight and opinionated T-SQL parser and formatter.
|