Learn R Programming

rpg (version 1.6)

begin: Transaction support

Description

Start, commit or rollback transactions or savepoints

Usage

begin()

commit(savepoint = NULL)

rollback(savepoint = NULL)

savepoint()

Arguments

savepoint

an object produced by savepoint

Value

savepoint: a savepoint object

Details

These functions allow manipulation of database transaction states. If no savepoint object is supplied, then an attempt is made to commit or rollback the current transaction.

The savepoint function will initiate a transaction if one is not currently active. In that case, no actual PostgreSQL savepoint will be used. Rolling back the savepoint will rollback the initiated transaction. If a tranaction is active, then a named savepoint will be generated. You can rollback to the database state when savepoint was called or commit all changes.

Examples

Run this code
# NOT RUN {
createdb("rpgtesting")
connect("rpgtesting")
begin()
sp1 = savepoint()

# nest savepoints
sp2 = savepoint()
data(mtcars)
write_table(mtcars, "testtab", overwrite = TRUE)
list_tables()
rollback(sp2)

list_tables()
# nest savepoints
sp3 = savepoint()
sp4 = savepoint()
write_table(mtcars, "testtab", overwrite = TRUE)
commit(sp4)
list_tables()
rollback(sp3)
list_tables()

rollback(sp1)
rollback()
disconnect()
dropdb("rpgtesting")
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab