niceql 0.5.1 → 0.6.1

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.
data/lib/niceql.rb CHANGED
@@ -1,33 +1,60 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "niceql/version"
4
+ require "securerandom"
5
+ require "forwardable"
2
6
 
3
7
  module Niceql
4
-
5
8
  module StringColorize
6
- def self.colorize_verb( str)
7
- # yellow ANSI color
8
- "\e[0;33;49m#{str}\e[0m"
9
- end
10
- def self.colorize_str(str)
11
- # cyan ANSI color
12
- "\e[0;36;49m#{str}\e[0m"
13
- end
14
- def self.colorize_err(err)
15
- # red ANSI color
16
- "\e[0;31;49m#{err}\e[0m"
9
+ class << self
10
+ def colorize_keyword(str)
11
+ # yellow ANSI color
12
+ "\e[0;33;49m#{str}\e[0m"
13
+ end
14
+
15
+ def colorize_str(str)
16
+ # cyan ANSI color
17
+ "\e[0;36;49m#{str}\e[0m"
18
+ end
19
+
20
+ def colorize_err(err)
21
+ # red ANSI color
22
+ "\e[0;31;49m#{err}\e[0m"
23
+ end
24
+
25
+ def colorize_comment(comment)
26
+ # bright black bold ANSI color
27
+ "\e[0;90;1;49m#{comment}\e[0m"
28
+ end
17
29
  end
18
30
  end
19
31
 
20
32
  module Prettifier
21
- INLINE_VERBS = %w(WITH ASC (IN\s) COALESCE AS WHEN THEN ELSE END AND UNION ALL ON DISTINCT INTERSECT EXCEPT EXISTS NOT COUNT ROUND CAST).join('| ')
22
- NEW_LINE_VERBS = 'SELECT|FROM|WHERE|CASE|ORDER BY|LIMIT|GROUP BY|(RIGHT |LEFT )*(INNER |OUTER )*JOIN( LATERAL)*|HAVING|OFFSET|UPDATE'
33
+ # ?= -- should be present but without being added to MatchData
34
+ AFTER_KEYWORD_SPACE = '(?=\s{1})'
35
+ JOIN_KEYWORDS = '(RIGHT\s+|LEFT\s+){0,1}(INNER\s+|OUTER\s+){0,1}JOIN(\s+LATERAL){0,1}'
36
+ INLINE_KEYWORDS = "WITH|ASC|COALESCE|AS|WHEN|THEN|ELSE|END|AND|UNION|ALL|ON|DISTINCT|"\
37
+ "INTERSECT|EXCEPT|EXISTS|NOT|COUNT|ROUND|CAST|IN"
38
+ NEW_LINE_KEYWORDS = "SELECT|FROM|WHERE|CASE|ORDER BY|LIMIT|GROUP BY|HAVING|OFFSET|UPDATE|SET|#{JOIN_KEYWORDS}"
39
+
23
40
  POSSIBLE_INLINER = /(ORDER BY|CASE)/
24
- VERBS = "#{NEW_LINE_VERBS}|#{INLINE_VERBS}"
25
- STRINGS = /("[^"]+")|('[^']+')/
41
+ KEYWORDS = "(#{NEW_LINE_KEYWORDS}|#{INLINE_KEYWORDS})#{AFTER_KEYWORD_SPACE}"
42
+ # ?: -- will not match partial enclosed by (..)
43
+ MULTILINE_INDENTABLE_LITERAL = /(?:'[^']+'\s*\n+\s*)+(?:'[^']+')+/
44
+ # STRINGS matched both kind of strings the multiline solid
45
+ # and single quoted multiline strings with \s*\n+\s* separation
46
+ STRINGS = /("[^"]+")|((?:'[^']+'\s*\n+\s*)*(?:'[^']+')+)/
26
47
  BRACKETS = '[\(\)]'
27
- SQL_COMMENTS = /(\s*?--.+\s*)|(\s*?\/\*[^\/\*]*\*\/\s*)/
28
- # only newlined comments will be matched
29
- SQL_COMMENTS_CLEARED = /(\s*?--.+\s{1})|(\s*$\s*\/\*[^\/\*]*\*\/\s{1})/
48
+ # will match all /* single line and multiline comments */ and -- based comments
49
+ # the last will be matched as single block whenever comment lines followed each other.
50
+ # For instance:
51
+ # SELECT * -- comment 1
52
+ # -- comment 2
53
+ # all comments will be matched as a single block
54
+ SQL_COMMENTS = %r{(\s*?--[^\n]+\n*)+|(\s*?/\*[^/\*]*\*/\s*)}m
30
55
  COMMENT_CONTENT = /[\S]+[\s\S]*[\S]+/
56
+ NAMED_DOLLAR_QUOTED_STRINGS_REGEX = /[^\$](\$[^\$]+\$)[^\$]/
57
+ DOLLAR_QUOTED_STRINGS = /(\$\$.*\$\$)/
31
58
 
32
59
  class << self
33
60
  def config
@@ -35,10 +62,9 @@ module Niceql
35
62
  end
36
63
 
37
64
  def prettify_err(err, original_sql_query = nil)
38
- prettify_pg_err( err.to_s, original_sql_query )
65
+ prettify_pg_err(err.to_s, original_sql_query)
39
66
  end
40
67
 
41
-
42
68
  # Postgres error output:
43
69
  # ERROR: VALUES in FROM must have an alias
44
70
  # LINE 2: FROM ( VALUES(1), (2) );
@@ -54,7 +80,7 @@ module Niceql
54
80
  # ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "usr" does not exist
55
81
  # LINE 1: SELECT usr FROM users ORDER BY 1
56
82
  # ^
57
- #: SELECT usr FROM users ORDER BY 1
83
+ # : SELECT usr FROM users ORDER BY 1
58
84
 
59
85
  # prettify_pg_err parses ActiveRecord::StatementInvalid string,
60
86
  # but you may use it without ActiveRecord either way:
@@ -62,6 +88,7 @@ module Niceql
62
88
  # don't mess with original sql query, or prettify_pg_err will deliver incorrect results
63
89
  def prettify_pg_err(err, original_sql_query = nil)
64
90
  return err if err[/LINE \d+/].nil?
91
+
65
92
  # LINE 2: ... -> err_line_num = 2
66
93
  err_line_num = err.match(/LINE (\d+):/)[1].to_i
67
94
  # LINE 1: SELECT usr FROM users ORDER BY 1
@@ -70,7 +97,11 @@ module Niceql
70
97
  sql_start_line_num = 3 if err.lines.length <= 3
71
98
  # error not always contains HINT
72
99
  sql_start_line_num ||= err.lines[3][/(HINT|DETAIL)/] ? 4 : 3
73
- sql_body_lines = sql_start_line_num < err.lines.length ? err.lines[sql_start_line_num..-1] : original_sql_query&.lines
100
+ sql_body_lines = if sql_start_line_num < err.lines.length
101
+ err.lines[sql_start_line_num..-1]
102
+ else
103
+ original_sql_query&.lines
104
+ end
74
105
 
75
106
  # this means original query is missing so it's nothing to prettify
76
107
  return err unless sql_body_lines
@@ -80,130 +111,338 @@ module Niceql
80
111
  # and to apply a full red colorizing schema on an SQL line with error
81
112
  err_line = sql_body_lines[err_line_num - 1]
82
113
 
