rails_sql_prettifier 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fd1acaf8f6f82312ccb1422330645c2365b3e4f93c9874da2645c62866310836
4
+ data.tar.gz: bad3c23c4af4d0d3d3d8200f2ae38309df6cc332e92ab6466713f1956ee91739
5
+ SHA512:
6
+ metadata.gz: 3b16a4ee0ba376dc3f7ae3e3d2eaec46bd08cdda2e525b4a59be827e82ae5c161d90fd236e418bb226a8cca54aaebf6e5e590161fa529f52c25e0f025db27e42
7
+ data.tar.gz: ab755ea34ed0bc3c910ee02895a94a35c7a1f4de4d123188f66985a9cd68950f8d708cdc3ba8d5cf7a590403b8e77c4186ebae44ecc31d5e1fcc8cc0dca63836
data/.byebug_history ADDED
@@ -0,0 +1,36 @@
1
+ c
2
+ ActiveRecord.version <= Gem::Version.new(5)
3
+ c
4
+ ActiveRecord.version
5
+ exit
6
+ ActiveRecord.version
7
+ c
8
+ exit
9
+ si.sql
10
+ si
11
+ c
12
+ sql_body
13
+ n
14
+ s
15
+ original_sql_query
16
+ err
17
+ s
18
+ up
19
+ s
20
+ up
21
+ s
22
+ up
23
+ s
24
+ super
25
+ try(:sql)
26
+ Niceql.config.prettify_pg_errors
27
+ c
28
+ exit
29
+ err_template
30
+ si.to_s
31
+ exit
32
+ err_caret_line
33
+ err_quote_caret_offset
34
+ c
35
+ err.lines[1].index( '...' ).to_i
36
+ err_caret_line
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /inspectionProfiles/
11
+ .idea
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ rails_sql_prettifier_ar_5
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.7.2
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4
5
+ - 2.5
6
+ - 2.3
7
+ - 2.6
data/CHANGELOG.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 alekseyl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # RailsSQLPrettifier
2
+
3
+ This is an ActiveRecord integration for a niceql gem.
4
+ Niceql is a small, nice, simple and zero dependency solution for SQL prettifying for Ruby.
5
+
6
+ Any reasonable suggestions are welcome.
7
+
8
+ **Please pay attention: until 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
+
10
+
11
+ ## Before/After
12
+ ### SQL prettifier:
13
+ ![alt text](https://github.com/alekseyl/niceql/raw/master/to_niceql.png "To_niceql")
14
+
15
+ ### PG errors prettifier
16
+
17
+ before:
18
+ ![alt text](https://github.com/alekseyl/niceql/raw/master/err_was.png "To_niceql")
19
+
20
+ after:
21
+ ![alt text](https://github.com/alekseyl/niceql/raw/master/err_now.png "To_niceql")
22
+
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ ```ruby
29
+ gem 'rails_sql_prettifier'
30
+ ```
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+ # if you are using rails, you may want to install niceql config:
36
+ rails g niceql:install
37
+
38
+ Or install it yourself as:
39
+
40
+ $ gem install niceql
41
+
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 execution. Formatted SQL will lead to much better SQL-query debugging and much more clearer error messages
48
+ # if you are using Postgresql as a data source.
49
+ # BUT do not use it in production until https://github.com/alekseyl/niceql/issues/16 is resolved
50
+ #
51
+ # You can adjust pg_adapter in production but do it at your own risk!
52
+ #
53
+ # If you need to debug SQL queries in production use exec_niceql
54
+ #
55
+ # default value for pg_adapter_with_nicesql: false
56
+ # uncomment next string to enable in development
57
+ # c.pg_adapter_with_nicesql = Rails.env.development?
58
+
59
+ # uncomment next string if you want to log prettified SQL inside ActiveRecord logging.
60
+ # default: false
61
+ # c.prettify_active_record_log_output = true
62
+
63
+ # Error prettifying is also configurable
64
+ # default: defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
65
+ # c.prettify_pg_errors = defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
66
+
67
+ # spaces count for one indentation, default is 2
68
+ c.indentation_base = 2
69
+
70
+ # setting open_bracket_is_newliner to true will start opening brackets '(' with nested subqueries from new line
71
+ # i.e. SELECT * FROM ( SELECT * FROM tags ) tags; will transform to:
72
+ # SELECT *
73
+ # FROM
74
+ # (
75
+ # SELECT * FROM tags
76
+ # ) tags;
77
+ # when open_bracket_is_newliner is false:
78
+ # SELECT *
79
+ # FROM (
80
+ # SELECT * FROM tags
81
+ # ) tags;
82
+ # default: false
83
+ c.open_bracket_is_newliner = false
84
+ end
85
+ ```
86
+
87
+ ## Usage
88
+
89
+ ### With ActiveRecord
90
+
91
+ ```ruby
92
+ # puts colorized and formatted corresponding SQL query
93
+ Model.scope.niceql
94
+
95
+ # only formatting without colorization, you can run output of to_niceql as a SQL query in connection.execute
96
+ Model.scope.to_niceql
97
+
98
+ # prettify PG errors if scope runs with any
99
+ Model.scope_with_err.exec_niceql
100
+ ```
101
+
102
+ ### Raw usage of niceql
103
+
104
+ ```ruby
105
+
106
+ puts Niceql::Prettifier.prettify_sql("SELECT * FROM ( VALUES(1), (2) ) AS tmp")
107
+ #=> SELECT *
108
+ #=> FROM ( VALUES(1), (2) ) AS tmp
109
+
110
+ puts Niceql::Prettifier.prettify_multiple("SELECT * FROM ( VALUES(1), (2) ) AS tmp; SELECT * FROM table")
111
+
112
+ #=> SELECT *
113
+ #=> FROM ( VALUES(1), (2) ) AS tmp;
114
+ #=>
115
+ #=> SELECT *
116
+ #=> FROM table
117
+
118
+
119
+ puts Niceql::Prettifier.prettify_pg_err( pg_err_output, sql_query )
120
+
121
+ # to get real nice result you should execute prettified version (i.e. execute( prettified_sql ) !) of query on your DB!
122
+ # otherwise you will not get such a nice output
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 )
130
+ ERROR: VALUES in FROM must have an alias
131
+ LINE 2: FROM ( VALUES(1), (2) )
132
+ ^
133
+ HINT: For example, FROM (VALUES ...) [AS] foo.
134
+ ERR
135
+
136
+
137
+ # ERROR: VALUES in FROM must have an alias
138
+ # LINE 2: FROM ( VALUES(1), (2) )
139
+ # ^
140
+ # HINT: For example, FROM (VALUES ...) [AS] foo.
141
+ # SELECT err
142
+ # FROM ( VALUES(1), (2) )
143
+ # ^
144
+ # ORDER BY 1
145
+
146
+ ```
147
+
148
+ ## Customizing colors
149
+ If your console support more colors or different schemes, or if you prefer different colorization, then you can override ColorizeString methods.
150
+ Current colors were selected with dark and white console themes in mind, so a niceql colorization works good for dark, and good enough for white.
151
+
152
+ ##
153
+
154
+ ## Contributing
155
+
156
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alekseyl/niceql.
157
+
158
+ ## License
159
+
160
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_sql_prettifier"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/err_now.png ADDED
Binary file
data/err_was.png ADDED
Binary file
@@ -0,0 +1,34 @@
1
+ require 'benchmark/ips'
2
+
3
+ # 2 + 1 = 3 object
4
+ def slow_plus
5
+ 'foo' + 'bar'
6
+ end
7
+
8
+ # 2 + 1 = 3 object
9
+ def slow_concat
10
+ 'foo'.concat 'bar'
11
+ end
12
+
13
+ # 2 + 1 = 3 object
14
+ def slow_append
15
+ 'foo' << 'bar'
16
+ end
17
+
18
+ # 1 object
19
+ def fast
20
+ 'foo' 'bar'
21
+ end
22
+
23
+ def fast_interpolation
24
+ "#{'foo'}#{'bar'}"
25
+ end
26
+
27
+ Benchmark.ips do |x|
28
+ x.report('String#+') { slow_plus }
29
+ x.report('String#concat') { slow_concat }
30
+ x.report('String#append') { slow_append }
31
+ x.report('"foo" "bar"') { fast }
32
+ x.report('"#{\'foo\'}#{\'bar\'}"') { fast_interpolation }
33
+ x.compare!
34
+ end
@@ -0,0 +1,34 @@
1
+ require 'benchmark/ips'
2
+
3
+ # 2 + 1 = 3 object
4
+ def slow_plus
5
+ 'foo' + 'bar'
6
+ end
7
+
8
+ # 2 + 1 = 3 object
9
+ def slow_concat
10
+ 'foo'.concat 'bar'
11
+ end
12
+
13
+ # 2 + 1 = 3 object
14
+ def slow_append
15
+ 'foo' << 'bar'
16
+ end
17
+
18
+ # 1 object
19
+ def fast
20
+ 'foo' 'bar'
21
+ end
22
+
23
+ def fast_interpolation
24
+ "#{'foo'}#{'bar'}"
25
+ end
26
+
27
+ Benchmark.ips do |x|
28
+ x.report('String#+') { slow_plus }
29
+ x.report('String#concat') { slow_concat }
30
+ x.report('String#append') { slow_append }
31
+ x.report('"foo" "bar"') { fast }
32
+ x.report('"#{\'foo\'}#{\'bar\'}"') { fast_interpolation }
33
+ x.compare!
34
+ end