Skip to content

Commit

Permalink
Suppport lists of Id objects in glue_sql
Browse files Browse the repository at this point in the history
  • Loading branch information
jimhester committed Jul 27, 2018
1 parent 0178a5c commit ea29e09
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* `glue_sql()` now supports unquoting lists of Id objects.

# glue 1.3.0

## Breaking changes
Expand Down
10 changes: 9 additions & 1 deletion R/sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#' AND {`tbl`}.species = {val}
#' ", .con = con)
#'
#'
#' # `glue_sql()` can be used in conjuction with parameterized queries using
#' # `DBI::dbBind()` to provide protection for SQL Injection attacks
#' sql <- glue_sql("
Expand Down Expand Up @@ -110,7 +111,14 @@ sql_quote_transformer <- function(connection) {
if (is_quoted) {
regmatches(text, m) <- ""
res <- eval(parse(text = text, keep.source = FALSE), envir)
res <- DBI::dbQuoteIdentifier(conn = connection, res)

if (length(res) == 1) {
res <- DBI::dbQuoteIdentifier(conn = connection, res)
} else {

# Support lists as well
res[] <- lapply(res, DBI::dbQuoteIdentifier, conn = connection)
}
} else {
res <- eval(parse(text = text, keep.source = FALSE), envir)

Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ describe("glue_sql", {
var <- "foo"
expect_identical(glue_sql("{`var`}", .con = con), DBI::SQL("`foo`"))
})
it("quotes Id identifiers", {
var <- DBI::Id(schema = "foo", table = "bar", columm = "baz")
expect_identical(glue_sql("{`var`}", .con = con), DBI::SQL("`foo`.`bar`.`baz`"))
})
it("quotes lists of Id identifiers", {
var <- c(
DBI::Id(schema = "foo", table = "bar", columm = "baz"),
DBI::Id(schema = "foo", table = "bar", columm = "baz2")

This comment has been minimized.

Copy link
@DavisVaughan

DavisVaughan Jul 27, 2018

Member

columm is spelled wrong if you want to be picky but I don't think it matters

This comment has been minimized.

Copy link
@jimhester

jimhester Jul 27, 2018

Author Collaborator

Thanks!

)
expect_identical(glue_sql("{`var`*}", .con = con), DBI::SQL("`foo`.`bar`.`baz`, `foo`.`bar`.`baz2`"))
})
it("Does not quote numbers", {
var <- 1
expect_identical(glue_sql("{var}", .con = con), DBI::SQL("1"))
Expand Down

0 comments on commit ea29e09

Please sign in to comment.