niceql 0.1.18 → 0.1.20

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b31c9be70eddb5f04784ebbf6fab44e3bb1cc85
4
- data.tar.gz: 96f8941c36a0cad00359bfedf8ec6d4cca4ec4f9
3
+ metadata.gz: 321c779b1a04b51acd414e9187733f8b50a7ab3d
4
+ data.tar.gz: 076c67bbf5825fc79df1c49fc99cf75c3e0a4a23
5
5
  SHA512:
6
- metadata.gz: b98310cb79e34175268cf09e966c27abdb76ecf8ab4e7ad5e66751f7198d397bdae7df8c667181f196ebb04f1046c288b5b8a83e2c5426ce43edb5a5a14e87f7
7
- data.tar.gz: e1fc4c8d3277c1961b13e85c14181b7cca4e326909bcb289953156156bb2f3202a343c05937c23102f0aade9b96bb5381cd5b786b48f13054e01bd672f80c337
6
+ metadata.gz: 8bf9b7cc77e345ea167f2e5bc72ca9628f880641d01cf99f3f18d97ef030f3be1e74112763523ec1699527be2513ad763e043c634c5528a13850e6bf15fec1d1
7
+ data.tar.gz: 8921f764d6ce823ac4e08143ce45e6c3b1b039163b4adb14e6f809694f10e6e20c089fde455619d6db3c56fe6bcf59ee7e2c949ebf3685c8f4ed273926ce6500
data/.travis.yml CHANGED
@@ -3,3 +3,4 @@ language: ruby
3
3
  rvm:
4
4
  - 2.4.4
5
5
  - 2.5.1
6
+ - 2.3.7
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ # 0.1.20
2
+ * Add respect for SQL comments single lined, multi lined, and inline
3
+
4
+ # 0.1.19
5
+ * add prettify_pg_errors to config - now pg errors prettified output is configurable,
6
+ default is true if ActiveRecord::Base defined and db adapter is pg
7
+
8
+ * tests for error prettifying
9
+
1
10
  # 0.1.18
2
11
  * add color to logger output
3
12
 
data/README.md CHANGED
@@ -50,9 +50,13 @@ Niceql.configure do |c|
50
50
  # c.pg_adapter_with_nicesql = Rails.env.development?
51
51
 
52
52
  # uncomment next string if you want to log prettified SQL inside ActiveRecord logging.
53
- # Default = false
53
+ # default: false
54
54
  # c.prettify_active_record_log_output = true
55
55
 
56
+ # now error prettifying is configurable
57
+ # default: defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
58
+ # c.prettify_pg_errors = defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
59
+
56
60
  # spaces count for one indentation
57
61
  c.indentation_base = 2
58
62
 
@@ -97,10 +101,11 @@ end
97
101
  #=> SELECT *
98
102
  #=> FROM ( VALUES(1), (2) ) AS tmp
99
103
 
100
- # rails combines err with query, so don't forget to do it yourself
101
- # to get real nice result you should executeprettified version (i.e. execute( prettified_sql ) !) of query on your DB!
102
- # otherwise you will not get such a nice output
104
+ # rails combines err with query, so don't forget to do it yourself:
105
+ puts Niceql::Prettifier.prettify_pg_err( "#{pg_err_output}\n#{sql_query}" )
103
106
 
107
+ # to get real nice result you should execute prettified version (i.e. execute( prettified_sql ) !) of query on your DB!
108
+ # otherwise you will not get such a nice output
104
109
  puts Niceql::Prettifier.prettify_pg_err(<<-ERR )
105
110
  ERROR: VALUES in FROM must have an alias
106
111
  LINE 2: FROM ( VALUES(1), (2) )
@@ -1,3 +1,3 @@
1
1
  module Niceql
2
- VERSION = "0.1.18"
2
+ VERSION = "0.1.20"
3
3
  end
data/lib/niceql.rb CHANGED
@@ -39,7 +39,10 @@ module Niceql
39
39
  VERBS = "#{NEW_LINE_VERBS}|#{INLINE_VERBS}"
40
40
  STRINGS = /("[^"]+")|('[^']+')/
41
41
  BRACKETS = '[\(\)]'
42
-
42
+ SQL_COMMENTS = /(\s*?--.+\s*)|(\s*?\/\*[^\/\*]*\*\/\s*)/
43
+ # only newlined comments will be matched
44
+ SQL_COMMENTS_CLEARED = /(\s*?--.+\s{1})|(\s*$\s*\/\*[^\/\*]*\*\/\s{1})/
45
+ COMMENT_CONTENT = /[\S]+[\s\S]*[\S]+/
43
46
 
44
47
  def self.config
45
48
  Niceql.config
@@ -51,34 +54,62 @@ module Niceql
51
54
  end
52
55
 
53
56
 
54
- def self.prettify_pg_err(err)
57
+ # Postgres error output:
58
+ # ERROR: VALUES in FROM must have an alias
59
+ # LINE 2: FROM ( VALUES(1), (2) );
60
+ # ^
61
+ # HINT: For example, FROM (VALUES ...) [AS] foo.
62
+
63
+ # May go without HINT or DETAIL:
64
+ # ERROR: column "usr" does not exist
65
+ # LINE 1: SELECT usr FROM users ORDER BY 1
66
+ # ^
67
+
68
+ # ActiveRecord::StatementInvalid will add original SQL query to the bottom like this:
69
+ # ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "usr" does not exist
70
+ # LINE 1: SELECT usr FROM users ORDER BY 1
71
+ # ^
72
+ #: SELECT usr FROM users ORDER BY 1
73
+
74
+ # prettify_pg_err parses ActiveRecord::StatementInvalid string,
75
+ # but you may use it without ActiveRecord either way:
76
+ # prettify_pg_err( err + "\n" + sql ) OR prettify_pg_err( err, sql )
77
+ # don't mess with original sql query, or prettify_pg_err will deliver incorrect results
78
+ def self.prettify_pg_err(err, original_sql_query = nil)
55
79
  return err if err[/LINE \d+/].nil?
56
80
  err_line_num = err[/LINE \d+/][5..-1].to_i
57
81
 
82
+ #
58
83
  start_sql_line = err.lines[3][/(HINT|DETAIL)/] ? 4 : 3
59
- err_body = err.lines[start_sql_line..-1]
84
+ err_body = start_sql_line < err.lines.length ? err.lines[start_sql_line..-1] : original_sql_query&.lines
85
+
86
+
87
+ # this means original query is missing so it's nothing to prettify
88
+ return err unless err_body
89
+
60
90
  err_quote = ( err.lines[1][/\.\.\..+\.\.\./] && err.lines[1][/\.\.\..+\.\.\./][3..-4] ) ||
61
91
  ( err.lines[1][/\.\.\..+/] && err.lines[1][/\.\.\..+/][3..-1] )
62
92
 
63
- # line 2 is err carret line
93
+ # line[2] is err carret line i.e.: ' ^'
64
94
  # err.lines[1][/LINE \d+:/].length+1..-1 - is a position from error quote begin
65
95
  err_carret_line = err.lines[2][err.lines[1][/LINE \d+:/].length+1..-1]
66
- # err line painted red completly, so we just remembering it and use
96
+ # err line will be painted in red completely, so we just remembering it and use
67
97
  # to replace after paiting the verbs
68
98
  err_line = err_body[err_line_num-1]
69
99
 
70
- # when err line is too long postgres quotes it part in doble ...
100
+ # when err line is too long postgres quotes it part in double '...'
71
101
  if err_quote
72
102
  err_quote_carret_offset = err_carret_line.length - err.lines[1].index( '...' ) + 3
73
103
  err_carret_line = ' ' * ( err_line.index( err_quote ) + err_quote_carret_offset ) + "^\n"
74
104
  end
75
105
 
106
+ err_carret_line = " " + err_carret_line if err_body[0].start_with?(': ')
76
107
  # if mistake is on last string than err_line.last != \n so we need to prepend \n to carret line
77
- err_carret_line = "\n" + err_carret_line unless err_line.last == "\n"
108
+ err_carret_line = "\n" + err_carret_line unless err_line[-1] == "\n"
78
109
 
79
110
  #colorizing verbs and strings
80
111
  err_body = err_body.join.gsub(/#{VERBS}/ ) { |verb| StringColorize.colorize_verb(verb) }
81
- err_body = err_body.gsub(STRINGS){ |str| StringColorize.colorize_str(str) }
112
+ .gsub(STRINGS){ |str| StringColorize.colorize_str(str) }
82
113
 
83
114
  #reassemling error message
84
115
  err_body = err_body.lines
@@ -92,16 +123,21 @@ module Niceql
92
123
  indent = 0
93
124
  parentness = []
94
125
 
95
- #it's better to remove all new lines because it will break formatting
96
- sql = sql.gsub("\n", ' ')
97
- # remove any additional formatting
98
- sql = sql.gsub(/[\s]+/, ' ')
126
+ #it's better to remove all new lines because it will break formatting + remove any additional formatting with many spaces
127
+ # second map! is add newlines at start and begining for all comments started with new line
128
+ sql = sql.split( SQL_COMMENTS ).each_slice(2).map{ | crmb, cmmnt |
129
+ [crmb.gsub(/[\s]+/, ' '),
130
+ cmmnt && ( cmmnt&.match?(/\A\s*$/) ? "\n" + cmmnt[/[\S]+[\s\S]*[\S]+/] + "\n" : cmmnt[/[\S]+[\s\S]*[\S]+/] ) ]
131
+ }.flatten.join(' ')
132
+
133
+ sql.gsub!(/ \n/, "\n")
134
+
135
+ sql.gsub!(STRINGS){ |str| StringColorize.colorize_str(str) } if colorize
99
136
 
100
- sql = sql.gsub(STRINGS){ |str| StringColorize.colorize_str(str) } if colorize
101
137
  first_verb = true
138
+ previous_was_comment = false
102
139
 
103
- sql.gsub( /(#{VERBS}|#{BRACKETS})/).with_index do |verb, index|
104
- add_new_line = false
140
+ sql.gsub!( /(#{VERBS}|#{BRACKETS}|#{SQL_COMMENTS_CLEARED})/) do |verb|
105
141
  if 'SELECT' == verb
106
142
  indent += config.indentation_base if !config.open_bracket_is_newliner || parentness.last.nil? || parentness.last[:nested]
107
143
  parentness.last[:nested] = true if parentness.last
@@ -124,8 +160,25 @@ module Niceql
124
160
  add_new_line = verb[/(#{INLINE_VERBS})/].nil?
125
161
  end
126
162
  first_verb = false
163
+
164
+ verb = verb[COMMENT_CONTENT] if verb[SQL_COMMENTS_CLEARED]
165
+ # !add_new_line && previous_was_comment means we had newlined comment, and now even
166
+ # if verb is inline verb we will need to add new line with indentation BUT all
167
+ # newliners match with a space before so we need to strip it
168
+ verb.lstrip! if !add_new_line && previous_was_comment
127
169
  verb = StringColorize.colorize_verb(verb) if !['(', ')'].include?(verb) && colorize
128
- add_new_line ? "\n#{' ' * indent}" + verb : verb
170
+ (previous_was_comment || add_new_line ? indent_multiline(verb, indent) : verb).tap{ previous_was_comment = !verb.to_s[SQL_COMMENTS_CLEARED].nil? }
171
+ end
172
+ sql.gsub( /\s+\n/, "\n" ).gsub(/\s+\z/, '')
173
+ end
174
+
175
+ private
176
+ def self.indent_multiline( verb, indent )
177
+ # byebug
178
+ if verb.match?(/.\n./)
179
+ verb.lines.map!{|ln| "\n#{' ' * indent}" + ln}.join
180
+ else
181
+ "\n#{' ' * indent}" + verb.to_s
129
182
  end
130
183
  end
131
184
  end
@@ -140,7 +193,7 @@ module Niceql
140
193
  module AbstractAdapterLogPrettifier
141
194
  def log( sql, *args, &block )
142
195
  # \n need to be placed because AR log will start with action description + time info.
143
- # rescue sql - just to be sure Prettifier didn't break production
196
+ # rescue sql - just to be sure Prettifier wouldn't break production
144
197
  formatted_sql = "\n" + Prettifier.prettify_sql(sql) rescue sql
145
198
  super( formatted_sql, *args, &block )
146
199
  end
@@ -148,8 +201,8 @@ module Niceql
148
201
 
149
202
  module ErrorExt
150
203
  def to_s
151
- if ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
152
- Prettifier.prettify_err( super )
204
+ if Niceql.config.prettify_pg_errors && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
205
+ Prettifier.prettify_err(super)
153
206
  else
154
207
  super
155
208
  end
@@ -160,13 +213,16 @@ module Niceql
160
213
  attr_accessor :pg_adapter_with_nicesql,
161
214
  :indentation_base,
162
215
  :open_bracket_is_newliner,
163
- :prettify_active_record_log_output
216
+ :prettify_active_record_log_output,
217
+ :prettify_pg_errors
218
+
164
219
 
165
220
  def initialize
166
221
  self.pg_adapter_with_nicesql = false
167
222
  self.indentation_base = 2
168
223
  self.open_bracket_is_newliner = false
169
224
  self.prettify_active_record_log_output = false
225
+ self.prettify_pg_errors = defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
170
226
  end
171
227
  end
172
228
 
@@ -179,7 +235,11 @@ module Niceql
179
235
  end
180
236
 
181
237
  if config.prettify_active_record_log_output
182
- ::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(AbstractAdapterLogPrettifier)
238
+ ::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend( AbstractAdapterLogPrettifier )
239
+ end
240
+
241
+ if config.prettify_pg_errors
242
+ ::ActiveRecord::StatementInvalid.include( Niceql::ErrorExt )
183
243
  end
184
244
  end
185
245
 
@@ -187,9 +247,7 @@ module Niceql
187
247
  @config ||= NiceQLConfig.new
188
248
  end
189
249
 
190
-
191
250
  if defined? ::ActiveRecord::Base
192
- ActiveRecord::StatementInvalid.include( Niceql::ErrorExt )
193
251
  ::ActiveRecord::Base.extend ArExtentions
194
252
  [::ActiveRecord::Relation, ::ActiveRecord::Associations::CollectionProxy].each { |klass| klass.send(:include, ArExtentions) }
195
253
  end
data/niceql.gemspec CHANGED
@@ -34,5 +34,6 @@ Gem::Specification.new do |spec|
34
34
  spec.add_development_dependency "rake", "~> 10.0"
35
35
  spec.add_development_dependency "minitest", "~> 5.0"
36
36
 
37
- spec.add_development_dependency 'differ'
37
+ spec.add_development_dependency "differ"
38
+ spec.add_development_dependency "pry-byebug"
38
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: niceql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.18
4
+ version: 0.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - alekseyl
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-30 00:00:00.000000000 Z
11
+ date: 2018-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: 'This is simple and nice sql prettifier, it splits, indent and colorize
70
84
  SQL query and PG error if any '
71
85
  email: