niceql 0.1.7 → 0.1.8
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/README.md +3 -1
- data/lib/generators/templates/niceql_initializer.rb +6 -0
- data/lib/niceql/version.rb +1 -1
- data/lib/niceql.rb +64 -9
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ceaa44117d9161231f6695b7d28b4819929cac6c
|
4
|
+
data.tar.gz: 74f5e28868936976ce3dbabf15b55d4384758b05
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f15980214bed358db1f6f4ecd3e5c96c998e9099452cdadc9134c5a3877f5279b5735ef353a91f850a2083292d64ec8b3ed49fd93c19cb990bb308e8b884f9b
|
7
|
+
data.tar.gz: 6adb2d6a14763db2732966529bd6d93830a375510f96f13f83074c36718e2dea45daef01af0ecb1fa02c5b950d52aed492bb96b3b886de6bf78fe4d1af5165e8
|
data/README.md
CHANGED
data/lib/niceql/version.rb
CHANGED
data/lib/niceql.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "niceql/version"
|
2
2
|
|
3
3
|
module Niceql
|
4
|
+
|
4
5
|
module StringColorize
|
5
6
|
def self.colorize_verb( str)
|
6
7
|
#yellow ANSI color
|
@@ -17,11 +18,11 @@ module Niceql
|
|
17
18
|
end
|
18
19
|
|
19
20
|
module ArExtentions
|
20
|
-
def
|
21
|
+
def exec_niceql
|
21
22
|
begin
|
22
|
-
connection.execute(
|
23
|
+
connection.execute( to_niceql )
|
23
24
|
rescue StandardError => e
|
24
|
-
|
25
|
+
raise e.class.new( Prettifier.prettify_err(e ) )
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
@@ -35,7 +36,7 @@ module Niceql
|
|
35
36
|
end
|
36
37
|
|
37
38
|
module Prettifier
|
38
|
-
INLINE_VERBS = %w(ASC IN AS WHEN THEN ELSE END AND UNION ALL WITH ON DISTINCT INTERSECT EXCEPT EXISTS NOT).join('| ')
|
39
|
+
INLINE_VERBS = %w(ASC IN AS WHEN THEN ELSE END AND UNION ALL WITH ON DISTINCT INTERSECT EXCEPT EXISTS NOT COUNT).join('| ')
|
39
40
|
NEW_LINE_VERBS = 'SELECT|FROM|WHERE|CASE|ORDER BY|LIMIT|GROUP BY|WITH|LEFT JOIN|RIGHT JOIN|JOIN|HAVING|OFFSET'
|
40
41
|
VERBS = "#{INLINE_VERBS}|#{NEW_LINE_VERBS}"
|
41
42
|
STRINGS = /("[^"]+")|('[^']+')/
|
@@ -45,24 +46,45 @@ module Niceql
|
|
45
46
|
if ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
|
46
47
|
prettify_pg_err( err.to_s )
|
47
48
|
else
|
48
|
-
|
49
|
+
err
|
49
50
|
end
|
50
51
|
end
|
51
52
|
|
52
53
|
|
53
54
|
def self.prettify_pg_err(err)
|
54
55
|
err_line_num = err[/LINE \d+/][5..-1].to_i
|
56
|
+
|
55
57
|
start_sql_line = err.lines[3][/(HINT|DETAIL)/] ? 4 : 3
|
56
58
|
err_body = err.lines[start_sql_line..-1]
|
57
|
-
|
59
|
+
err_quote = ( err.lines[1][/\.\.\..+\.\.\./] && err.lines[1][/\.\.\..+\.\.\./][3..-4] ) ||
|
60
|
+
( err.lines[1][/\.\.\..+/] && err.lines[1][/\.\.\..+/][3..-1] )
|
61
|
+
|
62
|
+
|
63
|
+
# line 2 is err carret line
|
64
|
+
err_carret_line = err.lines[2][err.lines[1][/LINE \d+:/].length+1..-1]
|
65
|
+
# err line painted red completly, so we just remembering it and use
|
66
|
+
# to replace after paiting the verbs
|
67
|
+
err_line = err_body[err_line_num-1]
|
68
|
+
|
69
|
+
# when err line is too long postgres quotes it part in doble ...
|
70
|
+
if err_quote
|
71
|
+
err_quote_carret_offset = err_carret_line.length - err.lines[1].index( '...' ) + 3
|
72
|
+
err_carret_line = ' ' * ( err_line.index( err_quote ) + err_quote_carret_offset )
|
73
|
+
end
|
74
|
+
|
75
|
+
# if mistake is on last string than err_line.last != \n so we need to prepend \n to carret line
|
76
|
+
err_line = ( err_line.last == "\n" ? err_line + "\n" : "\n" + err_line )
|
58
77
|
|
78
|
+
#colorizing verbs and strings
|
59
79
|
err_body = err_body.join.gsub(/#{VERBS}/ ) { |verb| StringColorize.colorize_verb(verb) }
|
60
80
|
err_body = err_body.gsub(STRINGS){ |str| StringColorize.colorize_str(str) }
|
61
81
|
|
82
|
+
#reassemling error message
|
62
83
|
err_body = err_body.lines
|
63
|
-
err_body[err_line_num-1]= err_line
|
64
|
-
err_body.insert( err_line_num, StringColorize.colorize_err(
|
65
|
-
|
84
|
+
err_body[err_line_num-1]= StringColorize.colorize_err( err_line )
|
85
|
+
err_body.insert( err_line_num, StringColorize.colorize_err( err_carret_line ) )
|
86
|
+
|
87
|
+
err.lines[0..start_sql_line-1].join + err_body.join
|
66
88
|
end
|
67
89
|
|
68
90
|
def self.prettify_sql( sql, colorize = true )
|
@@ -101,8 +123,41 @@ module Niceql
|
|
101
123
|
end
|
102
124
|
end
|
103
125
|
|
126
|
+
module PostgresAdapterNiceQL
|
127
|
+
def exec_query(sql, name = "SQL", binds = [], prepare: false)
|
128
|
+
begin
|
129
|
+
# replacing sql with prettified sql, thats all
|
130
|
+
super( Prettifier.prettify_sql(sql, false), name, binds, prepare: prepare )
|
131
|
+
rescue StandardError => e
|
132
|
+
raise e.class.new( Prettifier.prettify_err(e ) )
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
class NiceQLConfig
|
138
|
+
attr_accessor :pg_adapter_with_nicesql
|
139
|
+
def initialize
|
140
|
+
self.pg_adapter_with_nicesql = false
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
def self.configure
|
146
|
+
@config ||= NiceQLConfig.new
|
147
|
+
|
148
|
+
yield( @config )
|
149
|
+
|
150
|
+
if @config.pg_adapter_with_nicesql
|
151
|
+
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(PostgresAdapterNiceQL)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
|
104
156
|
if defined? ::ActiveRecord::Base
|
105
157
|
::ActiveRecord::Base.extend ArExtentions
|
106
158
|
[::ActiveRecord::Relation, ::ActiveRecord::Associations::CollectionProxy].each { |klass| klass.send(:include, ArExtentions) }
|
107
159
|
end
|
160
|
+
|
108
161
|
end
|
162
|
+
|
163
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: niceql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alekseyl
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- bin/setup
|
71
71
|
- err_now.png
|
72
72
|
- err_was.png
|
73
|
+
- lib/generators/templates/niceql_initializer.rb
|
73
74
|
- lib/niceql.rb
|
74
75
|
- lib/niceql/version.rb
|
75
76
|
- niceql.gemspec
|