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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c1abb2f5e4e5f877461b9ffc00fdd4c443be900e
4
- data.tar.gz: a14bdab780abdb5f9f70b7f3830787a193900d6e
3
+ metadata.gz: ceaa44117d9161231f6695b7d28b4819929cac6c
4
+ data.tar.gz: 74f5e28868936976ce3dbabf15b55d4384758b05
5
5
  SHA512:
6
- metadata.gz: b15bfd9c83732a44df015a9c04c19698fddc1c4dc8a036b879499de8771c0d2c1991f2ca283e8a001837db005cbf9d81bd3bb08ed7dd3ad12714a397b4a9a162
7
- data.tar.gz: 9ac6a5b8dbb7d70f9935fc8c6f750178d14371d507179cc59c1ae3f792206cf8e4028593d1a9079f883c1d9cf31dfcba4a5ef1a62561c2dac614bf7cbe0ed729
6
+ metadata.gz: 5f15980214bed358db1f6f4ecd3e5c96c998e9099452cdadc9134c5a3877f5279b5735ef353a91f850a2083292d64ec8b3ed49fd93c19cb990bb308e8b884f9b
7
+ data.tar.gz: 6adb2d6a14763db2732966529bd6d93830a375510f96f13f83074c36718e2dea45daef01af0ecb1fa02c5b950d52aed492bb96b3b886de6bf78fe4d1af5165e8
data/README.md CHANGED
@@ -46,7 +46,9 @@ Or install it yourself as:
46
46
  Model.scope.to_niceql
47
47
 
48
48
  # prettify PG errors
49
- Model.scope_with_err.explain_err
49
+ Model.scope_with_err.exec_niceql
50
+
51
+
50
52
  ```
51
53
 
52
54
  ### Without ActiveRecord
@@ -0,0 +1,6 @@
1
+ Niceql.configure do |c|
2
+ # You can adjust pg_adapter in prooduction at your own risk!
3
+ # If you need it in production use exec_niceql
4
+ # default: false
5
+ # c.pg_adapter_with_nicesql = Rails.env.development?
6
+ end
@@ -1,3 +1,3 @@
1
1
  module Niceql
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
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 explain_err
21
+ def exec_niceql
21
22
  begin
22
- connection.execute( "EXPLAIN #{to_niceql}" )
23
+ connection.execute( to_niceql )
23
24
  rescue StandardError => e
24
- puts Prettifier.prettify_err(e )
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
- puts err
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
- err_line = StringColorize.colorize_err( err_body[err_line_num-1] )
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( err.lines[2][err.lines[1][/LINE \d+:/].length+1..-1] ) )
65
- puts err.lines[0..start_sql_line-1].join + err_body.join
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.7
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