Skip to content

Commit

Permalink
add execution settings
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlavallee92 committed Oct 24, 2024
1 parent 3fb33c4 commit 7365ac0
Showing 1 changed file with 167 additions and 0 deletions.
167 changes: 167 additions & 0 deletions R/executionSettings.R
Original file line number Diff line number Diff line change
@@ -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"
)
}

)
)

0 comments on commit 7365ac0

Please sign in to comment.