diff --git a/NAMESPACE b/NAMESPACE index e0098385..4f569ea1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(Postgres) export(Redshift) export(postgresDefault) export(postgresHasDefault) +export(postgresIsTransacting) export(postgresWaitForNotify) exportClasses(PqConnection) exportClasses(PqDriver) diff --git a/R/PqConnection.R b/R/PqConnection.R index fb2162d3..f72ac3f6 100644 --- a/R/PqConnection.R +++ b/R/PqConnection.R @@ -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 diff --git a/man/postgresIsTransacting.Rd b/man/postgresIsTransacting.Rd new file mode 100644 index 00000000..aff30ad7 --- /dev/null +++ b/man/postgresIsTransacting.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/postgresIsTransacting.R +\name{postgresIsTransacting} +\alias{postgresIsTransacting} +\title{Return whether a transaction is ongoing} +\usage{ +postgresIsTransacting(conn) +} +\arguments{ +\item{conn}{a \linkS4class{PqConnection} object, produced by +\code{\link[DBI:dbConnect]{DBI::dbConnect()}}} +} +\value{ +A boolean, indicating if a transaction is ongoing. +} +\description{ +Detect whether the transaction is active for the given connection. A +transaction might be started with \code{\link[=dbBegin]{dbBegin()}} or wrapped within +\code{\link[DBI:dbWithTransaction]{DBI::dbWithTransaction()}}. +} diff --git a/tests/testthat/test-isTransacting.R b/tests/testthat/test-isTransacting.R new file mode 100644 index 00000000..de8eb785 --- /dev/null +++ b/tests/testthat/test-isTransacting.R @@ -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)) +})