Skip to content

Commit

Permalink
Drop odbc support
Browse files Browse the repository at this point in the history
Fix #93
  • Loading branch information
psychelzh committed Oct 19, 2024
1 parent 5193e81 commit 0cc6897
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 145 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Imports:
jsonlite,
memoise,
rlang (>= 1.0.0),
RMariaDB,
tarchetypes,
targets
Suggests:
Expand All @@ -33,9 +34,7 @@ Suggests:
data.iquizoo (>= 2024.7.14),
digest,
lifecycle,
odbc,
preproc.iquizoo (>= 2.6.0),
RMariaDB (>= 1.3.1),
roxygen2,
testthat (>= 3.0.0),
tibble
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export(fetch_iquizoo_mem)
export(get_users_props_names)
export(parse_data)
export(setup_option_file)
export(setup_source)
export(setup_templates)
export(tar_fetch_data)
export(tar_fetch_users)
Expand Down
28 changes: 4 additions & 24 deletions R/database.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,16 @@
#' @param ... Further arguments passed to [DBI::dbConnect()].
#' @param params The parameters to be bound to the query. Default to `NULL`, see
#' [DBI::dbGetQuery()] for more details.
#' @param source The data source from which data is fetched. See
#' [setup_source()] for details.
#' @param group Section identifier in the `default.file`. See
#' [RMariaDB::MariaDB()] for more information.
#' @return A [data.frame] contains the fetched data.
#' @seealso [fetch_iquizoo_mem()] for a memoised version of this function.
#' @export
fetch_iquizoo <- function(query, ...,
params = NULL,
source = setup_source()) {
group = getOption("tarflow.group")) {
check_dots_used()
if (!inherits(source, "tarflow.source")) {
cli::cli_abort(
"{.arg source} must be created by {.fun setup_source}.",
class = "tarflow_bad_source"
)
}
# connect to given database which is pre-configured
if (!inherits_any(source$driver, c("OdbcDriver", "MariaDBDriver"))) {
cli::cli_abort(
"Driver must be either OdbcDriver or MariaDBDriver.",
class = "tarflow_bad_driver"
)
}
# nocov start
if (inherits(source$driver, "OdbcDriver")) {
con <- DBI::dbConnect(source$driver, dsn = source$dsn, ...)
}
# nocov end
if (inherits(source$driver, "MariaDBDriver")) {
con <- DBI::dbConnect(source$driver, group = source$group, ...)
}
con <- DBI::dbConnect(RMariaDB::MariaDB(), group = group, ...)
on.exit(DBI::dbDisconnect(con))
DBI::dbGetQuery(con, query, params = params)
}
Expand Down
48 changes: 4 additions & 44 deletions R/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,54 +37,14 @@ setup_templates <- function(contents = NULL,
)
}

#' Set data source
#'
#' @param driver The driver used. Set as an option of `"tarflow.driver"`.
#' Options are [odbc::odbc()] and [RMariaDB::MariaDB()], both of which need
#' pre-configurations. Default to first available one.
#' @param dsn The data source name of an **ODBC** database connector. See
#' [odbc::dbConnect()] for more information. Used when `driver` is set as
#' [odbc::odbc()].
#' @param group Section identifier in the `default.file`. See
#' [RMariaDB::MariaDB()] for more information. Used when `driver` is set as
#' [RMariaDB::MariaDB()].
#' @return An S3 class of `tarflow.source` with the options.
#' @export
setup_source <- function(driver = getOption("tarflow.driver"),
dsn = getOption("tarflow.dsn"),
group = getOption("tarflow.group")) {
structure(
list(
driver = driver,
dsn = dsn,
group = group
),
class = "tarflow.source"
)
}

#' Check if the database based on the given data source is ready
#'
#' @param source The data source from which data is fetched. See
#' [setup_source()] for details.
#' @param group Section identifier in the `default.file`. See
#' [RMariaDB::MariaDB()] for more information.
#' @return TRUE if the database is ready, FALSE otherwise.
#' @export
check_source <- function(source = setup_source()) {
if (!inherits(source, "tarflow.source")) {
cli::cli_abort(
"{.arg source} must be created by {.fun setup_source}.",
class = "tarflow_bad_source"
)
}
# nocov start
if (inherits(source$driver, "OdbcDriver")) {
return(DBI::dbCanConnect(source$driver, dsn = source$dsn))
}
# nocov end
if (inherits(source$driver, "MariaDBDriver")) {
return(DBI::dbCanConnect(source$driver, group = source$group))
}
return(FALSE)
check_source <- function(group = getOption("tarflow.group")) {
return(DBI::dbCanConnect(RMariaDB::MariaDB(), group = group))
}

# nocov start
Expand Down
28 changes: 4 additions & 24 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,13 @@ NULL
# nocov start

.onLoad <- function(libname, pkgname) {
# options
# setup options
op <- options()
name_db_src_default <- "iquizoo-v3"
if (requireNamespace("RMariaDB", quietly = TRUE)) {
op_tarflow <- list(
tarflow.driver = RMariaDB::MariaDB(),
tarflow.group = name_db_src_default
)
} else if (requireNamespace("odbc", quietly = TRUE)) {
op_tarflow <- list(
tarflow.driver = odbc::odbc(),
tarflow.dsn = name_db_src_default
)
} else {
op_tarflow <- list()
}

op_tarflow <- list(tarflow.group = "iquizoo-v3")
toset <- !(names(op_tarflow) %in% names(op))
if (any(toset)) options(op_tarflow[toset])

# try setting up option file if source not working
if (!check_source()) {
tryCatch(setup_option_file(quietly = TRUE), error = \(e) {})
}
Expand All @@ -32,14 +19,7 @@ NULL
}

.onAttach <- function(libname, pkgname) {
if (!requireNamespace("odbc", quietly = TRUE) &&
!requireNamespace("RMariaDB", quietly = TRUE)) {
packageStartupMessage(
"Neither odbc nor RMariaDB is installed.",
" For better support, it is recommended to install RMariaDB.",
" Please install it with `install.packages('RMariaDB')`."
)
} else if (!check_source()) {
if (!check_source()) {
packageStartupMessage(
"The default database source is not working.",
" Please check your database settings first.",
Expand Down
6 changes: 3 additions & 3 deletions man/check_source.Rd

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

6 changes: 3 additions & 3 deletions man/fetch_iquizoo.Rd

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

31 changes: 0 additions & 31 deletions man/setup_source.Rd

This file was deleted.

13 changes: 0 additions & 13 deletions tests/testthat/test-database.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
test_that("Ensure source checking works", {
source <- list(driver = RMariaDB::MariaDB())
fetch_iquizoo(source = source) |>
expect_error(class = "tarflow_bad_source")
check_source(source = source) |>
expect_error(class = "tarflow_bad_source")
source_invalid <- setup_source(driver = NULL)
fetch_iquizoo(source = source_invalid) |>
expect_error(class = "tarflow_bad_driver")
check_source(source = source_invalid) |>
expect_false()
})

test_that("`parse_data()` works", {
js_str <- r"([{"a": 1, "b": 2}])"
data <- data.frame(game_data = js_str)
Expand Down

0 comments on commit 0cc6897

Please sign in to comment.