tsql_parser 0.0.2 → 0.0.4

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: b757cb7f1ee4f8b904d58f92dfe0c031a42e1c029c3fca2e9b9c276bc2c3954a
4
- data.tar.gz: c0eb3a98483345e7a365800e400756c6bbdc7898d706f09f8759f17f044e0c06
3
+ metadata.gz: 140f30630a7230c26d92685df9096e1ca6580b5b7e912abc67f9080ae059d548
4
+ data.tar.gz: 5ab346dacef176ca728d6443ce76d91cdd8c924e2b72489312d4bf8fb61efe77
5
5
  SHA512:
6
- metadata.gz: ca7f2b2f2c15d508bfddfa725ca8d979132c018e4b44fef321249329f55cc220cc8ca50b2dc2f414d17daf336ad97b6617ee6e28b5afde3a07f900f710e057cd
7
- data.tar.gz: 46171055a31591de3ff362ebf14dc45bd819399ab3e5acffad9810499f9440f4da99eace839b1b775a966b8ea31982691fdad746597c345e2c6aca14a0229c85
6
+ metadata.gz: 0bcb94444e554501b9e926ff95149e749c62728bc2b6fed26012405d87b358ea93ea14a275b570c5d673f4d2a038b92fc0081ec0bef7e43253d760f352471f37
7
+ data.tar.gz: 83f4f5aa3d64ecc5cddbd33346b2cbd8d81376ca4efdfa895d928f5fe9abfa759718d97c5700566ad45a06ebd653dd31937f32acee0390ced841e082e40ae0fa
@@ -36,6 +36,7 @@ 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
39
40
  end
40
41
 
41
42
  private
@@ -48,7 +49,7 @@ module TSqlParser::Parsing
48
49
  new_lines << ""
49
50
  next
50
51
  end
51
- if %w[IF RETURN INSERT DELETE WHILE].include? first or first.start_with? "/*"
52
+ if Parser.is_newline_required? first or first.start_with? "/*"
52
53
  new_lines << ""
53
54
  end
54
55
  new_lines << line
@@ -64,30 +65,26 @@ module TSqlParser::Parsing
64
65
  end
65
66
  sub_one = false
66
67
  work_lines = work_lines.flatten
67
- last = ''
68
+ last = ""
69
+ in_if_exists = false
68
70
  work_lines.each_with_index do |line, index|
69
71
  first = line.strip.split(" ").first
72
+ next_line = work_lines[index + 1] unless index + 1 > work_lines.size
73
+ next_line_first = next_line.strip.split(" ").first unless next_line.nil?
70
74
 
71
75
  if %w[CASE BEGIN SELECT].include? first or line.strip.start_with? "CREATE PROCEDURE"
72
76
  indented_lines << "#{tab * tab_count}#{line}"
73
77
  tab_count += 1
74
- elsif %w[END GO FROM].include? first and last != "DELETE"
78
+ elsif %w[FROM END GO].include? first and not %w[DELETE UPDATE INSERT].include? last
75
79
  tab_count -= 1 if tab_count > 0
76
80
  indented_lines << "#{tab * tab_count}#{line}"
77
- elsif %w[IF].include? first
78
- indented_lines << "#{tab * tab_count}#{line}"
79
- next_line = work_lines[index + 1] unless index + 1 > work_lines.size
80
- sub_one = true unless next_line.start_with? "BEGIN"
81
- tab_count += 1 if sub_one
82
- last = first
83
- next
84
81
  else
85
82
  indented_lines << "#{tab * tab_count}#{line}"
86
83
  end
87
84
 
88
85
  if sub_one
89
86
  sub_one = false
90
- tab_count -= 1
87
+ tab_count -= 1 if tab_count > 0
91
88
  end
92
89
  last = first
93
90
  end
@@ -25,7 +25,7 @@ module TSqlParser::Parsing
25
25
 
26
26
  def self.get_new_node_keywords
27
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]
28
+ - %w[WHERE SET WITH]
29
29
  end
30
30
 
31
31
  def self.get_begin_keyword
@@ -45,7 +45,7 @@ module TSqlParser::Parsing
45
45
  end
46
46
 
47
47
  def self.get_newline_keywords
48
- %w[INSERT UPDATE DELETE SELECT SET DECLARE CREATE FROM INNER FULL OUTER LEFT RIGHT CROSS JOIN IF BEGIN END RETURN WHERE PRINT GROUP ORDER WHILE]
48
+ %w[INSERT UPDATE DELETE SELECT CREATE IF RETURN PRINT WHILE]
49
49
  end
50
50
 
51
51
  def self.get_reserved_keywords
@@ -34,7 +34,7 @@ module TSqlParser::Parsing
34
34
  parts = line.strip.split(" SET ")
35
35
  tab_count = self.get_tab_count(line, tab)
36
36
  formatted << "#{tab * tab_count}#{parts[0]}\n"
37
- parts[1..].each {|p| formatted << "#{tab * tab_count}SET #{p}" }
37
+ parts[1..].each { |p| formatted << "#{tab * tab_count}SET #{p}" }
38
38
  else
39
39
  formatted << line
40
40
  end
@@ -159,24 +159,24 @@ module TSqlParser::Parsing
159
159
  def self.format_set(s, tab_count = 0, tab = " ")
160
160
  return s if s.nil?
161
161
  parts = []
162
- builder = ''
162
+ builder = ""
163
163
  parenthesis = 0
164
- s.split('').each do |c|
165
- parenthesis += 1 if c == '('
166
- parenthesis -= 1 if c == ')'
167
- if c == ','
164
+ s.split("").each do |c|
165
+ parenthesis += 1 if c == "("
166
+ parenthesis -= 1 if c == ")"
167
+ if c == ","
168
168
  if parenthesis > 0
169
169
  builder << c
170
170
  else
171
171
  parts << builder
172
- builder = ''
172
+ builder = ""
173
173
  end
174
174
  else
175
175
  builder << c
176
176
  end
177
177
  end
178
178
  parts << builder unless builder.empty?
179
- "\n#{parts.map {|p| "#{tab * (tab_count + 1)}#{p.strip}"}.join(",\n")}"
179
+ "\n#{parts.map { |p| "#{tab * (tab_count + 1)}#{p.strip}" }.join(",\n")}"
180
180
  end
181
181
 
182
182
  def self.format_update(s, tab_count = 0, tab = " ")
@@ -184,10 +184,8 @@ module TSqlParser::Parsing
184
184
  formatted = []
185
185
  parts = s.split(" SET ")
186
186
  table = parts[0]
187
- where_parts = parts[1].split(" WHERE ")
188
187
  formatted << "\n#{tab * (tab_count + 1)}#{table}"
189
- formatted << "#{tab * tab_count}SET #{where_parts[0]}"
190
- formatted << "#{tab * tab_count}WHERE #{where_parts[1]}" if where_parts.size > 0
188
+ formatted << "#{tab * tab_count}SET #{parts[1]}"
191
189
  formatted.join("\n")
192
190
  end
193
191
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tsql_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Stauffer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-27 00:00:00.000000000 Z
11
+ date: 2023-03-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A very light-weight and opinionated T-SQL parser and formatter.
14
14
  email: scott@fuseraft.com