Skip to content

Commit

Permalink
Merge pull request #351 from jakob-r/main
Browse files Browse the repository at this point in the history
- New `postgresIsTransacting()` (#351, @jakob-r).
  • Loading branch information
krlmlr authored Dec 12, 2021
2 parents eb50523 + cbb73f0 commit de53077
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(Postgres)
export(Redshift)
export(postgresDefault)
export(postgresHasDefault)
export(postgresIsTransacting)
export(postgresWaitForNotify)
exportClasses(PqConnection)
exportClasses(PqDriver)
Expand Down
13 changes: 13 additions & 0 deletions R/PqConnection.R
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,19 @@ postgresWaitForNotify <- function (conn, timeout = 1) {
if ('pid' %in% names(out)) out else NULL
}

#' Return whether a transaction is ongoing
#'
#' Detect whether the transaction is active for the given connection. A
#' transaction might be started with [dbBegin()] or wrapped within
#' [DBI::dbWithTransaction()].
#' @export
#' @param conn a [PqConnection-class] object, produced by
#' [DBI::dbConnect()]
#' @return A boolean, indicating if a transaction is ongoing.
postgresIsTransacting <- function(conn) {
connection_is_transacting(conn@ptr)
}

#' Determine database type for R vector.
#'
#' @export
Expand Down
20 changes: 20 additions & 0 deletions man/postgresIsTransacting.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test-isTransacting.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
context("isTransacting")

test_that("isTransacting detects transactions correctly", {
skip_on_cran()
con <- postgresDefault()
expect_false(postgresIsTransacting(con))
dbBegin(con)
expect_true(postgresIsTransacting(con))
dbCommit(con)
expect_false(postgresIsTransacting(con))
})

0 comments on commit de53077

Please sign in to comment.