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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a9ed47e9a89eea192bf6ee2734fc45549044a73ec9bc4add20ac30c2e692014
4
- data.tar.gz: b13bb7742969615d67aa8b3d340042c8fdc2dc5a8360c5c092a6f775c4887edc
3
+ metadata.gz: 1e022a597acf397e44458f241077015a8edaec759737e95eac06b1df212cf83c
4
+ data.tar.gz: '08bf19ebbebcb8c2daf355ce58675e04512346fa9addd21c9c82413a522cb363'
5
5
  SHA512:
6
- metadata.gz: 1ce328f09d0ae039cc6819a162bd5a14af315cf1ddb05eccb416d3deca13d9d59f64cefe28ac32b204912ec6768cbd2f78d2bb0f66cb9c70ab247757ddfa9895
7
- data.tar.gz: c4a7a8f75b4eb86ce149538374cc43120601243894bc3e6ce35417e34bcb46010cd632b54e9ee81a3a44e8948386ffa988079078f6a103453080c7910f0b23cd
6
+ metadata.gz: ef7e9fe5a549bb9cba6e4c3bea09c8c10fa6ff848141e9c1891bb9e12d2459c430c1132dd0e1bc3d27552319ebb11f12b86d6d2959654243651930c99298938a
7
+ data.tar.gz: b2b3835cc8b61bd81729090eac1391fa6a6dffc43c721e9f3d13d2b545eeb228ba586811eb0ff345011560fb33647ef76073f8c1ab8b76fb840d9dc50ee7088f
data/.rubocop.yml CHANGED
@@ -21,3 +21,5 @@ Metrics/MethodLength:
21
21
  Max: 100
22
22
  Metrics/PerceivedComplexity:
23
23
  Max: 10
24
+ Metrics/ParameterLists:
25
+ Max: 6
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(host: 'localhost', port:, dbname:, user:, password:)
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
- @conn.exec_params(query, args, result) do |res|
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
@@ -28,5 +28,5 @@ require_relative '../pgtk'
28
28
  # License:: MIT
29
29
  module Pgtk
30
30
  # Current version of the library.
31
- VERSION = '0.3.0'
31
+ VERSION = '0.4.0'
32
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgtk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko