Skip to content

Commit

Permalink
feat: Better checks in get_connection (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Munch authored Jan 16, 2024
2 parents 6967839 + c2e84c7 commit 06fbf7f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
16 changes: 10 additions & 6 deletions R/connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
#' @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
#' Username to login with
#' @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
Expand Down Expand Up @@ -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")) {
Expand Down
6 changes: 3 additions & 3 deletions man/get_connection.Rd

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

0 comments on commit 06fbf7f

Please sign in to comment.