83
- #colorizing verbs, strings and error line
84
- err_body = sql_body_lines.map { |ln| ln == err_line ? StringColorize.colorize_err( ln ) : colorize_err_line(ln) }
114
+ # colorizing keywords, strings and error line
115
+ err_body = sql_body_lines.map do |ln|
116
+ ln == err_line ? StringColorize.colorize_err(ln) : colorize_err_line(ln)
117
+ end
118
+
119
+ err_caret_line = extract_err_caret_line(err_address_line, err_line, sql_body_lines, err)
120
+ err_body.insert(err_line_num, StringColorize.colorize_err(err_caret_line))
121
+
122
+ err.lines[0..sql_start_line_num - 1].join + err_body.join
123
+ end
124
+
125
+ def prettify_sql(sql, colorize = true)
126
+ QueryNormalizer.new(sql, colorize).prettified_sql
127
+ end
128
+
129
+ def prettify_multiple(sql_multi, colorize = true)
130
+ sql_multi.split(/(?>#{SQL_COMMENTS})|(\;)/).each_with_object([""]) do |pattern, queries|
131
+ queries[-1] += pattern
132
+ queries << "" if pattern == ";"
133
+ end.map! do |sql|
134
+ # we were splitting by comments and ';', so if next sql start with comment we've got a misplaced \n\n
135
+ sql.match?(/\A\s+\z/) ? nil : prettify_sql(sql, colorize)
136
+ end.compact.join("\n")
137
+ end
138
+
139
+ private
140
+
141
+ def colorize_err_line(line)
142
+ line.gsub(/#{KEYWORDS}/) { |keyword| StringColorize.colorize_keyword(keyword) }
143
+ .gsub(STRINGS) { |str| StringColorize.colorize_str(str) }
144
+ end
145
+
146
+ def extract_err_caret_line(err_address_line, err_line, sql_body, err)
147
+ # LINE could be quoted ( both sides and sometimes only from one ):
148
+ # "LINE 1: ...t_id\" = $13 AND \"products\".\"carrier_id\" = $14 AND \"product_t...\n",
149
+ err_quote = (err_address_line.match(/\.\.\.(.+)\.\.\./) || err_address_line.match(/\.\.\.(.+)/))&.send(:[], 1)
150
+
151
+ # line[2] is original err caret line i.e.: ' ^'
152
+ # err_address_line[/LINE \d+:/].length+1..-1 - is a position from error quote begin
153
+ err_caret_line = err.lines[2][err_address_line[/LINE \d+:/].length + 1..-1]
85
154
 
86
- err_caret_line = extract_err_caret_line( err_address_line, err_line, sql_body_lines, err )
87
- err_body.insert( err_line_num, StringColorize.colorize_err( err_caret_line ) )
155
+ # when err line is too long postgres quotes it in double '...'
156
+ # so we need to reposition caret against original line
157
+ if err_quote
158
+ err_quote_caret_offset = err_caret_line.length - err_address_line.index("...").to_i + 3
159
+ err_caret_line = " " * (err_line.index(err_quote) + err_quote_caret_offset) + "^\n"
160
+ end
88
161
 
89
- err.lines[0..sql_start_line_num-1].join + err_body.join
162
+ # older versions of ActiveRecord were adding ': ' before an original query :(
163
+ err_caret_line.prepend(" ") if sql_body[0].start_with?(": ")
164
+ # if mistake is on last string than err_line.last != \n then we need to prepend \n to caret line
165
+ err_caret_line.prepend("\n") unless err_line[-1] == "\n"
166
+ err_caret_line
167
+ end
168
+ end
169
+
170
+ # The normalizing and formatting logic:
171
+ # 1. Split the original query onto the query part + literals + comments
172
+ # a. find all potential dollar-signed separators
173
+ # b. prepare full literal extractor regex
174
+ # 2. Find and separate all literals and comments into mutable/format-able types
175
+ # and immutable ( see the typing and formatting rules below )
176
+ # 3. Replace all literals and comments with uniq ids on the original query to get the parametrized query
177
+ # 4. Format parametrized query alongside with mutable/format-able comments and literals
178
+ # a. clear space characters: replace all \s+ to \s, remove all "\n" e.t.c
179
+ # b. split in lines -> indent -> colorize
180
+ # 5. Restore literals and comments with their values
181
+ class QueryNormalizer
182
+ extend Forwardable
183
+ def_delegator :Niceql, :config
184
+
185
+ # Literals content should not be indented, only string parts separated by new lines can be indented
186
+ # indentable_string:
187
+ # UPDATE docs SET body = 'First line'
188
+ # 'Second line'
189
+ # 'Third line', ...
190
+ #
191
+ # SQL standard allow such multiline separation.
192
+
193
+ # newline_end_comments:
194
+ # SELECT * -- get all column
195
+ # SELECT * /* get all column */
196
+ #
197
+ # SELECT * -- get all column
198
+ # -- we need all columns for this request
199
+ # SELECT * /* get all column
200
+ # we need all columns for this request */
201
+ #
202
+ # rare case newline_start_comments:
203
+ # SELECT *
204
+ # /* get all column
205
+ # we need all columns for this request */ FROM table
206
+ #
207
+ # newline_wrapped_comments:
208
+ # SELECT *
209
+ # /* get all column
210
+ # we need all columns for this request */
211
+ # FROM table
212
+ #
213
+ # SELECT *
214
+ # -- get all column
215
+ # -- we need all columns for this request
216
+ # FROM ...
217
+ # Potentially we could prettify different type of comments and strings a little bit differently,
218
+ # but right now there is no difference between the
219
+ # newline_wrapped_comment, newline_start_comment, newline_end_comment, they all will be wrapped in newlines
220
+ COMMENT_AND_LITERAL_TYPES = [:immutable_string, :indentable_string, :inline_comment, :newline_wrapped_comment,
221
+ :newline_start_comment, :newline_end_comment,]
222
+
223
+ attr_reader :parametrized_sql, :initial_sql, :string_regex, :literals_and_comments_types, :colorize
224
+
225
+ def initialize(sql, colorize)
226
+ @initial_sql = sql
227
+ @colorize = colorize
228
+ @parametrized_sql = ""
229
+ @guids_to_content = {}
230
+ @literals_and_comments_types = {}
231
+ @counter = Hash.new(0)
232
+
233
+ init_strings_regex
234
+ prepare_parametrized_sql
235
+ prettify_parametrized_sql
236
+ end
237
+
238
+ def prettified_sql
239
+ @parametrized_sql % @guids_to_content.transform_keys(&:to_sym)
90
240
  end
91
241
 
92
- def prettify_sql( sql, colorize = true )
242
+ private
243
+
244
+ def prettify_parametrized_sql
93
245
  indent = 0
94
- parentness = []
95
-
96
- sql = sql.split( SQL_COMMENTS ).each_slice(2).map{ | sql_part, comment |
97
- # remove additional formatting for sql_parts but leave comment intact
98
- [sql_part.gsub(/[\s]+/, ' '),
99
- # comment.match?(/\A\s*$/) - SQL_COMMENTS gets all comment content + all whitespaced chars around
100
- # so this sql_part.length == 0 || comment.match?(/\A\s*$/) checks does the comment starts from new line
101
- comment && ( sql_part.length == 0 || comment.match?(/\A\s*$/) ? "\n#{comment[COMMENT_CONTENT]}\n" : comment[COMMENT_CONTENT] ) ]
102
- }.flatten.join(' ')
103
-
104
- sql.gsub!(/ \n/, "\n")
105
-
106
- sql.gsub!(STRINGS){ |str| StringColorize.colorize_str(str) } if colorize
107
-
108
- first_verb = true
109
- prev_was_comment = false
110
-
111
- sql.gsub!( /(#{VERBS}|#{BRACKETS}|#{SQL_COMMENTS_CLEARED})/) do |verb|
112
- if 'SELECT' == verb
113
- indent += config.indentation_base if !config.open_bracket_is_newliner || parentness.last.nil? || parentness.last[:nested]
114
- parentness.last[:nested] = true if parentness.last
115
- add_new_line = !first_verb
116
- elsif verb == '('
117
- next_closing_bracket = Regexp.last_match.post_match.index(')')
246
+ brackets = []
247
+ first_keyword = true
248
+
249
+ parametrized_sql.gsub!(query_split_regex) do |matched_part|
250
+ if inline_piece?(matched_part)
251
+ first_keyword = false
252
+ next matched_part
253
+ end
254
+ post_match_str = Regexp.last_match.post_match
255
+
256
+ if ["SELECT", "UPDATE", "INSERT"].include?(matched_part)
257
+ indent_block = !config.open_bracket_is_newliner || brackets.last.nil? || brackets.last[:nested]
258
+ indent += config.indentation_base if indent_block
259
+ brackets.last[:nested] = true if brackets.last
260
+ add_new_line = !first_keyword
261
+ elsif matched_part == "("
262
+ next_closing_bracket = post_match_str.index(")")
118
263
  # check if brackets contains SELECT statement
119
- add_new_line = !!Regexp.last_match.post_match[0..next_closing_bracket][/SELECT/] && config.open_bracket_is_newliner
120
- parentness << { nested: add_new_line }
121
- elsif verb == ')'
264
+ add_new_line = !!post_match_str[0..next_closing_bracket][/SELECT/] && config.open_bracket_is_newliner
265
+ brackets << { nested: add_new_line }
266
+ elsif matched_part == ")"
122
267
  # this also covers case when right bracket is used without corresponding left one
123
- add_new_line = parentness.last.nil? || parentness.last[:nested]
124
- indent -= ( parentness.last.nil? ? 2 * config.indentation_base : (parentness.last[:nested] ? config.indentation_base : 0) )
268
+ add_new_line = brackets.last.nil? || brackets.last[:nested]
269
+ indent -= (brackets.last.nil? && 2 || brackets.last[:nested] && 1 || 0) * config.indentation_base
125
270
  indent = 0 if indent < 0
126
- parentness.pop
127
- elsif verb[POSSIBLE_INLINER]
271
+ brackets.pop
272
+ elsif matched_part[POSSIBLE_INLINER]
128
273
  # in postgres ORDER BY can be used in aggregation function this will keep it
129
274
  # inline with its agg function
130
- add_new_line = parentness.last.nil? || parentness.last[:nested]
275
+ add_new_line = brackets.last.nil? || brackets.last[:nested]
131
276
  else
132
- add_new_line = verb[/(#{INLINE_VERBS})/].nil?
277
+ # since we are matching KEYWORD without space on the end
278
+ # IN will be present in JOIN, DISTINCT e.t.c, so we need to exclude it explicitly
279
+ add_new_line = matched_part.match?(/(#{NEW_LINE_KEYWORDS})/)
133
280
  end
134
281
 
135
- # !add_new_line && previous_was_comment means we had newlined comment, and now even
136
- # if verb is inline verb we will need to add new line with indentation BUT all
137
- # inliners match with a space before so we need to strip it
138
- verb.lstrip! if !add_new_line && prev_was_comment
282
+ # do not indent first keyword in query, and indent everytime we started new line
283
+ add_indent_to_keyword = !first_keyword && add_new_line
139
284
 
140
- add_new_line = prev_was_comment unless add_new_line
141
- add_indent = !first_verb && add_new_line
142
-
143
- if verb[SQL_COMMENTS_CLEARED]
144
- verb = verb[COMMENT_CONTENT]
145
- prev_was_comment = true
285
+ if literals_and_comments_types[matched_part]
286
+ # this is a case when comment followed by ordinary SQL part not by any keyword
287
+ # this means that it will not be gsubed and no indent will be added before this part, while needed
288
+ last_comment_followed_by_keyword = post_match_str.match?(/\A\}\s{0,1}(?:#{KEYWORDS})/)
289
+ indent_parametrized_part(matched_part, indent, !last_comment_followed_by_keyword, !first_keyword)
290
+ matched_part
146
291
  else
147
- first_verb = false
148
- prev_was_comment = false
292
+ first_keyword = false
293
+ indented_sql = (add_indent_to_keyword ? indent_multiline(matched_part, indent) : matched_part)
294
+ add_new_line ? "\n" + indented_sql : indented_sql
149
295
  end
296
+ end
297
+
298
+ parametrized_sql.gsub!(" \n", "\n") # moved keywords could keep space before it, we can crop it anyway
150
299
 
151
- verb = StringColorize.colorize_verb(verb) if !%w[( )].include?(verb) && colorize
300
+ clear_extra_newline_after_comments
152
301
 
153
- subs = ( add_indent ? indent_multiline(verb, indent) : verb)
154
- !first_verb && add_new_line ? "\n" + subs : subs
302
+ colorize_query if colorize
303
+ end
304
+
305
+ def add_string_or_comment(string_or_comment)
306
+ # when we splitting original SQL, it could and could not end with literal/comment
307
+ # hence we could try to add nil...
308
+ return if string_or_comment.nil?
309
+
310
+ type = get_placeholder_type(string_or_comment)
311
+ # will be formatted to comment_1_guid
312
+ typed_id = new_placeholder_name(type)
313
+ @guids_to_content[typed_id] = string_or_comment
314
+ @counter[type] += 1
315
+ @literals_and_comments_types[typed_id] = type
316
+ "%{#{typed_id}}"
317
+ end
318
+
319
+ def literal_and_comments_placeholders_regex
320
+ /(#{@literals_and_comments_types.keys.join("|")})/
321
+ end
322
+
323
+ def inline_piece?(comment_or_string)
324
+ [:immutable_string, :inline_comment].include?(literals_and_comments_types[comment_or_string])
325
+ end
326
+
327
+ def prepare_parametrized_sql
328
+ @parametrized_sql = @initial_sql.split(/#{SQL_COMMENTS}|#{string_regex}/)
329
+ .each_slice(2).map do |sql_part, comment_or_string|
330
+ # remove additional formatting for sql_parts and replace comment and strings with a guids
331
+ [sql_part.gsub(/[\s]+/, " "), add_string_or_comment(comment_or_string)]
332
+ end.flatten.compact.join("")
333
+ end
334
+
335
+ def query_split_regex(with_brackets = true)
336
+ if with_brackets
337
+ /(#{KEYWORDS}|#{BRACKETS}|#{literal_and_comments_placeholders_regex})/
338
+ else
339
+ /(#{KEYWORDS}|#{literal_and_comments_placeholders_regex})/
155
340
  end
341
+ end
156
342
 
157
- # clear all spaces before newlines, and all whitespaces before strings endings
158
- sql.tap{ |slf| slf.gsub!( /\s+\n/, "\n" ) }.tap{ |slf| slf.gsub!(/\s+\z/, '') }
343
+ # when comment ending with newline followed by a keyword we should remove double newlines
344
+ def clear_extra_newline_after_comments
345
+ newlined_comments = @literals_and_comments_types.select { |k,| new_line_ending_comment?(k) }
346
+ return if newlined_comments.length == 0
347
+ parametrized_sql.gsub!(/(#{newlined_comments.keys.join("}\n|")}}\n)/, &:chop)
159
348
  end
160
349
 
161
- def prettify_multiple( sql_multi, colorize = true )
162
- sql_multi.split( /(?>#{SQL_COMMENTS})|(\;)/ ).inject(['']) { |queries, pattern|
163
- queries.last << pattern
164
- queries << '' if pattern == ';'
165
- queries
166
- }.map!{ |sql|
167
- # we were splitting by comments and ;, so if next sql start with comment we've got a misplaced \n\n
168
- sql.match?(/\A\s+\z/) ? nil : prettify_sql( sql, colorize )
169
- }.compact.join("\n\n")
350
+ def colorize_query
351
+ parametrized_sql.gsub!(query_split_regex(false)) do |matched_part|
352
+ if literals_and_comments_types[matched_part]
353
+ colorize_comment_or_literal(matched_part)
354
+ matched_part
355
+ else
356
+ StringColorize.colorize_keyword(matched_part)
357
+ end
358
+ end
170
359
  end
171
360
 
172
- private_class_method
173
- def indent_multiline( verb, indent )
174
- if verb.match?(/.\s*\n\s*./)
175
- verb.lines.map!{|ln| ln.prepend(' ' * indent)}.join("\n")
361
+ def indent_parametrized_part(matched_typed_id, indent, indent_after_comment, start_with_newline = true)
362
+ case @literals_and_comments_types[matched_typed_id]
363
+ # technically we will not get here, since this types of literals/comments are not indentable
364
+ when :inline_comment, :immutable_string
365
+ when :indentable_string
366
+ lines = @guids_to_content[matched_typed_id].lines
367
+ @guids_to_content[matched_typed_id] = lines[0] +
368
+ lines[1..-1].map! { |ln| indent_multiline(ln[/'[^']+'/], indent) }.join("\n")
176
369
  else
177
- verb.prepend(' ' * indent)
370
+ content = @guids_to_content[matched_typed_id][COMMENT_CONTENT]
371
+ @guids_to_content[matched_typed_id] = (start_with_newline ? "\n" : "") +
372
+ "#{indent_multiline(content, indent)}\n" +
373
+ (indent_after_comment ? indent_multiline("", indent) : "")
178
374
  end
179
375
  end
180
376
 
181
- def colorize_err_line( line )
182
- line.gsub(/#{VERBS}/ ) { |verb| StringColorize.colorize_verb(verb) }
183
- .gsub(STRINGS) { |str| StringColorize.colorize_str(str) }
377
+ def colorize_comment_or_literal(matched_typed_id)
378
+ @guids_to_content[matched_typed_id] = if comment?(@literals_and_comments_types[matched_typed_id])
379
+ StringColorize.colorize_comment(@guids_to_content[matched_typed_id])
380
+ else
381
+ StringColorize.colorize_str(@guids_to_content[matched_typed_id])
382
+ end
184
383
  end
185
384
 
186
- def extract_err_caret_line( err_address_line, err_line, sql_body, err )
187
- # LINE could be quoted ( both sides and sometimes only from one ):
188
- # "LINE 1: ...t_id\" = $13 AND \"products\".\"carrier_id\" = $14 AND \"product_t...\n",
189
- err_quote = (err_address_line.match(/\.\.\.(.+)\.\.\./) || err_address_line.match(/\.\.\.(.+)/) )&.send(:[], 1)
385
+ def get_placeholder_type(comment_or_string)
386
+ if SQL_COMMENTS.match?(comment_or_string)
387
+ get_comment_type(comment_or_string)
388
+ else
389
+ get_string_type(comment_or_string)
390
+ end
391
+ end
190
392
 
191
- # line[2] is original err caret line i.e.: ' ^'
192
- # err_address_line[/LINE \d+:/].length+1..-1 - is a position from error quote begin
193
- err_caret_line = err.lines[2][err_address_line[/LINE \d+:/].length+1..-1]
393
+ def get_comment_type(comment)
394
+ case comment
395
+ when /\s*\n+\s*.+\s*\n+\s*/ then :newline_wrapped_comment
396
+ when /\s*\n+\s*.+/ then :newline_start_comment
397
+ when /.+\s*\n+\s*/ then :newline_end_comment
398
+ else :inline_comment
399
+ end
400
+ end
194
401
 
195
- # when err line is too long postgres quotes it in double '...'
196
- # so we need to reposition caret against original line
197
- if err_quote
198
- err_quote_caret_offset = err_caret_line.length - err_address_line.index( '...' ).to_i + 3
199
- err_caret_line = ' ' * ( err_line.index( err_quote ) + err_quote_caret_offset ) + "^\n"
402
+ def get_string_type(string)
403
+ MULTILINE_INDENTABLE_LITERAL.match?(string) ? :indentable_string : :immutable_string
404
+ end
405
+
406
+ def new_placeholder_name(placeholder_type)
407
+ "#{placeholder_type}_#{@counter[placeholder_type]}_#{SecureRandom.uuid}"
408
+ end
409
+
410
+ def get_sql_named_strs(sql)
411
+ freq = Hash.new(0)
412
+ sql.scan(NAMED_DOLLAR_QUOTED_STRINGS_REGEX).select do |str|
413
+ freq[str] += 1
414
+ freq[str] == 2
200
415
  end
416
+ .flatten
417
+ .map { |str| str.gsub!("$", '\$') }
418
+ end
201
419
 
202
- # older versions of ActiveRecord were adding ': ' before an original query :(
203
- err_caret_line.prepend(' ') if sql_body[0].start_with?(': ')
204
- # if mistake is on last string than err_line.last != \n then we need to prepend \n to caret line
205
- err_caret_line.prepend("\n") unless err_line[-1] == "\n"
206
- err_caret_line
420
+ def init_strings_regex
421
+ # /($STR$.+$STR$|$$[^$]$$|'[^']'|"[^"]")/
422
+ strs = get_sql_named_strs(initial_sql).map { |dq_str| "#{dq_str}.+#{dq_str}" }
423
+ strs = ["(#{strs.join("|")})"] if strs != []
424
+ @string_regex ||= /#{[*strs, DOLLAR_QUOTED_STRINGS, STRINGS].join("|")}/m
425
+ end
426
+
427
+ def comment?(piece_type)
428
+ !literal?(piece_type)
429
+ end
430
+
431
+ def literal?(piece_type)
432
+ [:indentable_string, :immutable_string].include?(piece_type)
433
+ end
434
+
435
+ def new_line_ending_comment?(comment_or_literal)
436
+ [:newline_wrapped_comment, :newline_end_comment, :newline_start_comment]
437
+ .include?(@literals_and_comments_types[comment_or_literal])
438
+ end
439
+
440
+ def indent_multiline(keyword, indent)
441
+ if keyword.match?(/.\s*\n\s*./)
442
+ keyword.lines.map! { |ln| " " * indent + ln }.join("")
443
+ else
444
+ " " * indent + keyword
445
+ end
207
446
  end
208
447
  end
209
448
  end
@@ -217,8 +456,13 @@ module Niceql
217
456
  end
218
457
  end
219
458
 
220
- def self.configure; yield( config ) end
221
-
222
- def self.config; @config ||= NiceQLConfig.new end
459
+ class << self
460
+ def configure
461
+ yield(config)
462
+ end
223
463
 
464
+ def config
465
+ @config ||= NiceQLConfig.new
466
+ end
467
+ end
224
468
  end
data/niceql.gemspec CHANGED
@@ -1,4 +1,6 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
3
+
2
4
  lib = File.expand_path("../lib", __FILE__)
3
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
6
  require "niceql/version"
@@ -9,8 +11,12 @@ Gem::Specification.new do |spec|
9
11
  spec.authors = ["alekseyl"]
10
12
  spec.email = ["leshchuk@gmail.com"]
11
13
 
12
- spec.summary = %q{This is a simple and nice gem for SQL prettifying and formatting. Niceql splits, indent and colorize SQL query and PG errors if any. }
13
- spec.description = %q{This is a simple and nice gem for SQL prettifying and formatting. Niceql splits, indent and colorize SQL query and PG errors if any. Could be used as a standalone gem without any dependencies. Seamless ActiveRecord integration via rails_sql_prettifier gem. }
14
+ spec.summary = "This is a simple and nice gem for SQL prettifying and formatting. "\
15
+ "Niceql splits, indent and colorize SQL query and PG errors if any. "
16
+ spec.description = "This is a simple and nice gem for SQL prettifying and formatting. "\
17
+ "Niceql splits, indent and colorize SQL query and PG errors if any. "\
18
+ "Could be used as a standalone gem without any dependencies. "\
19
+ "Seamless ActiveRecord integration via rails_sql_prettifier gem. "
14
20
  spec.homepage = "https://github.com/alekseyl/niceql"
15
21
  spec.license = "MIT"
16
22
 
@@ -23,21 +29,23 @@ Gem::Specification.new do |spec|
23
29
  "public gem pushes."
24
30
  end
25
31
 
26
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
32
+ spec.files = %x(git ls-files -z).split("\x0").reject do |f|
27
33
  f.match(%r{^(test|spec|features)/})
28
34
  end
29
35
  spec.bindir = "exe"
30
36
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
37
  spec.require_paths = ["lib"]
32
38
 
33
- spec.required_ruby_version = '>= 2.4'
39
+ spec.required_ruby_version = ">= 2.5"
34
40
 
35
- spec.add_development_dependency "bundler", ">= 1"
36
- spec.add_development_dependency "rake", ">= 12.3.3"
37
- spec.add_development_dependency "minitest", "~> 5.0"
41
+ spec.add_development_dependency("awesome_print")
42
+ spec.add_development_dependency("bundler", ">= 1")
43
+ spec.add_development_dependency("minitest", "~> 5.0")
44
+ spec.add_development_dependency("rake", ">= 12.3.3")
45
+ spec.add_development_dependency("rubocop-shopify", "~> 2.0")
38
46
 
39
- spec.add_development_dependency "differ"
40
- spec.add_development_dependency "pry-byebug"
41
- spec.add_development_dependency "benchmark-ips"
42
- spec.add_development_dependency 'sqlite3'
47
+ spec.add_development_dependency("benchmark-ips")
48
+ spec.add_development_dependency("differ")
49
+ spec.add_development_dependency("pry-byebug")
50
+ spec.add_development_dependency("sqlite3")
43
51
  end