Class: Pgtk::Pool::Txn
- Inherits:
-
Object
- Object
- Pgtk::Pool::Txn
- Defined in:
- lib/pgtk/pool.rb
Overview
A temporary class to execute a single SQL request.
Instance Method Summary collapse
-
#exec(query, args = [], result = 0) {|Hash| ... } ⇒ Object
Exec a single parameterized command.
-
#initialize(conn, log) ⇒ Txn
constructor
A new instance of Txn.
Constructor Details
#initialize(conn, log) ⇒ Txn
Returns a new instance of Txn.
301 302 303 304 |
# File 'lib/pgtk/pool.rb', line 301 def initialize(conn, log) @conn = conn @log = log end |
Instance Method Details
#exec(query, args = [], result = 0) {|Hash| ... } ⇒ Object
Exec a single parameterized command.
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/pgtk/pool.rb', line 311 def exec(query, args = [], result = 0) start = Time.now sql = query.is_a?(Array) ? query.join(' ') : query begin out = if args.empty? @conn.exec(sql) do |res| if block_given? yield(res) else res.each.to_a end end else @conn.exec_params(sql, args, result) do |res| if block_given? yield(res) else res.each.to_a end end end rescue StandardError => e @log.error("#{sql} -> #{e.}") raise(e) end lag = Time.now - start if lag < 1 @log.debug("#{sql} >> #{start.ago} / #{@conn.object_id}") else @log.info("#{sql} >> #{start.ago}") end out end |