class SqlPostgres::Savepoint
This class handles a savepoint. Example:
** example: savepoint
Transaction.new(connection) do insert = Insert.new('foo', connection) insert.insert('i', 1) insert.exec Savepoint.new('bar', connection) do |sp| insert = Insert.new('foo', connection) insert.insert('i', 2) sp.abort end insert = Insert.new('foo', connection) insert.insert('i', 3) insert.exec end p connection.query("select i from foo order by i") #[["1"], ["3"]]
**
Public Class Methods
new(name, connection = Connection.default) { |self| ... }
click to toggle source
Create an SQL savepoint, yield, and then commit the savepoint. If an exception occurs, the savepoint is aborted.
If no connection is given, then the default connection is used.
# File lib/sqlpostgres/Savepoint.rb, line 35 def initialize(name, connection = Connection.default) @name = name @state = :open @finished = false @connection = connection @connection.exec("savepoint #{name}") begin result = yield(self) commit result rescue Exception abort raise end end
Public Instance Methods
abort()
click to toggle source
Abort this savepoint. This is done for you when an exception occurs within the block you passed to “new”. Call this when you want to abort a savepoint without throwing an exception.
# File lib/sqlpostgres/Savepoint.rb, line 67 def abort unless @finished do_abort end end
commit()
click to toggle source
Commit this savepoit. This is done for you unless an exception occurs within the block you passed to “new”. Call this when you want to commit the savepoint before raising an exception – in other words, when you want to keep your database changes even though an exception is about to occur.
# File lib/sqlpostgres/Savepoint.rb, line 57 def commit unless @finished do_commit end end
Private Instance Methods
do_abort()
click to toggle source
# File lib/sqlpostgres/Savepoint.rb, line 80 def do_abort @connection.exec("rollback to #{@name}") release_savepoint @finished = true end
do_commit()
click to toggle source
# File lib/sqlpostgres/Savepoint.rb, line 75 def do_commit release_savepoint @finished = true end
release_savepoint()
click to toggle source
# File lib/sqlpostgres/Savepoint.rb, line 86 def release_savepoint @connection.exec("release savepoint #{@name}") end