Skip to content

Commit

Permalink
separate options for opening/closing delimiters
Browse files Browse the repository at this point in the history
  • Loading branch information
daranzolin committed Jun 4, 2024
1 parent 3a0faa0 commit 51f96b6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
5 changes: 2 additions & 3 deletions R/source_sql_to_dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion R/sqltargets-option.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion man/sqltargets-options.Rd

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

9 changes: 6 additions & 3 deletions tests/testthat/test-tar-sqltargets-options.R
Original file line number Diff line number Diff line change
@@ -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", {
Expand All @@ -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(
Expand Down

0 comments on commit 51f96b6

Please sign in to comment.