Commit 0b196d1c authored by Jonathan Mang's avatar Jonathan Mang 💡
Browse files

refactor: more robust functions

parent 05b5d66a
Pipeline #23529 passed with stages
in 43 minutes and 6 seconds
......@@ -46,3 +46,4 @@ Language: en-US
LazyData: true
SystemRequirements: libpq >= 9.0: libpq-dev (deb) or
postgresql-devel (rpm)
RoxygenNote: 7.1.1
......@@ -28,18 +28,20 @@
#' )}
#' @export
#'
close_all_connections <- function(logfile_dir, headless) {
feedback(
"Doing application cleanup",
findme = "8b224d503c",
logfile_dir = logfile_dir,
headless = headless
)
closeAllConnections()
feedback(
"Closed all file-connections.",
findme = "0c5cb72ecc",
logfile_dir = logfile_dir,
headless = headless
)
}
close_all_connections <-
function(logfile_dir = tempdir(),
headless = TRUE) {
feedback(
"Doing application cleanup",
findme = "8b224d503c",
logfile_dir = logfile_dir,
headless = headless
)
closeAllConnections()
feedback(
"Closed all file/database connections.",
findme = "0c5cb72ecc",
logfile_dir = logfile_dir,
headless = headless
)
}
......@@ -64,19 +64,16 @@ db_connection <- function(db_name,
timeout = 30,
logfile_dir = NULL,
lib_path = NULL) {
db_con <- NULL
tryCatch({
stopifnot(
is.character(db_name),
is.character(db_type),
is.logical(headless),
is.logical(from_env),
ifelse(is.null(settings), TRUE, is.list(settings)),
is.numeric(timeout),
ifelse(is.null(logfile_dir), TRUE, is.character(logfile_dir)),
ifelse(is.null(lib_path), TRUE, is.character(lib_path))
)
stopifnot(is.character(db_name))
stopifnot(is.character(db_type))
stopifnot(is.logical(headless))
stopifnot(is.logical(from_env))
stopifnot(ifelse(is.null(settings), TRUE, is.list(settings)))
stopifnot(is.numeric(timeout))
stopifnot(ifelse(is.null(logfile_dir), TRUE, dir.exists(logfile_dir)))
stopifnot(ifelse(is.null(lib_path), TRUE, file.exists(lib_path)))
res <- tryCatch({
db_type <- toupper(db_type)
db_name <- toupper(db_name)
......@@ -110,6 +107,7 @@ db_connection <- function(db_name,
"password" = password
)
## Check if all necessary parameters are filled:
for (param in names(necessary_vars)) {
if (necessary_vars[[param]] == "" ||
......@@ -118,7 +116,6 @@ db_connection <- function(db_name,
}
}
if (db_type == "ORACLE") {
if (is.null(lib_path)) {
if (isTRUE(from_env)) {
......@@ -131,8 +128,8 @@ db_connection <- function(db_name,
stopifnot(lib_path != "" || !is.null(lib_path))
## create driver
drv <- RJDBC::JDBC("oracle.jdbc.OracleDriver",
classPath = lib_path)
drv <-
RJDBC::JDBC(driverClass = "oracle.jdbc.OracleDriver", classPath = lib_path)
if (isTRUE(from_env)) {
sid <- Sys.getenv(paste0(db_name, "_SID"))
......@@ -145,13 +142,18 @@ db_connection <- function(db_name,
if (db_name == "" || is.null(db_name)) {
stop("Missing SID for db-connection to oracle.")
} else {
feedback(print_this = "`SID` is empty. Using the `db_name` instead",
type = "Warning",
findme = "e38041e91c")
feedback(
print_this = "`SID` is empty. Using the `db_name` instead",
type = "Warning",
logfile_dir = logfile_dir,
headless = headless,
findme = "e38041e91c"
)
sid <- db_name
}
}
## create URL
url <-
paste0("jdbc:oracle:thin:@//", host, ":", port, "/", sid)
......@@ -166,10 +168,14 @@ db_connection <- function(db_name,
)
conn
}, error = function(e) {
conn <- NULL
conn
}, finally = function(f) {
return(conn)
DIZutils::feedback(
print_this = paste0("Error while connection to oracle: ", e),
type = "Error",
logfile_dir = logfile_dir,
headless = headless,
findme = "0a50850ccd"
)
return(NA)
})
} else if (db_type == "POSTGRES") {
drv <- RPostgres::Postgres()
......@@ -186,21 +192,29 @@ db_connection <- function(db_name,
)
conn
}, error = function(e) {
conn <- NULL
conn
}, finally = function(f) {
return(conn)
DIZutils::feedback(
print_this = paste0("Error while connection to postgres: ", e),
type = "Error",
logfile_dir = logfile_dir,
headless = headless,
findme = "0a50850ccd"
)
return(NA)
})
}
if (is.null(db_con)) {
feedback(
"DB connection error",
print_this = "DB connection error",
findme = "9431c8c61f",
logfile_dir = logfile_dir,
headless = headless,
type = "Error"
)
stop()
}
## Return the connection, but without the `return()`
## (because war are not inside of a function in the try-part):
db_con
},
error = function(cond) {
feedback(
......@@ -211,7 +225,7 @@ db_connection <- function(db_name,
headless = headless,
findme = "c16b60a6ff"
)
return(NULL)
return(NA)
})
return(db_con)
return(res)
}
......@@ -4,7 +4,7 @@
\alias{close_all_connections}
\title{Cleanup function to unset/close all open connections}
\usage{
close_all_connections(logfile_dir, headless)
close_all_connections(logfile_dir = tempdir(), headless = TRUE)
}
\arguments{
\item{logfile_dir}{(Optional, String, default: "tempdir()")
......
......@@ -21,17 +21,18 @@ db_connection(
\item{db_type}{A character. Type of the database system. Currently
implemented systems are: 'postgres', 'oracle'.}
\item{headless}{A boolean (default: FALSE). Indicating, if the function is
run only in the console (headless = TRUE) or on a GUI frontend
(headless = FALSE).}
\item{headless}{A boolean (default: `FALSE`). Indicating, if the function is
run only in the console (`headless = TRUE`) or on a GUI frontend
(`headless = FALSE`).}
\item{from_env}{A boolean (default: TRUE). Should database connection
\item{from_env}{A boolean (default: `TRUE`). Should database connection
be read from the environment or from a settings file.}
\item{settings}{A list. Required if `from_env=TRUE`. A list containing
settings for the database connection. Required fields are 'host',
'db_name', 'port', 'user' and 'password'.
Additionally for Oracle DB's: 'sid'.}
\item{settings}{A list. Required if `from_env = TRUE`. A list containing
settings for the database connection. Required fields are `host`,
`db_name`, `port`, `user` and `password`.
Additionally for Oracle DB's: `sid` (instead of `db_name`).
If `settings` is set, `from_env` will be set to `FALSE` automatically.}
\item{timeout}{A timeout in sec. for the db-connection establishment.
Values below 2 seconds are not recommended.
......
......@@ -23,7 +23,7 @@ feedback(
\item{print_this}{(Optional, String, default: "")}
\item{type}{(Optional, String, default: "Info")
E.g. "Warning", "Error. Default: "Info"}
E.g. "Warning", "Error". Default: "Info"}
\item{ui}{(Optional, Boolean/String, default: FALSE)
If true, the message will also be printed to the user in form of a modal.
......
......@@ -10,7 +10,7 @@ feedback_get_formatted_string(print_this, type, findme, prefix, suffix)
\item{print_this}{(Optional, String, default: "")}
\item{type}{(Optional, String, default: "Info")
E.g. "Warning", "Error. Default: "Info"}
E.g. "Warning", "Error". Default: "Info"}
\item{findme}{(Optional, String, default: "")
Recommended with length 10.
......
......@@ -19,7 +19,7 @@ feedback_to_console(
\item{print_this}{(Optional, String, default: "")}
\item{type}{(Optional, String, default: "Info")
E.g. "Warning", "Error. Default: "Info"}
E.g. "Warning", "Error". Default: "Info"}
\item{findme}{(Optional, String, default: "")
Recommended with length 10.
......
......@@ -10,7 +10,7 @@ feedback_to_logfile(print_this, type, findme, prefix, suffix, logfile_dir)
\item{print_this}{(Optional, String, default: "")}
\item{type}{(Optional, String, default: "Info")
E.g. "Warning", "Error. Default: "Info"}
E.g. "Warning", "Error". Default: "Info"}
\item{findme}{(Optional, String, default: "")
Recommended with length 10.
......
......@@ -10,7 +10,7 @@ feedback_to_ui(print_this, type, logfile_dir, headless = FALSE)
\item{print_this}{(Optional, String, default: "")}
\item{type}{(Optional, String, default: "Info")
E.g. "Warning", "Error. Default: "Info"}
E.g. "Warning", "Error". Default: "Info"}
\item{logfile_dir}{(Optional, String, default: "tempdir()")
The absolute path to folder where the logfile will be stored.}
......
......@@ -19,9 +19,9 @@ to get the environment variables with 'SYSTEM_KEY', e.g. 'I2B2_DBNAME').}
\item{logfile_dir}{(Optional, String, default: "tempdir()")
The absolute path to folder where the logfile will be stored.}
\item{headless}{A boolean (default: FALSE). Indicating, if the function is
run only in the console (headless = TRUE) or on a GUI frontend
(headless = FALSE).}
\item{headless}{A boolean (default: `FALSE`). Indicating, if the function is
run only in the console (`headless = TRUE`) or on a GUI frontend
(`headless = FALSE`).}
\item{ignore.presets}{(boolean) Only return something if all elements
from the presets are found? These are currently `host`, `port`, `user`,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment