niceql 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27f3ce5a573a6fa12858d8ab31254dc1f230a12f
4
- data.tar.gz: ea0392deadddf634888a663e7770da9ed89e47b5
3
+ metadata.gz: 340c6606042dc38f152d064b26ca01be534bf454
4
+ data.tar.gz: 39f96a56add99895bcadc8491918d8dca0491e7a
5
5
  SHA512:
6
- metadata.gz: 3018c15653659c8a2bebe7c853c671633194e2be0bb24f5fd7e486b784b227998aa5f95d118821183080912197adf9c10bd22d454d8c54b1c5846d099118a026
7
- data.tar.gz: 931f9a384176b17e941c5f802705adb62a0889c645d30c7e3deb1bde2d583bf1d9faa0fe96e57deb7b41909abb3218b94f2aff0cf6f0ae153c18ecf432013c61
6
+ metadata.gz: 82768c5e9b46831390e44e0eacf75d3121ca0b6c809526e63ffa32299a0968fea3295e5d0b446a407768f46415a908468db0119f429d0098b9bcfc41ae144f27
7
+ data.tar.gz: d303f5804a27a2ada8d922186ae834c956e9a5bfcdb9a27427e3eecdd79a3ec401e56bba7238eb4bca5934904a9e914e39591c7c940462e969908cc17b541ca7
data/README.md CHANGED
@@ -34,6 +34,39 @@ Or install it yourself as:
34
34
 
35
35
  $ gem install niceql
36
36
 
37
+ ## Configuration
38
+
39
+ ```ruby
40
+ Niceql.configure do |c|
41
+ # Setting pg_adapter_with_nicesql to true will force formatting SQL queries
42
+ # before executing them, this will lead to better SQL-query debugging and much more clearer error messages
43
+ # if you are using Postgresql as a data source.
44
+ # You can adjust pg_adapter in prooduction but do it at your own risk!
45
+ # If you need to debug SQL queries in production use exec_niceql
46
+ # default: false
47
+ # uncomment next string to enable in development
48
+ # c.pg_adapter_with_nicesql = Rails.env.development?
49
+
50
+ # spaces count for one indentation
51
+ c.indentation_base = 2
52
+
53
+ # setting open_bracket_is_newliner to true will start opening brackets '(' with nested subqueries from new line
54
+ # i.e. SELECT * FROM ( SELECT * FROM tags ) tags; will transform to:
55
+ # SELECT *
56
+ # FROM
57
+ # (
58
+ # SELECT * FROM tags
59
+ # ) tags;
60
+ # when open_bracket_is_newliner is false:
61
+ # SELECT *
62
+ # FROM (
63
+ # SELECT * FROM tags
64
+ # ) tags;
65
+ # default: false
66
+ c.open_bracket_is_newliner = false
67
+ end
68
+ ```
69
+
37
70
  ## Usage
38
71
 
39
72
  ### With ActiveRecord
@@ -42,13 +75,11 @@ Or install it yourself as:
42
75
  # puts colorized ( or not if you are willing so ) to_niceql ( you need to call puts otherwise to_niceql looks ugly )
43
76
  Model.scope.niceql
44
77
 
45
- # only formatting without colorization can run as a SQL query in connection.execute
78
+ # only formatting without colorization, you can run output of to_niceql as a SQL query in connection.execute
46
79
  Model.scope.to_niceql
47
80
 
48
- # prettify PG errors
81
+ # prettify PG errors if scope runs with any
49
82
  Model.scope_with_err.exec_niceql
50
-
51
-
52
83
  ```
53
84
 
54
85
  ### Without ActiveRecord
@@ -85,13 +116,16 @@ ERR
85
116
  # ORDER BY 1
86
117
 
87
118
  ```
88
- ## Limitations
89
-
90
- Right now gem detects only uppercased form of verbs with very simple indentation and parsing options.
91
119
 
92
120
  ## Customizing colors
93
121
  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.
94
122
 
123
+ ## Limitations
124
+
125
+ Right now gem detects only uppercased form of verbs with simple indentation and parsing options.
126
+
127
+ ##
128
+
95
129
  ## Contributing
96
130
 
97
131
  Bug reports and pull requests are welcome on GitHub at https://github.com/alekseyl/niceql.
@@ -3,4 +3,6 @@ Niceql.configure do |c|
3
3
  # If you need it in production use exec_niceql
