niceql 0.3.1 → 0.5.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/README.md +19 -15
- data/lib/niceql/version.rb +1 -1
- data/lib/niceql.rb +26 -99
- data/niceql.gemspec +2 -3
- metadata +9 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 126874d18a0ec3ce725d5f62a63a0cc57012cee9d5268eb189f060d2d1e0b6d0
|
4
|
+
data.tar.gz: '090a0208059e848eda15e721857238c9916a97527e75b672e5216c30b3745ed9'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 400ee6d267eb95de34d1c1e018fceaab42dac6054f08d4bbe90c45c4ea7c366855c45da67b0f3d963d880893e21cbcb70117aa46f7ffe3aff8eacce1aa5d8f75
|
7
|
+
data.tar.gz: 7417f1ddf85cb2959f8eb59a6997154f721dc7696ca66caf60d16b095f7390e17233915680fa176b455a761b135582b76c085ae92b0d89fb53d6a2043c36fc1e
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
# 0.5.1
|
2
|
+
* No features just some code refactoring
|
3
|
+
|
4
|
+
# 0.5.0
|
5
|
+
* BREAKING CHANGE! ActiveRecord compatibility extracted to the rails_sql_prettifier gem!
|
6
|
+
If you need niceql funcitonality with rails / active_record plz include rails_sql_prettifier has a
|
7
|
+
a versioning aligned to the active_record versions and has same DSL for ActiveRecord the niceql was providing prior.
|
8
|
+
|
9
|
+
# 0.4.1
|
10
|
+
* description update
|
11
|
+
|
12
|
+
# 0.4.0
|
13
|
+
* merged PR https://github.com/alekseyl/niceql/pull/19, now Arel is also extended with niceql methods!!
|
14
|
+
* test and better niceql comparisons assertion
|
15
|
+
* tests were trialed against rails 4.2 and some additional conditions were added for later cases
|
16
|
+
|
1
17
|
# 0.3.0
|
2
18
|
* ruby forced to >= 2.4
|
3
19
|
* String match extension no longer needed
|
data/README.md
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
# Niceql
|
2
2
|
|
3
|
-
|
3
|
+
ATTENTION: Since ver 0.5.0 the ActiveRecord integration will be provided via standalone gem: [rails_sql_prettifier](https://github.com/alekseyl/rails_sql_prettifier)!
|
4
|
+
|
5
|
+
This is a small, nice, simple and zero dependency solution for SQL prettifying for Ruby.
|
4
6
|
It can be used in an irb console without any dependencies ( run bin/console and look for examples ).
|
5
7
|
|
6
|
-
Any reasonable suggestions
|
8
|
+
Any reasonable suggestions are welcome.
|
7
9
|
|
8
|
-
**Please pay attention: untill issue https://github.com/alekseyl/niceql/issues/16 is resolved any UPDATE or INSERT request
|
10
|
+
**Please pay attention: untill issue https://github.com/alekseyl/niceql/issues/16 is resolved any UPDATE or INSERT request might corrupt your data, don't use on production!**
|
9
11
|
|
10
12
|
|
11
13
|
## Before/After
|
@@ -44,9 +46,11 @@ Or install it yourself as:
|
|
44
46
|
```ruby
|
45
47
|
Niceql.configure do |c|
|
46
48
|
# Setting pg_adapter_with_nicesql to true will force formatting SQL queries
|
47
|
-
# before
|
49
|
+
# before execution. Formatted SQL will lead to much better SQL-query debugging and much more clearer error messages
|
48
50
|
# if you are using Postgresql as a data source.
|
51
|
+
#
|
49
52
|
# You can adjust pg_adapter in production but do it at your own risk!
|
53
|
+
#
|
50
54
|
# If you need to debug SQL queries in production use exec_niceql
|
51
55
|
# default: false
|
52
56
|
# uncomment next string to enable in development
|
@@ -56,7 +60,7 @@ Niceql.configure do |c|
|
|
56
60
|
# default: false
|
57
61
|
# c.prettify_active_record_log_output = true
|
58
62
|
|
59
|
-
#
|
63
|
+
# Error prettifying is also configurable
|
60
64
|
# default: defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
|
61
65
|
# c.prettify_pg_errors = defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
|
62
66
|
|
@@ -85,7 +89,7 @@ end
|
|
85
89
|
### With ActiveRecord
|
86
90
|
|
87
91
|
```ruby
|
88
|
-
# puts colorized
|
92
|
+
# puts colorized and formatted corresponding SQL query
|
89
93
|
Model.scope.niceql
|
90
94
|
|
91
95
|
# only formatting without colorization, you can run output of to_niceql as a SQL query in connection.execute
|
@@ -110,23 +114,23 @@ end
|
|
110
114
|
#=>
|
111
115
|
#=> SELECT *
|
112
116
|
#=> FROM table
|
113
|
-
|
114
|
-
|
115
|
-
|
117
|
+
|
116
118
|
|
117
|
-
|
118
|
-
puts Niceql::Prettifier.prettify_pg_err( "#{pg_err_output}\n#{sql_query}" )
|
119
|
+
puts Niceql::Prettifier.prettify_pg_err( pg_err_output, sql_query )
|
119
120
|
|
120
121
|
# to get real nice result you should execute prettified version (i.e. execute( prettified_sql ) !) of query on your DB!
|
121
122
|
# otherwise you will not get such a nice output
|
122
|
-
|
123
|
+
raw_sql = <<~SQL
|
124
|
+
SELECT err
|
125
|
+
FROM ( VALUES(1), (2) )
|
126
|
+
ORDER BY 1
|
127
|
+
SQL
|
128
|
+
|
129
|
+
puts Niceql::Prettifier.prettify_pg_err(<<~ERR, raw_sql )
|
123
130
|
ERROR: VALUES in FROM must have an alias
|
124
131
|
LINE 2: FROM ( VALUES(1), (2) )
|
125
132
|
^
|
126
133
|
HINT: For example, FROM (VALUES ...) [AS] foo.
|
127
|
-
SELECT err
|
128
|
-
FROM ( VALUES(1), (2) )
|
129
|
-
ORDER BY 1
|
130
134
|
ERR
|
131
135
|
|
132
136
|
|
data/lib/niceql/version.rb
CHANGED
data/lib/niceql.rb
CHANGED
@@ -4,34 +4,19 @@ module Niceql
|
|
4
4
|
|
5
5
|
module StringColorize
|
6
6
|
def self.colorize_verb( str)
|
7
|
-
#yellow ANSI color
|
7
|
+
# yellow ANSI color
|
8
8
|
"\e[0;33;49m#{str}\e[0m"
|
9
9
|
end
|
10
10
|
def self.colorize_str(str)
|
11
|
-
#cyan ANSI color
|
11
|
+
# cyan ANSI color
|
12
12
|
"\e[0;36;49m#{str}\e[0m"
|
13
13
|
end
|
14
14
|
def self.colorize_err(err)
|
15
|
-
#red ANSI color
|
15
|
+
# red ANSI color
|
16
16
|
"\e[0;31;49m#{err}\e[0m"
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
module ArExtentions
|
21
|
-
def exec_niceql
|
22
|
-
connection.execute( to_niceql )
|
23
|
-
end
|
24
|
-
|
25
|
-
def to_niceql
|
26
|
-
Prettifier.prettify_sql(to_sql, false)
|
27
|
-
end
|
28
|
-
|
29
|
-
def niceql( colorize = true )
|
30
|
-
puts Prettifier.prettify_sql( to_sql, colorize )
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
20
|
module Prettifier
|
36
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('| ')
|
37
22
|
NEW_LINE_VERBS = 'SELECT|FROM|WHERE|CASE|ORDER BY|LIMIT|GROUP BY|(RIGHT |LEFT )*(INNER |OUTER )*JOIN( LATERAL)*|HAVING|OFFSET|UPDATE'
|
@@ -77,36 +62,31 @@ module Niceql
|
|
77
62
|
# don't mess with original sql query, or prettify_pg_err will deliver incorrect results
|
78
63
|
def prettify_pg_err(err, original_sql_query = nil)
|
79
64
|
return err if err[/LINE \d+/].nil?
|
80
|
-
err_line_num =
|
65
|
+
# LINE 2: ... -> err_line_num = 2
|
66
|
+
err_line_num = err.match(/LINE (\d+):/)[1].to_i
|
81
67
|
# LINE 1: SELECT usr FROM users ORDER BY 1
|
82
68
|
err_address_line = err.lines[1]
|
83
69
|
|
84
|
-
|
70
|
+
sql_start_line_num = 3 if err.lines.length <= 3
|
85
71
|
# error not always contains HINT
|
86
|
-
|
87
|
-
|
72
|
+
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
|
88
74
|
|
89
75
|
# this means original query is missing so it's nothing to prettify
|
90
|
-
return err unless
|
91
|
-
|
92
|
-
# err line will be painted in red completely, so we just remembering it and use
|
93
|
-
# to replace after painting the verbs
|
94
|
-
err_line = sql_body[err_line_num - 1]
|
76
|
+
return err unless sql_body_lines
|
95
77
|
|
78
|
+
# this is an SQL line with an error.
|
79
|
+
# we need err_line to properly align the caret in the caret line
|
80
|
+
# and to apply a full red colorizing schema on an SQL line with error
|
81
|
+
err_line = sql_body_lines[err_line_num - 1]
|
96
82
|
|
97
|
-
#colorizing verbs and
|
98
|
-
|
99
|
-
.gsub(STRINGS){ |str| StringColorize.colorize_str(str) }
|
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) }
|
100
85
|
|
101
|
-
|
102
|
-
err_body = colorized_sql_body.lines
|
103
|
-
# replacing colorized line contained error and adding caret line
|
104
|
-
err_body[err_line_num - 1]= StringColorize.colorize_err( err_line )
|
105
|
-
|
106
|
-
err_caret_line = extract_err_caret_line( err_address_line, err_line, sql_body, err )
|
86
|
+
err_caret_line = extract_err_caret_line( err_address_line, err_line, sql_body_lines, err )
|
107
87
|
err_body.insert( err_line_num, StringColorize.colorize_err( err_caret_line ) )
|
108
88
|
|
109
|
-
err.lines[0..
|
89
|
+
err.lines[0..sql_start_line_num-1].join + err_body.join
|
110
90
|
end
|
111
91
|
|
112
92
|
def prettify_sql( sql, colorize = true )
|
@@ -198,10 +178,15 @@ module Niceql
|
|
198
178
|
end
|
199
179
|
end
|
200
180
|
|
181
|
+
def colorize_err_line( line )
|
182
|
+
line.gsub(/#{VERBS}/ ) { |verb| StringColorize.colorize_verb(verb) }
|
183
|
+
.gsub(STRINGS) { |str| StringColorize.colorize_str(str) }
|
184
|
+
end
|
185
|
+
|
201
186
|
def extract_err_caret_line( err_address_line, err_line, sql_body, err )
|
202
187
|
# LINE could be quoted ( both sides and sometimes only from one ):
|
203
188
|
# "LINE 1: ...t_id\" = $13 AND \"products\".\"carrier_id\" = $14 AND \"product_t...\n",
|
204
|
-
err_quote = (err_address_line.match(/\.\.\.(.+)\.\.\./) || err_address_line.match(/\.\.\.(.+)/) )
|
189
|
+
err_quote = (err_address_line.match(/\.\.\.(.+)\.\.\./) || err_address_line.match(/\.\.\.(.+)/) )&.send(:[], 1)
|
205
190
|
|
206
191
|
# line[2] is original err caret line i.e.: ' ^'
|
207
192
|
# err_address_line[/LINE \d+:/].length+1..-1 - is a position from error quote begin
|
@@ -223,75 +208,17 @@ module Niceql
|
|
223
208
|
end
|
224
209
|
end
|
225
210
|
|
226
|
-
module PostgresAdapterNiceQL
|
227
|
-
def exec_query(sql, name = "SQL", binds = [], prepare: false)
|
228
|
-
# replacing sql with prettified sql, thats all
|
229
|
-
super( Prettifier.prettify_sql(sql, false), name, binds, prepare: prepare )
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
module AbstractAdapterLogPrettifier
|
234
|
-
def log( sql, *args, &block )
|
235
|
-
# \n need to be placed because AR log will start with action description + time info.
|
236
|
-
# rescue sql - just to be sure Prettifier wouldn't break production
|
237
|
-
formatted_sql = "\n" + Prettifier.prettify_sql(sql) rescue sql
|
238
|
-
super( formatted_sql, *args, &block )
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
module ErrorExt
|
243
|
-
def to_s
|
244
|
-
# older rails version do not provide sql as a standalone query, instead they
|
245
|
-
# deliver joined message
|
246
|
-
Niceql.config.prettify_pg_errors ? Prettifier.prettify_err(super, try(:sql) ) : super
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
211
|
class NiceQLConfig
|
251
|
-
|
252
|
-
return false unless defined?(::ActiveRecord::Base)
|
253
|
-
|
254
|
-
adapter = ActiveRecord::Base.try(:connection_db_config).try(:adapter) ||
|
255
|
-
ActiveRecord::Base.try(:connection_config)&.with_indifferent_access&.dig(:adapter)
|
256
|
-
|
257
|
-
adapter == 'postgresql'
|
258
|
-
end
|
259
|
-
|
260
|
-
attr_accessor :pg_adapter_with_nicesql,
|
261
|
-
:indentation_base,
|
262
|
-
:open_bracket_is_newliner,
|
263
|
-
:prettify_active_record_log_output,
|
264
|
-
:prettify_pg_errors
|
265
|
-
|
212
|
+
attr_accessor :indentation_base, :open_bracket_is_newliner
|
266
213
|
|
267
214
|
def initialize
|
268
|
-
self.pg_adapter_with_nicesql = false
|
269
215
|
self.indentation_base = 2
|
270
216
|
self.open_bracket_is_newliner = false
|
271
|
-
self.prettify_active_record_log_output = false
|
272
|
-
self.prettify_pg_errors = ar_using_pg_adapter?
|
273
217
|
end
|
274
218
|
end
|
275
219
|
|
276
|
-
def self.configure
|
277
|
-
yield( config )
|
220
|
+
def self.configure; yield( config ) end
|
278
221
|
|
279
|
-
|
280
|
-
|
281
|
-
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(PostgresAdapterNiceQL) if config.pg_adapter_with_nicesql
|
282
|
-
|
283
|
-
::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend( AbstractAdapterLogPrettifier ) if config.prettify_active_record_log_output
|
284
|
-
|
285
|
-
::ActiveRecord::StatementInvalid.include( Niceql::ErrorExt ) if config.prettify_pg_errors && config.ar_using_pg_adapter?
|
286
|
-
end
|
287
|
-
|
288
|
-
def self.config
|
289
|
-
@config ||= NiceQLConfig.new
|
290
|
-
end
|
291
|
-
|
292
|
-
if defined? ::ActiveRecord::Base
|
293
|
-
::ActiveRecord::Base.extend ArExtentions
|
294
|
-
[::ActiveRecord::Relation, ::ActiveRecord::Associations::CollectionProxy].each { |klass| klass.send(:include, ArExtentions) }
|
295
|
-
end
|
222
|
+
def self.config; @config ||= NiceQLConfig.new end
|
296
223
|
|
297
224
|
end
|
data/niceql.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["alekseyl"]
|
10
10
|
spec.email = ["leshchuk@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{This is simple and nice gem for
|
13
|
-
spec.description = %q{This is simple and nice gem for
|
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
14
|
spec.homepage = "https://github.com/alekseyl/niceql"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
@@ -31,7 +31,6 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.require_paths = ["lib"]
|
32
32
|
|
33
33
|
spec.required_ruby_version = '>= 2.4'
|
34
|
-
spec.add_development_dependency "activerecord", ">= 6.1"
|
35
34
|
|
36
35
|
spec.add_development_dependency "bundler", ">= 1"
|
37
36
|
spec.add_development_dependency "rake", ">= 12.3.3"
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: niceql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alekseyl
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: activerecord
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '6.1'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '6.1'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: bundler
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,9 +108,10 @@ dependencies:
|
|
122
108
|
- - ">="
|
123
109
|
- !ruby/object:Gem::Version
|
124
110
|
version: '0'
|
125
|
-
description: 'This is simple and nice gem for
|
126
|
-
splits, indent and colorize SQL query and PG errors if any.
|
127
|
-
integration
|
111
|
+
description: 'This is a simple and nice gem for SQL prettifying and formatting. Niceql
|
112
|
+
splits, indent and colorize SQL query and PG errors if any. Could be used as a standalone
|
113
|
+
gem without any dependencies. Seamless ActiveRecord integration via rails_sql_prettifier
|
114
|
+
gem. '
|
128
115
|
email:
|
129
116
|
- leshchuk@gmail.com
|
130
117
|
executables: []
|
@@ -171,9 +158,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
158
|
- !ruby/object:Gem::Version
|
172
159
|
version: '0'
|
173
160
|
requirements: []
|
174
|
-
rubygems_version: 3.
|
161
|
+
rubygems_version: 3.0.9
|
175
162
|
signing_key:
|
176
163
|
specification_version: 4
|
177
|
-
summary: This is simple and nice gem for
|
178
|
-
indent and colorize SQL query and PG errors if any
|
164
|
+
summary: This is a simple and nice gem for SQL prettifying and formatting. Niceql
|
165
|
+
splits, indent and colorize SQL query and PG errors if any.
|
179
166
|
test_files: []
|