pp_sql 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -3
- data/lib/pp_sql/version.rb +1 -1
- data/lib/pp_sql.rb +38 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4372a6aad0cedf3145eff498f19164e31e2918c
|
4
|
+
data.tar.gz: a8596ffe4a78038d3817fe0b6127761cf66777e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae497360ecd64748259be104332eb38ae2a07a833e58e63f222bf8b14c4ec9523ea40495622f1c95ca52c6d0a21a3891a60e8b6d7cdc1551b4282b06ddbafe62
|
7
|
+
data.tar.gz: 4208494b16e764d76460c59e6038c7af289c1749118791c17f3f13a342ec9d05b8d60a7a333376805c26d25dd4ca66f0e83c5fab7afbffb6bdba97e9f62bdcd5
|
data/README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# PpSql
|
2
2
|
|
3
|
-
Replace stanpard `
|
4
|
-
gem for pretty SQL code output in console. For example:
|
3
|
+
Replace stanpard `ActiveRecord#to_sql` method with [`anbt-sql-formatter`](https://github.com/sonota88/anbt-sql-formatter)
|
4
|
+
gem for pretty SQL code output in console. Rails log will be formatted also. For example:
|
5
|
+
|
5
6
|
```
|
6
7
|
# was
|
7
8
|
User.all # =>
|
@@ -15,13 +16,17 @@ SELECT
|
|
15
16
|
`users`
|
16
17
|
```
|
17
18
|
|
19
|
+
## Require
|
20
|
+
|
21
|
+
Ruby 2.0+
|
22
|
+
|
18
23
|
## Usage
|
19
24
|
|
20
25
|
```
|
21
26
|
Post.first.to_sql
|
22
27
|
```
|
23
28
|
|
24
|
-
If you need yo use it in some
|
29
|
+
If you need yo use it in some custom strings, you may include this funcionality with
|
25
30
|
|
26
31
|
```
|
27
32
|
String.send :include, PpSql::ToSqlBeautify
|
data/lib/pp_sql/version.rb
CHANGED
data/lib/pp_sql.rb
CHANGED
@@ -1,13 +1,5 @@
|
|
1
1
|
module PpSql
|
2
|
-
module
|
3
|
-
def to_sql
|
4
|
-
_sql_formatter.format(super)
|
5
|
-
end
|
6
|
-
|
7
|
-
def pp_sql
|
8
|
-
puts to_sql
|
9
|
-
end
|
10
|
-
|
2
|
+
module Formatter
|
11
3
|
private
|
12
4
|
|
13
5
|
def _sql_formatter
|
@@ -20,10 +12,47 @@ module PpSql
|
|
20
12
|
@_sql_formatter = AnbtSql::Formatter.new(rule)
|
21
13
|
end
|
22
14
|
end
|
15
|
+
module ToSqlBeautify
|
16
|
+
include Formatter
|
17
|
+
def to_sql
|
18
|
+
_sql_formatter.format(super)
|
19
|
+
end
|
20
|
+
|
21
|
+
def pp_sql
|
22
|
+
puts to_sql
|
23
|
+
end
|
24
|
+
end
|
25
|
+
module LogSubscriberPrettyPrint
|
26
|
+
include Formatter
|
27
|
+
def sql(event)
|
28
|
+
return unless logger.debug?
|
29
|
+
|
30
|
+
self.class.runtime += event.duration
|
31
|
+
|
32
|
+
payload = event.payload
|
33
|
+
|
34
|
+
return if ActiveRecord::LogSubscriber::IGNORE_PAYLOAD_NAMES.include?(payload[:name])
|
35
|
+
|
36
|
+
name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
|
37
|
+
sql = payload[:sql]
|
38
|
+
binds = nil
|
39
|
+
|
40
|
+
unless (payload[:binds] || []).empty?
|
41
|
+
binds = " " + payload[:binds].map { |attr| render_bind(attr) }.inspect
|
42
|
+
end
|
43
|
+
|
44
|
+
name = colorize_payload_name(name, payload[:name])
|
45
|
+
# only this line was rewritten from the AR
|
46
|
+
sql = color(_sql_formatter.format(sql.dup), sql_color(sql), true)
|
47
|
+
|
48
|
+
debug " #{name} #{sql}#{binds}"
|
49
|
+
end
|
50
|
+
end
|
23
51
|
class Railtie < Rails::Railtie
|
24
52
|
initializer "pp_sql.override_to_sql" do
|
25
53
|
ActiveSupport.on_load(:active_record) do
|
26
54
|
ActiveRecord::Relation.send(:prepend, ToSqlBeautify)
|
55
|
+
ActiveRecord::LogSubscriber.send(:prepend, LogSubscriberPrettyPrint)
|
27
56
|
end
|
28
57
|
end
|
29
58
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pp_sql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kvokka
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|