Module: Refile::Postgres::SmartTransaction
- Included in:
- Backend, Backend::Reader
- Defined in:
- lib/refile/postgres/smart_transaction.rb
Constant Summary collapse
- INIT_CONNECTION_ARG_ERROR_MSG =
"When initializing new Refile::Postgres::Backend first argument should be an instance of PG::Connection or a lambda/proc that yields it."
- PQTRANS_INTRANS =
(idle, within transaction block)
2
Instance Method Summary collapse
- #ensure_in_transaction(connection) ⇒ Object
- #smart_transaction(connection) ⇒ Object
- #with_connection ⇒ Object
Instance Method Details
#ensure_in_transaction(connection) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# 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) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# 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 ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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 |