Skip to content

Commit 85bee82

Browse files
authored
Merge pull request #43 from OHDSI/fix_pool_linux_issue
Fix pool linux issue
2 parents 5b8f001 + 2584943 commit 85bee82

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+249
-113
lines changed

DESCRIPTION

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: ResultModelManager
22
Title: Result Model Manager (RMM) for OHDSI packages
3-
Version: 0.5.1
3+
Version: 0.5.2
44
Authors@R:
55
person("Jamie", "Gilbert", , "[email protected]", role = c("aut", "cre"))
66
Description: Database data model management utilities for OHDSI packages.
@@ -29,8 +29,14 @@ Imports:
2929
Suggests:
3030
testthat (>= 3.0.0),
3131
RSQLite,
32+
duckdb,
33+
RPostgreSQL,
3234
withr,
3335
knitr,
3436
rmarkdown,
35-
keyring
37+
keyring,
38+
devtools,
39+
pkgdown,
40+
remotes,
41+
styler
3642
Config/testthat/edition: 3

NEWS.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# ResultModelManager 0.5.2
2+
3+
Changes:
4+
5+
1. Allow `PooledConnectionHandler` classes to use DBI connections to bypass use of JDBC on systems where it may not be
6+
supported.
7+
8+
Bug Fixes:
9+
10+
1. Fixed issue in platform specific migrations where SqlRender sometimes fails to add attribute when calling
11+
`loadRenderTranslateSql`
12+
113
# ResultModelManager 0.5.1
214

315
Bug fixes:

R/ConnectionHandler.R

