niceql 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +2 -4
- data/README.md +35 -95
- data/err_now.png +0 -0
- data/err_was.png +0 -0
- data/lib/niceql/version.rb +1 -1
- data/lib/niceql.rb +33 -207
- data/niceql.gemspec +2 -6
- data/to_niceql.png +0 -0
- metadata +16 -65
- data/CHANGELOG.md +0 -44
- data/lib/benchmark/cat.rb +0 -34
- data/lib/benchmark/gsub.rb +0 -34
- data/lib/benchmark/txt +0 -748
- data/lib/generators/niceql/install_generator.rb +0 -14
- data/lib/generators/templates/niceql_initializer.rb +0 -11
- data/lib/niceql/string.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2efa447fea2f15e044d0a45b3ded037e4c217fbd
|
4
|
+
data.tar.gz: 5dab381173c89ee753d0acd16e7188fdf0e9f9d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49542e72e28b3a823306092b2bdb86eca0de2ae88cb74422c694aa7c92fa010d67e35f7feacc03689dfb89e8176c6412028a3ada6620f5c3926f587fc123067d
|
7
|
+
data.tar.gz: d124bc68f6699adc1ef8ce1f5efa8ae3372023e835a62753465849ba2e0b5bab5809601aca295e8f40aa79b7f050636c088c2c9c326a04ba10e1245e685e54eb
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,13 +1,10 @@
|
|
1
1
|
# Niceql
|
2
2
|
|
3
|
-
This is
|
4
|
-
It can be used in
|
3
|
+
This is small, nice, simple and dependentless solution for SQL prettifiyng for Ruby.
|
4
|
+
It can be used in irb console without any dependencies ( run ./console from bin and look for examples ).
|
5
5
|
|
6
6
|
Any reasonable suggestions on formatting/coloring are welcome
|
7
7
|
|
8
|
-
**Please pay attention: untill issue https://github.com/alekseyl/niceql/issues/16 is resolved any UPDATE or INSERT request will corrupt your data, don't use on production!**
|
9
|
-
|
10
|
-
|
11
8
|
## Before/After
|
12
9
|
### SQL prettifier:
|
13
10
|
![alt text](https://github.com/alekseyl/niceql/raw/master/to_niceql.png "To_niceql")
|
@@ -32,123 +29,66 @@ gem 'niceql'
|
|
32
29
|
And then execute:
|
33
30
|
|
34
31
|
$ bundle
|
35
|
-
# if you are using rails, you may want to install niceql config:
|
36
|
-
rails g niceql:install
|
37
32
|
|
38
33
|
Or install it yourself as:
|
39
34
|
|
40
35
|
$ gem install niceql
|
41
36
|
|
42
|
-
## Configuration
|
43
|
-
|
44
|
-
```ruby
|
45
|
-
Niceql.configure do |c|
|
46
|
-
# Setting pg_adapter_with_nicesql to true will force formatting SQL queries
|
47
|
-
# before executing them, this will lead to better SQL-query debugging and much more clearer error messages
|
48
|
-
# if you are using Postgresql as a data source.
|
49
|
-
# You can adjust pg_adapter in production but do it at your own risk!
|
50
|
-
# If you need to debug SQL queries in production use exec_niceql
|
51
|
-
# default: false
|
52
|
-
# uncomment next string to enable in development
|
53
|
-
# c.pg_adapter_with_nicesql = Rails.env.development?
|
54
|
-
|
55
|
-
# uncomment next string if you want to log prettified SQL inside ActiveRecord logging.
|
56
|
-
# default: false
|
57
|
-
# c.prettify_active_record_log_output = true
|
58
|
-
|
59
|
-
# now error prettifying is configurable
|
60
|
-
# default: defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
|
61
|
-
# c.prettify_pg_errors = defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
|
62
|
-
|
63
|
-
# spaces count for one indentation
|
64
|
-
c.indentation_base = 2
|
65
|
-
|
66
|
-
# setting open_bracket_is_newliner to true will start opening brackets '(' with nested subqueries from new line
|
67
|
-
# i.e. SELECT * FROM ( SELECT * FROM tags ) tags; will transform to:
|
68
|
-
# SELECT *
|
69
|
-
# FROM
|
70
|
-
# (
|
71
|
-
# SELECT * FROM tags
|
72
|
-
# ) tags;
|
73
|
-
# when open_bracket_is_newliner is false:
|
74
|
-
# SELECT *
|
75
|
-
# FROM (
|
76
|
-
# SELECT * FROM tags
|
77
|
-
# ) tags;
|
78
|
-
# default: false
|
79
|
-
c.open_bracket_is_newliner = false
|
80
|
-
end
|
81
|
-
```
|
82
|
-
|
83
37
|
## Usage
|
84
38
|
|
85
39
|
### With ActiveRecord
|
86
40
|
|
87
41
|
```ruby
|
88
|
-
# puts
|
89
|
-
Model.scope.
|
42
|
+
# puts prettified to_sql ( you need to call puts otherwise to_niceql looks ugly )
|
43
|
+
Model.scope.puts_niceql
|
90
44
|
|
91
|
-
# only formatting without colorization
|
45
|
+
# only formatting without colorization can run as SQL query in connection.execute
|
92
46
|
Model.scope.to_niceql
|
93
47
|
|
94
|
-
# prettify PG errors
|
95
|
-
Model.scope_with_err.
|
48
|
+
# prettify PG errors
|
49
|
+
Model.scope_with_err.explain_err
|
96
50
|
```
|
97
51
|
|
98
52
|
### Without ActiveRecord
|
99
53
|
|
100
54
|
```ruby
|
55
|
+
puts Niceql::Prettifier.prettify_sql("SELECT * FROM ( VALUES(1), (2) ) AS tmp")
|
101
56
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
#=> FROM ( VALUES(1), (2) ) AS tmp;
|
110
|
-
#=>
|
111
|
-
#=> SELECT *
|
112
|
-
#=> FROM table
|
57
|
+
# see colors in irb %)
|
58
|
+
#=> SELECT *
|
59
|
+
#=> FROM ( VALUES(1), (2) ) AS tmp
|
60
|
+
|
61
|
+
# rails combines err with query, so don't forget to do it yourself
|
62
|
+
# to get real nice result you should execute on your DB prettified_sql!
|
63
|
+
# otherwise you will not get such a nice output
|
113
64
|
|
65
|
+
puts Niceql::Prettifier.prettify_pg_err(<<-ERR )
|
66
|
+
ERROR: VALUES in FROM must have an alias
|
67
|
+
LINE 2: FROM ( VALUES(1), (2) )
|
68
|
+
^
|
69
|
+
HINT: For example, FROM (VALUES ...) [AS] foo.
|
70
|
+
SELECT err
|
71
|
+
FROM ( VALUES(1), (2) )
|
72
|
+
ORDER BY 1
|
73
|
+
ERR
|
114
74
|
|
115
75
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
LINE 2: FROM ( VALUES(1), (2) )
|
125
|
-
^
|
126
|
-
HINT: For example, FROM (VALUES ...) [AS] foo.
|
127
|
-
SELECT err
|
128
|
-
FROM ( VALUES(1), (2) )
|
129
|
-
ORDER BY 1
|
130
|
-
ERR
|
131
|
-
|
132
|
-
|
133
|
-
# ERROR: VALUES in FROM must have an alias
|
134
|
-
# LINE 2: FROM ( VALUES(1), (2) )
|
135
|
-
# ^
|
136
|
-
# HINT: For example, FROM (VALUES ...) [AS] foo.
|
137
|
-
# SELECT err
|
138
|
-
# FROM ( VALUES(1), (2) )
|
139
|
-
# ^
|
140
|
-
# ORDER BY 1
|
76
|
+
# ERROR: VALUES in FROM must have an alias
|
77
|
+
# LINE 2: FROM ( VALUES(1), (2) )
|
78
|
+
# ^
|
79
|
+
# HINT: For example, FROM (VALUES ...) [AS] foo.
|
80
|
+
# SELECT err
|
81
|
+
# FROM ( VALUES(1), (2) )
|
82
|
+
# ^
|
83
|
+
# ORDER BY 1
|
141
84
|
|
142
85
|
```
|
143
|
-
|
144
|
-
## Customizing colors
|
145
|
-
If your console support more colors or different schemes, or if you prefer different colorization, then you can override ColorizeString methods. Current colors are selected with dark and white console themes in mind, so a niceql colorization works good for dark, and good enough for white.
|
146
|
-
|
147
86
|
## Limitations
|
148
87
|
|
149
|
-
Right now gem detects only uppercased form of verbs with simple indentation and parsing options.
|
88
|
+
Right now gem detects only uppercased form of verbs with very simple indentation and parsing options.
|
150
89
|
|
151
|
-
##
|
90
|
+
## Customizing colors
|
91
|
+
If your console support more colors or different schemes, or you prefer different colorization, then you can override ColorizeString methods. Current color are selected with dark and white console themes in mind, so niceql colorization works good for dark, and good enough for white.
|
152
92
|
|
153
93
|
## Contributing
|
154
94
|
|
data/err_now.png
CHANGED
Binary file
|
data/err_was.png
CHANGED
Binary file
|
data/lib/niceql/version.rb
CHANGED
data/lib/niceql.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
require "niceql/version"
|
2
|
-
require 'niceql/string'
|
3
2
|
|
4
3
|
module Niceql
|
5
|
-
|
6
4
|
module StringColorize
|
7
5
|
def self.colorize_verb( str)
|
8
6
|
#yellow ANSI color
|
@@ -19,264 +17,92 @@ module Niceql
|
|
19
17
|
end
|
20
18
|
|
21
19
|
module ArExtentions
|
22
|
-
def
|
23
|
-
|
20
|
+
def explain_err
|
21
|
+
begin
|
22
|
+
connection.execute( "EXPLAIN #{to_niceql}" )
|
23
|
+
rescue StandardError => e
|
24
|
+
puts Prettifier.prettify_err(e )
|
25
|
+
end
|
24
26
|
end
|
25
27
|
|
26
28
|
def to_niceql
|
27
29
|
Prettifier.prettify_sql(to_sql, false)
|
28
30
|
end
|
29
31
|
|
30
|
-
def
|
32
|
+
def puts_niceql( colorize = true )
|
31
33
|
puts Prettifier.prettify_sql( to_sql, colorize )
|
32
34
|
end
|
33
|
-
|
34
35
|
end
|
35
36
|
|
36
37
|
module Prettifier
|
37
|
-
INLINE_VERBS = %w(
|
38
|
-
NEW_LINE_VERBS = 'SELECT|FROM|WHERE|CASE|ORDER BY|LIMIT|GROUP BY|
|
39
|
-
|
40
|
-
VERBS = "#{NEW_LINE_VERBS}|#{INLINE_VERBS}"
|
38
|
+
INLINE_VERBS = %w(ASC IN AS WHEN THEN ELSE END AND UNION ALL WITH ON DISTINCT INTERSECT EXCEPT EXISTS NOT).join('| ')
|
39
|
+
NEW_LINE_VERBS = 'SELECT|FROM|WHERE|CASE|ORDER BY|LIMIT|GROUP BY|WITH|LEFT JOIN|RIGHT JOIN|JOIN|HAVING|OFFSET'
|
40
|
+
VERBS = "#{INLINE_VERBS}|#{NEW_LINE_VERBS}"
|
41
41
|
STRINGS = /("[^"]+")|('[^']+')/
|
42
42
|
BRACKETS = '[\(\)]'
|
43
|
-
SQL_COMMENTS = /(\s*?--.+\s*)|(\s*?\/\*[^\/\*]*\*\/\s*)/
|
44
|
-
# only newlined comments will be matched
|
45
|
-
SQL_COMMENTS_CLEARED = /(\s*?--.+\s{1})|(\s*$\s*\/\*[^\/\*]*\*\/\s{1})/
|
46
|
-
COMMENT_CONTENT = /[\S]+[\s\S]*[\S]+/
|
47
|
-
|
48
|
-
def self.config
|
49
|
-
Niceql.config
|
50
|
-
end
|
51
43
|
|
52
44
|
def self.prettify_err(err)
|
53
|
-
|
45
|
+
if ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
|
46
|
+
prettify_pg_err( err.to_s )
|
47
|
+
else
|
48
|
+
puts err
|
49
|
+
end
|
54
50
|
end
|
55
51
|
|
56
52
|
|
57
|
-
|
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)
|
79
|
-
return err if err[/LINE \d+/].nil?
|
53
|
+
def self.prettify_pg_err(err)
|
80
54
|
err_line_num = err[/LINE \d+/][5..-1].to_i
|
55
|
+
start_sql_line = err.lines[3][/HINT/] ? 4 : 3
|
56
|
+
err_body = err.lines[start_sql_line..-1]
|
57
|
+
err_line = StringColorize.colorize_err( err_body[err_line_num-1] )
|
81
58
|
|
82
|
-
#
|
83
|
-
start_sql_line = err.lines[3][/(HINT|DETAIL)/] ? 4 : 3
|
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
|
-
|
90
|
-
err_quote = ( err.lines[1][/\.\.\..+\.\.\./] && err.lines[1][/\.\.\..+\.\.\./][3..-4] ) ||
|
91
|
-
( err.lines[1][/\.\.\..+/] && err.lines[1][/\.\.\..+/][3..-1] )
|
92
|
-
|
93
|
-
# line[2] is err carret line i.e.: ' ^'
|
94
|
-
# err.lines[1][/LINE \d+:/].length+1..-1 - is a position from error quote begin
|
95
|
-
err_carret_line = err.lines[2][err.lines[1][/LINE \d+:/].length+1..-1]
|
96
|
-
# err line will be painted in red completely, so we just remembering it and use
|
97
|
-
# to replace after paiting the verbs
|
98
|
-
err_line = err_body[err_line_num-1]
|
99
|
-
|
100
|
-
# when err line is too long postgres quotes it part in double '...'
|
101
|
-
if err_quote
|
102
|
-
err_quote_carret_offset = err_carret_line.length - err.lines[1].index( '...' ) + 3
|
103
|
-
err_carret_line = ' ' * ( err_line.index( err_quote ) + err_quote_carret_offset ) + "^\n"
|
104
|
-
end
|
105
|
-
|
106
|
-
err_carret_line = " " + err_carret_line if err_body[0].start_with?(': ')
|
107
|
-
# if mistake is on last string than err_line.last != \n so we need to prepend \n to carret line
|
108
|
-
err_carret_line = "\n" + err_carret_line unless err_line[-1] == "\n"
|
109
|
-
|
110
|
-
#colorizing verbs and strings
|
111
59
|
err_body = err_body.join.gsub(/#{VERBS}/ ) { |verb| StringColorize.colorize_verb(verb) }
|
112
|
-
|
60
|
+
err_body = err_body.gsub(STRINGS){ |str| StringColorize.colorize_str(str) }
|
113
61
|
|
114
|
-
#reassemling error message
|
115
62
|
err_body = err_body.lines
|
116
|
-
err_body[err_line_num-1]=
|
117
|
-
err_body.insert( err_line_num, StringColorize.colorize_err(
|
118
|
-
|
119
|
-
err.lines[0..start_sql_line-1].join + err_body.join
|
63
|
+
err_body[err_line_num-1]= err_line
|
64
|
+
err_body.insert( err_line_num, StringColorize.colorize_err( err.lines[2][err.lines[1][/LINE \d+:/].length+1..-1] ) )
|
65
|
+
puts err.lines[0..start_sql_line-1].join + err_body.join
|
120
66
|
end
|
121
67
|
|
122
68
|
def self.prettify_sql( sql, colorize = true )
|
123
69
|
indent = 0
|
124
70
|
parentness = []
|
125
71
|
|
126
|
-
sql = sql.
|
127
|
-
# remove additional formatting for sql_parts but leave comment intact
|
128
|
-
[sql_part.gsub(/[\s]+/, ' '),
|
129
|
-
# comment.match?(/\A\s*$/) - SQL_COMMENTS gets all comment content + all whitespaced chars around
|
130
|
-
# so this sql_part.length == 0 || comment.match?(/\A\s*$/) checks does the comment starts from new line
|
131
|
-
comment && ( sql_part.length == 0 || comment.match?(/\A\s*$/) ? "\n#{comment[COMMENT_CONTENT]}\n" : comment[COMMENT_CONTENT] ) ]
|
132
|
-
}.flatten.join(' ')
|
133
|
-
|
134
|
-
sql.gsub!(/ \n/, "\n")
|
135
|
-
|
136
|
-
sql.gsub!(STRINGS){ |str| StringColorize.colorize_str(str) } if colorize
|
137
|
-
|
72
|
+
sql = sql.gsub(STRINGS){ |str| StringColorize.colorize_str(str) } if colorize
|
138
73
|
first_verb = true
|
139
|
-
prev_was_comment = false
|
140
74
|
|
141
|
-
sql.gsub
|
75
|
+
sql.gsub( /(#{VERBS}|#{BRACKETS})/) do |verb|
|
76
|
+
add_new_line = false
|
142
77
|
if 'SELECT' == verb
|
143
|
-
indent +=
|
78
|
+
indent += 1
|
144
79
|
parentness.last[:nested] = true if parentness.last
|
145
80
|
add_new_line = !first_verb
|
81
|
+
first_verb = false
|
146
82
|
elsif verb == '('
|
147
|
-
|
148
|
-
|
149
|
-
add_new_line = !!Regexp.last_match.post_match[0..next_closing_bracket][/SELECT/] && config.open_bracket_is_newliner
|
150
|
-
parentness << { nested: add_new_line }
|
83
|
+
parentness << { nested: false }
|
84
|
+
indent += 1
|
151
85
|
elsif verb == ')'
|
152
86
|
# this also covers case when right bracket is used without corresponding left one
|
153
87
|
add_new_line = parentness.last.nil? || parentness.last[:nested]
|
154
|
-
indent -=
|
88
|
+
indent -= add_new_line ? 2 : 1
|
155
89
|
indent = 0 if indent < 0
|
156
90
|
parentness.pop
|
157
|
-
elsif verb
|
91
|
+
elsif verb == 'ORDER BY'
|
158
92
|
# in postgres ORDER BY can be used in aggregation function this will keep it
|
159
93
|
# inline with its agg function
|
160
94
|
add_new_line = parentness.last.nil? || parentness.last[:nested]
|
161
95
|
else
|
162
96
|
add_new_line = verb[/(#{INLINE_VERBS})/].nil?
|
163
97
|
end
|
164
|
-
|
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
|
-
# inliners match with a space before so we need to strip it
|
168
|
-
verb.lstrip! if !add_new_line && prev_was_comment
|
169
|
-
|
170
|
-
add_new_line = prev_was_comment unless add_new_line
|
171
|
-
add_indent = !first_verb && add_new_line
|
172
|
-
|
173
|
-
if verb[SQL_COMMENTS_CLEARED]
|
174
|
-
verb = verb[COMMENT_CONTENT]
|
175
|
-
prev_was_comment = true
|
176
|
-
else
|
177
|
-
first_verb = false
|
178
|
-
prev_was_comment = false
|
179
|
-
end
|
180
|
-
|
181
98
|
verb = StringColorize.colorize_verb(verb) if !['(', ')'].include?(verb) && colorize
|
182
|
-
|
183
|
-
subs = ( add_indent ? indent_multiline(verb, indent) : verb)
|
184
|
-
!first_verb && add_new_line ? "\n" + subs : subs
|
185
|
-
end
|
186
|
-
|
187
|
-
# clear all spaces before newlines, and all whitespaces before string end
|
188
|
-
sql.tap{ |slf| slf.gsub!( /\s+\n/, "\n" ) }.tap{ |slf| slf.gsub!(/\s+\z/, '') }
|
189
|
-
end
|
190
|
-
|
191
|
-
def self.prettify_multiple( sql_multi, colorize = true )
|
192
|
-
sql_multi.split( /(?>#{SQL_COMMENTS})|(\;)/ ).inject(['']) { |queries, pattern|
|
193
|
-
queries.last << pattern
|
194
|
-
queries << '' if pattern == ';'
|
195
|
-
queries
|
196
|
-
}.map!{ |sql|
|
197
|
-
# we were splitting by comments and ;, so if next sql start with comment we've got a misplaced \n\n
|
198
|
-
sql.match?(/\A\s+\z/) ? nil : prettify_sql( sql, colorize )
|
199
|
-
}.compact.join("\n\n")
|
200
|
-
end
|
201
|
-
|
202
|
-
private
|
203
|
-
def self.indent_multiline( verb, indent )
|
204
|
-
#
|
205
|
-
if verb.match?(/.\s*\n\s*./)
|
206
|
-
verb.lines.map!{|ln| "#{' ' * indent}" + ln}.join("\n")
|
207
|
-
else
|
208
|
-
"#{' ' * indent}" + verb.to_s
|
99
|
+
add_new_line ? "\n#{' ' * indent}" + verb : verb
|
209
100
|
end
|
210
101
|
end
|
211
102
|
end
|
212
103
|
|
213
|
-
module PostgresAdapterNiceQL
|
214
|
-
def exec_query(sql, name = "SQL", binds = [], prepare: false)
|
215
|
-
# replacing sql with prettified sql, thats all
|
216
|
-
super( Prettifier.prettify_sql(sql, false), name, binds, prepare: prepare )
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
module AbstractAdapterLogPrettifier
|
221
|
-
def log( sql, *args, &block )
|
222
|
-
# \n need to be placed because AR log will start with action description + time info.
|
223
|
-
# rescue sql - just to be sure Prettifier wouldn't break production
|
224
|
-
formatted_sql = "\n" + Prettifier.prettify_sql(sql) rescue sql
|
225
|
-
super( formatted_sql, *args, &block )
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
module ErrorExt
|
230
|
-
def to_s
|
231
|
-
Niceql.config.prettify_pg_errors ? Prettifier.prettify_err(super) : super
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
class NiceQLConfig
|
236
|
-
def ar_using_pg_adapter?
|
237
|
-
return false unless defined?(::ActiveRecord::Base)
|
238
|
-
|
239
|
-
config = ActiveRecord::Base.try(:connection_db_config) || ActiveRecord::Base.try(:connection_config)
|
240
|
-
config&.dig('adapter') == 'postgresql'
|
241
|
-
end
|
242
|
-
|
243
|
-
attr_accessor :pg_adapter_with_nicesql,
|
244
|
-
:indentation_base,
|
245
|
-
:open_bracket_is_newliner,
|
246
|
-
:prettify_active_record_log_output,
|
247
|
-
:prettify_pg_errors
|
248
|
-
|
249
|
-
|
250
|
-
def initialize
|
251
|
-
self.pg_adapter_with_nicesql = false
|
252
|
-
self.indentation_base = 2
|
253
|
-
self.open_bracket_is_newliner = false
|
254
|
-
self.prettify_active_record_log_output = false
|
255
|
-
self.prettify_pg_errors = ar_using_pg_adapter?
|
256
|
-
end
|
257
|
-
end
|
258
|
-
|
259
|
-
def self.configure
|
260
|
-
yield( config )
|
261
|
-
|
262
|
-
return unless defined? ::ActiveRecord::Base
|
263
|
-
|
264
|
-
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(PostgresAdapterNiceQL) if config.pg_adapter_with_nicesql
|
265
|
-
|
266
|
-
::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend( AbstractAdapterLogPrettifier ) if config.prettify_active_record_log_output
|
267
|
-
|
268
|
-
::ActiveRecord::StatementInvalid.include( Niceql::ErrorExt ) if config.prettify_pg_errors && config.ar_using_pg_adapter?
|
269
|
-
end
|
270
|
-
|
271
|
-
def self.config
|
272
|
-
@config ||= NiceQLConfig.new
|
273
|
-
end
|
274
|
-
|
275
104
|
if defined? ::ActiveRecord::Base
|
276
105
|
::ActiveRecord::Base.extend ArExtentions
|
277
106
|
[::ActiveRecord::Relation, ::ActiveRecord::Associations::CollectionProxy].each { |klass| klass.send(:include, ArExtentions) }
|
278
107
|
end
|
279
|
-
|
280
108
|
end
|
281
|
-
|
282
|
-
|
data/niceql.gemspec
CHANGED
@@ -30,12 +30,8 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
31
|
spec.require_paths = ["lib"]
|
32
32
|
|
33
|
-
spec.
|
34
|
-
spec.add_development_dependency "
|
35
|
-
spec.add_development_dependency "rake", ">= 12.3.3"
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
35
|
spec.add_development_dependency "minitest", "~> 5.0"
|
37
36
|
|
38
|
-
spec.add_development_dependency "differ"
|
39
|
-
spec.add_development_dependency "pry-byebug"
|
40
|
-
spec.add_development_dependency "benchmark-ips"
|
41
37
|
end
|
data/to_niceql.png
CHANGED
Binary file
|