tsql_parser 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 140f30630a7230c26d92685df9096e1ca6580b5b7e912abc67f9080ae059d548
4
- data.tar.gz: 5ab346dacef176ca728d6443ce76d91cdd8c924e2b72489312d4bf8fb61efe77
3
+ metadata.gz: ee8b4d7afa83f298c53d2f34be1012948466a1dc17dfb022bc59a723e7558c84
4
+ data.tar.gz: 29154ff1736be248f22c84a3e985699b1b3c1eadca44295634a9c27dba4cd848
5
5
  SHA512:
6
- metadata.gz: 0bcb94444e554501b9e926ff95149e749c62728bc2b6fed26012405d87b358ea93ea14a275b570c5d673f4d2a038b92fc0081ec0bef7e43253d760f352471f37
7
- data.tar.gz: 83f4f5aa3d64ecc5cddbd33346b2cbd8d81376ca4efdfa895d928f5fe9abfa759718d97c5700566ad45a06ebd653dd31937f32acee0390ced841e082e40ae0fa
6
+ metadata.gz: 01de8daed01b64881f3f112dc2233ea2a3fe27ffddd3e95940ed353425b36a2b2ae5631661cd81faab998494992149be1bc20d1adbd6939305350e2e8e9ab03e
7
+ data.tar.gz: f3212645945c40035938a96664e3f6df3044716750e12874f0cf4d40424729cea5054d42bbeb3335be7b914b7a5fbd14fb29c158163a39344b0f956dfd09c6b4
@@ -36,7 +36,6 @@ module TSqlParser::Parsing
36
36
  text = TextFormatter.format_selects(text, tab)
37
37
  text = TextFormatter.format_sets(text, tab)
38
38
  text
39
- puts text
40
39
  end
41
40
 
42
41
  private
@@ -49,9 +48,16 @@ module TSqlParser::Parsing
49
48
  new_lines << ""
50
49
  next
51
50
  end
51
+
52
52
  if Parser.is_newline_required? first or first.start_with? "/*"
53
53
  new_lines << ""
54
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
+
55
61
  new_lines << line
56
62
  end
57
63
  new_lines
@@ -72,10 +78,13 @@ module TSqlParser::Parsing
72
78
  next_line = work_lines[index + 1] unless index + 1 > work_lines.size
73
79
  next_line_first = next_line.strip.split(" ").first unless next_line.nil?
74
80
 
75
- if %w[CASE BEGIN SELECT].include? first or line.strip.start_with? "CREATE PROCEDURE"
81
+ if Parser.is_label? first
76
82
  indented_lines << "#{tab * tab_count}#{line}"
77
83
  tab_count += 1
78
- elsif %w[FROM END GO].include? first and not %w[DELETE UPDATE INSERT].include? last
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
79
88
  tab_count -= 1 if tab_count > 0
80
89
  indented_lines << "#{tab * tab_count}#{line}"
81
90
  else
@@ -129,8 +138,26 @@ module TSqlParser::Parsing
129
138
  def self.as_containers(tokens)
130
139
  containers = []
131
140
  container = nil
132
- tokens.each do |t|
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]
133
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]
134
161
  containers << container unless container.nil?
135
162
  container = SqlContainer.new(t)
136
163
  else
@@ -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[WHERE SET WITH]
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
@@ -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
- formatted = []
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 = " ")
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.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Stauffer