niceql 0.3.0 → 0.5.0

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
  SHA256:
3
- metadata.gz: d33cb207d137e2c0e3b3fe59c2b9d525878a6def4a0be7b178b3555c98417148
4
- data.tar.gz: c02577dea05cb51e74f1478be10241c7e6ef5a4554a92ef38da85b706fb11737
3
+ metadata.gz: 61e17e9e56ddebb633c9ff0051e0fc400842f030a9d89236d484107dbf55389d
4
+ data.tar.gz: a80a5c8720b2da17f44e37e56a4543360e1424fad18c67d869ca466bbe19c0e0
5
5
  SHA512:
6
- metadata.gz: 7e03b3cbb34a26b1bc246b3aba84a775fca4cd0aaac2c87ed7b8310cbf3c796c3afa81d0407f3282d4bc888356c179ce1ce5bb88cb2cfbbcc5e1c959de00a24e
7
- data.tar.gz: 21a7493a4c284fb2d24866dab5d39b71eece8507a6f066bb7226bcfd25b28f2e60ec2dcea5c12134413458dda1076f815bda3f8d759cee341114dd8000e99af0
6
+ metadata.gz: 83302f8bff5ec7942338b93ec77739948606a34a85480e38ee19b16785391081d99865b6f31dd86ede787aef17061d0918bb8c77cb3eb84aa2bca3030bce30a2
7
+ data.tar.gz: 3ab3a19673a3e6d47d1d344c38bb42717526ca84e39a999c7933a15cfbaf07a361c6b7789089f3bc4c0dcb224d2d596050141ee460c5c6d3a532ea240505450a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ # 0.5.0
2
+ * BREAKING CHANGE! ActiveRecord compatibility extracted to the rails_sql_prettifier gem!
3
+ If you need niceql funcitonality with rails / active_record plz include rails_sql_prettifier has a
4
+ a versioning aligned to the active_record versions and has same DSL for AR niceql provide prior.
5
+
6
+ # 0.4.1
7
+ * description update
8
+
9
+ # 0.4.0
10
+ * merged PR https://github.com/alekseyl/niceql/pull/19, now Arel is also extended with niceql methods!!
11
+ * test and better niceql comparisons assertion
12
+ * tests were trialed against rails 4.2 and some additional conditions were added for later cases
13
+
14
+ # 0.3.0
15
+ * ruby forced to >= 2.4
16
+ * String match extension no longer needed
17
+ * fixed issue with missing HINT and DETAIL string ( https://github.com/alekseyl/niceql/issues/18 )
18
+ * both new and old activerecord StatementInvalid formats supported
19
+ * major prettify_pg_err refactoring ( much cleaner code now )
20
+
1
21
  # 0.2.0
2
22
  * Fix to issue https://github.com/alekseyl/niceql/pull/17#issuecomment-924278172. ActiveRecord base config is no longer a hash,
3
23
  so it does not have dig method, hence it's breaking the ar_using_pg_adapter? method.
data/README.md CHANGED
@@ -1,11 +1,13 @@
1
1
  # Niceql
2
2
 
3
- This is a small, nice, simple and no-dependency solution for SQL prettifying for Ruby.
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 on formatting/coloring are welcome
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 will corrupt your data, don't use on production!**
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 executing them, this will lead to better SQL-query debugging and much more clearer error messages
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
- # now error prettifying is configurable
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 ( or not if you are willing so ) to_niceql ( you need to call puts otherwise to_niceql looks ugly )
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
- # rails combines err with query, so don't forget to do it yourself:
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
- puts Niceql::Prettifier.prettify_pg_err(<<~ERR )
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
 
@@ -1,3 +1,3 @@
1
1
  module Niceql
2
- VERSION = "0.3.0"
2
+ VERSION = '0.5.0'
3
3
  end
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'
@@ -198,10 +183,11 @@ module Niceql
198
183
  end
199
184
  end
200
185
 
186
+ private_class_method
201
187
  def extract_err_caret_line( err_address_line, err_line, sql_body, err )
202
188
  # LINE could be quoted ( both sides and sometimes only from one ):
203
189
  # "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(/\.\.\.(.+)/) ).try(:[], 1)
190
+ err_quote = (err_address_line.match(/\.\.\.(.+)\.\.\./) || err_address_line.match(/\.\.\.(.+)/) )&.send(:[], 1)
205
191
 
206
192
  # line[2] is original err caret line i.e.: ' ^'
207
193
  # err_address_line[/LINE \d+:/].length+1..-1 - is a position from error quote begin
@@ -223,75 +209,17 @@ module Niceql
223
209
  end
224
210
  end
225
211
 
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
212
  class NiceQLConfig
251
- def ar_using_pg_adapter?
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
-
213
+ attr_accessor :indentation_base, :open_bracket_is_newliner
266
214
 
267
215
  def initialize
268
- self.pg_adapter_with_nicesql = false
269
216
  self.indentation_base = 2
270
217
  self.open_bracket_is_newliner = false
271
- self.prettify_active_record_log_output = false
272
- self.prettify_pg_errors = ar_using_pg_adapter?
273
218
  end
274
219
  end
275
220
 
276
- def self.configure
277
- yield( config )
278
-
279
- return unless defined? ::ActiveRecord::Base
221
+ def self.configure; yield( config ) end
280
222
 
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
223
+ def self.config; @config ||= NiceQLConfig.new end
296
224
 
297
225
  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 sql prettifier, it splits, indent and colorize SQL query and PG errors if any }
13
- spec.description = %q{This is simple and nice sql prettifier, it splits, indent and colorize SQL query and PG error if any }
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.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alekseyl
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-12 00:00:00.000000000 Z
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,8 +108,10 @@ dependencies:
122
108
  - - ">="
123
109
  - !ruby/object:Gem::Version
124
110
  version: '0'
125
- description: 'This is simple and nice sql prettifier, it splits, indent and colorize
126
- SQL query and PG error if any '
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. '
127
115
  email:
128
116
  - leshchuk@gmail.com
129
117
  executables: []
@@ -170,9 +158,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
158
  - !ruby/object:Gem::Version
171
159
  version: '0'
172
160
  requirements: []
173
- rubygems_version: 3.1.4
161
+ rubygems_version: 3.0.9
174
162
  signing_key:
175
163
  specification_version: 4
176
- summary: This is simple and nice sql prettifier, it splits, indent and colorize SQL
177
- 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.
178
166
  test_files: []