Class: Pgtk::Pool::Txn

Inherits:
Object
  • Object
show all
Defined in:
lib/pgtk/pool.rb

Overview

A temporary class to execute a single SQL request.

Instance Method Summary collapse

Constructor Details

#initialize(conn, log) ⇒ Txn

Returns a new instance of Txn.



132
133
134
135
# File 'lib/pgtk/pool.rb', line 132

def initialize(conn, log)
  @conn = conn
  @log = log
end

Instance Method Details

#exec(query, args = [], result = 0) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/pgtk/pool.rb', line 137

def exec(query, args = [], result = 0)
  start = Time.now
  sql = query.is_a?(Array) ? query.join(' ') : query
  begin
    out = @conn.exec_params(sql, args, result) do |res|
      if block_given?
        yield res
      else
        rows = []
        res.each { |r| rows << r }
        rows
      end
    end
  rescue StandardError => e
    @log.error("#{sql}: #{e.message}")
    raise e
  end
  lag = Time.now - start
  if lag < 1
    @log.debug("#{sql}: #{(lag * 1000).round}ms / #{@conn.object_id}")
  else
    @log.info("#{sql}: #{format('%.02f', lag)}s")
  end
  out
end