From 51f96b6de49941b67d600a12b834eecab23d6fca Mon Sep 17 00:00:00 2001 From: daranzolin Date: Tue, 4 Jun 2024 11:12:01 -0700 Subject: [PATCH] separate options for opening/closing delimiters --- R/source_sql_to_dataframe.R | 5 ++--- R/sqltargets-option.R | 3 ++- R/zzz.R | 3 ++- man/sqltargets-options.Rd | 3 ++- tests/testthat/test-tar-sqltargets-options.R | 9 ++++++--- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/R/source_sql_to_dataframe.R b/R/source_sql_to_dataframe.R index 410b4b8..93b6cbd 100644 --- a/R/source_sql_to_dataframe.R +++ b/R/source_sql_to_dataframe.R @@ -4,9 +4,8 @@ source_sql_to_dataframe <- function(path, query_params = NULL) { connection_call <- paste0("con <- ", connection_string) eval(parse(text = connection_call)) on.exit(DBI::dbDisconnect(con)) - delimiters <- strsplit(sqltargets_option_get("sqltargets.glue_sql_delimiters"), "")[[1]] - open <- delimiters[1] - close <- delimiters[2] + open <- sqltargets_option_get("sqltargets.glue_sql_opening_delimiter") + close <- sqltargets_option_get("sqltargets.glue_sql_closing_delimiter") query <- readr::read_file(path) query <- glue::glue_sql(query, .con = con, .open = open, .close = close, .envir = query_params) out <- DBI::dbGetQuery(con, query) diff --git a/R/sqltargets-option.R b/R/sqltargets-option.R index 3156aa6..ed376df 100644 --- a/R/sqltargets-option.R +++ b/R/sqltargets-option.R @@ -10,7 +10,8 @@ #' ## Available Options #' #' - `"sqltargets.target_file_suffix"` - character. Length 1. Suffix appended to target name for SQL file dependency. -#' - `"sqltargets.glue_sql_delimiters"` - character. Length 1. Two characters. The opening and closing delimiters passed to `glue::glue_sql()`. +#' - `"sqltargets.glue_sql_opening_delimiter"` - character. Length 1. Two characters. The opening delimiter passed to `glue::glue_sql()`. +#' - `"sqltargets.glue_sql_closing_delimiter"` - character. Length 1. Two characters. The closing delimiter passed to `glue::glue_sql()`. #' @rdname sqltargets-options #' @export sqltargets_option_get <- function(option_name) { diff --git a/R/zzz.R b/R/zzz.R index 95667d2..65f46a5 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -6,5 +6,6 @@ sqltargets_env <- function() { .onAttach <- function(lib, pkg) { sqltargets.env$sqltargets.target_file_suffix <- "_query_file" - sqltargets.env$sqltargets.glue_sql_delimiters <- "{}" + sqltargets.env$sqltargets.glue_sql_opening_delimiter <- formals(glue::glue)$.open + sqltargets.env$sqltargets.glue_sql_closing_delimiter <- formals(glue::glue)$.close } diff --git a/man/sqltargets-options.Rd b/man/sqltargets-options.Rd index 83537f8..e373406 100644 --- a/man/sqltargets-options.Rd +++ b/man/sqltargets-options.Rd @@ -24,5 +24,6 @@ Get or Set sqltargets Options ## Available Options - `"sqltargets.target_file_suffix"` - character. Length 1. Suffix appended to target name for SQL file dependency. - - `"sqltargets.glue_sql_delimiters"` - character. Length 1. Two characters. The opening and closing delimiters passed to `glue::glue_sql()`. + - `"sqltargets.glue_sql_opening_delimiter"` - character. Length 1. Two characters. The opening delimiter passed to `glue::glue_sql()`. + - `"sqltargets.glue_sql_closing_delimiter"` - character. Length 1. Two characters. The closing delimiter passed to `glue::glue_sql()`. } diff --git a/tests/testthat/test-tar-sqltargets-options.R b/tests/testthat/test-tar-sqltargets-options.R index 511e6f1..bd2a44c 100644 --- a/tests/testthat/test-tar-sqltargets-options.R +++ b/tests/testthat/test-tar-sqltargets-options.R @@ -1,8 +1,10 @@ test_that("sqltargets_option_set() works", { sqltargets_option_set("sqltargets.target_file_suffix", "_x_query") - sqltargets_option_set("sqltargets.glue_sql_delimiters", "<>") + sqltargets_option_set("sqltargets.glue_sql_opening_delimiter", "<<") + sqltargets_option_set("sqltargets.glue_sql_closing_delimiter", ">>") expect_equal(sqltargets_option_get("sqltargets.target_file_suffix"), "_x_query") - expect_equal(sqltargets_option_get("sqltargets.glue_sql_delimiters"), "<>") + expect_equal(sqltargets_option_get("sqltargets.glue_sql_opening_delimiter"), "<<") + expect_equal(sqltargets_option_get("sqltargets.glue_sql_closing_delimiter"), ">>") }) test_that("different delimiters work", { @@ -14,7 +16,8 @@ test_that("different delimiters work", { ) writeLines(lines, "query.sql") targets::tar_script({ - sqltargets_option_set("sqltargets.glue_sql_delimiters", "@@") + sqltargets_option_set("sqltargets.glue_sql_opening_delimiter", "@") + sqltargets_option_set("sqltargets.glue_sql_closing_delimiter", "@") list( targets::tar_target(query_params, list(val = 3, col_name = "column1")), tar_sql(