Class: SqlPostgres::Savepoint
- Inherits:
-
Object
- Object
- SqlPostgres::Savepoint
- Defined in:
- lib/sqlpostgres/Savepoint.rb
Overview
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"]]
**
Instance Method Summary collapse
-
#abort ⇒ Object
Abort this savepoint.
-
#commit ⇒ Object
Commit this savepoit.
-
#initialize(name, connection = Connection.default) ⇒ Savepoint
constructor
Create an SQL savepoint, yield, and then commit the savepoint.
Constructor Details
#initialize(name, connection = Connection.default) ⇒ Savepoint
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.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# 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 |
Instance Method Details
#abort ⇒ Object
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.
67 68 69 70 71 |
# File 'lib/sqlpostgres/Savepoint.rb', line 67 def abort unless @finished do_abort end end |
#commit ⇒ Object
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.
57 58 59 60 61 |
# File 'lib/sqlpostgres/Savepoint.rb', line 57 def commit unless @finished do_commit end end |