4
4
  # default: false
5
5
  # c.pg_adapter_with_nicesql = Rails.env.development?
6
+ # c.indentation_base = 2
7
+ # c.open_bracket_is_newliner = false
6
8
  end
@@ -1,3 +1,3 @@
1
1
  module Niceql
2
- VERSION = "0.1.9"
2
+ VERSION = "0.1.10"
3
3
  end
data/lib/niceql.rb CHANGED
@@ -19,11 +19,7 @@ module Niceql
19
19
 
20
20
  module ArExtentions
21
21
  def exec_niceql
22
- begin
23
- connection.execute( to_niceql )
24
- rescue StandardError => e
25
- raise e.class.new( Prettifier.prettify_err(e ) )
26
- end
22
+ connection.execute( to_niceql )
27
23
  end
28
24
 
29
25
  def to_niceql
@@ -33,21 +29,25 @@ module Niceql
33
29
  def niceql( colorize = true )
34
30
  puts Prettifier.prettify_sql( to_sql, colorize )
35
31
  end
32
+
36
33
  end
37
34
 
38
35
  module Prettifier
39
- INLINE_VERBS = %w(ASC IN AS WHEN THEN ELSE END AND UNION ALL WITH ON DISTINCT INTERSECT EXCEPT EXISTS NOT COUNT).join('| ')
40
- NEW_LINE_VERBS = 'SELECT|FROM|WHERE|CASE|ORDER BY|LIMIT|GROUP BY|WITH|LEFT JOIN|RIGHT JOIN|JOIN|HAVING|OFFSET'
36
+ INLINE_VERBS = %w(WITH ASC IN COALESCE AS WHEN THEN ELSE END AND UNION ALL WITH ON DISTINCT INTERSECT EXCEPT EXISTS NOT COUNT ROUND CAST).join('| ')
37
+ NEW_LINE_VERBS = 'SELECT|FROM|WHERE|CASE|ORDER BY|LIMIT|GROUP BY|LEFT JOIN|RIGHT JOIN|JOIN|HAVING|OFFSET|UPDATE'
38
+ POSSIBLE_INLINER = /(ORDER BY|CASE)/
41
39
  VERBS = "#{INLINE_VERBS}|#{NEW_LINE_VERBS}"
42
40
  STRINGS = /("[^"]+")|('[^']+')/
43
41
  BRACKETS = '[\(\)]'
44
42
 
43
+
44
+ def self.config
45
+ Niceql.config
46
+ end
47
+
48
+
45
49
  def self.prettify_err(err)
46
- if ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
47
- prettify_pg_err( err.to_s )
48
- else
49
- err
50
- end
50
+ prettify_pg_err( err.to_s )
51
51
  end
52
52
 
53
53
 
@@ -59,8 +59,8 @@ module Niceql
59
59
  err_quote = ( err.lines[1][/\.\.\..+\.\.\./] && err.lines[1][/\.\.\..+\.\.\./][3..-4] ) ||
60
60
  ( err.lines[1][/\.\.\..+/] && err.lines[1][/\.\.\..+/][3..-1] )
61
61
 
62
-
63
62
  # line 2 is err carret line
63
+ # err.lines[1][/LINE \d+:/].length+1..-1 - is a position from error quote begin
64
64
  err_carret_line = err.lines[2][err.lines[1][/LINE \d+:/].length+1..-1]
65
65
  # err line painted red completly, so we just remembering it and use
66
66
  # to replace after paiting the verbs
@@ -69,11 +69,11 @@ module Niceql
69
69
  # when err line is too long postgres quotes it part in doble ...
70
70
  if err_quote
71
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 )
72
+ err_carret_line = ' ' * ( err_line.index( err_quote ) + err_quote_carret_offset ) + "^\n"
73
73
  end
74
74
 
75
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 )
76
+ err_carret_line = "\n" + err_carret_line unless err_line.last == "\n"
77
77
 
78
78
  #colorizing verbs and strings
