Commit e45c420c authored by Jonathan Mang's avatar Jonathan Mang 💡
Browse files

fix: fixed oracle and postrges connection setup

parent f9696d73
Pipeline #19398 passed with stages
in 20 minutes and 1 second
Package: DIZutils
Title: Utilities for 'DIZ' R Package Development
Version: 0.0.5.9003
Date: 2020-10-28
Version: 0.0.5.9004
Date: 2020-11-13
Authors@R:
c(person(given = "Jonathan M.",
family = "Mang",
......@@ -42,4 +42,3 @@ Language: en-US
LazyData: true
SystemRequirements: libpq >= 9.0: libpq-dev (deb) or
postgresql-devel (rpm)
RoxygenNote: 7.1.1
......@@ -47,106 +47,122 @@ db_connection <- function(db_name,
timeout = 30,
logfile_dir = NULL,
lib_path = NULL) {
stopifnot(
is.character(db_name),
is.character(db_type),
is.logical(headless),
is.logical(from_env),
ifelse(is.null(settings), TRUE, is.list(settings)),
is.numeric(timeout),
ifelse(is.null(logfile_dir), TRUE, is.character(logfile_dir)),
ifelse(is.null(lib_path), TRUE, is.character(lib_path))
)
db_con <- NULL
tryCatch({
stopifnot(is.character(db_name),
is.character(db_type),
is.logical(headless),
is.logical(from_env),
ifelse(is.null(settings), TRUE, is.list(settings)),
is.numeric(timeout),
ifelse(is.null(logfile_dir), TRUE, is.character(logfile_dir)),
ifelse(is.null(lib_path), TRUE, is.character(lib_path)))
db_type <- toupper(db_type)
db_name <- toupper(db_name)
db_type <- toupper(db_type)
db_name <- toupper(db_name)
if (isTRUE(from_env)) {
dbname <- Sys.getenv(paste0(db_name, "_DBNAME"))
host <- Sys.getenv(paste0(db_name, "_HOST"))
port <- Sys.getenv(paste0(db_name, "_PORT"))
user <- Sys.getenv(paste0(db_name, "_USER"))
password <- Sys.getenv(paste0(db_name, "_PASSWORD"))
if (isTRUE(from_env)) {
dbname <- Sys.getenv(paste0(db_name, "_DBNAME"))
host <- Sys.getenv(paste0(db_name, "_HOST"))
port <- Sys.getenv(paste0(db_name, "_PORT"))
user <- Sys.getenv(paste0(db_name, "_USER"))
password <- Sys.getenv(paste0(db_name, "_PASSWORD"))
} else if (isFALSE(from_env)) {
stopifnot(
is.list(settings),
length(settings) >= 4
)
} else if (isFALSE(from_env)) {
stopifnot(is.list(settings),
length(settings) >= 4)
dbname <- settings$dbname
host <- settings$host
port <- settings$port
user <- settings$user
password <- settings$password
}
host <- settings$host
port <- settings$port
user <- settings$user
password <- settings$password
}
if (db_type == "ORACLE") {
if (is.null(lib_path)) {
if (isTRUE(from_env)) {
lib_path <- Sys.getenv(paste0(db_name, "_DRIVER"))
} else {
lib_path <- settings$lib_path
}
}
print(lib_path)
if (db_type == "ORACLE") {
stopifnot(lib_path != "" || !is.null(lib_path))
if (is.null(lib_path)) {
lib_path <- Sys.getenv(paste0(db_name, "_DRIVER"))
}
## create driver
drv <- RJDBC::JDBC("oracle.jdbc.OracleDriver",
classPath = lib_path)
stopifnot(
lib_path != "" || !is.null(lib_path)
)
if (isTRUE(from_env)) {
sid <- Sys.getenv(paste0(db_name, "_SID"))
} else if (isFALSE(from_env)) {
sid <- settings$sid
}
## create driver
drv <- RJDBC::JDBC("oracle.jdbc.OracleDriver",
classPath = lib_path)
## create URL
url <- paste0("jdbc:oracle:thin:@//", host, ":", port, "/", sid)
if (isTRUE(from_env)) {
sid <- Sys.getenv(paste0(db_name, "_SID"))
} else if (isFALSE(from_env)) {
sid <- settings$sid
}
## create URL
url <- paste0("jdbc:oracle:thin:@//", host, ":", port, "/", sid)
print("Test 1")
## create connection
db_con <- tryCatch({
conn <- DBI::dbConnect(
drv = drv,
url = url,
user = user,
password = password
)
conn
}, error = function(e) {
conn <- NULL
conn
}, finally = function(f) {
return(conn)
})
## create connection
db_con <- tryCatch({
conn <- DBI::dbConnect(
drv = drv,
url = url,
user = user,
password = password
)
conn
}, error = function(e) {
conn <- NULL
conn
}, finally = function(f) {
return(conn)
})
print("Test 2")
} else if (db_type == "POSTGRES") {
drv <- RPostgres::Postgres()
} else if (db_type == "POSTGRES") {
drv <- RPostgres::Postgres()
db_con <- tryCatch({
conn <- RPostgres::dbConnect(
drv = drv,
dbname = dbname,
host = host,
port = port,
user = user,
password = password,
connect_timeout = timeout
db_con <- tryCatch({
conn <- RPostgres::dbConnect(
drv = drv,
dbname = dbname,
host = host,
port = port,
user = user,
password = password,
connect_timeout = timeout
)
conn
}, error = function(e) {
conn <- NULL
conn
}, finally = function(f) {
return(conn)
})
}
if (is.null(db_con)) {
feedback(
"DB connection error",
findme = "9431c8c61f",
logfile_dir = logfile_dir,
headless = headless,
type = "Error"
)
conn
}, error = function(e) {
conn <- NULL
conn
}, finally = function(f) {
return(conn)
})
}
if (is.null(db_con)) {
}
},
error = function(cond) {
feedback(
"DB connection error",
findme = "9431c8c61f",
print_this = paste0("Error while trying to establish a db-connection: ",
cond),
type = "Error",
logfile_dir = logfile_dir,
headless = headless
headless = headless,
findme = "c16b60a6ff"
)
}
return(NULL)
})
return(db_con)
}
......@@ -34,7 +34,7 @@ my_desc$set_authors(c(
my_desc$del("Maintainer")
# Set the version
my_desc$set_version("0.0.5.9003")
my_desc$set_version("0.0.5.9004")
# The title of your package
my_desc$set(Title = "Utilities for 'DIZ' R Package Development")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment