Skip to content

Commit

Permalink
Use examplesIf
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Sep 7, 2021
1 parent b3a83f7 commit 1ec44fc
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 161 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ VignetteBuilder:
Encoding: UTF-8
LazyLoad: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
RoxygenNote: 7.1.1.9001
SystemRequirements: libpq >= 9.0: libpq-dev (deb) or
postgresql-devel (rpm)
Collate:
Expand Down
61 changes: 28 additions & 33 deletions R/PqConnection.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,11 @@ setMethod("dbGetInfo", "PqConnection", function(dbObj, ...) {
#' @param conn Connection to disconnect.
#' @export
#' @rdname Postgres
#' @examples
#' if (postgresHasDefault()) {
#' library(DBI)
#' # Pass more arguments as necessary to dbConnect()
#' con <- dbConnect(RPostgres::Postgres())
#' dbDisconnect(con)
#' }
#' @examplesIf postgresHasDefault()
#' library(DBI)
#' # Pass more arguments as necessary to dbConnect()
#' con <- dbConnect(RPostgres::Postgres())
#' dbDisconnect(con)
setMethod("dbConnect", "PqDriver",
function(drv, dbname = NULL,
host = NULL, port = NULL, password = NULL, user = NULL, service = NULL, ...,
Expand Down Expand Up @@ -278,36 +276,33 @@ setMethod("dbDisconnect", "PqConnection", function(conn, ...) {
#' \item{payload}{Content of notification}
#' }
#' If no notifications are available, return NULL
#' @examples
#' # For running the examples on systems without PostgreSQL connection:
#' if (postgresHasDefault()) {
#' library(DBI)
#' library(callr)
#'
#' # listen for messages on the grapevine
#' db_listen <- dbConnect(RPostgres::Postgres())
#' dbExecute(db_listen, "LISTEN grapevine")
#' @examplesIf postgresHasDefault()
#' library(DBI)
#' library(callr)
#'
#' # Start another process, which sends a message after a delay
#' rp <- r_bg(function () {
#' library(DBI)
#' Sys.sleep(0.3)
#' db_notify <- dbConnect(RPostgres::Postgres())
#' dbExecute(db_notify, "NOTIFY grapevine, 'psst'")
#' dbDisconnect(db_notify)
#' })
#' # listen for messages on the grapevine
#' db_listen <- dbConnect(RPostgres::Postgres())
#' dbExecute(db_listen, "LISTEN grapevine")
#'
#' # Sleep until we get the message
#' n <- NULL
#' while (is.null(n)) {
#' n <- RPostgres::postgresWaitForNotify(db_listen, 60)
#' }
#' stopifnot(n$payload == 'psst')
#' # Start another process, which sends a message after a delay
#' rp <- r_bg(function () {
#' library(DBI)
#' Sys.sleep(0.3)
#' db_notify <- dbConnect(RPostgres::Postgres())
#' dbExecute(db_notify, "NOTIFY grapevine, 'psst'")
#' dbDisconnect(db_notify)
#' })
#'
#' # Tidy up
#' rp$wait()
#' dbDisconnect(db_listen)
#' # Sleep until we get the message
#' n <- NULL
#' while (is.null(n)) {
#' n <- RPostgres::postgresWaitForNotify(db_listen, 60)
#' }
#' stopifnot(n$payload == 'psst')
#'
#' # Tidy up
#' rp$wait()
#' dbDisconnect(db_listen)
postgresWaitForNotify <- function (conn, timeout = 1) {
out <- connection_wait_for_notify(conn@ptr, timeout)
if ('pid' %in% names(out)) out else NULL
Expand Down
25 changes: 11 additions & 14 deletions R/PqResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,24 @@ setMethod("dbColumnInfo", "PqResult", function(res, ...) {
#' cast the parameter with e.g. `"$1::bigint"`.
#' @param ... Other arguments needed for compatibility with generic (currently
#' ignored).
#' @examples
#' # For running the examples on systems without PostgreSQL connection:
#' run <- postgresHasDefault()
#'
#' @examplesIf postgresHasDefault()
#' library(DBI)
#' if (run) db <- dbConnect(RPostgres::Postgres())
#' if (run) dbWriteTable(db, "usarrests", datasets::USArrests, temporary = TRUE)
#' db <- dbConnect(RPostgres::Postgres())
#' dbWriteTable(db, "usarrests", datasets::USArrests, temporary = TRUE)
#'
#' # Run query to get results as dataframe
#' if (run) dbGetQuery(db, "SELECT * FROM usarrests LIMIT 3")
#' dbGetQuery(db, "SELECT * FROM usarrests LIMIT 3")
#'
#' # Send query to pull requests in batches
#' if (run) res <- dbSendQuery(db, "SELECT * FROM usarrests")
#' if (run) dbFetch(res, n = 2)
#' if (run) dbFetch(res, n = 2)
#' if (run) dbHasCompleted(res)
#' if (run) dbClearResult(res)
#' res <- dbSendQuery(db, "SELECT * FROM usarrests")
#' dbFetch(res, n = 2)
#' dbFetch(res, n = 2)
#' dbHasCompleted(res)
#' dbClearResult(res)
#'
#' if (run) dbRemoveTable(db, "usarrests")
#' dbRemoveTable(db, "usarrests")
#'
#' if (run) dbDisconnect(db)
#' dbDisconnect(db)
#' @name postgres-query
NULL

Expand Down
4 changes: 3 additions & 1 deletion R/default.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
#' @examples
#' if (postgresHasDefault()) {
#' db <- postgresDefault()
#' dbListTables(db)
#' print(dbListTables(db))
#' dbDisconnect(db)
#' } else {
#' message("No database connection.")
#' }
postgresHasDefault <- function(...) {
tryCatch({
Expand Down
13 changes: 5 additions & 8 deletions R/quote.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ NULL
#' @param x A character vector to be quoted.
#' @param ... Other arguments needed for compatibility with generic (currently
#' ignored).
#' @examples
#' # For running the examples on systems without PostgreSQL connection:
#' run <- postgresHasDefault()
#'
#' @examplesIf postgresHasDefault()
#' library(DBI)
#' if (run) con <- dbConnect(RPostgres::Postgres())
#' con <- dbConnect(RPostgres::Postgres())
#'
#' x <- c("a", "b c", "d'e", "\\f")
#' if (run) dbQuoteString(con, x)
#' if (run) dbQuoteIdentifier(con, x)
#' if (run) dbDisconnect(con)
#' dbQuoteString(con, x)
#' dbQuoteIdentifier(con, x)
#' dbDisconnect(con)
#' @name quote
NULL

Expand Down
23 changes: 10 additions & 13 deletions R/tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,21 @@
#' all postgres servers (e.g. Amazon's Redshift). If `FALSE`, generates
#' a single SQL string. This is slower, but always supported.
#'
#' @examples
#' # For running the examples on systems without PostgreSQL connection:
#' run <- postgresHasDefault()
#'
#' @examplesIf postgresHasDefault()
#' library(DBI)
#' if (run) con <- dbConnect(RPostgres::Postgres())
#' if (run) dbListTables(con)
#' if (run) dbWriteTable(con, "mtcars", mtcars, temporary = TRUE)
#' if (run) dbReadTable(con, "mtcars")
#' con <- dbConnect(RPostgres::Postgres())
#' dbListTables(con)
#' dbWriteTable(con, "mtcars", mtcars, temporary = TRUE)
#' dbReadTable(con, "mtcars")
#'
#' if (run) dbListTables(con)
#' if (run) dbExistsTable(con, "mtcars")
#' dbListTables(con)
#' dbExistsTable(con, "mtcars")
#'
#' # A zero row data frame just creates a table definition.
#' if (run) dbWriteTable(con, "mtcars2", mtcars[0, ], temporary = TRUE)
#' if (run) dbReadTable(con, "mtcars2")
#' dbWriteTable(con, "mtcars2", mtcars[0, ], temporary = TRUE)
#' dbReadTable(con, "mtcars2")
#'
#' if (run) dbDisconnect(con)
#' dbDisconnect(con)
#' @name postgres-tables
NULL

Expand Down
25 changes: 11 additions & 14 deletions R/transactions.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,22 @@
#' [DBI::dbConnect()]
#' @param ... Unused, for extensibility.
#' @return A boolean, indicating success or failure.
#' @examples
#' # For running the examples on systems without PostgreSQL connection:
#' run <- postgresHasDefault()
#'
#' @examplesIf postgresHasDefault()
#' library(DBI)
#' if (run) con <- dbConnect(RPostgres::Postgres())
#' if (run) dbWriteTable(con, "USarrests", datasets::USArrests, temporary = TRUE)
#' if (run) dbGetQuery(con, 'SELECT count(*) from "USarrests"')
#' con <- dbConnect(RPostgres::Postgres())
#' dbWriteTable(con, "USarrests", datasets::USArrests, temporary = TRUE)
#' dbGetQuery(con, 'SELECT count(*) from "USarrests"')
#'
#' if (run) dbBegin(con)
#' if (run) dbExecute(con, 'DELETE from "USarrests" WHERE "Murder" > 1')
#' if (run) dbGetQuery(con, 'SELECT count(*) from "USarrests"')
#' if (run) dbRollback(con)
#' dbBegin(con)
#' dbExecute(con, 'DELETE from "USarrests" WHERE "Murder" > 1')
#' dbGetQuery(con, 'SELECT count(*) from "USarrests"')
#' dbRollback(con)
#'
#' # Rolling back changes leads to original count
#' if (run) dbGetQuery(con, 'SELECT count(*) from "USarrests"')
#' dbGetQuery(con, 'SELECT count(*) from "USarrests"')
#'
#' if (run) dbRemoveTable(con, "USarrests")
#' if (run) dbDisconnect(con)
#' dbRemoveTable(con, "USarrests")
#' dbDisconnect(con)
#' @name postgres-transactions
NULL

Expand Down
12 changes: 6 additions & 6 deletions man/Postgres.Rd

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

25 changes: 12 additions & 13 deletions man/postgres-query.Rd

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

23 changes: 11 additions & 12 deletions man/postgres-tables.Rd

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

25 changes: 12 additions & 13 deletions man/postgres-transactions.Rd

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

4 changes: 3 additions & 1 deletion man/postgresHasDefault.Rd

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

Loading

0 comments on commit 1ec44fc

Please sign in to comment.