module Refile::Postgres::SmartTransaction

Constants

INIT_CONNECTION_ARG_ERROR_MSG
PQTRANS_INTRANS

Public Instance Methods

ensure_in_transaction(connection) { || ... } click to toggle source
# File lib/refile/postgres/smart_transaction.rb, line 19
def ensure_in_transaction(connection)
  if connection.transaction_status == PQTRANS_INTRANS
    yield
  else
    connection.transaction do
      yield
    end
  end
end
smart_transaction(connection) { |handle| ... } click to toggle source
# File lib/refile/postgres/smart_transaction.rb, line 7
def smart_transaction(connection)
  result = nil
  ensure_in_transaction(connection) do
    begin
      handle = connection.lo_open(oid)
      result = yield handle
      connection.lo_close(handle)
    end
  end
  result
end
with_connection() { |connection_or_proc| ... } click to toggle source
# File lib/refile/postgres/smart_transaction.rb, line 29
def with_connection
  if @connection_or_proc.is_a?(PG::Connection)
    yield @connection_or_proc
  else
    if @connection_or_proc.is_a?(Proc)
      block_has_been_executed = false
      value = nil
      @connection_or_proc.call do |connection| 
        block_has_been_executed = true
        raise ArgumentError.new(INIT_CONNECTION_ARG_ERROR_MSG) unless connection.is_a?(PG::Connection)
        value = yield connection
      end
      raise ArgumentError.new(INIT_CONNECTION_ARG_ERROR_MSG) unless block_has_been_executed
      value
    else
      raise ArgumentError.new(INIT_CONNECTION_ARG_ERROR_MSG)
    end
  end
end