assert_connection {assertions} | R Documentation |
Assert input is a database connection
Description
Assert the input object is a database connection, specifically of the "DBIConnection" class, which is the standard virtual class used by the DBI package for database connections. Note this assertion does not test if the database connection is valid and/or active.
Usage
assert_connection(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
Arguments
x |
An object to assert is a database connection |
msg |
A custom error message displayed if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
Details
This function is designed for use with objects inheriting from the "DBIConnection" class, which is used widely across database connection implementations in R. As other database interface packages are introduced, additional checks may be added to support other connection classes.
Value
invisible(TRUE)
if x
is a valid database connection, otherwise aborts with an error message.
Examples
try({
# Assuming a valid DBI connection `conn`:
assert_connection(conn) # Passes if `conn` is a DBI connection
assert_connection(42) # Fails with error message
})