diff --git a/NEWS.md b/NEWS.md index aa4aadb1..5c35d14f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,14 @@ # SCDB (development version) +## Minor Improvements and Fixes + +* Improved checks on `get_connection()`: + - If given, `host` does not need to look like an IP address (e.g. "localhost" is not unrealistic). + - A `character` input for `port` is allowed if it is a string of digits. + - Now checks if `timezone` and `timezone_out` is an IANA time zone. + +* `get_connection()` now checks the value of any `timezone` and `timezone_out` arguments. + # SCDB 0.3 * Added support for Microsoft SQL Server using ODBC (#77). diff --git a/R/connection.R b/R/connection.R index e51476d0..5df6fe28 100644 --- a/R/connection.R +++ b/R/connection.R @@ -8,7 +8,7 @@ #' @param host #' Character string giving the ip of the host to connect to #' @param port -#' Host port to connect to (numeric) +#' Host port to connect to. Must be a number or a numeric string. #' @param dbname #' Name of the database located at the host #' @param user @@ -16,9 +16,9 @@ #' @param password #' Password to login with #' @param timezone -#' Sets the timezone of DBI::dbConnect() +#' Sets the timezone of DBI::dbConnect(). Must be in [OlsonNames()]. #' @param timezone_out -#' Sets the timezone_out of DBI::dbConnect() +#' Sets the timezone_out of DBI::dbConnect(). Must be in [OlsonNames()]. #' @param ... #' Additional parameters sent to DBI::dbConnect() #' @inheritParams RPostgres::dbConnect_PqDriver @@ -47,13 +47,17 @@ get_connection <- function(drv = RPostgres::Postgres(), check_interrupts = TRUE) { # Check arguments - checkmate::assert_character(host, pattern = r"{^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$}", null.ok = TRUE) + checkmate::assert_character(host, null.ok = TRUE) + if (is.character(port)) { + checkmate::assert_character(port, pattern = "^[:digit:]*$") + port <- as.numeric(port) # nocov + } checkmate::assert_numeric(port, null.ok = TRUE) checkmate::assert_character(dbname, null.ok = TRUE) checkmate::assert_character(user, null.ok = TRUE) checkmate::assert_character(password, null.ok = TRUE) - checkmate::assert_character(timezone, null.ok = TRUE) - checkmate::assert_character(timezone_out, null.ok = TRUE) + checkmate::assert_choice(timezone, OlsonNames(), null.ok = TRUE) + checkmate::assert_choice(timezone_out, OlsonNames(), null.ok = TRUE) # Set PostgreSQL-specific options if (inherits(drv, "PqDriver")) { diff --git a/man/get_connection.Rd b/man/get_connection.Rd index 72e427e8..d8cda298 100644 --- a/man/get_connection.Rd +++ b/man/get_connection.Rd @@ -23,7 +23,7 @@ get_connection( \item{host}{Character string giving the ip of the host to connect to} -\item{port}{Host port to connect to (numeric)} +\item{port}{Host port to connect to. Must be a number or a numeric string.} \item{dbname}{Name of the database located at the host} @@ -31,9 +31,9 @@ get_connection( \item{password}{Password to login with} -\item{timezone}{Sets the timezone of DBI::dbConnect()} +\item{timezone}{Sets the timezone of DBI::dbConnect(). Must be in \code{\link[=OlsonNames]{OlsonNames()}}.} -\item{timezone_out}{Sets the timezone_out of DBI::dbConnect()} +\item{timezone_out}{Sets the timezone_out of DBI::dbConnect(). Must be in \code{\link[=OlsonNames]{OlsonNames()}}.} \item{...}{Additional parameters sent to DBI::dbConnect()}