tsql_parser 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/parsing/formatter.rb +31 -3
- data/lib/parsing/keyword.rb +2 -2
- data/lib/parsing/model/sql_container.rb +11 -0
- data/lib/parsing/parser.rb +8 -0
- data/lib/parsing/text_formatter.rb +1 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee8b4d7afa83f298c53d2f34be1012948466a1dc17dfb022bc59a723e7558c84
|
4
|
+
data.tar.gz: 29154ff1736be248f22c84a3e985699b1b3c1eadca44295634a9c27dba4cd848
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01de8daed01b64881f3f112dc2233ea2a3fe27ffddd3e95940ed353425b36a2b2ae5631661cd81faab998494992149be1bc20d1adbd6939305350e2e8e9ab03e
|
7
|
+
data.tar.gz: f3212645945c40035938a96664e3f6df3044716750e12874f0cf4d40424729cea5054d42bbeb3335be7b914b7a5fbd14fb29c158163a39344b0f956dfd09c6b4
|
data/lib/parsing/formatter.rb
CHANGED
@@ -48,9 +48,16 @@ module TSqlParser::Parsing
|
|
48
48
|
new_lines << ""
|
49
49
|
next
|
50
50
|
end
|
51
|
+
|
51
52
|
if Parser.is_newline_required? first or first.start_with? "/*"
|
52
53
|
new_lines << ""
|
53
54
|
end
|
55
|
+
|
56
|
+
if Parser.is_label? first
|
57
|
+
#tab_count = self.get_tab_count(line)
|
58
|
+
# TODO: tab it out
|
59
|
+
end
|
60
|
+
|
54
61
|
new_lines << line
|
55
62
|
end
|
56
63
|
new_lines
|
@@ -71,10 +78,13 @@ module TSqlParser::Parsing
|
|
71
78
|
next_line = work_lines[index + 1] unless index + 1 > work_lines.size
|
72
79
|
next_line_first = next_line.strip.split(" ").first unless next_line.nil?
|
73
80
|
|
74
|
-
if
|
81
|
+
if Parser.is_label? first
|
75
82
|
indented_lines << "#{tab * tab_count}#{line}"
|
76
83
|
tab_count += 1
|
77
|
-
elsif %w[
|
84
|
+
elsif %w[CASE BEGIN SELECT].include? first or line.strip.start_with? "CREATE PROCEDURE"
|
85
|
+
indented_lines << "#{tab * tab_count}#{line}"
|
86
|
+
tab_count += 1
|
87
|
+
elsif %w[FROM END GO].include? first and not %w[DELETE UPDATE INSERT SET].include? last
|
78
88
|
tab_count -= 1 if tab_count > 0
|
79
89
|
indented_lines << "#{tab * tab_count}#{line}"
|
80
90
|
else
|
@@ -128,8 +138,26 @@ module TSqlParser::Parsing
|
|
128
138
|
def self.as_containers(tokens)
|
129
139
|
containers = []
|
130
140
|
container = nil
|
131
|
-
|
141
|
+
skip_count = 0
|
142
|
+
tokens.each_with_index do |t, index|
|
143
|
+
if skip_count > 0
|
144
|
+
skip_count -= 1
|
145
|
+
next
|
146
|
+
end
|
147
|
+
|
148
|
+
next_token = tokens[index + 1]
|
132
149
|
if Parser.is_new_node_keyword? t[:value]
|
150
|
+
if not next_token.nil? and Parser.is_new_node_keyword? next_token[:value]
|
151
|
+
if Parser.is_new_node_composite?(t[:value], next_token[:value])
|
152
|
+
containers << container unless container.nil?
|
153
|
+
container = SqlContainer.combine(t, next_token)
|
154
|
+
skip_count = 1
|
155
|
+
next
|
156
|
+
end
|
157
|
+
end
|
158
|
+
containers << container unless container.nil?
|
159
|
+
container = SqlContainer.new(t)
|
160
|
+
elsif t[:label]
|
133
161
|
containers << container unless container.nil?
|
134
162
|
container = SqlContainer.new(t)
|
135
163
|
else
|
data/lib/parsing/keyword.rb
CHANGED
@@ -24,8 +24,8 @@ module TSqlParser::Parsing
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def self.get_new_node_keywords
|
27
|
-
%w[CREATE ALTER DROP RENAME SELECT INSERT UPDATE DELETE WHILE IF ELSE DECLARE SET WITH BEGIN FROM WHERE INNER LEFT JOIN END GO GROUP ORDER CASE PRINT RETURN] \
|
28
|
-
- %w[
|
27
|
+
%w[CREATE ALTER DROP RENAME SELECT INSERT UPDATE DELETE WHILE IF ELSE DECLARE SET WITH BEGIN FROM WHERE INNER LEFT JOIN END GO GROUP ORDER CASE PRINT RETURN GOTO OPEN CLOSE DEALLOCATE FETCH] \
|
28
|
+
- %w[WITH LEFT RIGHT]
|
29
29
|
end
|
30
30
|
|
31
31
|
def self.get_begin_keyword
|
@@ -21,6 +21,17 @@ module TSqlParser::Parsing
|
|
21
21
|
@nodes = []
|
22
22
|
end
|
23
23
|
|
24
|
+
def self.combine(first, second)
|
25
|
+
token = {}
|
26
|
+
token[:value] = "#{first[:value]} #{second[:value]}"
|
27
|
+
[first, second].each do |t|
|
28
|
+
t.each do |k, v|
|
29
|
+
token[k] = v unless token.has_key? k
|
30
|
+
end
|
31
|
+
end
|
32
|
+
SqlContainer.new(token)
|
33
|
+
end
|
34
|
+
|
24
35
|
def set_token(token)
|
25
36
|
@token = token
|
26
37
|
end
|
data/lib/parsing/parser.rb
CHANGED
@@ -138,8 +138,16 @@ module TSqlParser::Parsing
|
|
138
138
|
Keyword.get_new_node_keywords.include? s.upcase
|
139
139
|
end
|
140
140
|
|
141
|
+
def self.is_new_node_composite?(s, next_s)
|
142
|
+
["INNER JOIN", "LEFT JOIN", "RIGHT JOIN", "ELSE IF"].include? "#{s} #{next_s}"
|
143
|
+
end
|
144
|
+
|
141
145
|
def self.is_terminator?(s)
|
142
146
|
s == ";"
|
143
147
|
end
|
148
|
+
|
149
|
+
def self.is_label?(s)
|
150
|
+
s.end_with? ":"
|
151
|
+
end
|
144
152
|
end
|
145
153
|
end
|
@@ -181,12 +181,7 @@ module TSqlParser::Parsing
|
|
181
181
|
|
182
182
|
def self.format_update(s, tab_count = 0, tab = " ")
|
183
183
|
return s if s.nil?
|
184
|
-
|
185
|
-
parts = s.split(" SET ")
|
186
|
-
table = parts[0]
|
187
|
-
formatted << "\n#{tab * (tab_count + 1)}#{table}"
|
188
|
-
formatted << "#{tab * tab_count}SET #{parts[1]}"
|
189
|
-
formatted.join("\n")
|
184
|
+
"\n#{tab * (tab_count + 1)}#{s}"
|
190
185
|
end
|
191
186
|
|
192
187
|
def self.format_insert(s, tab_count = 0, tab = " ")
|