pgtk 0.3.0 → 0.4.0
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 +4 -4
- data/.rubocop.yml +2 -0
- data/lib/pgtk/pool.rb +29 -5
- data/lib/pgtk/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e022a597acf397e44458f241077015a8edaec759737e95eac06b1df212cf83c
|
4
|
+
data.tar.gz: '08bf19ebbebcb8c2daf355ce58675e04512346fa9addd21c9c82413a522cb363'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef7e9fe5a549bb9cba6e4c3bea09c8c10fa6ff848141e9c1891bb9e12d2459c430c1132dd0e1bc3d27552319ebb11f12b86d6d2959654243651930c99298938a
|
7
|
+
data.tar.gz: b2b3835cc8b61bd81729090eac1391fa6a6dffc43c721e9f3d13d2b545eeb228ba586811eb0ff345011560fb33647ef76073f8c1ab8b76fb840d9dc50ee7088f
|
data/.rubocop.yml
CHANGED
data/lib/pgtk/pool.rb
CHANGED
@@ -28,14 +28,33 @@ require_relative '../pgtk'
|
|
28
28
|
# Copyright:: Copyright (c) 2019 Yegor Bugayenko
|
29
29
|
# License:: MIT
|
30
30
|
class Pgtk::Pool
|
31
|
+
# A temporary class for logging.
|
32
|
+
class Log
|
33
|
+
def initialize(out)
|
34
|
+
@out = out
|
35
|
+
end
|
36
|
+
|
37
|
+
def debug(msg)
|
38
|
+
if @out.respond_to?(:debug)
|
39
|
+
@out.debug(msg)
|
40
|
+
elsif @out.respond_to?(:puts)
|
41
|
+
@out.puts(msg)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
31
46
|
# Constructor.
|
32
|
-
def initialize(
|
47
|
+
def initialize(
|
48
|
+
host: 'localhost', port:, dbname:, user:,
|
49
|
+
password:, log: STDOUT
|
50
|
+
)
|
33
51
|
@host = host
|
34
52
|
@port = port
|
35
53
|
@dbname = dbname
|
36
54
|
@user = user
|
37
55
|
@password = password
|
38
56
|
@pool = Queue.new
|
57
|
+
@log = Log.new(log)
|
39
58
|
end
|
40
59
|
|
41
60
|
# Start it with a fixed number of connections. The amount of connections
|
@@ -51,6 +70,7 @@ class Pgtk::Pool
|
|
51
70
|
user: @user, password: @password
|
52
71
|
)
|
53
72
|
end
|
73
|
+
@log.debug("PostgreSQL pool started with #{max} connections")
|
54
74
|
self
|
55
75
|
end
|
56
76
|
|
@@ -90,7 +110,7 @@ class Pgtk::Pool
|
|
90
110
|
# here: https://www.rubydoc.info/gems/pg/0.17.1/PG%2FConnection:exec_params
|
91
111
|
def exec(query, args = [], result = 0)
|
92
112
|
connect do |c|
|
93
|
-
t = Txn.new(c)
|
113
|
+
t = Txn.new(c, @log)
|
94
114
|
if block_given?
|
95
115
|
t.exec(query, args, result) do |res|
|
96
116
|
yield res
|
@@ -111,19 +131,21 @@ class Pgtk::Pool
|
|
111
131
|
# end
|
112
132
|
def transaction
|
113
133
|
connect do |c|
|
114
|
-
t = Txn.new(c)
|
134
|
+
t = Txn.new(c, @log)
|
115
135
|
yield t
|
116
136
|
end
|
117
137
|
end
|
118
138
|
|
119
139
|
# A temporary class to execute a single SQL request.
|
120
140
|
class Txn
|
121
|
-
def initialize(conn)
|
141
|
+
def initialize(conn, log)
|
122
142
|
@conn = conn
|
143
|
+
@log = log
|
123
144
|
end
|
124
145
|
|
125
146
|
def exec(query, args = [], result = 0)
|
126
|
-
|
147
|
+
start = Time.now
|
148
|
+
out = @conn.exec_params(query, args, result) do |res|
|
127
149
|
if block_given?
|
128
150
|
yield res
|
129
151
|
else
|
@@ -132,6 +154,8 @@ class Pgtk::Pool
|
|
132
154
|
rows
|
133
155
|
end
|
134
156
|
end
|
157
|
+
@log.debug("#{query}: #{(start - Time.now).round}ms")
|
158
|
+
out
|
135
159
|
end
|
136
160
|
end
|
137
161
|
|
data/lib/pgtk/version.rb
CHANGED