Skip to content

Commit

Permalink
Add named argument to transaction functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Sep 5, 2014
1 parent 0b68bf7 commit a028da4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
3 changes: 3 additions & 0 deletions NEWS
@@ -1,5 +1,8 @@
Version 0.11.6.9000

- `dbBegin()`, `dbCommit()` and `dbRollback()` gain a named argument to
specify named savepoints.

- Remove SqliteObject and dbObject classes, modifying SqliteDriver,
SqliteConnection, and SqliteResult to use composition instead of multiple
inheritance.
Expand Down
34 changes: 28 additions & 6 deletions R/ConnectionTransactions.R
Expand Up @@ -12,6 +12,8 @@ NULL
#'
#' @param conn a \code{\linkS4class{SQLiteConnection}} object, produced by
#' \code{\link[DBI]{dbConnect}}
#' @param name Supply a name to use a named savepoint. This allows you to
#' nest multiple transaction
#' @return A boolean, indicating success or failure.
#' @examples
#' con <- dbConnect(SQLite(), ":memory:")
Expand All @@ -34,27 +36,47 @@ NULL
#' dbCommit(con)
#' dbGetQuery(con, "SELECT count(*) FROM arrests")[1, ]
#'
#' # Named savepoints can be nested --------------------------------------------
#' dbBegin(con, "a")
#' dbBegin(con, "b")
#' dbRollback(con, "b")
#' dbCommit(con, "a")
#'
#' dbDisconnect(con)
#' @name transactions
NULL

#' @export
#' @rdname transactions
setMethod("dbBegin", "SQLiteConnection", function(conn) {
dbGetQuery(conn, "BEGIN")
setMethod("dbBegin", "SQLiteConnection", function(conn, name = NULL) {
if (is.null(name)) {
dbGetQuery(conn, "BEGIN")
} else {
dbGetQuery(conn, paste("SAVEPOINT ", name))
}

TRUE
})

#' @export
#' @rdname transactions
setMethod("dbCommit", "SQLiteConnection", function(conn) {
dbGetQuery(conn, "COMMIT")
setMethod("dbCommit", "SQLiteConnection", function(conn, name = NULL) {
if (is.null(name)) {
dbGetQuery(conn, "COMMIT")
} else {
dbGetQuery(conn, paste("RELEASE SAVEPOINT ", name))
}

TRUE
})

#' @export
#' @rdname transactions
setMethod("dbRollback", "SQLiteConnection", function(conn) {
dbGetQuery(conn, "ROLLBACK")
setMethod("dbRollback", "SQLiteConnection", function(conn, name = NULL) {
if (is.null(name)) {
dbGetQuery(conn, "ROLLBACK")
} else {
dbGetQuery(conn, paste("ROLLBACK TO ", name))
}
TRUE
})
15 changes: 12 additions & 3 deletions man/transactions.Rd
Expand Up @@ -7,15 +7,18 @@
\alias{transactions}
\title{SQLite transaction management.}
\usage{
\S4method{dbBegin}{SQLiteConnection}(conn)
\S4method{dbBegin}{SQLiteConnection}(conn, name = NULL)

\S4method{dbCommit}{SQLiteConnection}(conn)
\S4method{dbCommit}{SQLiteConnection}(conn, name = NULL)

\S4method{dbRollback}{SQLiteConnection}(conn)
\S4method{dbRollback}{SQLiteConnection}(conn, name = NULL)
}
\arguments{
\item{conn}{a \code{\linkS4class{SQLiteConnection}} object, produced by
\code{\link[DBI]{dbConnect}}}

\item{name}{Supply a name to use a named savepoint. This allows you to
nest multiple transaction}
}
\value{
A boolean, indicating success or failure.
Expand Down Expand Up @@ -47,6 +50,12 @@ dbClearResult(rs)
dbCommit(con)
dbGetQuery(con, "SELECT count(*) FROM arrests")[1, ]

# Named savepoints can be nested --------------------------------------------
dbBegin(con, "a")
dbBegin(con, "b")
dbRollback(con, "b")
dbCommit(con, "a")

dbDisconnect(con)
}

0 comments on commit a028da4

Please sign in to comment.