tsql_parser 0.0.5 → 0.0.7
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 +6 -7
- 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: 18f0af056c66e37a5b5f541779ee7387fc5756884f5e2d302329f7c8b35b9bfe
|
4
|
+
data.tar.gz: e914e56c485ec9fc0e282f27734d36e17b8a16bb220a4288744f7e45ac263614
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 565804ec4625e5bde30d5ef68d52a44d98317a803f1e4e90dedbb13f0805906ed165f9b4ac972bad845d13a41d72926344c02c8cf30a17c6b2b537a8a85eab42
|
7
|
+
data.tar.gz: c52b196c627315e281b33f4b7cb20ed0ac11d7b0290a1ead14cb45feb71b4c165e7bdf87098f9ea1c36aaa2814be38adc35251313a90bc9731e5a1df2e827690
|
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 = " ")
|
@@ -200,7 +195,11 @@ module TSqlParser::Parsing
|
|
200
195
|
formatted << "\n#{tab * (tab_count + 1)}#{table}"
|
201
196
|
formatted << "#{tab * (tab_count + 2)}(#{columns})"
|
202
197
|
formatted << "#{tab * (tab_count + 1)}VALUES"
|
203
|
-
|
198
|
+
if s.end_with? ");"
|
199
|
+
formatted << "#{tab * (tab_count + 2)}(#{values}"
|
200
|
+
else
|
201
|
+
formatted << "#{tab * (tab_count + 2)}(#{values})"
|
202
|
+
end
|
204
203
|
end
|
205
204
|
formatted.join("\n") unless formatted.empty?
|
206
205
|
end
|