diff --git a/R/ConnectionHandler.R b/R/ConnectionHandler.R index e9051de..68a0b4d 100644 --- a/R/ConnectionHandler.R +++ b/R/ConnectionHandler.R @@ -35,12 +35,23 @@ #' @export #' @field connectionDetails DatabaseConnector connectionDetails object #' @field con DatabaseConnector connection object -#' @field isActive Is connection active or not#' +#' @field isActive Is connection active or not' #' @field snakeCaseToCamelCase (Optional) Boolean. return the results columns in camel case (default) +#' @field queryOptions (Optional) Active field. Named list of options that are wrapped when queries are translated. For example `list(sqlRenderTempSchema = 'my_scratch_space')`. Will override default global behaviour without altering global option state ConnectionHandler <- R6::R6Class( classname = "ConnectionHandler", private = list( - .dbms = "" + .dbms = "", + .queryOptions = list() + ), + active = list( + queryOptions = function (val) { + if (missing(val)) + return(private$.queryOptions) + # must be a named list - strictly + checkmate::assertList(val, names = "strict") + private$.queryOptions <- val + } ), public = list( connectionDetails = NULL, @@ -51,7 +62,8 @@ ConnectionHandler <- R6::R6Class( #' @param connectionDetails DatabaseConnector::connectionDetails class #' @param loadConnection Boolean option to load connection right away #' @param snakeCaseToCamelCase (Optional) Boolean. return the results columns in camel case (default) - initialize = function(connectionDetails, loadConnection = TRUE, snakeCaseToCamelCase = TRUE) { + #' @param queryOptions (Optional) named list of options that are wrapped when queries are translated. For example `list(sqlRenderTempSchema = 'my_scratch_space')`. Will override default global behaviour without altering global option state + initialize = function(connectionDetails, loadConnection = TRUE, snakeCaseToCamelCase = TRUE, queryOptions = list()) { checkmate::assertClass(connectionDetails, "ConnectionDetails") self$connectionDetails <- connectionDetails self$snakeCaseToCamelCase <- snakeCaseToCamelCase @@ -59,6 +71,7 @@ ConnectionHandler <- R6::R6Class( self$initConnection() } private$.dbms <- connectionDetails$dbms + self$queryOptions <- queryOptions }, #' get dbms @@ -91,11 +104,13 @@ ConnectionHandler <- R6::R6Class( mustTranslate <- FALSE } - sql <- SqlRender::render(sql = sql, ...) - # Only translate if translate is needed. - if (mustTranslate) { - sql <- SqlRender::translate(sql, targetDialect = self$dbms()) - } + withr::with_options(private$.queryOptions, { + sql <- SqlRender::render(sql = sql, ...) + # Only translate if translate is needed. + if (mustTranslate) { + sql <- SqlRender::translate(sql, targetDialect = self$dbms()) + } + }) return(sql) }, @@ -152,7 +167,7 @@ ConnectionHandler <- R6::R6Class( #' Closes connection (if active) finalize = function() { if (interactive()) { - rlang::inform("Due to changes in the R6 package, this method is deprecated and will be removed in a future version. Please use closeConnection instead") + rlang::inform("Due to changes in the R6 package, the ConnectionHandler$finalize method is deprecated and will be removed in a future version. Please use $closeConnection instead") } if (self$isActive & self$dbIsValid()) { self$closeConnection() diff --git a/R/PooledConnectionHandler.R b/R/PooledConnectionHandler.R index cfe8afa..300c0c6 100644 --- a/R/PooledConnectionHandler.R +++ b/R/PooledConnectionHandler.R @@ -95,11 +95,13 @@ PooledConnectionHandler <- R6::R6Class( #' @param snakeCaseToCamelCase (Optional) Boolean. return the results columns in camel case (default) #' @param dbConnectArgs Optional arguments to call pool::dbPool overrides default usage of connectionDetails #' @param forceJdbcConnection Force JDBC connection (requires using DatabaseConnector ConnectionDetails) + #' @param queryOptions (Optional) named list of options that are wrapped when queries are translated. For example `list(sqlRenderTempSchema = 'my_scratch_space')`. Will override default global behaviour without altering global option state initialize = function(connectionDetails = NULL, snakeCaseToCamelCase = TRUE, loadConnection = TRUE, dbConnectArgs = NULL, - forceJdbcConnection = TRUE) { + forceJdbcConnection = TRUE, + queryOptions = list()) { checkmate::assertList(dbConnectArgs, null.ok = TRUE) checkmate::assertClass(connectionDetails, "ConnectionDetails", null.ok = TRUE) @@ -125,6 +127,8 @@ PooledConnectionHandler <- R6::R6Class( if (loadConnection) { self$initConnection() } + + self$queryOptions <- queryOptions }, #' initialize pooled db connection diff --git a/R/QueryNamespace.R b/R/QueryNamespace.R index 989d945..6b3df8c 100644 --- a/R/QueryNamespace.R +++ b/R/QueryNamespace.R @@ -308,6 +308,7 @@ QueryNamespace <- R6::R6Class( #' @param connectionHandler ResultModelManager ConnectionHandler or PooledConnectionHandler instance #' @param tableSpecification Table specfication data.frame #' @param snakeCaseToCamelCase convert snakecase results to camelCase field names (TRUE by default) +#' @param queryOptions (Optional) Active field. Named list of options that are wrapped when queries are translated. For example `list(sqlRenderTempSchema = 'my_scratch_space')`. Will override default global behaviour without altering global option state #' @param ... Elipsis - use for any additional string keys to replace createQueryNamespace <- function(connectionDetails = NULL, connectionHandler = NULL, @@ -316,6 +317,7 @@ createQueryNamespace <- function(connectionDetails = NULL, resultModelSpecificationPath = NULL, tablePrefix = "", snakeCaseToCamelCase = TRUE, + queryOptions = NULL, ...) { checkmate::assertClass(connectionDetails, "ConnectionDetails", null.ok = TRUE) checkmate::assertClass(connectionHandler, "ConnectionHandler", null.ok = TRUE) @@ -353,6 +355,10 @@ createQueryNamespace <- function(connectionDetails = NULL, } connectionHandler$snakeCaseToCamelCase <- snakeCaseToCamelCase + if (!is.null(queryOptions)) { + connectionHandler$queryOptions <- queryOptions + } + qns <- QueryNamespace$new(connectionHandler, tableSpecification = tableSpecification, tablePrefix = tablePrefix, diff --git a/man/ConnectionHandler.Rd b/man/ConnectionHandler.Rd index 0cd92ec..7f636b5 100644 --- a/man/ConnectionHandler.Rd +++ b/man/ConnectionHandler.Rd @@ -24,12 +24,19 @@ Allows a connection to cleanly be opened and closed and stored within class/obje \item{\code{con}}{DatabaseConnector connection object} -\item{\code{isActive}}{Is connection active or not#'} +\item{\code{isActive}}{Is connection active or not'} \item{\code{snakeCaseToCamelCase}}{(Optional) Boolean. return the results columns in camel case (default)} } \if{html}{\out{}} } +\section{Active bindings}{ +\if{html}{\out{