diff --git a/DESCRIPTION b/DESCRIPTION index 609f6ed..6c89f9b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: tarflow.iquizoo Title: Setup "targets" Workflows for "iquizoo" Data Processing -Version: 3.12.2 +Version: 3.12.3 Authors@R: c( person("Liang", "Zhang", , "psychelzh@outlook.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-9041-1150")), @@ -25,6 +25,7 @@ Imports: jsonlite, memoise, rlang (>= 1.0.0), + RMariaDB, tarchetypes, targets Suggests: @@ -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 diff --git a/NAMESPACE b/NAMESPACE index 82e825b..ac5cbe4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/NEWS.md b/NEWS.md index b51eeda..24a2c24 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# tarflow.iquizoo 3.12.3 + +## Upkeep + +* Dropped support for odbc driver, and only RMariaDB driver is supported now (#93). +* Removed `setup_source()` for there is no need for database source option setup when only `RMariaDB::MariaDB()` is supported. + # tarflow.iquizoo 3.12.2 ## Enhancements diff --git a/R/database.R b/R/database.R index 4ddb718..9c7e2dd 100644 --- a/R/database.R +++ b/R/database.R @@ -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) } diff --git a/R/setup.R b/R/setup.R index 4f1ddf5..439cc6c 100644 --- a/R/setup.R +++ b/R/setup.R @@ -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 diff --git a/R/targets.R b/R/targets.R index 41a9625..a51e917 100644 --- a/R/targets.R +++ b/R/targets.R @@ -185,7 +185,7 @@ tar_fetch_users <- function(contents, subset_users_props = NULL, bquote( fetch_iquizoo( .(glue::glue(read_file(templates[["users"]]), - .envir = list(columns = columns) + .envir = env(columns = columns) )), params = list(.(unique(contents$project_id))) ) |> diff --git a/R/zzz.R b/R/zzz.R index 0971106..cf57234 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -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) {}) } @@ -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.", diff --git a/_pkgdown.yml b/_pkgdown.yml index 83e2440..1488f75 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -23,7 +23,6 @@ reference: desc: Functions to help you set up options. contents: - setup_templates - - setup_source - setup_option_file - check_source - get_users_props_names diff --git a/man/check_source.Rd b/man/check_source.Rd index 0114107..a3dbc23 100644 --- a/man/check_source.Rd +++ b/man/check_source.Rd @@ -4,11 +4,11 @@ \alias{check_source} \title{Check if the database based on the given data source is ready} \usage{ -check_source(source = setup_source()) +check_source(group = getOption("tarflow.group")) } \arguments{ -\item{source}{The data source from which data is fetched. See -\code{\link[=setup_source]{setup_source()}} for details.} +\item{group}{Section identifier in the \code{default.file}. See +\code{\link[RMariaDB:dbConnect-MariaDBDriver-method]{RMariaDB::MariaDB()}} for more information.} } \value{ TRUE if the database is ready, FALSE otherwise. diff --git a/man/fetch_iquizoo.Rd b/man/fetch_iquizoo.Rd index 4104f1a..a73658a 100644 --- a/man/fetch_iquizoo.Rd +++ b/man/fetch_iquizoo.Rd @@ -4,7 +4,7 @@ \alias{fetch_iquizoo} \title{Fetch result of query from iQuizoo database} \usage{ -fetch_iquizoo(query, ..., params = NULL, source = setup_source()) +fetch_iquizoo(query, ..., params = NULL, group = getOption("tarflow.group")) } \arguments{ \item{query}{A character string containing SQL.} @@ -14,8 +14,8 @@ fetch_iquizoo(query, ..., params = NULL, source = setup_source()) \item{params}{The parameters to be bound to the query. Default to \code{NULL}, see \code{\link[DBI:dbGetQuery]{DBI::dbGetQuery()}} for more details.} -\item{source}{The data source from which data is fetched. See -\code{\link[=setup_source]{setup_source()}} for details.} +\item{group}{Section identifier in the \code{default.file}. See +\code{\link[RMariaDB:dbConnect-MariaDBDriver-method]{RMariaDB::MariaDB()}} for more information.} } \value{ A \link{data.frame} contains the fetched data. diff --git a/man/setup_source.Rd b/man/setup_source.Rd deleted file mode 100644 index 5be8e03..0000000 --- a/man/setup_source.Rd +++ /dev/null @@ -1,31 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/setup.R -\name{setup_source} -\alias{setup_source} -\title{Set data source} -\usage{ -setup_source( - driver = getOption("tarflow.driver"), - dsn = getOption("tarflow.dsn"), - group = getOption("tarflow.group") -) -} -\arguments{ -\item{driver}{The driver used. Set as an option of \code{"tarflow.driver"}. -Options are \code{\link[odbc:dbConnect-OdbcDriver-method]{odbc::odbc()}} and \code{\link[RMariaDB:dbConnect-MariaDBDriver-method]{RMariaDB::MariaDB()}}, both of which need -pre-configurations. Default to first available one.} - -\item{dsn}{The data source name of an \strong{ODBC} database connector. See -\code{\link[odbc:dbConnect-OdbcDriver-method]{odbc::dbConnect()}} for more information. Used when \code{driver} is set as -\code{\link[odbc:dbConnect-OdbcDriver-method]{odbc::odbc()}}.} - -\item{group}{Section identifier in the \code{default.file}. See -\code{\link[RMariaDB:dbConnect-MariaDBDriver-method]{RMariaDB::MariaDB()}} for more information. Used when \code{driver} is set as -\code{\link[RMariaDB:dbConnect-MariaDBDriver-method]{RMariaDB::MariaDB()}}.} -} -\value{ -An S3 class of \code{tarflow.source} with the options. -} -\description{ -Set data source -} diff --git a/tests/testthat/test-database.R b/tests/testthat/test-database.R index ab73c59..f229262 100644 --- a/tests/testthat/test-database.R +++ b/tests/testthat/test-database.R @@ -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)