Skip to content
Permalink
Browse files

Use `requireNamespace("pkg", quietly = TRUE)` for RMySQL and RSQLite …

…in the examples/tests since these are "Suggests" packages (i.e. not "Depends")
  • Loading branch information
bborgesr committed Nov 3, 2017
1 parent f5d278b commit 4205feb8a685384fb8db93114c35220fb9611752
Showing with 340 additions and 313 deletions.
  1. +34 −29 R/DBI-connection-transaction.R
  2. +34 −29 R/DBI-connection.R
  3. +20 −15 R/DBI-object.R
  4. +20 −15 R/DBI.R
  5. +26 −21 R/dbplyr.R
  6. +34 −29 man/DBI-connection.Rd
  7. +20 −15 man/DBI-object.Rd
  8. +20 −15 man/dbPool.Rd
  9. +26 −21 man/dplyr-db-methods.Rd
  10. +0 −26 man/pool.Rd
  11. +31 −26 man/poolWithTransaction.Rd
  12. +75 −72 tests/testthat/test-dplyr.R
@@ -106,40 +106,45 @@ setMethod("dbWithTransaction", "Pool", function(conn, code) {
#' @return \code{func}'s return value.
#' @export
#' @examples
#' pool <- dbPool(RSQLite::SQLite(), dbname = ":memory:")
#' if (requireNamespace("RSQLite", quietly = TRUE)) {
#' pool <- dbPool(RSQLite::SQLite(), dbname = ":memory:")
#'
#' dbWriteTable(pool, "cars", head(cars, 3))
#' dbReadTable(pool, "cars") # there are 3 rows
#' dbWriteTable(pool, "cars", head(cars, 3))
#' dbReadTable(pool, "cars") # there are 3 rows
#'
#' ## successful transaction
#' poolWithTransaction(pool, function(conn) {
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (1, 1);")
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (2, 2);")
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (3, 3);")
#' })
#' dbReadTable(pool, "cars") # there are now 6 rows
#'
#' ## failed transaction -- note the missing comma
#' tryCatch(
#' ## successful transaction
#' poolWithTransaction(pool, function(conn) {
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (1, 1);")
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (2, 2);")
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (3, 3);")
#' })
#' dbReadTable(pool, "cars") # there are now 6 rows
#'
#' ## failed transaction -- note the missing comma
#' tryCatch(
#' poolWithTransaction(pool, function(conn) {
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (1, 1);")
#' dbExecute(conn, "INSERT INTO cars (speed dist) VALUES (2, 2);")
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (3, 3);")
#' }),
#' error = identity
#' )
#' dbReadTable(pool, "cars") # still 6 rows
#'
#' ## early exit, silently
#' poolWithTransaction(pool, function(conn) {
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (1, 1);")
#' dbExecute(conn, "INSERT INTO cars (speed dist) VALUES (2, 2);")
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (2, 2);")
#' if (nrow(dbReadTable(conn, "cars")) > 7) dbBreak()
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (3, 3);")
#' }),
#' error = identity
#' )
#' dbReadTable(pool, "cars") # still 6 rows
#'
#' ## early exit, silently
#' poolWithTransaction(pool, function(conn) {
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (1, 1);")
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (2, 2);")
#' if (nrow(dbReadTable(conn, "cars")) > 7) dbBreak()
#' dbExecute(conn, "INSERT INTO cars (speed, dist) VALUES (3, 3);")
#' })
#' dbReadTable(pool, "cars") # still 6 rows
#'
#' poolClose(pool)
#' })
#' dbReadTable(pool, "cars") # still 6 rows
#'
#' poolClose(pool)
#'
#' } else {
#' message("Please install the 'RSQLite' package to run this example")
#' }
poolWithTransaction <- function(pool, func) {
conn <- poolCheckout(pool)
on.exit(poolReturn(conn))
@@ -20,48 +20,53 @@ NULL
#'
#' @name DBI-connection
#' @examples
#' mtcars1 <- mtcars[ c(1:16), ] # first half of the mtcars dataset
#' mtcars2 <- mtcars[-c(1:16), ] # second half of the mtcars dataset
#' if (requireNamespace("RSQLite", quietly = TRUE)) {
#' mtcars1 <- mtcars[ c(1:16), ] # first half of the mtcars dataset
#' mtcars2 <- mtcars[-c(1:16), ] # second half of the mtcars dataset
#'
#' pool <- dbPool(RSQLite::SQLite(), dbname = ":memory:")
#' pool <- dbPool(RSQLite::SQLite(), dbname = ":memory:")
#'
#' # write the mtcars1 table into the database
#' dbWriteTable(pool, "mtcars", mtcars1, row.names = TRUE)
#' # write the mtcars1 table into the database
#' dbWriteTable(pool, "mtcars", mtcars1, row.names = TRUE)
#'
#' # list the current tables in the database
#' dbListTables(pool)
#' # list the current tables in the database
#' dbListTables(pool)
#'
#' # read the "mtcars" table from the database (only 16 rows)
#' dbReadTable(pool, "mtcars")
#' # read the "mtcars" table from the database (only 16 rows)
#' dbReadTable(pool, "mtcars")
#'
#' # append mtcars2 to the "mtcars" table already in the database
#' dbWriteTable(pool, "mtcars", mtcars2, row.names = TRUE, append = TRUE)
#' # append mtcars2 to the "mtcars" table already in the database
#' dbWriteTable(pool, "mtcars", mtcars2, row.names = TRUE, append = TRUE)
#'
#' # read the "mtcars" table from the database (all 32 rows)
#' dbReadTable(pool, "mtcars")
#' # read the "mtcars" table from the database (all 32 rows)
#' dbReadTable(pool, "mtcars")
#'
#' # get the names of the columns in the databases's table
#' dbListFields(pool, "mtcars")
#' # get the names of the columns in the databases's table
#' dbListFields(pool, "mtcars")
#'
#' # use dbExecute to change the "mpg" and "cyl" values of the 1st row
#' dbExecute(pool,
#' paste(
#' "UPDATE mtcars",
#' "SET mpg = '22.0', cyl = '10'",
#' "WHERE row_names = 'Mazda RX4'"
#' # use dbExecute to change the "mpg" and "cyl" values of the 1st row
#' dbExecute(pool,
#' paste(
#' "UPDATE mtcars",
#' "SET mpg = '22.0', cyl = '10'",
#' "WHERE row_names = 'Mazda RX4'"
#' )
#' )
#' )
#'
#' # read the 1st row of "mtcars" table to confirm the previous change
#' dbGetQuery(pool, "SELECT * FROM mtcars WHERE row_names = 'Mazda RX4'")
#' # read the 1st row of "mtcars" table to confirm the previous change
#' dbGetQuery(pool, "SELECT * FROM mtcars WHERE row_names = 'Mazda RX4'")
#'
#' # drop the "mtcars" table from the database
#' dbRemoveTable(pool, "mtcars")
#' # drop the "mtcars" table from the database
#' dbRemoveTable(pool, "mtcars")
#'
#' # list the current tables in the database
#' dbListTables(pool)
#' # list the current tables in the database
#' dbListTables(pool)
#'
#' poolClose(pool)
#' poolClose(pool)
#'
#' } else {
#' message("Please install the 'RSQLite' package to run this example")
#' }
NULL

## Throw error here since this would require keeping a connection
@@ -10,25 +10,30 @@ NULL
#'
#' @name DBI-object
#' @examples
#' pool <- dbPool(RSQLite::SQLite(), dbname = ":memory:")
#' if (requireNamespace("RSQLite", quietly = TRUE)) {
#' pool <- dbPool(RSQLite::SQLite(), dbname = ":memory:")
#'
#' dbGetInfo(pool)
#' dbIsValid(pool)
#' dbGetInfo(pool)
#' dbIsValid(pool)
#'
#' dbDataType(pool, 1:5)
#' dbDataType(pool, 1)
#' dbDataType(pool, TRUE)
#' dbDataType(pool, Sys.Date())
#' dbDataType(pool, Sys.time())
#' dbDataType(pool, Sys.time() - as.POSIXct(Sys.Date()))
#' dbDataType(pool, c("x", "abc"))
#' dbDataType(pool, list(raw(10), raw(20)))
#' dbDataType(pool, I(3))
#' dbDataType(pool, iris)
#' dbDataType(pool, 1:5)
#' dbDataType(pool, 1)
#' dbDataType(pool, TRUE)
#' dbDataType(pool, Sys.Date())
#' dbDataType(pool, Sys.time())
#' dbDataType(pool, Sys.time() - as.POSIXct(Sys.Date()))
#' dbDataType(pool, c("x", "abc"))
#' dbDataType(pool, list(raw(10), raw(20)))
#' dbDataType(pool, I(3))
#' dbDataType(pool, iris)
#'
#' poolClose(pool)
#' poolClose(pool)
#'
#' dbIsValid(pool)
#' dbIsValid(pool)
#'
#' } else {
#' message("Please install the 'RSQLite' package to run this example")
#' }
NULL

#' @param dbObj,obj,... See \code{\link[DBI]{dbDataType}}.
35 R/DBI.R
@@ -38,23 +38,28 @@ NULL
#'
#' @export
#' @examples
#' pool <- dbPool(
#' drv = RMySQL::MySQL(),
#' dbname = "shinydemo",
#' host = "shiny-demo.csa7qlmguqrf.us-east-1.rds.amazonaws.com",
#' username = "guest",
#' password = "guest"
#' )
#' if (requireNamespace("RMySQL", quietly = TRUE)) {
#' pool <- dbPool(
#' drv = RMySQL::MySQL(),
#' dbname = "shinydemo",
#' host = "shiny-demo.csa7qlmguqrf.us-east-1.rds.amazonaws.com",
#' username = "guest",
#' password = "guest"
#' )
#'
#' dbGetQuery(pool, "SELECT * from City LIMIT 5;")
#' #> ID Name CountryCode District Population
#' #> 1 1 Kabul AFG Kabol 1780000
#' #> 2 2 Qandahar AFG Qandahar 237500
#' #> 3 3 Herat AFG Herat 186800
#' #> 4 4 Mazar-e-Sharif AFG Balkh 127800
#' #> 5 5 Amsterdam NLD Noord-Holland 731200
#' dbGetQuery(pool, "SELECT * from City LIMIT 5;")
#' #> ID Name CountryCode District Population
#' #> 1 1 Kabul AFG Kabol 1780000
#' #> 2 2 Qandahar AFG Qandahar 237500
#' #> 3 3 Herat AFG Herat 186800
#' #> 4 4 Mazar-e-Sharif AFG Balkh 127800
#' #> 5 5 Amsterdam NLD Noord-Holland 731200
#'
#' poolClose(pool)
#' poolClose(pool)
#'
#' } else {
#' message("Please install the 'RMySQL' package to run this example")
#' }
dbPool <- function(drv, ..., validateQuery = NULL) {
state <- new.env(parent = emptyenv())
state$validateQuery <- validateQuery
@@ -14,38 +14,43 @@ NULL
#' @name dplyr-db-methods
#'
#' @examples
#' library(dplyr)
#' if (requireNamespace("RSQLite", quietly = TRUE)) {
#' library(dplyr)
#'
#' pool <- dbPool(RSQLite::SQLite(), dbname = ":memory:")
#' pool <- dbPool(RSQLite::SQLite(), dbname = ":memory:")
#'
#' # describe the type of the pool/its connections
#' db_desc(pool)
#' # describe the type of the pool/its connections
#' db_desc(pool)
#'
#' # use dplyr syntax to copy a table into the database
#' copy_to(pool, mtcars, "mtcars", temporary = FALSE)
#' # use dplyr syntax to copy a table into the database
#' copy_to(pool, mtcars, "mtcars", temporary = FALSE)
#'
#' # list the current tables in the database
#' db_list_tables(pool)
#' # list the current tables in the database
#' db_list_tables(pool)
#'
#' # extract a table from the database
#' mtcars_db <- tbl(pool, "mtcars")
#' # extract a table from the database
#' mtcars_db <- tbl(pool, "mtcars")
#'
#' # select only 3 columns
#' mtcars_db_thin <- select(mtcars_db, mpg, cyl, disp)
#' # select only 3 columns
#' mtcars_db_thin <- select(mtcars_db, mpg, cyl, disp)
#'
#' # get the names of the columns in the databases's table
#' db_query_fields(pool, "mtcars")
#' # get the names of the columns in the databases's table
#' db_query_fields(pool, "mtcars")
#'
#' # get the number of rows in the databases's table
#' db_query_rows(pool, "mtcars")
#' # get the number of rows in the databases's table
#' db_query_rows(pool, "mtcars")
#'
#' # drop the "mtcars" table from the database
#' db_drop_table(pool, "mtcars")
#' # drop the "mtcars" table from the database
#' db_drop_table(pool, "mtcars")
#'
#' # list the current tables in the database
#' db_list_tables(pool)
#' # list the current tables in the database
#' db_list_tables(pool)
#'
#' poolClose(pool)
#' poolClose(pool)
#'
#' } else {
#' message("Please install the 'RSQLite' package to run this example")
#' }
NULL

stopIfTemporary <- function(temporary) {

Some generated files are not rendered by default. Learn more.

0 comments on commit 4205feb

Please sign in to comment.
You can’t perform that action at this time.