+9-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#' @export ConnectionHandler
3333
ConnectionHandler <- R6::R6Class(
3434
classname = "ConnectionHandler",
35+
private = list(
36+
.dbms = ""
37+
),
3538
public = list(
3639
connectionDetails = NULL,
3740
con = NULL,
@@ -48,12 +51,13 @@ ConnectionHandler <- R6::R6Class(
4851
if (loadConnection) {
4952
self$initConnection()
5053
}
54+
private$.dbms <- connectionDetails$dbms
5155
},
5256

5357
#' get dbms
5458
#' @description Get the dbms type of the connection
5559
dbms = function() {
56-
DatabaseConnector::dbms(self$getConnection())
60+
private$.dbms
5761
},
5862

5963
#' get table
@@ -76,14 +80,14 @@ ConnectionHandler <- R6::R6Class(
7680
#' @param ... Elipsis
7781
renderTranslateSql = function(sql, ...) {
7882
mustTranslate <- TRUE
79-
if (isTRUE(attr(sql, "sqlDialect", TRUE) == gsub(" ", "_", self$connectionDetails$dbms))) {
83+
if (isTRUE(attr(sql, "sqlDialect", TRUE) == gsub(" ", "_", self$dbms()))) {
8084
mustTranslate <- FALSE
8185
}
8286

8387
sql <- SqlRender::render(sql = sql, ...)
8488
# Only translate if translate is needed.
8589
if (mustTranslate) {
86-
sql <- SqlRender::translate(sql, targetDialect = self$connectionDetails$dbms)
90+
sql <- SqlRender::translate(sql, targetDialect = self$dbms())
8791
}
8892
return(sql)
8993
},
@@ -179,7 +183,7 @@ ConnectionHandler <- R6::R6Class(
179183
data <- self$queryFunction(sql, snakeCaseToCamelCase = snakeCaseToCamelCase)
180184
},
181185
error = function(error) {
182-
if (self$connectionDetails$dbms %in% c("postgresql", "redshift")) {
186+
if (self$dbms() %in% c("postgresql", "redshift")) {
183187
DatabaseConnector::dbExecute(self$getConnection(), "ABORT;")
184188
}
185189
print(sql)
@@ -202,7 +206,7 @@ ConnectionHandler <- R6::R6Class(
202206
data <- self$executeFunction(sql)
203207
},
204208
error = function(error) {
205-
if (self$connectionDetails$dbms %in% c("postgresql", "redshift")) {
209+
if (self$dbms() %in% c("postgresql", "redshift")) {
206210
self$executeFunction("ABORT;")
207211
}
208212
print(sql)

R/DataMigrationManager.R

+8
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ DataMigrationManager <- R6::R6Class(
266266
table_prefix = self$tablePrefix,
267267
packageName = self$packageName
268268
)
269+
270+
dialect <- attr(sql, "sqlDialect", TRUE)
271+
272+
if (is.null(dialect)) {
273+
# Unknown as to why SqlRender doesn't always set this
274+
attr(sql, "sqlDialect") <- private$connectionDetails$dbms
275+
}
276+
269277
private$connectionHandler$executeSql(sql)
270278
} else {
271279
# Check to see if a file for database platform exists

R/PooledConnectionHandler.R

+90-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,59 @@
1515
# limitations under the License.
1616

1717

18+
requiredPackage <- function(packageName) {
19+
packages <- utils::installed.packages()
20+
packages <- as.data.frame(packages)
21+
if (!packageName %in% packages$Package) {
22+
utils::install.packages(packageName)
23+
}
24+
}
25+
26+
# map a ConnectionDetails objects and translates them to DBI pool args
27+
.DBCToDBIArgs <- list(
28+
"sqlite" = function(cd) {
29+
requiredPackage("RSQLite")
30+
list(
31+
drv = RSQLite::SQLite(),
32+
dbname = cd$server()
33+
)
34+
},
35+
"postgresql" = function(cd) {
36+
requiredPackage("RPostgreSQL")
37+
host <- strsplit(cd$server(), "/")[[1]][1]
38+
dbname <- strsplit(cd$server(), "/")[[1]][2]
39+
list(
40+
drv = RPostgreSQL::PostgreSQL(),
41+
dbname = dbname,
42+
host = host,
43+
user = cd$user(),
44+
port = cd$port(),
45+
password = cd$password(),
46+
options = "sslmode=require"
47+
)
48+
},
49+
"duckdb" = function(cd) {
50+
requiredPackage("duckdb")
51+
list(
52+
drv = duckdb::duckdb(),
53+
dbdir = cd$server()
54+
)
55+
},
56+
"jdbc" = function(cd) {
57+
ParallelLogger::logInfo("Reverting to use of DatabaseConnector jdbc driver. May fail on some systems")
58+
list(
59+
drv = DatabaseConnector::DatabaseConnectorDriver(),
60+
dbms = cd$connectionDetails$dbms,
61+
server = cd$connectionDetails$server(),
62+
port = cd$connectionDetails$port(),
63+
user = cd$connectionDetails$user(),
64+
password = cd$connectionDetails$password(),
65+
connectionString = cd$connectionDetails$connectionString()
66+
)
67+
}
68+
)
69+
70+
1871
#' Pooled Connection Handler
1972
#'
2073
#' @description
@@ -28,11 +81,42 @@
2881
PooledConnectionHandler <- R6::R6Class(
2982
classname = "PooledConnectionHandler",
3083
inherit = ConnectionHandler,
84+
private = list(
85+
dbConnectArgs = NULL
86+
),
3187
public = list(
32-
#' @param ... Elisis @seealso[ConnectionHandler]
33-
initialize = function(...) {
34-
## Note this function is just a dummy because of roxygen
35-
super$initialize(...)
88+
#' @param connectionDetails DatabaseConnector::connectionDetails class
89+
#' @param loadConnection Boolean option to load connection right away
90+
#' @param snakeCaseToCamelCase (Optional) Boolean. return the results columns in camel case (default)
91+
#' @param dbConnectArgs Optional arguments to call pool::dbPool overrides default usage of connectionDetails
92+
#' @param forceJdbcConnection Force JDBC connection (requires using DatabaseConnector ConnectionDetails)
93+
initialize = function(connectionDetails = NULL,
94+
snakeCaseToCamelCase = TRUE,
95+
loadConnection = TRUE,
96+
dbConnectArgs = NULL,
97+
forceJdbcConnection = FALSE) {
98+
checkmate::assertList(dbConnectArgs, null.ok = TRUE)
99+
checkmate::assertClass(connectionDetails, "ConnectionDetails", null.ok = TRUE)
100+
101+
if (is.null(connectionDetails) && is.null(dbConnectArgs)) {
102+
stop("Must either specify usage of poolArgs or DatabaseConnector connection details object")
103+
}
104+
105+
if (is.null(dbConnectArgs)) {
106+
if (!forceJdbcConnection && connectionDetails$dbms %in% names(.DBCToDBIArgs)) {
107+
fun <- .DBCToDBIArgs[[connectionDetails$dbms]]
108+
} else {
109+
fun <- .DBCToDBIArgs$jdbc
110+
}
111+
dbConnectArgs <- fun(connectionDetails)
112+
}
113+
114+
private$dbConnectArgs <- dbConnectArgs
115+
self$connectionDetails <- connectionDetails
116+
self$snakeCaseToCamelCase <- snakeCaseToCamelCase
117+
if (loadConnection) {
118+
self$initConnection()
119+
}
36120
},
37121
#' initialize pooled db connection
38122
#' @description
@@ -42,15 +126,9 @@ PooledConnectionHandler <- R6::R6Class(
42126
warning("Closing existing connection")
43127
self$closeConnection()
44128
}
129+
ParallelLogger::logInfo("Initalizing pooled connection")
130+
self$con <- do.call(pool::dbPool, private$dbConnectArgs)
45131

46-
self$con <- pool::dbPool(
47-
drv = DatabaseConnector::DatabaseConnectorDriver(),
48-
dbms = self$connectionDetails$dbms,
49-
server = self$connectionDetails$server(),
50-
port = self$connectionDetails$port(),
51-
user = self$connectionDetails$user(),
52-
password = self$connectionDetails$password()
53-
)
54132
self$isActive <- TRUE
55133
},
56134

docs/404.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/LICENSE-text.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/CreatingMigrations.html

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/ExampleProject.html

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/PackageDesign.html

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/UploadFunctionality.html

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/UsingAnExportManager.html

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)