diff --git a/R/executionSettings.R b/R/executionSettings.R new file mode 100644 index 0000000..fca6cf3 --- /dev/null +++ b/R/executionSettings.R @@ -0,0 +1,167 @@ + +# ExecutionSettings ---- + +#' @description +#' An R6 class to define an ExecutionSettings object +#' +#' @export +ExecutionSettings <- R6::R6Class( + classname = "ExecutionSettings", + public = list( + initialize = function(connectionDetails = NULL, + connection = NULL, + cdmDatabaseSchema = NULL, + workDatabaseSchema = NULL, + tempEmulationSchema = NULL, + targetCohortTable = NULL, + cdmSourceName = NULL) { + stopifnot(is.null(connectionDetails) || is.null(connection)) + .setClass(private = private, key = "connectionDetails", value = connectionDetails, class = "ConnectionDetails") + .setClass(private = private, key = ".connection", value = connection, + class = "DatabaseConnectorJdbcConnection", nullable = TRUE) + .setString(private = private, key = ".cdmDatabaseSchema", value = cdmDatabaseSchema) + .setString(private = private, key = ".workDatabaseSchema", value = workDatabaseSchema) + .setString(private = private, key = ".tempEmulationSchema", value = tempEmulationSchema) + .setString(private = private, key = ".targetCohortTable", value = targetCohortTable) + .setString(private = private, key = ".cdmSourceName", value = cdmSourceName) + }, + + getDbms = function() { + dbms <- private$connectionDetails$dbms + return(dbms) + }, + + # connect to database + connect = function() { + + # check if private$connection is NULL + conObj <- private$.connection + if (is.null(conObj)) { + private$.connection <- DatabaseConnector::connect(private$connectionDetails) + } else{ + cli::cat_bullet( + "Connection object already open", + bullet = "info", + bullet_col = "blue" + ) + } + }, + + # disconnect to database + disconnect = function() { + + # check if private$connection is NULL + conObj <- private$.connection + if (class(conObj) == "DatabaseConnectorJdbcConnection") { + # disconnect connection + DatabaseConnector::disconnect(private$.connection) + private$.connection <- NULL + } + + cli::cat_bullet( + "Connection object has been disconected", + bullet = "info", + bullet_col = "blue" + ) + invisible(conObj) + }, + + #TODO make this more rigorous + # add warning if no connection available + getConnection = function() { + conObj <- private$.connection + return(conObj) + } + + ), + + private = list( + connectionDetails = NULL, + .connection = NULL, + .cdmDatabaseSchema = NULL, + .workDatabaseSchema = NULL, + .tempEmulationSchema = NULL, + .targetCohortTable = NULL, + .cdmSourceName = NULL + ), + + active = list( + + cdmDatabaseSchema = function(value) { + # return the value if nothing added + if(missing(value)) { + cds <- private$.cdmDatabaseSchema + return(cds) + } + # replace the cdmDatabaseSchema + .setString(private = private, key = ".cdmDatabaseSchema", value = value) + cli::cat_bullet( + glue::glue("Replaced {crayon::cyan('cdmDatabaseSchema')} with {crayon::green(value)}"), + bullet = "info", + bullet_col = "blue" + ) + }, + + workDatabaseSchema = function(value) { + # return the value if nothing added + if(missing(value)) { + cds <- private$.workDatabaseSchema + return(cds) + } + # replace the workDatabaseSchema + .setString(private = private, key = ".workDatabaseSchema", value = value) + cli::cat_bullet( + glue::glue("Replaced {crayon::cyan('workDatabaseSchema')} with {crayon::green(value)}"), + bullet = "info", + bullet_col = "blue" + ) + }, + + + tempEmulationSchema = function(value) { + # return the value if nothing added + if(missing(value)) { + tes <- private$.tempEmulationSchema + return(tes) + } + # replace the tempEmulationSchema + .setString(private = private, key = ".tempEmulationSchema", value = value) + cli::cat_bullet( + glue::glue("Replaced {crayon::cyan('tempEmulationSchema')} with {crayon::green(value)}"), + bullet = "info", + bullet_col = "blue" + ) + }, + + targetCohortTable = function(value) { + # return the value if nothing added + if(missing(value)) { + tct <- private$.targetCohortTable + return(tct) + } + # replace the targetCohortTable + .setString(private = private, key = ".targetCohortTable", value = value) + cli::cat_bullet( + glue::glue("Replaced {crayon::cyan('targetCohortTable')} with {crayon::green(value)}"), + bullet = "info", + bullet_col = "blue" + ) + }, + + cdmSourceName = function(value) { + # return the value if nothing added + if(missing(value)) { + csn <- private$.cdmSourceName + return(csn) + } + # replace the cdmSourceName + .setString(private = private, key = ".cdmSourceName", value = value) + cli::cat_bullet( + glue::glue("Replaced {crayon::cyan('cdmSourceName')} with {crayon::green(value)}"), + bullet = "info", + bullet_col = "blue" + ) + } + + ) +)