Sha256: f6f8fb6ef1227f87204c8ad236e00ac23e16e554084c925c40d859ff2103f9e5

Contents?: true

Size: 1.16 KB

Versions: 9

Compression:

Stored size: 1.16 KB

Contents

# rubocop:disable Metrics/MethodLength
# rubocop:disable Style/IfUnlessModifier

# private
module Simple::SQL::SimpleTransactions
  def tx_nesting_level
    @tx_nesting_level ||= 0
  end

  def tx_nesting_level=(tx_nesting_level)
    @tx_nesting_level = tx_nesting_level
  end

  def transaction(&_block)
    # Notes: by using "ensure" (as opposed to rescue) we are rolling back
    # both when an exception was raised and when a value was thrown. This
    # also means we have to track whether or not to rollback. i.e. do roll
    # back when we yielded to &block but not otherwise.
    #
    # Also the transaction support is a bit limited: you cannot rollback.
    # Rolling back from inside a nested transaction would require SAVEPOINT
    # support; without the code is simpler at least :)

    if tx_nesting_level == 0
      exec "BEGIN"
      tx_started = true
    end

    self.tx_nesting_level += 1

    return_value = yield

    # Only commit if we started a transaction here.
    if tx_started
      exec "COMMIT"
      tx_committed = true
    end

    return_value
  ensure
    self.tx_nesting_level -= 1
    if tx_started && !tx_committed
      exec "ROLLBACK"
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
simple-sql-0.4.9 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.8 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.7 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.5 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.4 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.3 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.2 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.1 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.0 lib/simple/sql/simple_transactions.rb