79
79
  err_body = err_body.join.gsub(/#{VERBS}/ ) { |verb| StringColorize.colorize_verb(verb) }
@@ -91,32 +91,38 @@ module Niceql
91
91
  indent = 0
92
92
  parentness = []
93
93
 
94
+ #it's better to remove all new lines because it will break formatting
95
+ sql = sql.gsub("\n", ' ')
96
+ # remove any additional formatting
97
+ sql = sql.gsub(/[ ]+/, ' ')
98
+
94
99
  sql = sql.gsub(STRINGS){ |str| StringColorize.colorize_str(str) } if colorize
95
100
  first_verb = true
96
101
 
97
- sql.gsub( /(#{VERBS}|#{BRACKETS})/) do |verb|
102
+ sql.gsub( /(#{VERBS}|#{BRACKETS})/).with_index do |verb, index|
98
103
  add_new_line = false
99
104
  if 'SELECT' == verb
100
- indent += 1
105
+ indent += config.indentation_base if parentness.last.nil? || parentness.last[:nested]
101
106
  parentness.last[:nested] = true if parentness.last
102
107
  add_new_line = !first_verb
103
- first_verb = false
104
108
  elsif verb == '('
105
- parentness << { nested: false }
106
- indent += 1
109
+ next_closing_bracket = Regexp.last_match.post_match.index(')')
110
+ add_new_line = !!Regexp.last_match.post_match[0..next_closing_bracket][/SELECT/] && config.open_bracket_is_newliner
111
+ parentness << { nested: add_new_line }
107
112
  elsif verb == ')'
108
113
  # this also covers case when right bracket is used without corresponding left one
109
114
  add_new_line = parentness.last.nil? || parentness.last[:nested]
110
- indent -= add_new_line ? 2 : 1
115
+ indent -= ( parentness.last.nil? ? 2 * config.indentation_base : (parentness.last[:nested] ? config.indentation_base : 0) )
111
116
  indent = 0 if indent < 0
112
117
  parentness.pop
113
- elsif verb == 'ORDER BY'
118
+ elsif verb[POSSIBLE_INLINER]
114
119
  # in postgres ORDER BY can be used in aggregation function this will keep it
115
120
  # inline with its agg function
116
121
  add_new_line = parentness.last.nil? || parentness.last[:nested]
117
122
  else
118
123
  add_new_line = verb[/(#{INLINE_VERBS})/].nil?
119
124
  end
125
+ first_verb = false
120
126
  verb = StringColorize.colorize_verb(verb) if !['(', ')'].include?(verb) && colorize
121
127
  add_new_line ? "\n#{' ' * indent}" + verb : verb
122
128
  end
@@ -125,35 +131,51 @@ module Niceql
125
131
 
126
132
  module PostgresAdapterNiceQL
127
133
  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 ) )
134
+ # replacing sql with prettified sql, thats all
135
+ super( Prettifier.prettify_sql(sql, false), name, binds, prepare: prepare )
136
+ end
137
+ end
138
+
139
+ module ErrorExt
140
+ def to_s
141
+ if ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
142
+ Prettifier.prettify_err( super )
143
+ else
144
+ super
133
145
  end
134
146
  end
135
147
  end
136
148
 
137
149
  class NiceQLConfig
138
150
  attr_accessor :pg_adapter_with_nicesql
151
+
152
+ attr_accessor :indentation_base
153
+
154
+ attr_accessor :open_bracket_is_new_liner
155
+
139
156
  def initialize
140
157
  self.pg_adapter_with_nicesql = false
158
+ self.indentation_base = 2
159
+ self.open_bracket_is_new_liner = false
141
160
  end
142
161
  end
143
162
 
144
163
 
145
164
  def self.configure
146
- @config ||= NiceQLConfig.new
147
-
148
- yield( @config )
165
+ yield( config )
149
166
 
150
- if @config.pg_adapter_with_nicesql
167
+ if config.pg_adapter_with_nicesql
151
168
  ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(PostgresAdapterNiceQL)
152
169
  end
153
170
  end
154
171
 
172
+ def self.config
173
+ @config ||= NiceQLConfig.new
174
+ end
175
+
155
176
 
156
177
  if defined? ::ActiveRecord::Base
178
+ ActiveRecord::StatementInvalid.include( Niceql::ErrorExt )
157
179
  ::ActiveRecord::Base.extend ArExtentions
158
180
  [::ActiveRecord::Relation, ::ActiveRecord::Associations::CollectionProxy].each { |klass| klass.send(:include, ArExtentions) }
159
181
  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.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - alekseyl
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-11 00:00:00.000000000 Z
11
+ date: 2017-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler