Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions R/ConnectionHandler.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -51,14 +62,16 @@ 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
if (loadConnection) {
self$initConnection()
}
private$.dbms <- connectionDetails$dbms
self$queryOptions <- queryOptions
},

#' get dbms
Expand Down Expand Up @@ -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)
},

Expand Down Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion R/PooledConnectionHandler.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -125,6 +127,8 @@ PooledConnectionHandler <- R6::R6Class(
if (loadConnection) {
self$initConnection()
}

self$queryOptions <- queryOptions
},

#' initialize pooled db connection
Expand Down
6 changes: 6 additions & 0 deletions R/QueryNamespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 13 additions & 3 deletions man/ConnectionHandler.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions man/PooledConnectionHandler.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading